@@ -44,6 +44,9 @@ function finiteNumber(value: unknown): number | null {
4444 * Entries without both an input and an output rate are dropped: a half-priced
4545 * model would silently under-report cost, which is worse than reporting the
4646 * model as unpriced.
47+ *
48+ * Entries keep their full normalized key; a bare name is aliased only when no
49+ * canonical entry exists and every qualified entry has the same rate.
4750 */
4851export function parseRateTable ( document : unknown ) : RateTable {
4952 const table = new Map < string , ModelRate > ( ) ;
@@ -56,7 +59,9 @@ export function parseRateTable(document: unknown): RateTable {
5659 const output = finiteNumber ( entry . output_cost_per_token ) ;
5760 if ( input === null || output === null ) continue ;
5861
59- table . set ( normalizeModelName ( name ) , {
62+ const key = normalizeRateKey ( name ) ;
63+ if ( key . length === 0 ) continue ;
64+ table . set ( key , {
6065 inputCostPerToken : input ,
6166 outputCostPerToken : output ,
6267 // Anthropic bills cache reads at a discount and cache writes at a
@@ -66,20 +71,52 @@ export function parseRateTable(document: unknown): RateTable {
6671 cacheCreationCostPerToken : finiteNumber ( entry . cache_creation_input_token_cost ) ?? input ,
6772 } ) ;
6873 }
74+
75+ // `null` marks a bare name claimed at conflicting rates: no alias for it.
76+ const aliasCandidates = new Map < string , ModelRate | null > ( ) ;
77+ for ( const [ key , rate ] of table ) {
78+ const alias = bareModelName ( key ) ;
79+ if ( alias . length === 0 || alias === key || table . has ( alias ) ) continue ;
80+ const held = aliasCandidates . get ( alias ) ;
81+ if ( held === undefined ) {
82+ aliasCandidates . set ( alias , rate ) ;
83+ } else if ( held !== null && ! sameRate ( held , rate ) ) {
84+ aliasCandidates . set ( alias , null ) ;
85+ }
86+ }
87+ for ( const [ alias , rate ] of aliasCandidates ) {
88+ if ( rate !== null ) table . set ( alias , rate ) ;
89+ }
90+
6991 return table ;
7092}
7193
94+ function sameRate ( a : ModelRate , b : ModelRate ) : boolean {
95+ return (
96+ a . inputCostPerToken === b . inputCostPerToken &&
97+ a . outputCostPerToken === b . outputCostPerToken &&
98+ a . cacheReadCostPerToken === b . cacheReadCostPerToken &&
99+ a . cacheCreationCostPerToken === b . cacheCreationCostPerToken
100+ ) ;
101+ }
102+
103+ function normalizeRateKey ( model : string ) : string {
104+ return model . trim ( ) . toLowerCase ( ) ;
105+ }
106+
72107/**
73108 * Canonicalises a model name for lookup.
74109 *
75- * Strips a `provider/` prefix (LiteLLM publishes both `claude-opus-5` and
76- * `anthropic/claude-opus-5`) and lowercases, since transcripts are inconsistent
77- * about casing.
110+ * Strips a `provider/` prefix and lowercases, since transcripts are
111+ * inconsistent about casing.
78112 */
79113export function normalizeModelName ( model : string ) : string {
80- const trimmed = model . trim ( ) . toLowerCase ( ) ;
81- const slash = trimmed . lastIndexOf ( "/" ) ;
82- return slash === - 1 ? trimmed : trimmed . slice ( slash + 1 ) ;
114+ return bareModelName ( normalizeRateKey ( model ) ) ;
115+ }
116+
117+ function bareModelName ( key : string ) : string {
118+ const slash = key . lastIndexOf ( "/" ) ;
119+ return slash === - 1 ? key : key . slice ( slash + 1 ) ;
83120}
84121
85122/**
@@ -99,9 +136,10 @@ const UNPRICEABLE_MODELS = new Set([
99136] ) ;
100137
101138export function lookupRate ( table : RateTable , model : string ) : ModelRate | null {
102- const normalized = normalizeModelName ( model ) ;
103- if ( normalized . length === 0 || UNPRICEABLE_MODELS . has ( normalized ) ) return null ;
104- return table . get ( normalized ) ?? null ;
139+ const key = normalizeRateKey ( model ) ;
140+ const bareName = bareModelName ( key ) ;
141+ if ( bareName . length === 0 || UNPRICEABLE_MODELS . has ( bareName ) ) return null ;
142+ return table . get ( key ) ?? null ;
105143}
106144
107145export interface PricedUsage {
0 commit comments