Skip to content

Commit 2500c5b

Browse files
committed
fix(pricing): resolve default family alias before fuzzy matching
The unsuffixed family alias matched the longest sibling key and selected the balanced tier rather than the documented flagship tier. Preserve exact user overrides, resolve canonical aliases before fuzzy fallback, and apply the same mapping to context limits and request-tier thresholds. Partial overrides now inherit unspecified canonical rates.
1 parent e1e47a7 commit 2500c5b

1 file changed

Lines changed: 58 additions & 5 deletions

File tree

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

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,11 @@ impl PricingMap {
453453
}
454454

455455
fn find_entry_or_alias(&self, model: &str) -> Option<Pricing> {
456-
self.find_entry(model)
456+
self.entries
457+
.get(model)
458+
.copied()
457459
.or_else(|| pricing_alias(model).and_then(|alias| self.find_entry(alias)))
460+
.or_else(|| self.find_entry(model))
458461
}
459462

460463
fn find_entry(&self, model: &str) -> Option<Pricing> {
@@ -500,8 +503,11 @@ impl PricingMap {
500503
}
501504

502505
fn context_limit_entry_or_alias(&self, model: &str) -> Option<u64> {
503-
self.context_limit_entry(model)
506+
self.context_limits
507+
.get(model)
508+
.copied()
504509
.or_else(|| pricing_alias(model).and_then(|alias| self.context_limit_entry(alias)))
510+
.or_else(|| self.context_limit_entry(model))
505511
}
506512

507513
fn context_limit_entry(&self, model: &str) -> Option<u64> {
@@ -534,6 +540,7 @@ impl PricingMap {
534540
.entries
535541
.get(model)
536542
.copied()
543+
.or_else(|| pricing_alias(model).and_then(|alias| self.entries.get(alias).copied()))
537544
.unwrap_or_else(Pricing::empty);
538545

539546
let new_input = override_value.input_cost_per_token.unwrap_or(base.input);
@@ -1353,7 +1360,9 @@ fn builtin_long_context_rates(base_model: &str) -> Option<LongContextRates> {
13531360
/// `Pricing::long_context_threshold`, and falls back to the default 200K
13541361
/// boundary used for LiteLLM `*_above_200k_tokens` data.
13551362
pub(crate) fn long_context_split_threshold(model: &str) -> u64 {
1356-
builtin_long_context_rates(model_without_date_suffix(model))
1363+
let base_model = model_without_date_suffix(model);
1364+
let canonical_model = pricing_alias(base_model).unwrap_or(base_model);
1365+
builtin_long_context_rates(canonical_model)
13571366
.map(|rates| rates.threshold)
13581367
.unwrap_or(DEFAULT_LONG_CONTEXT_THRESHOLD_TOKENS)
13591368
}
@@ -1385,10 +1394,10 @@ fn model_without_date_suffix(model: &str) -> &str {
13851394
model
13861395
}
13871396

1388-
/// Maps Codex log labels that upstream pricing sources do not publish to
1389-
/// canonical pricing keys.
1397+
/// Maps model aliases to canonical pricing keys before fuzzy matching.
13901398
fn pricing_alias(model: &str) -> Option<&'static str> {
13911399
match model {
1400+
"gpt-5.6" => Some("gpt-5.6-sol"),
13921401
"gpt-5.3-spark" => Some("gpt-5.3-codex-spark"),
13931402
_ => None,
13941403
}
@@ -2072,6 +2081,25 @@ mod tests {
20722081
assert_eq!(luna.output_above_200k, Some(9e-6));
20732082
}
20742083

2084+
#[test]
2085+
fn gpt_5_6_alias_resolves_to_sol_across_pricing_metadata() {
2086+
let pricing = PricingMap::load_embedded();
2087+
let alias = pricing.find("gpt-5.6").unwrap();
2088+
let sol = pricing.find("gpt-5.6-sol").unwrap();
2089+
2090+
assert_eq!(alias.input, sol.input);
2091+
assert_eq!(alias.output, sol.output);
2092+
assert_eq!(alias.cache_create, sol.cache_create);
2093+
assert_eq!(alias.cache_read, sol.cache_read);
2094+
assert_eq!(alias.input_above_200k, sol.input_above_200k);
2095+
assert_eq!(alias.output_above_200k, sol.output_above_200k);
2096+
assert_eq!(
2097+
pricing.context_limit("gpt-5.6"),
2098+
pricing.context_limit("gpt-5.6-sol")
2099+
);
2100+
assert_eq!(long_context_split_threshold("gpt-5.6"), 272_000);
2101+
}
2102+
20752103
#[test]
20762104
fn embedded_pricing_fills_gpt_long_context_tier_rates() {
20772105
let pricing = PricingMap::load_embedded();
@@ -2571,6 +2599,31 @@ mod tests {
25712599
assert_eq!(pricing.context_limit("custom-model"), Some(123_456));
25722600
}
25732601

2602+
#[test]
2603+
fn exact_override_wins_over_gpt_5_6_alias() {
2604+
let mut pricing = PricingMap::load_embedded();
2605+
let sol = pricing.find("gpt-5.6-sol").unwrap();
2606+
let overrides = build_overrides("gpt-5.6", |o| {
2607+
o.input_cost_per_token = Some(42e-6);
2608+
o.max_input_tokens = Some(654_321);
2609+
});
2610+
2611+
pricing.apply_overrides(overrides.iter());
2612+
2613+
let entry = pricing.find("gpt-5.6").unwrap();
2614+
assert_eq!(entry.input, 42e-6);
2615+
assert_eq!(entry.output, sol.output);
2616+
assert_eq!(entry.cache_create, sol.cache_create);
2617+
assert_eq!(entry.cache_read, sol.cache_read);
2618+
assert_eq!(entry.input_above_200k, sol.input_above_200k);
2619+
assert_eq!(entry.output_above_200k, sol.output_above_200k);
2620+
assert_eq!(entry.cache_create_above_200k, sol.cache_create_above_200k);
2621+
assert_eq!(entry.cache_read_above_200k, sol.cache_read_above_200k);
2622+
assert_eq!(entry.long_context_threshold, sol.long_context_threshold);
2623+
assert_eq!(entry.fast_multiplier, sol.fast_multiplier);
2624+
assert_eq!(pricing.context_limit("gpt-5.6"), Some(654_321));
2625+
}
2626+
25742627
#[test]
25752628
fn partial_override_preserves_existing_fields() {
25762629
let mut pricing = PricingMap::default();

0 commit comments

Comments
 (0)