@@ -7,6 +7,9 @@ import { prefetchCodexPricing } from './_macro.ts' with { type: 'macro' };
77import { logger } from './logger.ts' ;
88
99const CODEX_PROVIDER_PREFIXES = [ 'openai/' , 'azure/' , 'openrouter/openai/' ] ;
10+ const CODEX_MODEL_ALIASES : Record < string , string > = {
11+ 'gpt-5-codex' : 'gpt-5' ,
12+ } ;
1013
1114function toPerMillion ( value : number | undefined , fallback ?: number ) : number {
1215 const perToken = value ?? fallback ?? 0 ;
@@ -37,21 +40,26 @@ export class CodexPricingSource implements PricingSource, Disposable {
3740 }
3841
3942 async getPricing ( model : string ) : Promise < ModelPricing > {
40- const pricingResult = await this . fetcher . getModelPricing ( model ) ;
41- if ( Result . isFailure ( pricingResult ) ) {
42- throw pricingResult . error ;
43- }
43+ const alias = CODEX_MODEL_ALIASES [ model ] ;
44+ const lookupCandidates = Array . from ( new Set ( [ model , alias ] . filter ( ( candidate ) : candidate is string => candidate != null ) ) ) ;
45+
46+ for ( const candidate of lookupCandidates ) {
47+ const pricingResult = await this . fetcher . getModelPricing ( candidate ) ;
48+ if ( Result . isFailure ( pricingResult ) ) {
49+ throw pricingResult . error ;
50+ }
4451
45- const pricing = pricingResult . value ;
46- if ( pricing == null ) {
47- throw new Error ( `Pricing not found for model ${ model } ` ) ;
52+ const pricing = pricingResult . value ;
53+ if ( pricing != null ) {
54+ return {
55+ inputCostPerMToken : toPerMillion ( pricing . input_cost_per_token ) ,
56+ cachedInputCostPerMToken : toPerMillion ( pricing . cache_read_input_token_cost , pricing . input_cost_per_token ) ,
57+ outputCostPerMToken : toPerMillion ( pricing . output_cost_per_token ) ,
58+ } ;
59+ }
4860 }
4961
50- return {
51- inputCostPerMToken : toPerMillion ( pricing . input_cost_per_token ) ,
52- cachedInputCostPerMToken : toPerMillion ( pricing . cache_read_input_token_cost , pricing . input_cost_per_token ) ,
53- outputCostPerMToken : toPerMillion ( pricing . output_cost_per_token ) ,
54- } ;
62+ throw new Error ( `Pricing not found for model ${ model } ` ) ;
5563 }
5664}
5765
@@ -69,7 +77,7 @@ if (import.meta.vitest != null) {
6977 } ) ,
7078 } ) ;
7179
72- const pricing = await source . getPricing ( 'gpt-5' ) ;
80+ const pricing = await source . getPricing ( 'gpt-5-codex ' ) ;
7381 expect ( pricing . inputCostPerMToken ) . toBeCloseTo ( 1.25 ) ;
7482 expect ( pricing . outputCostPerMToken ) . toBeCloseTo ( 10 ) ;
7583 expect ( pricing . cachedInputCostPerMToken ) . toBeCloseTo ( 0.125 ) ;
0 commit comments