126
let tokenPrices: ICAPIModelBilling['tokenPrices'] = undefined;
127
if (rawTokenPrices && typeof rawTokenPrices === 'object') {
128
>
// The CAPI snake_case format nests prices under `default` / `long_context` tiers;
agentModelPricing.ts
129
>
// the camelCase format flattens them at the top level of `tokenPrices`.
130
>
const defaultTier = rawTokenPrices.default as Record<string, unknown> | undefined;
131
>
const hasDefault = defaultTier && typeof defaultTier === 'object';
132
>
const batchSize = asNumber(rawTokenPrices.batchSize) ?? asNumber(rawTokenPrices.batch_size) ?? 1_000_000;
133
>
const scale = batchSize > 0 ? 1_000_000 / batchSize : 1;
134
>
const price = (...values: unknown[]): number | undefined => {
135
>
const value = values.map(asNumber).find(candidate => candidate !== undefined);
136
>
return value === undefined ? undefined : value * scale;
137
>
};
138
>
139
>
const inputPrice = price(rawTokenPrices.inputPrice, hasDefault ? defaultTier.input_price : undefined);
140
>
const cachePrice = price(rawTokenPrices.cacheReadPrice, rawTokenPrices.cachePrice, hasDefault ? defaultTier.cache_read_price : undefined, hasDefault ? defaultTier.cache_price : undefined);
141
>
const cacheWritePrice = price(rawTokenPrices.cacheWritePrice, hasDefault ? defaultTier.cache_write_price : undefined);
142
>
const outputPrice = price(rawTokenPrices.outputPrice, hasDefault ? defaultTier.output_price : undefined);
143
>
const contextMax = asNumber(rawTokenPrices.maxPromptTokens) ?? asNumber(rawTokenPrices.contextMax) ?? asNumber(hasDefault ? defaultTier.max_prompt_tokens : undefined) ?? asNumber(hasDefault ? defaultTier.context_max : undefined);
144
>
145
>
const rawLong = (rawTokenPrices.longContext ?? rawTokenPrices.long_context) as Record<string, unknown> | undefined;
146
>
let longContext: { readonly contextMax?: number; readonly inputPrice?: number; readonly cachePrice?: number; readonly cacheWritePrice?: number; readonly outputPrice?: number } | undefined;
147
>
if (rawLong && typeof rawLong === 'object') {
148
>
longContext = {
149
>
inputPrice: price(rawLong.inputPrice, rawLong.input_price),
150
>
cachePrice: price(rawLong.cacheReadPrice, rawLong.cachePrice, rawLong.cache_read_price, rawLong.cache_price),
151
>
cacheWritePrice: price(rawLong.cacheWritePrice, rawLong.cache_write_price),
152
>
outputPrice: price(rawLong.outputPrice, rawLong.output_price),
153
>
contextMax: asNumber(rawLong.maxPromptTokens) ?? asNumber(rawLong.contextMax) ?? asNumber(rawLong.max_prompt_tokens) ?? asNumber(rawLong.context_max),
154
>
};
155
>
}
156
>
157
>
tokenPrices = { inputPrice, cachePrice, cacheWritePrice, outputPrice, contextMax, longContext };
158
>
}
159
160
return { multiplier, priceCategory, discountPercent, promo: normalizePromo(billing), tokenPrices };
161
}
162
164
>
return typeof v === 'number' ? v : undefined;
165
>
}
166
167
function normalizePromo(billing: Record<string, unknown>): ICAPIModelBilling['promo'] {