Skip to content

Commit 72ebaca

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/claude-date-bounded-loading
2 parents 86e8b28 + 448fff7 commit 72ebaca

5 files changed

Lines changed: 116 additions & 18 deletions

File tree

‎.github/workflows/pr-gate.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
contents: read
2121
issues: read
2222
pull-requests: read
23-
id-token: none
23+
id-token: write
2424
outputs:
2525
access_outcome: ${{ steps.access.outcome }}
2626
skip: ${{ steps.access.outputs.skip }}

‎flake.lock‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"preinstall": "npx only-allow pnpm"
1313
},
1414
"dependencies": {},
15-
"packageManager": "[email protected].0"
15+
"packageManager": "[email protected].1"
1616
}

‎rust/crates/ccusage-core/src/models-dev-pricing.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25468,7 +25468,7 @@
2546825468
"output": 0.52
2546925469
},
2547025470
"limit": {
25471-
"context": 131072
25471+
"context": 262144
2547225472
}
2547325473
},
2547425474
"qwen/qwen3-vl-30b-a3b-thinking": {
@@ -32542,8 +32542,8 @@
3254232542
"~moonshotai/kimi-latest": {
3254332543
"cost": {
3254432544
"cache_read": 0.3,
32545-
"input": 1.35,
32546-
"output": 11.5
32545+
"input": 0.8845,
32546+
"output": 10.5346
3254732547
},
3254832548
"exactOnly": true,
3254932549
"limit": {

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

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ pub struct PricingMap {
423423
enable_models_dev_fallback: bool,
424424
enable_embedded_models_dev_fallback: bool,
425425
find_cache: OnceLock<Mutex<FxHashMap<String, Option<Pricing>>>>,
426+
/// Every entry key under its normalized spelling, mapped to the one key
427+
/// carrying that spelling, or `None` when several keys share it.
428+
///
429+
/// Built on the first normalized lookup so [`Self::find_exact_normalized`]
430+
/// does not rescan the whole table per call; parsers that price every
431+
/// event through it would otherwise spend most of a report there. Keys
432+
/// rather than rates are stored, so only a change to the key set has to
433+
/// drop it.
434+
normalized_keys: OnceLock<FxHashMap<String, Option<String>>>,
426435
}
427436

428437
/// The ids of [`PricingMap::exact_only`], indexed both as written and under the
@@ -517,6 +526,7 @@ impl Default for PricingMap {
517526
enable_models_dev_fallback: false,
518527
enable_embedded_models_dev_fallback: false,
519528
find_cache: OnceLock::new(),
529+
normalized_keys: OnceLock::new(),
520530
}
521531
}
522532
}
@@ -1465,12 +1475,26 @@ impl PricingMap {
14651475
.and_then(|id| self.entries.get(id).copied());
14661476
}
14671477

1468-
let normalized_model = normalized_pricing_key(model);
1469-
let mut matches = self.entries.iter().filter(|(candidate, _)| {
1470-
normalized_pricing_key(candidate).as_ref() == normalized_model.as_ref()
1471-
});
1472-
let (_, pricing) = matches.next()?;
1473-
matches.next().is_none().then_some(*pricing)
1478+
let key = self
1479+
.normalized_keys()
1480+
.get(normalized_pricing_key(model).as_ref())?
1481+
.as_deref()?;
1482+
self.entries.get(key).copied()
1483+
})
1484+
}
1485+
1486+
fn normalized_keys(&self) -> &FxHashMap<String, Option<String>> {
1487+
self.normalized_keys.get_or_init(|| {
1488+
let mut index = FxHashMap::default();
1489+
for key in self.entries.keys() {
1490+
index
1491+
.entry(normalized_pricing_key(key).into_owned())
1492+
// A second key with the same spelling makes it name
1493+
// neither entry.
1494+
.and_modify(|slot| *slot = None)
1495+
.or_insert_with(|| Some(key.clone()));
1496+
}
1497+
index
14741498
})
14751499
}
14761500

@@ -1838,11 +1862,12 @@ impl PricingMap {
18381862
}
18391863
}
18401864

1841-
fn clear_find_cache(&self) {
1865+
fn clear_find_cache(&mut self) {
18421866
if let Some(cache) = self.find_cache.get() {
18431867
let mut guard = cache.lock().unwrap_or_else(|error| error.into_inner());
18441868
guard.clear();
18451869
}
1870+
self.normalized_keys.take();
18461871
}
18471872

18481873
#[cfg(test)]
@@ -1902,6 +1927,7 @@ impl PricingMap {
19021927

19031928
fn put_builtin_entry(&mut self, model: String, pricing: Pricing) {
19041929
self.entries.entry(model).or_insert(pricing);
1930+
self.clear_find_cache();
19051931
}
19061932

19071933
/// z.ai's catalog needs one provider fact LiteLLM does not publish: GLM
@@ -1926,6 +1952,7 @@ impl PricingMap {
19261952
slot.insert(pricing);
19271953
}
19281954
}
1955+
self.clear_find_cache();
19291956
}
19301957

19311958
/// Last-resort rates for models ccusage must always price, used only when
@@ -3426,6 +3453,77 @@ mod tests {
34263453
assert_eq!(resolved.output, 0.000010);
34273454
}
34283455

3456+
#[test]
3457+
fn exact_fallback_lookup_rejects_a_spelling_shared_by_two_entries() {
3458+
// `acme-alpha-1.5` and `[email protected]` both normalize to
3459+
// `acme-alpha-1-5`, so that spelling names neither entry.
3460+
let mut pricing = PricingMap::default();
3461+
pricing.load_json(
3462+
r#"{
3463+
"acme-alpha-1.5": { "input_cost_per_token": 0.000001 },
3464+
"[email protected]": { "input_cost_per_token": 0.000002 }
3465+
}"#,
3466+
);
3467+
3468+
assert!(pricing.find_exact_with_fallback("acme-alpha-1-5").is_none());
3469+
}
3470+
3471+
#[test]
3472+
fn exact_fallback_lookup_sees_a_colliding_entry_added_after_a_lookup() {
3473+
let mut pricing = PricingMap::default();
3474+
pricing.put_builtin_entry(
3475+
"acme-alpha-1.5".to_string(),
3476+
Pricing {
3477+
input: 1e-6,
3478+
..Pricing::empty()
3479+
},
3480+
);
3481+
let resolved = pricing
3482+
.find_exact_with_fallback("acme-alpha-1-5")
3483+
.expect("a single normalized match should resolve");
3484+
assert_eq!(resolved.input, 1e-6);
3485+
3486+
pricing.put_builtin_entry(
3487+
"[email protected]".to_string(),
3488+
Pricing {
3489+
input: 2e-6,
3490+
..Pricing::empty()
3491+
},
3492+
);
3493+
3494+
assert!(pricing.find_exact_with_fallback("acme-alpha-1-5").is_none());
3495+
}
3496+
3497+
#[test]
3498+
fn exact_fallback_lookup_sees_rates_patched_after_a_lookup() {
3499+
let mut pricing = PricingMap::default();
3500+
pricing.load_json(
3501+
r#"{
3502+
"zai/glm-test.5": {
3503+
"input_cost_per_token": 0.2,
3504+
"output_cost_per_token": 1.1
3505+
}
3506+
}"#,
3507+
);
3508+
let before = pricing
3509+
.find_exact_with_fallback("zai/glm-test-5")
3510+
.expect("the normalized entry should resolve");
3511+
assert!(!before.cache_read_explicit);
3512+
3513+
pricing.put_builtin_glm(
3514+
"zai/glm-test.5",
3515+
Pricing {
3516+
cache_read: 0.03,
3517+
cache_read_explicit: true,
3518+
cache_create_explicit: true,
3519+
..Pricing::empty()
3520+
},
3521+
);
3522+
3523+
let after = pricing.find_exact_with_fallback("zai/glm-test-5").unwrap();
3524+
assert_eq!(after.cache_read, 0.03);
3525+
}
3526+
34293527
#[test]
34303528
fn configured_alias_of_an_exact_only_tier_beats_a_fuzzy_match_on_the_alias() {
34313529
// The alias target is only tried after this map has answered for the

0 commit comments

Comments
 (0)