99 */
1010import type { UsageCostSource , UsageModelPriceOverride } from "@t3tools/contracts" ;
1111
12- import type { UsageRecord } from "./usageTranscripts.ts" ;
12+ import type { UsageRecord , UsageSpeed } from "./usageTranscripts.ts" ;
1313
14- /**
15- * The subset of a LiteLLM entry we price against. All values are USD per token.
16- *
17- * LiteLLM also publishes tiered variants (`*_above_272k_tokens`, `*_flex`,
18- * `*_priority`, `*_batches`). We deliberately price at the base tier: the
19- * transcripts don't record which tier served a request, so anything else would
20- * be a guess dressed up as precision.
21- */
22- export interface ModelRate {
14+ /** Token rates for one billing speed. All values are USD per token. */
15+ export interface TokenRates {
2316 readonly inputCostPerToken : number ;
2417 readonly outputCostPerToken : number ;
2518 readonly cacheReadCostPerToken : number ;
2619 readonly cacheCreationCostPerToken : number ;
20+ }
21+
22+ /**
23+ * The subset of a LiteLLM entry we price against: standard rates, plus rates
24+ * for each faster speed the model publishes. A request at a speed with no
25+ * published rates bills at the standard rates.
26+ *
27+ * LiteLLM also publishes `*_above_272k_tokens`, `*_flex`, and `*_batches`
28+ * variants. Transcripts don't record those, so we don't price them.
29+ */
30+ export interface ModelRate extends TokenRates {
2731 /**
28- * Multiple of the rates above billed for a fast-mode request, from LiteLLM's
29- * `provider_specific_entry.fast`. `1` when the model publishes no fast tier .
32+ * From LiteLLM's `provider_specific_entry.fast` multiple (Claude fast mode),
33+ * or else its `*_priority` rates (Codex `priority`) .
3034 */
31- readonly fastMultiplier : number ;
35+ readonly fast : TokenRates | null ;
36+ /** From LiteLLM's `*_ultrafast` rates (Codex `ultrafast`). */
37+ readonly ultrafast : TokenRates | null ;
3238}
3339
3440export type RateTable = ReadonlyMap < string , ModelRate > ;
3541
3642/**
3743 * Custom IDs keep their case, provider prefix, and variant suffix. Custom rates
38- * apply as entered, fast-mode requests included .
44+ * apply as entered, at every speed .
3945 */
4046export function createOverrideRateTable (
4147 overrides : Readonly < Record < string , UsageModelPriceOverride > > ,
@@ -50,31 +56,68 @@ export function createOverrideRateTable(
5056 ( prices . cacheReadCostPerMillionTokens ?? prices . inputCostPerMillionTokens ) / 1_000_000 ,
5157 cacheCreationCostPerToken :
5258 ( prices . cacheWriteCostPerMillionTokens ?? prices . inputCostPerMillionTokens ) / 1_000_000 ,
53- fastMultiplier : 1 ,
59+ fast : null ,
60+ ultrafast : null ,
5461 } ,
5562 ] ) ,
5663 ) ;
5764}
5865
59- /** Raw shape of one LiteLLM entry, narrowed to the fields we read. */
60- interface LiteLlmEntry {
61- readonly input_cost_per_token ?: unknown ;
62- readonly output_cost_per_token ?: unknown ;
63- readonly cache_read_input_token_cost ?: unknown ;
64- readonly cache_creation_input_token_cost ?: unknown ;
65- readonly provider_specific_entry ?: unknown ;
66- }
66+ /** One raw LiteLLM entry. Field names carry a tier suffix, e.g. `_priority`. */
67+ type LiteLlmEntry = Readonly < Record < string , unknown > > ;
6768
6869function finiteNumber ( value : unknown ) : number | null {
6970 return typeof value === "number" && Number . isFinite ( value ) ? value : null ;
7071}
7172
73+ /**
74+ * Reads one rate set, `suffix` selecting a tier such as `_priority`. Returns
75+ * `null` without both an input and an output rate.
76+ *
77+ * Anthropic bills cache reads at a discount and cache writes at a premium.
78+ * When the standard tier omits them, cached input is priced as plain input
79+ * rather than as free. A faster tier that omits them keeps the standard tier's
80+ * cache-to-input ratio.
81+ */
82+ function readTokenRates (
83+ entry : LiteLlmEntry ,
84+ suffix : string ,
85+ standard ?: TokenRates ,
86+ ) : TokenRates | null {
87+ const input = finiteNumber ( entry [ `input_cost_per_token${ suffix } ` ] ) ;
88+ const output = finiteNumber ( entry [ `output_cost_per_token${ suffix } ` ] ) ;
89+ if ( input === null || output === null ) return null ;
90+ const cacheRate = ( name : string , field : "cacheReadCostPerToken" | "cacheCreationCostPerToken" ) =>
91+ finiteNumber ( entry [ `${ name } ${ suffix } ` ] ) ??
92+ ( standard !== undefined && standard . inputCostPerToken > 0
93+ ? ( standard [ field ] / standard . inputCostPerToken ) * input
94+ : input ) ;
95+ return {
96+ inputCostPerToken : input ,
97+ outputCostPerToken : output ,
98+ cacheReadCostPerToken : cacheRate ( "cache_read_input_token_cost" , "cacheReadCostPerToken" ) ,
99+ cacheCreationCostPerToken : cacheRate (
100+ "cache_creation_input_token_cost" ,
101+ "cacheCreationCostPerToken" ,
102+ ) ,
103+ } ;
104+ }
105+
106+ function scaleTokenRates ( rates : TokenRates , multiple : number ) : TokenRates {
107+ return {
108+ inputCostPerToken : rates . inputCostPerToken * multiple ,
109+ outputCostPerToken : rates . outputCostPerToken * multiple ,
110+ cacheReadCostPerToken : rates . cacheReadCostPerToken * multiple ,
111+ cacheCreationCostPerToken : rates . cacheCreationCostPerToken * multiple ,
112+ } ;
113+ }
114+
72115/** Reads `provider_specific_entry.fast`, e.g. `2` for Claude Opus 5.5. */
73- function fastMultiplier ( entry : LiteLlmEntry ) : number {
74- const specific = entry . provider_specific_entry ;
75- if ( typeof specific !== "object" || specific === null ) return 1 ;
116+ function fastMultiplier ( entry : LiteLlmEntry ) : number | null {
117+ const specific = entry [ " provider_specific_entry" ] ;
118+ if ( typeof specific !== "object" || specific === null ) return null ;
76119 const fast = finiteNumber ( ( specific as Record < string , unknown > ) [ "fast" ] ) ;
77- return fast !== null && fast > 0 ? fast : 1 ;
120+ return fast !== null && fast > 0 ? fast : null ;
78121}
79122
80123/**
@@ -94,21 +137,19 @@ export function parseRateTable(document: unknown): RateTable {
94137 for ( const [ name , raw ] of Object . entries ( document as Record < string , unknown > ) ) {
95138 if ( typeof raw !== "object" || raw === null ) continue ;
96139 const entry = raw as LiteLlmEntry ;
97- const input = finiteNumber ( entry . input_cost_per_token ) ;
98- const output = finiteNumber ( entry . output_cost_per_token ) ;
99- if ( input === null || output === null ) continue ;
140+ const standard = readTokenRates ( entry , "" ) ;
141+ if ( standard === null ) continue ;
100142
101143 const key = normalizeRateKey ( name ) ;
102144 if ( key . length === 0 ) continue ;
145+ const multiple = fastMultiplier ( entry ) ;
103146 table . set ( key , {
104- inputCostPerToken : input ,
105- outputCostPerToken : output ,
106- // Anthropic bills cache reads at a discount and cache writes at a
107- // premium. When a model omits them, cached input is priced as plain
108- // input rather than as free.
109- cacheReadCostPerToken : finiteNumber ( entry . cache_read_input_token_cost ) ?? input ,
110- cacheCreationCostPerToken : finiteNumber ( entry . cache_creation_input_token_cost ) ?? input ,
111- fastMultiplier : fastMultiplier ( entry ) ,
147+ ...standard ,
148+ fast :
149+ multiple === null
150+ ? readTokenRates ( entry , "_priority" , standard )
151+ : scaleTokenRates ( standard , multiple ) ,
152+ ultrafast : readTokenRates ( entry , "_ultrafast" , standard ) ,
112153 } ) ;
113154 }
114155
@@ -131,16 +172,29 @@ export function parseRateTable(document: unknown): RateTable {
131172 return table ;
132173}
133174
134- function sameRate ( a : ModelRate , b : ModelRate ) : boolean {
175+ function sameTokenRates ( a : TokenRates | null , b : TokenRates | null ) : boolean {
176+ if ( a === null || b === null ) return a === b ;
135177 return (
136178 a . inputCostPerToken === b . inputCostPerToken &&
137179 a . outputCostPerToken === b . outputCostPerToken &&
138180 a . cacheReadCostPerToken === b . cacheReadCostPerToken &&
139- a . cacheCreationCostPerToken === b . cacheCreationCostPerToken &&
140- a . fastMultiplier === b . fastMultiplier
181+ a . cacheCreationCostPerToken === b . cacheCreationCostPerToken
141182 ) ;
142183}
143184
185+ function sameRate ( a : ModelRate , b : ModelRate ) : boolean {
186+ return (
187+ sameTokenRates ( a , b ) &&
188+ sameTokenRates ( a . fast , b . fast ) &&
189+ sameTokenRates ( a . ultrafast , b . ultrafast )
190+ ) ;
191+ }
192+
193+ /** The rates a request at `speed` bills at. */
194+ function ratesAt ( rate : ModelRate , speed : UsageSpeed ) : TokenRates {
195+ return ( speed === "standard" ? null : rate [ speed ] ) ?? rate ;
196+ }
197+
144198function normalizeRateKey ( model : string ) : string {
145199 return model . trim ( ) . toLowerCase ( ) ;
146200}
@@ -186,7 +240,7 @@ export function lookupRate(table: RateTable, model: string): ModelRate | null {
186240/** The parts of a transcript record that decide its price. */
187241export type PricedRecord = Pick <
188242 UsageRecord ,
189- "model" | "rateModel" | "totals" | "fast " | "reportedCostUsd"
243+ "model" | "rateModel" | "totals" | "speed " | "reportedCostUsd"
190244> ;
191245
192246export interface PricedUsage {
@@ -214,14 +268,13 @@ export function priceUsage(
214268 const rate = override ?? lookupRate ( table , record . rateModel ?? model ) ;
215269 if ( rate === null ) return { costUsd : 0 , costSource : "unpriced" } ;
216270
217- const standardCostUsd =
218- totals . uncachedInputTokens * rate . inputCostPerToken +
219- totals . cachedInputTokens * rate . cacheReadCostPerToken +
220- totals . cacheCreationTokens * rate . cacheCreationCostPerToken +
221- totals . outputTokens * rate . outputCostPerToken ;
222-
271+ const rates = ratesAt ( rate , record . speed ) ;
223272 return {
224- costUsd : standardCostUsd * ( record . fast ? rate . fastMultiplier : 1 ) ,
273+ costUsd :
274+ totals . uncachedInputTokens * rates . inputCostPerToken +
275+ totals . cachedInputTokens * rates . cacheReadCostPerToken +
276+ totals . cacheCreationTokens * rates . cacheCreationCostPerToken +
277+ totals . outputTokens * rates . outputCostPerToken ,
225278 costSource : "modelPriced" ,
226279 } ;
227280}
@@ -238,9 +291,6 @@ export function cacheSavingsUsd(
238291 const rate =
239292 overrides ?. get ( record . model . trim ( ) ) ?? lookupRate ( table , record . rateModel ?? record . model ) ;
240293 if ( rate === null ) return 0 ;
241- return (
242- record . totals . cachedInputTokens *
243- ( rate . inputCostPerToken - rate . cacheReadCostPerToken ) *
244- ( record . fast ? rate . fastMultiplier : 1 )
245- ) ;
294+ const rates = ratesAt ( rate , record . speed ) ;
295+ return record . totals . cachedInputTokens * ( rates . inputCostPerToken - rates . cacheReadCostPerToken ) ;
246296}
0 commit comments