Skip to content

Commit e7ab886

Browse files
committed
fix(pricing): resolve Claude dot-separated model aliases
Normalize pricing lookup keys so Claude model identifiers that use dot or provider separators, such as claude-opus-4.7, resolve to the same embedded pricing as their canonical hyphenated forms. This prevents Opus 4.7 usage from falling back to older Opus 4 pricing in calculate mode while keeping exact model entries authoritative and requiring boundaries for fuzzy matches. Also updates embedded context fallbacks for current Claude Opus 4.6, Opus 4.7, and Sonnet 4.6 models to match their 1M context windows. Validation: pnpm run format; env -u CFLAGS direnv exec . cargo test --manifest-path rust/Cargo.toml -p ccusage pricing::tests:: -- --nocapture; env -u CFLAGS direnv exec . pnpm run test; pnpm typecheck
1 parent bf84d4b commit e7ab886

1 file changed

Lines changed: 121 additions & 6 deletions

File tree

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

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::time::Duration;
1+
use std::{borrow::Cow, time::Duration};
22

33
use serde::Deserialize;
44

@@ -176,9 +176,12 @@ impl PricingMap {
176176

177177
pub(crate) fn find(&self, model: &str) -> Option<Pricing> {
178178
self.entries.get(model).copied().or_else(|| {
179+
let normalized_model = normalized_pricing_key(model);
179180
self.entries
180181
.iter()
181-
.filter(|(candidate, _)| model.contains(*candidate) || candidate.contains(model))
182+
.filter(|(candidate, _)| {
183+
pricing_key_matches(candidate, model, normalized_model.as_ref())
184+
})
182185
.max_by(|(left, _), (right, _)| {
183186
left.len().cmp(&right.len()).then_with(|| right.cmp(left))
184187
})
@@ -188,9 +191,12 @@ impl PricingMap {
188191

189192
pub(crate) fn context_limit(&self, model: &str) -> Option<u64> {
190193
self.context_limits.get(model).copied().or_else(|| {
194+
let normalized_model = normalized_pricing_key(model);
191195
self.context_limits
192196
.iter()
193-
.filter(|(candidate, _)| model.contains(*candidate) || candidate.contains(model))
197+
.filter(|(candidate, _)| {
198+
pricing_key_matches(candidate, model, normalized_model.as_ref())
199+
})
194200
.max_by(|(left, _), (right, _)| {
195201
left.len().cmp(&right.len()).then_with(|| right.cmp(left))
196202
})
@@ -512,14 +518,18 @@ impl PricingMap {
512518
self.context_limits
513519
.insert("grok-4.3".to_string(), 1_000_000);
514520
self.context_limits.insert("gpt-5.4".to_string(), 1_050_000);
521+
for model in [
522+
"claude-opus-4-7",
523+
"claude-opus-4-6",
524+
"claude-sonnet-4-6",
525+
] {
526+
self.context_limits.insert(model.to_string(), 1_000_000);
527+
}
515528

516529
for model in [
517530
"claude-opus-4-5",
518-
"claude-opus-4-6",
519-
"claude-opus-4-7",
520531
"claude-haiku-4-5",
521532
"claude-opus-4",
522-
"claude-sonnet-4-6",
523533
"claude-sonnet-4",
524534
"claude-3-5-haiku",
525535
"claude-3-5-haiku-20241022",
@@ -532,6 +542,38 @@ impl PricingMap {
532542
}
533543
}
534544

545+
fn pricing_key_matches(candidate: &str, model: &str, normalized_model: &str) -> bool {
546+
if contains_pricing_key(model, candidate) || contains_pricing_key(candidate, model) {
547+
return true;
548+
}
549+
let normalized_candidate = normalized_pricing_key(candidate);
550+
contains_pricing_key(normalized_model, normalized_candidate.as_ref())
551+
|| contains_pricing_key(normalized_candidate.as_ref(), normalized_model)
552+
}
553+
554+
fn contains_pricing_key(value: &str, key: &str) -> bool {
555+
value.match_indices(key).any(|(index, _)| {
556+
let before = index
557+
.checked_sub(1)
558+
.and_then(|before| value.as_bytes().get(before))
559+
.copied();
560+
let after = value.as_bytes().get(index + key.len()).copied();
561+
before.is_none_or(is_pricing_key_boundary) && after.is_none_or(is_pricing_key_boundary)
562+
})
563+
}
564+
565+
fn is_pricing_key_boundary(byte: u8) -> bool {
566+
!byte.is_ascii_alphanumeric()
567+
}
568+
569+
fn normalized_pricing_key(value: &str) -> Cow<'_, str> {
570+
if value.contains(['.', '@']) {
571+
Cow::Owned(value.replace(['.', '@'], "-"))
572+
} else {
573+
Cow::Borrowed(value)
574+
}
575+
}
576+
535577
fn matches_model_suffix(part: &str, base: &str) -> bool {
536578
let Some(index) = part.rfind(base) else {
537579
return false;
@@ -713,6 +755,79 @@ mod tests {
713755
);
714756
}
715757

758+
#[test]
759+
fn embedded_pricing_resolves_opus_47_dot_model_names() {
760+
let pricing = PricingMap::load_embedded();
761+
762+
assert_eq!(pricing.find("claude-opus-4.7-20260416").unwrap().input, 5e-6);
763+
assert_eq!(pricing.context_limit("claude-opus-4.7"), Some(1_000_000));
764+
assert_eq!(
765+
pricing
766+
.find("openrouter/anthropic/claude-opus-4.7")
767+
.unwrap()
768+
.input,
769+
5e-6
770+
);
771+
}
772+
773+
#[test]
774+
fn embedded_pricing_resolves_separator_aliases_for_other_claude_models() {
775+
let pricing = PricingMap::load_embedded();
776+
let sonnet_46 = pricing.find("claude-sonnet-4-6").unwrap();
777+
let haiku_45 = pricing.find("claude-haiku-4-5").unwrap();
778+
779+
assert_eq!(
780+
pricing.find("claude-sonnet-4.6-20260416").unwrap().input,
781+
sonnet_46.input
782+
);
783+
assert_eq!(pricing.find("claude-haiku-4.5").unwrap().input, haiku_45.input);
784+
assert_eq!(
785+
pricing.context_limit("claude-sonnet-4.6"),
786+
pricing.context_limit("claude-sonnet-4-6")
787+
);
788+
assert_eq!(
789+
pricing.context_limit("claude-haiku-4.5"),
790+
pricing.context_limit("claude-haiku-4-5")
791+
);
792+
}
793+
794+
#[test]
795+
fn fuzzy_match_requires_model_key_boundaries() {
796+
let mut pricing = PricingMap::default();
797+
pricing.entries.insert(
798+
"claude-opus-4-7".to_string(),
799+
Pricing {
800+
input: 5e-6,
801+
output: 25e-6,
802+
cache_create: 6.25e-6,
803+
cache_read: 0.5e-6,
804+
cache_read_explicit: true,
805+
input_above_200k: None,
806+
output_above_200k: None,
807+
cache_create_above_200k: None,
808+
cache_read_above_200k: None,
809+
fast_multiplier: 1.0,
810+
},
811+
);
812+
pricing.entries.insert(
813+
"claude-opus-4".to_string(),
814+
Pricing {
815+
input: 15e-6,
816+
output: 75e-6,
817+
cache_create: 18.75e-6,
818+
cache_read: 1.5e-6,
819+
cache_read_explicit: true,
820+
input_above_200k: None,
821+
output_above_200k: None,
822+
cache_create_above_200k: None,
823+
cache_read_above_200k: None,
824+
fast_multiplier: 1.0,
825+
},
826+
);
827+
828+
assert_eq!(pricing.find("claude-opus-4.70").unwrap().input, 15e-6);
829+
}
830+
716831
#[test]
717832
fn fills_codex_fast_multiplier_when_litellm_pricing_omits_it() {
718833
let mut pricing = PricingMap::default();

0 commit comments

Comments
 (0)