Skip to content

Commit 3684c80

Browse files
committed
fix(pricing): harden compact snapshot parsing
Align the LiteLLM compact-shape detector with the build-time emitter by requiring both short price keys before compact decoding. If compact decoding fails, the parser now falls back to the full LiteLLM shape so unrelated metadata keys do not drop otherwise valid pricing rows. Also make the models.dev snapshot generator ignore empty catalog ids and fall back to the source model key. This prevents a malformed empty id from becoming the embedded pricing key while keeping the committed snapshot format unchanged.
1 parent 015de4c commit 3684c80

5 files changed

Lines changed: 59 additions & 18 deletions

File tree

‎nix/models-dev-compact.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { selectModelsDevPricingKey } from './models-dev-compact.ts';
2+
3+
it('falls back to the source model id when the catalog id is empty', () => {
4+
expect(selectModelsDevPricingKey('anthropic/claude-sonnet-4', '')).toBe(
5+
'anthropic/claude-sonnet-4',
6+
);
7+
});

‎nix/models-dev-compact.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function selectModelsDevPricingKey(modelId: string, catalogId: string | undefined): string {
2+
return catalogId != null && catalogId.length > 0 ? catalogId : modelId;
3+
}

‎nix/models-dev-gen.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* via `just gen-models-dev-pricing` (see `nix/models-dev-pricing.nix`).
1212
*/
1313
import { generateCatalog } from './packages/core/src/generate.ts';
14+
import { selectModelsDevPricingKey } from './models-dev-compact.ts';
1415

1516
/** Model ids/keys we keep; ccusage is Claude-first, so we embed Anthropic models. */
1617
const KEEP = /claude|anthropic/i;
@@ -45,7 +46,7 @@ for (const provider of Object.values(providers)) {
4546
if (cost.input == null || cost.output == null) {
4647
continue;
4748
}
48-
const pricingKey = model.id ?? modelId;
49+
const pricingKey = selectModelsDevPricingKey(modelId, model.id);
4950
if (out[pricingKey] != null) {
5051
continue;
5152
}

‎rust/crates/ccusage/src/pricing.rs‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -987,23 +987,24 @@ impl PricingMap {
987987
fn parse_litellm_pricing(value: Value) -> Option<LiteLlmPricing> {
988988
if value
989989
.as_object()
990-
.is_some_and(|entry| entry.contains_key("i"))
990+
.is_some_and(|entry| entry.contains_key("i") && entry.contains_key("o"))
991991
{
992-
let compact = serde_json::from_value::<CompactLiteLlmPricing>(value).ok()?;
993-
return Some(LiteLlmPricing {
994-
input_cost_per_token: Some(compact.i),
995-
output_cost_per_token: Some(compact.o),
996-
cache_creation_input_token_cost: compact.cc,
997-
cache_read_input_token_cost: compact.cr,
998-
input_cost_per_token_above_200k_tokens: compact.ia,
999-
output_cost_per_token_above_200k_tokens: compact.oa,
1000-
cache_creation_input_token_cost_above_200k_tokens: compact.cca,
1001-
cache_read_input_token_cost_above_200k_tokens: compact.cra,
1002-
max_input_tokens: compact.ctx,
1003-
provider_specific_entry: compact
1004-
.fast
1005-
.map(|fast| ProviderSpecificEntry { fast: Some(fast) }),
1006-
});
992+
if let Ok(compact) = serde_json::from_value::<CompactLiteLlmPricing>(value.clone()) {
993+
return Some(LiteLlmPricing {
994+
input_cost_per_token: Some(compact.i),
995+
output_cost_per_token: Some(compact.o),
996+
cache_creation_input_token_cost: compact.cc,
997+
cache_read_input_token_cost: compact.cr,
998+
input_cost_per_token_above_200k_tokens: compact.ia,
999+
output_cost_per_token_above_200k_tokens: compact.oa,
1000+
cache_creation_input_token_cost_above_200k_tokens: compact.cca,
1001+
cache_read_input_token_cost_above_200k_tokens: compact.cra,
1002+
max_input_tokens: compact.ctx,
1003+
provider_specific_entry: compact
1004+
.fast
1005+
.map(|fast| ProviderSpecificEntry { fast: Some(fast) }),
1006+
});
1007+
}
10071008
}
10081009
let pricing = serde_json::from_value::<LiteLlmPricing>(value).ok()?;
10091010
pricing
@@ -1411,6 +1412,25 @@ mod tests {
14111412
assert_eq!(pricing.context_limit("gpt-compact"), Some(123456));
14121413
}
14131414

1415+
#[test]
1416+
fn falls_back_to_full_litellm_pricing_when_compact_shape_is_incomplete() {
1417+
let mut pricing = PricingMap::default();
1418+
let loaded = pricing.load_json(
1419+
r#"{
1420+
"gpt-full-with-extra-i": {
1421+
"i": "provider metadata",
1422+
"input_cost_per_token": 0.000001,
1423+
"output_cost_per_token": 0.000010
1424+
}
1425+
}"#,
1426+
);
1427+
1428+
assert_eq!(loaded, 1);
1429+
let full = pricing.find("gpt-full-with-extra-i").unwrap();
1430+
assert_eq!(full.input, 1e-6);
1431+
assert_eq!(full.output, 10e-6);
1432+
}
1433+
14141434
#[test]
14151435
fn keeps_models_dev_fallback_disabled_for_embedded_and_offline_pricing() {
14161436
use ccusage_cli::PricingOverride;

‎vitest.config.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ export default defineConfig({
88
passWithNoTests: true,
99
watch: false,
1010
reporters: isGitHubActions ? ['default', 'github-actions'] : ['default'],
11-
projects: ['apps/*/vitest.config.ts', 'packages/*/vitest.config.ts'],
11+
projects: [
12+
'apps/*/vitest.config.ts',
13+
'packages/*/vitest.config.ts',
14+
{
15+
test: {
16+
name: 'nix',
17+
include: ['nix/**/*.test.ts'],
18+
globals: true,
19+
},
20+
},
21+
],
1222
},
1323
});

0 commit comments

Comments
 (0)