Skip to content

Commit bce58fb

Browse files
committed
codex: handle pricing aliases efficiently
1 parent 84ee2d9 commit bce58fb

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

‎apps/codex/src/pricing.ts‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { prefetchCodexPricing } from './_macro.ts' with { type: 'macro' };
77
import { logger } from './logger.ts';
88

99
const CODEX_PROVIDER_PREFIXES = ['openai/', 'azure/', 'openrouter/openai/'];
10-
const CODEX_MODEL_ALIASES: Record<string, string> = {
11-
'gpt-5-codex': 'gpt-5',
12-
};
10+
const CODEX_MODEL_ALIASES_MAP = new Map<string, string>([
11+
['gpt-5-codex', 'gpt-5'],
12+
]);
1313

1414
function toPerMillion(value: number | undefined, fallback?: number): number {
1515
const perToken = value ?? fallback ?? 0;
@@ -40,26 +40,32 @@ export class CodexPricingSource implements PricingSource, Disposable {
4040
}
4141

4242
async getPricing(model: string): Promise<ModelPricing> {
43-
const alias = CODEX_MODEL_ALIASES[model];
44-
const lookupCandidates = Array.from(new Set([model, alias].filter((candidate): candidate is string => candidate != null)));
43+
const directLookup = await this.fetcher.getModelPricing(model);
44+
if (Result.isFailure(directLookup)) {
45+
throw directLookup.error;
46+
}
4547

46-
for (const candidate of lookupCandidates) {
47-
const pricingResult = await this.fetcher.getModelPricing(candidate);
48-
if (Result.isFailure(pricingResult)) {
49-
throw pricingResult.error;
48+
let pricing = directLookup.value;
49+
if (pricing == null) {
50+
const alias = CODEX_MODEL_ALIASES_MAP.get(model);
51+
if (alias != null) {
52+
const aliasLookup = await this.fetcher.getModelPricing(alias);
53+
if (Result.isFailure(aliasLookup)) {
54+
throw aliasLookup.error;
55+
}
56+
pricing = aliasLookup.value;
5057
}
58+
}
5159

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-
}
60+
if (pricing == null) {
61+
throw new Error(`Pricing not found for model ${model}`);
6062
}
6163

62-
throw new Error(`Pricing not found for model ${model}`);
64+
return {
65+
inputCostPerMToken: toPerMillion(pricing.input_cost_per_token),
66+
cachedInputCostPerMToken: toPerMillion(pricing.cache_read_input_token_cost, pricing.input_cost_per_token),
67+
outputCostPerMToken: toPerMillion(pricing.output_cost_per_token),
68+
};
6369
}
6470
}
6571

0 commit comments

Comments
 (0)