Skip to content

Commit 69fca50

Browse files
committed
fix(pricing): warn when model pricing is missing
Track models whose token usage cannot be priced instead of silently folding their cost into zero. Propagate that internal state through Claude and aggregated reports so human-readable tables print a single warning per missing model while JSON output keeps the existing schema. Keep unknown numeric model versions from falling back to older base model prices, and cover future Claude Opus 4.9/5 names with regression tests while still allowing date-like suffixes on known versioned models.
1 parent f38b057 commit 69fca50

28 files changed

Lines changed: 220 additions & 10 deletions

File tree

‎rust/crates/ccusage/src/adapter/all/loader.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ pub(super) fn codex_group_row(
622622
cache_read_tokens: usage.cached_input_tokens,
623623
extra_total_tokens: 0,
624624
cost: codex::calculate_codex_model_cost(model, usage, pricing, speed),
625+
missing_pricing: false,
625626
}
626627
})
627628
.collect();

‎rust/crates/ccusage/src/adapter/all/report.rs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde_json::{json, Value};
55
use crate::{
66
cli::{AgentReportKind, SharedArgs, SortOrder},
77
color, format_currency, format_models_multiline, format_number, json_float, print_box_title,
8-
short_model_name, Align, Color, ModelBreakdown, Result, SimpleTable,
8+
short_model_name, Align, Color, ModelBreakdown, Result, SimpleTable, UsageSummary,
99
};
1010

1111
use super::types::AllRow;
@@ -167,13 +167,39 @@ pub(super) fn print_table(
167167
]);
168168
}
169169
table.print()?;
170+
crate::print_missing_pricing_warnings(&all_rows_as_usage_summaries(rows), shared.offline);
170171
if compact {
171172
eprintln!("\nRunning in Compact Mode");
172173
eprintln!("Expand terminal width to see cache metrics and total tokens");
173174
}
174175
Ok(())
175176
}
176177

178+
fn all_rows_as_usage_summaries(rows: &[AllRow]) -> Vec<UsageSummary> {
179+
rows.iter()
180+
.map(|row| UsageSummary {
181+
date: None,
182+
month: None,
183+
week: None,
184+
session_id: None,
185+
project_path: None,
186+
last_activity: None,
187+
input_tokens: row.input_tokens,
188+
output_tokens: row.output_tokens,
189+
cache_creation_tokens: row.cache_creation_tokens,
190+
cache_read_tokens: row.cache_read_tokens,
191+
extra_total_tokens: row.total_tokens.saturating_sub(table_total_tokens(row)),
192+
total_cost: row.total_cost,
193+
credits: None,
194+
message_count: None,
195+
models_used: row.models_used.clone(),
196+
model_breakdowns: row.model_breakdowns.clone(),
197+
project: None,
198+
versions: None,
199+
})
200+
.collect()
201+
}
202+
177203
pub(super) fn all_report_title(
178204
kind: AgentReportKind,
179205
rows: &[AllRow],

‎rust/crates/ccusage/src/adapter/all/tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ fn aggregates_model_breakdowns_across_agents() {
410410
cache_creation_tokens: 1,
411411
cache_read_tokens: 2,
412412
cost: 0.01,
413+
missing_pricing: true,
413414
..ModelBreakdown::default()
414415
},
415416
ModelBreakdown {
@@ -440,6 +441,7 @@ fn aggregates_model_breakdowns_across_agents() {
440441
assert_eq!(rows[0].model_breakdowns[1].cache_creation_tokens, 1);
441442
assert_eq!(rows[0].model_breakdowns[1].cache_read_tokens, 4);
442443
assert_eq!(rows[0].model_breakdowns[1].cost, 0.04);
444+
assert!(rows[0].model_breakdowns[1].missing_pricing);
443445
}
444446

445447
#[test]

‎rust/crates/ccusage/src/adapter/all/types.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ fn merge_model_breakdowns(
148148
b.cache_read_tokens += item.cache_read_tokens;
149149
b.extra_total_tokens += item.extra_total_tokens;
150150
b.cost += item.cost;
151+
b.missing_pricing |= item.missing_pricing;
151152
}
152153
breakdowns.sort_by(|a, b| b.cost.total_cmp(&a.cost));
153154
breakdowns
@@ -173,6 +174,7 @@ pub(super) fn aggregate_model_breakdowns(rows: &[AllRow]) -> Vec<ModelBreakdown>
173174
b.cache_read_tokens += item.cache_read_tokens;
174175
b.extra_total_tokens += item.extra_total_tokens;
175176
b.cost += item.cost;
177+
b.missing_pricing |= item.missing_pricing;
176178
}
177179
}
178180
breakdowns

‎rust/crates/ccusage/src/adapter/amp/parser.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ fn parse_ledger_events(
128128
message_count: None,
129129
model: Some(model),
130130
usage_limit_reset_time: None,
131+
missing_pricing_model: None,
131132
data,
132133
});
133134
}
@@ -229,6 +230,7 @@ fn parse_message_usage(
229230
message_count: None,
230231
model: Some(model),
231232
usage_limit_reset_time: None,
233+
missing_pricing_model: None,
232234
data,
233235
});
234236
}

‎rust/crates/ccusage/src/adapter/claude/daily.rs‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ use crate::{
1414
calculate_cost_for_usage,
1515
cli::{CostMode, SharedArgs},
1616
fast::{byte_lines, suffix_string, FxHashMap, SmallIndexVec},
17-
format_date_tz, log_level, parse_ts_timestamp, parse_tz, ModelBreakdown, PricingMap, Result,
18-
Speed, TimestampMs, TokenCounts, TokenUsageRaw, UsageSummary,
17+
format_date_tz, log_level, missing_pricing_model_for_usage, parse_ts_timestamp, parse_tz,
18+
ModelBreakdown, PricingMap, Result, Speed, TimestampMs, TokenCounts, TokenUsageRaw,
19+
UsageSummary,
1920
};
2021

2122
use super::{
@@ -113,6 +114,7 @@ struct DailyLoadedEntry {
113114
usage: TokenUsageRaw,
114115
cost: f64,
115116
model: Option<String>,
117+
missing_pricing_model: Option<String>,
116118
message_id: Option<String>,
117119
request_id: Option<String>,
118120
is_sidechain: Option<bool>,
@@ -278,6 +280,13 @@ fn read_daily_usage_file(
278280
mode,
279281
pricing,
280282
);
283+
let missing_pricing_model = missing_pricing_model_for_usage(
284+
data.message.model.as_deref(),
285+
usage,
286+
data.cost_usd,
287+
mode,
288+
pricing,
289+
);
281290
let model = data.message.model.as_ref().and_then(|model| {
282291
if model == "<synthetic>" {
283292
None
@@ -293,6 +302,7 @@ fn read_daily_usage_file(
293302
usage,
294303
cost,
295304
model,
305+
missing_pricing_model,
296306
message_id: data.message.id,
297307
request_id: data.request_id,
298308
is_sidechain: data.is_sidechain,
@@ -463,6 +473,9 @@ impl DailyAccumulator {
463473
breakdown.cache_creation_tokens += entry.usage.cache_creation_input_tokens;
464474
breakdown.cache_read_tokens += entry.usage.cache_read_input_tokens;
465475
breakdown.cost += entry.cost;
476+
if entry.missing_pricing_model.is_some() {
477+
breakdown.missing_pricing = true;
478+
}
466479
}
467480
}
468481

@@ -580,6 +593,7 @@ mod tests {
580593
},
581594
cost: 0.0,
582595
model: Some("claude-sonnet-4-20250514".to_string()),
596+
missing_pricing_model: None,
583597
message_id: Some(fixture.message_id.to_string()),
584598
request_id: Some(fixture.request_id.to_string()),
585599
is_sidechain: Some(fixture.is_sidechain),

‎rust/crates/ccusage/src/adapter/claude/mod.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use crate::{
1818
cli::{CostMode, SharedArgs},
1919
debug_log,
2020
fast::{byte_lines, suffix_string, FxHashMap, SmallIndexVec},
21-
format_date_tz, log_level, parse_ts_timestamp, parse_tz, progress, LoadedEntry, LoadedFile,
22-
PricingMap, Result, Speed, TimestampMs, UsageEntry, UsageSummary,
21+
format_date_tz, log_level, missing_pricing_model_for_usage, parse_ts_timestamp, parse_tz,
22+
progress, LoadedEntry, LoadedFile, PricingMap, Result, Speed, TimestampMs, UsageEntry,
23+
UsageSummary,
2324
};
2425

2526
#[cfg(test)]
@@ -362,6 +363,13 @@ fn read_usage_file(
362363
}
363364
let date = format_date_tz(timestamp, tz);
364365
let cost = calculate_cost(&data, mode, pricing);
366+
let missing_pricing_model = missing_pricing_model_for_usage(
367+
data.message.model.as_deref(),
368+
data.message.usage,
369+
data.cost_usd,
370+
mode,
371+
pricing,
372+
);
365373
let usage_limit_reset_time =
366374
usage_limit_reset_time_from_line_bytes(line, data.is_api_error_message);
367375
let model = data.message.model.as_ref().and_then(|model| {
@@ -386,6 +394,7 @@ fn read_usage_file(
386394
message_count: None,
387395
model,
388396
usage_limit_reset_time,
397+
missing_pricing_model,
389398
});
390399
}
391400
loaded_file
@@ -772,6 +781,7 @@ mod tests {
772781
message_count: None,
773782
model: Some("claude-sonnet-4-20250514".to_string()),
774783
usage_limit_reset_time: None,
784+
missing_pricing_model: None,
775785
}
776786
}
777787
}

‎rust/crates/ccusage/src/adapter/codebuff/loader.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ fn to_loaded_entry(
6868
credits: (entry.credits > 0.0).then_some(entry.credits),
6969
model: Some(entry.model),
7070
usage_limit_reset_time: None,
71+
missing_pricing_model: None,
7172
message_count: None,
7273
data,
7374
}
@@ -210,6 +211,7 @@ mod tests {
210211
credits: Some(1.25),
211212
model: Some("claude-sonnet-4-20250514".to_string()),
212213
usage_limit_reset_time: None,
214+
missing_pricing_model: None,
213215
message_count: None,
214216
};
215217
let rows = summarize_entries(&[entry], AgentReportKind::Daily).unwrap();

‎rust/crates/ccusage/src/adapter/copilot/loader.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ fn usage_entry_to_loaded(
9292
model: Some(entry.model),
9393
data,
9494
usage_limit_reset_time: None,
95+
missing_pricing_model: None,
9596
}
9697
}
9798

‎rust/crates/ccusage/src/adapter/droid/loader.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn to_loaded_entry(
7070
extra_total_tokens: entry.reasoning_tokens,
7171
model: Some(entry.model),
7272
usage_limit_reset_time: None,
73+
missing_pricing_model: None,
7374
message_count: None,
7475
data,
7576
}
@@ -265,6 +266,7 @@ mod tests {
265266
extra_total_tokens: 5,
266267
model: Some("claude-sonnet-4".to_string()),
267268
usage_limit_reset_time: None,
269+
missing_pricing_model: None,
268270
message_count: None,
269271
};
270272
let rows = summarize_entries(&[entry], AgentReportKind::Daily).unwrap();

0 commit comments

Comments
 (0)