Repository navigation
Commit c17c591
feat(adapter): report 1h cache writes in JSON model breakdowns (ccusage#2059)
* feat(adapter): report 1h cache writes in JSON model breakdowns
Claude logs split cache writes into `ephemeral_5m_input_tokens` and
`ephemeral_1h_input_tokens`, and the cost calculation already prices
the two tiers separately. Every report path, however, collapsed the
split into one `cacheCreationTokens` count before it reached
`ModelBreakdown`, so the TTL tier the server picked was invisible even
though it changes the bill.
`ModelBreakdown` now carries an optional `cache_creation_1h_tokens`,
serialized as `cacheCreation1hTokens` only when at least one record
reported the split. Records without a `cache_creation` object are
billed at the 5-minute rate, so `cacheCreationTokens -
cacheCreation1hTokens` is always the 5-minute part and no separate 5m
field is needed. Other agents never report a split and keep their
existing JSON shape.
The field had to be threaded through five hand-written copies of the
per-model accumulation (Claude daily, core summaries, monthly/weekly
merges, and the two all-agents merges). Those copies now go through
`ModelBreakdown::add_usage` and `ModelBreakdown::merge`, so a future
field cannot be dropped by one path while the others carry it.
Fixes ccusage#2015
Co-authored-by: XixianWasTaken <[email protected]>
* test(ccusage): cover unified reports in the 1h cache split test
Cubic pointed out that the unified weekly, monthly and session reports
build their modelBreakdowns through their own merge paths, which the
test did not exercise. Add them to the existing report loop.
* test(ccusage): spread the 1h cache split fixture over two days
CodeRabbit noted that every fixture record fell on one date, so weekly,
monthly and session reports never merged two rows and a merge that
dropped cacheCreation1hTokens would still pass. Add records on the next
day, keep daily reports on the first day with --until, and assert the
summed counts for the merged reports.
* test(adapter): cover 1h cache split in cross-agent breakdown merge
Per CodeRabbit, aggregates_model_breakdowns_across_agents now checks
that a split merged with a breakdown lacking one keeps the sum, and that
two breakdowns without a split stay without one.
* docs: link the Claude guide from the cache write TTL section
CodeRabbit asked for a cross-link from the new section; the Claude Code
guide is the source the split comes from.
* docs: describe the non-1h cache write remainder accurately
CodeRabbit pointed out that cacheCreationTokens minus
cacheCreation1hTokens also includes writes from records that reported
no TTL split, not only 5-minute writes. Both are billed at the 5-minute
rate, which is what the section now says.
* docs: say unsplit cache writes are priced at the 5-minute rate
CodeRabbit noted that the 5-minute rate for writes without a TTL split
is an assumption ccusage makes when it calculates cost, not what the
vendor bills; display mode and stored costs never apply it.
* docs: list the optional modelBreakdowns keys in the JSON guarantees
Programmatic Usage promised that every field is always present, which
was already false for missingPricing and storedCost and is now false
for cacheCreation1hTokens. Name those keys and the --no-cost exception.
---------
Co-authored-by: XixianWasTaken <[email protected]>1 parent 7e7ac1f commit c17c591
13 files changed
Lines changed: 205 additions & 60 deletions
File tree
- docs/guide
- rust
- adapters
- claude/src
- common/src
- opencode/src
- crates
- ccusage-adapter-all/src
- ccusage-core/src
- ccusage-report/src
- ccusage/tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
305 | 321 | | |
306 | 322 | | |
307 | 323 | | |
| |||
700 | 716 | | |
701 | 717 | | |
702 | 718 | | |
703 | | - | |
| 719 | + | |
704 | 720 | | |
705 | 721 | | |
706 | 722 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
687 | 687 | | |
688 | 688 | | |
689 | 689 | | |
690 | | - | |
691 | | - | |
692 | | - | |
693 | | - | |
| 690 | + | |
694 | 691 | | |
695 | 692 | | |
696 | 693 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
| 324 | + | |
324 | 325 | | |
325 | 326 | | |
326 | 327 | | |
| |||
332 | 333 | | |
333 | 334 | | |
334 | 335 | | |
| 336 | + | |
335 | 337 | | |
336 | 338 | | |
337 | 339 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1168 | 1168 | | |
1169 | 1169 | | |
1170 | 1170 | | |
| 1171 | + | |
1171 | 1172 | | |
1172 | 1173 | | |
1173 | 1174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
379 | 379 | | |
380 | 380 | | |
381 | 381 | | |
| 382 | + | |
382 | 383 | | |
383 | 384 | | |
384 | 385 | | |
| |||
390 | 391 | | |
391 | 392 | | |
392 | 393 | | |
| 394 | + | |
393 | 395 | | |
394 | 396 | | |
395 | 397 | | |
| |||
2281 | 2283 | | |
2282 | 2284 | | |
2283 | 2285 | | |
2284 | | - | |
2285 | | - | |
2286 | | - | |
2287 | | - | |
2288 | | - | |
2289 | | - | |
2290 | | - | |
2291 | | - | |
2292 | | - | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
| 2289 | + | |
| 2290 | + | |
| 2291 | + | |
| 2292 | + | |
| 2293 | + | |
| 2294 | + | |
| 2295 | + | |
| 2296 | + | |
| 2297 | + | |
| 2298 | + | |
| 2299 | + | |
| 2300 | + | |
| 2301 | + | |
2293 | 2302 | | |
2294 | 2303 | | |
2295 | 2304 | | |
| |||
2311 | 2320 | | |
2312 | 2321 | | |
2313 | 2322 | | |
| 2323 | + | |
2314 | 2324 | | |
2315 | 2325 | | |
2316 | 2326 | | |
| |||
2324 | 2334 | | |
2325 | 2335 | | |
2326 | 2336 | | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
2327 | 2342 | | |
2328 | 2343 | | |
2329 | 2344 | | |
| |||
2332 | 2347 | | |
2333 | 2348 | | |
2334 | 2349 | | |
2335 | | - | |
| 2350 | + | |
2336 | 2351 | | |
2337 | 2352 | | |
2338 | 2353 | | |
| |||
2345 | 2360 | | |
2346 | 2361 | | |
2347 | 2362 | | |
| 2363 | + | |
| 2364 | + | |
| 2365 | + | |
| 2366 | + | |
| 2367 | + | |
| 2368 | + | |
| 2369 | + | |
2348 | 2370 | | |
2349 | 2371 | | |
2350 | 2372 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 170 | + | |
181 | 171 | | |
182 | 172 | | |
183 | 173 | | |
| |||
196 | 186 | | |
197 | 187 | | |
198 | 188 | | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
| 189 | + | |
210 | 190 | | |
211 | 191 | | |
212 | 192 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
667 | 667 | | |
668 | 668 | | |
669 | 669 | | |
| 670 | + | |
670 | 671 | | |
671 | 672 | | |
672 | 673 | | |
| |||
863 | 864 | | |
864 | 865 | | |
865 | 866 | | |
| 867 | + | |
866 | 868 | | |
867 | 869 | | |
868 | 870 | | |
| |||
900 | 902 | | |
901 | 903 | | |
902 | 904 | | |
| 905 | + | |
903 | 906 | | |
904 | 907 | | |
905 | 908 | | |
| |||
911 | 914 | | |
912 | 915 | | |
913 | 916 | | |
| 917 | + | |
914 | 918 | | |
915 | 919 | | |
916 | 920 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 81 | + | |
89 | 82 | | |
90 | 83 | | |
91 | 84 | | |
| |||
267 | 260 | | |
268 | 261 | | |
269 | 262 | | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
| 263 | + | |
285 | 264 | | |
286 | 265 | | |
287 | 266 | | |
| |||
705 | 684 | | |
706 | 685 | | |
707 | 686 | | |
| 687 | + | |
708 | 688 | | |
709 | 689 | | |
710 | 690 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
50 | 55 | | |
51 | 56 | | |
52 | 57 | | |
| |||
103 | 108 | | |
104 | 109 | | |
105 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
106 | 116 | | |
107 | 117 | | |
108 | 118 | | |
| |||
115 | 125 | | |
116 | 126 | | |
117 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
118 | 166 | | |
119 | 167 | | |
120 | 168 | | |
| |||
0 commit comments