Skip to content

Commit 259ec33

Browse files
ryoppippicursoragenterenozen
authored
feat(adapter): report stored cost in JSON model breakdowns (#1903)
* feat(adapter): report stored cost in JSON model breakdowns In auto cost mode a modelBreakdowns entry's cost mixes costUSD values stored in agent logs with costs calculated from the pricing table, and the JSON output gave consumers no way to tell the two apart. Each loaded entry now records the stored cost auto mode actually used, following the same path as missing_pricing_model, and every breakdown aggregation and merge sums it into an optional storedCost key. The key is omitted when no record in the breakdown used a stored cost, and calculate and display modes never emit it, so existing output keeps its shape. --no-cost strips it with the other cost fields. Adapters with stored costs (Claude, Copilot, Hermes, Kilo, OpenClaw, OpenCode, Pi) pass the stored value their own auto-mode rule selects. Refs #1898 Co-authored-by: erenozen <[email protected]> * docs: document storedCost in JSON output Describe the storedCost model breakdown field, link it from the auto cost mode's mixed-data rule, and list it among the fields --no-cost removes. Refs #1898 Co-authored-by: erenozen <[email protected]> * fix(grok): mark recorded costUsdTicks as stored cost Grok's auto mode bills the recorded costUsdTicks figure when a turn carries one, but its loaded entries left stored_cost unset, so Grok breakdowns omitted storedCost even though cost included it. Raised by CodeRabbit, Cubic, and Pullfrog on #1903. Co-authored-by: erenozen <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: erenozen <[email protected]>
1 parent 89c50a7 commit 259ec33

41 files changed

Lines changed: 276 additions & 19 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/guide/cost-modes.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ccusage daily
2626

2727
1. **Pre-calculated costs available** → Uses Claude's `costUSD` values
2828
2. **No pre-calculated costs** → Calculates from token counts using model pricing
29-
3. **Mixed data** → Uses the best method for each entry
29+
3. **Mixed data** → Uses the best method for each entry; JSON output reports the stored part of each model's cost as [`storedCost`](/guide/json-output#stored-costs)
3030

3131
#### Best for:
3232

‎docs/guide/json-output.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ccusage daily --json --no-cost
2727
ccusage blocks --json --no-cost
2828
```
2929

30-
This removes cost fields such as `totalCost`, `costUSD`, and nested `cost` values while keeping token, model, date, block, burn-rate, and projection fields.
30+
This removes cost fields such as `totalCost`, `costUSD`, `storedCost`, and nested `cost` values while keeping token, model, date, block, burn-rate, and projection fields.
3131

3232
## Unpriced Models
3333

@@ -286,6 +286,22 @@ ccusage daily --sections daily,monthly,session --by-agent --json
286286
}
287287
```
288288

289+
## Stored Costs
290+
291+
In `auto` [cost mode](/guide/cost-modes), an entry's `cost` can add together costs stored in the agent's logs (such as Claude's `costUSD`) and costs calculated from the pricing table. Each `modelBreakdowns` entry where at least one record used its stored cost carries `storedCost`, the part of `cost` that came from stored values; `cost - storedCost` is the calculated part. Entries without stored costs omit the key, and `calculate` and `display` modes never emit it.
292+
293+
```json
294+
{
295+
"modelName": "claude-sonnet-4-20250514",
296+
"inputTokens": 1500,
297+
"outputTokens": 100,
298+
"cacheCreationTokens": 20,
299+
"cacheReadTokens": 30,
300+
"cost": 0.503084,
301+
"storedCost": 0.5
302+
}
303+
```
304+
289305
## JSON Structure
290306

291307
### Daily Reports (Standard)

‎rust/adapters/amp/src/parser.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ fn parse_ledger_events(
215215
usage_limit_reset_time: None,
216216
missing_pricing_model,
217217
data,
218+
stored_cost: None,
218219
});
219220
}
220221
entries
@@ -323,6 +324,7 @@ fn parse_message_usage(
323324
usage_limit_reset_time: None,
324325
missing_pricing_model,
325326
data,
327+
stored_cost: None,
326328
});
327329
}
328330
entries

‎rust/adapters/antigravity/src/parser.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ pub(super) fn event_to_loaded(
10191019
usage_limit_reset_time: None,
10201020
missing_pricing_model,
10211021
data,
1022+
stored_cost: None,
10221023
}
10231024
}
10241025

‎rust/adapters/antigravity/src/report.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ mod tests {
112112
model: Some("gemini-3-pro".to_string()),
113113
usage_limit_reset_time: None,
114114
missing_pricing_model: None,
115+
stored_cost: None,
115116
}
116117
}
117118

‎rust/adapters/claude/src/daily.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
cli::{CostMode, SharedArgs},
1717
fast::{FxHashMap, byte_lines, suffix_string},
1818
format_date_tz, log_level, missing_pricing_model_for_usage, parse_ts_timestamp, parse_tz,
19-
utc_now,
19+
stored_cost_for_mode, utc_now,
2020
};
2121

2222
use super::{
@@ -161,6 +161,7 @@ struct DailyLoadedEntry {
161161
cost: f64,
162162
model: Option<String>,
163163
missing_pricing_model: Option<String>,
164+
stored_cost: Option<f64>,
164165
message_id: Option<String>,
165166
request_id: Option<String>,
166167
is_sidechain: Option<bool>,
@@ -361,6 +362,7 @@ fn read_daily_usage_file(
361362
cost,
362363
model,
363364
missing_pricing_model,
365+
stored_cost: stored_cost_for_mode(data.cost_usd, mode),
364366
message_id: data.message.id,
365367
request_id: data.request_id,
366368
is_sidechain: data.is_sidechain,
@@ -389,6 +391,7 @@ fn read_daily_usage_file(
389391
),
390392
model: Some(advisor.model),
391393
missing_pricing_model,
394+
stored_cost: None,
392395
message_id: message_id
393396
.as_ref()
394397
.map(|message_id| format!("{message_id}:advisor:{index}")),
@@ -640,6 +643,7 @@ impl DailyAccumulator {
640643
if entry.missing_pricing_model.is_some() {
641644
breakdown.missing_pricing = true;
642645
}
646+
breakdown.add_stored_cost(entry.stored_cost);
643647
}
644648
}
645649

@@ -1166,6 +1170,7 @@ mod tests {
11661170
message_id: Some(fixture.message_id.to_string()),
11671171
request_id: Some(fixture.request_id.to_string()),
11681172
is_sidechain: Some(fixture.is_sidechain),
1173+
stored_cost: None,
11691174
}
11701175
}
11711176

‎rust/adapters/claude/src/lib.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
debug_log,
2828
fast::{FxHashMap, byte_lines, suffix_string},
2929
format_date_tz, log_level, missing_pricing_model_for_usage, parse_ts_timestamp, parse_tz,
30-
progress,
30+
progress, stored_cost_for_mode,
3131
};
3232

3333
#[doc(hidden)]
@@ -530,6 +530,7 @@ fn read_usage_file(
530530
mode,
531531
pricing,
532532
);
533+
let stored_cost = stored_cost_for_mode(data.cost_usd, mode);
533534
let usage_limit_reset_time =
534535
usage_limit_reset_time_from_line_bytes(line, data.is_api_error_message);
535536
let model = data.message.model.as_ref().and_then(|model| {
@@ -555,6 +556,7 @@ fn read_usage_file(
555556
model,
556557
usage_limit_reset_time,
557558
missing_pricing_model,
559+
stored_cost,
558560
};
559561
let mut advisor_entries = Vec::new();
560562
for (index, advisor) in advisor_usages_from_line(line).into_iter().enumerate() {
@@ -594,6 +596,7 @@ fn read_usage_file(
594596
model: Some(advisor.model),
595597
usage_limit_reset_time,
596598
missing_pricing_model,
599+
stored_cost: None,
597600
});
598601
}
599602
loaded_file.entries.push(entry);
@@ -1577,6 +1580,7 @@ mod tests {
15771580
model: Some("claude-sonnet-4-20250514".to_string()),
15781581
usage_limit_reset_time: None,
15791582
missing_pricing_model: None,
1583+
stored_cost: None,
15801584
}
15811585
}
15821586
}

‎rust/adapters/codebuff/src/loader.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fn to_loaded_entry(
8787
missing_pricing_model,
8888
message_count: None,
8989
data,
90+
stored_cost: None,
9091
}
9192
}
9293

@@ -207,6 +208,7 @@ mod tests {
207208
usage_limit_reset_time: None,
208209
missing_pricing_model: None,
209210
message_count: None,
211+
stored_cost: None,
210212
};
211213
let rows = summarize_entries(&[entry], AgentReportKind::Daily).unwrap();
212214
let report = report_from_rows(&rows, AgentReportKind::Daily);

‎rust/adapters/common/src/report.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ mod tests {
324324
extra_total_tokens: 4,
325325
cost: 0.15,
326326
missing_pricing: false,
327+
stored_cost: None,
327328
},
328329
ModelBreakdown {
329330
model_name: "model-b".to_string(),
@@ -334,6 +335,7 @@ mod tests {
334335
extra_total_tokens: 3,
335336
cost: 0.10,
336337
missing_pricing: false,
338+
stored_cost: None,
337339
},
338340
],
339341
project: None,

‎rust/adapters/copilot/src/loader.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::{
1616
use crate::{
1717
LoadedEntry, Result, TimestampMs, TokenUsageRaw, UsageEntry, UsageMessage,
1818
calculate_cost_for_usage_at, cli::CostMode, date_range_bounds_ms, debug_log, format_date_tz,
19-
missing_pricing_model_for_usage, parse_tz, read_files_parallel,
19+
missing_pricing_model_for_usage, parse_tz, read_files_parallel, stored_cost_for_mode,
2020
};
2121

2222
pub fn load_entries(
@@ -503,6 +503,7 @@ fn usage_entry_to_loaded(
503503
mode,
504504
Some(pricing),
505505
);
506+
let stored_cost = stored_cost_for_mode(entry.cost_usd, mode);
506507
LoadedEntry {
507508
date: format_date_tz(entry.timestamp, tz),
508509
timestamp: entry.timestamp,
@@ -517,6 +518,7 @@ fn usage_entry_to_loaded(
517518
data,
518519
usage_limit_reset_time: None,
519520
missing_pricing_model,
521+
stored_cost,
520522
}
521523
}
522524

0 commit comments

Comments
 (0)