Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { priceDetailed } from './evaluate.ts'
4import { compileTokenCard } from './token-card.ts'
5import { normalizeUsage, usageFormat } from './usage.ts'
6
7const source = {
8 url: 'https://example.test/pricing',
9 hash: 'a'.repeat(64),
10 extractedAt: '2026-10-15T00:00:00Z',
11}
12
13const card = (rates: Record<string, number>) => {
14 const compiled = compileTokenCard(rates, [], source)
15 if (!compiled) throw new Error('no card')
16 return compiled
17}
18
19// gpt-5: $1.25 in, $0.125 cached in, $10 out per 1M.
20const GPT_5 = card({
21 input_tokens: 1.25e-6,
22 cache_read_tokens: 0.125e-6,
23 output_tokens: 10e-6,
24})
25
26// Claude: $5 in, $6.25 5m writes, $10 1h writes, $0.5 hits, $25 out per 1M.
27const CLAUDE = card({
28 input_tokens: 5e-6,
29 cache_write_tokens: 6.25e-6,
30 cache_write_1h_tokens: 10e-6,
31 cache_read_tokens: 0.5e-6,
32 output_tokens: 25e-6,
33})
34
35const amount = (c: typeof GPT_5, usage: Record<string, unknown>) =>
36 priceDetailed(c, {}, normalizeUsage(c, usage)).amount
37
38describe('normalizeUsage', () => {
39 it('subtracts OpenAI Responses cached tokens from input', () => {
40 const usage = {
41 input_tokens: 12_000,
42 input_tokens_details: { cached_tokens: 4000 },
43 output_tokens: 800,
44 output_tokens_details: { reasoning_tokens: 300 },
45 total_tokens: 12_800,
46 }
47 expect(usageFormat(GPT_5, usage)).toBe('openai-responses')
48 expect(normalizeUsage(GPT_5, usage)).toEqual({
49 input_tokens: 8000,
50 cache_read_tokens: 4000,
51 output_tokens: 800,
52 })
53 // 8000 × 1.25/M + 4000 × 0.125/M + 800 × 10/M
54 expect(amount(GPT_5, usage)).toBeCloseTo(0.0185, 10)
55 })
56
57 it('subtracts OpenAI cache writes from the prompt total', () => {
58 // Writes bill at 1.25× input: 1.25e-6 × 1.25.
59 const priced = card({
60 input_tokens: 1.25e-6,
61 cache_read_tokens: 0.125e-6,
62 cache_write_tokens: 1.5625e-6,
63 output_tokens: 10e-6,
64 })
65 const usage = {
66 input_tokens: 10_000,
67 input_tokens_details: { cached_tokens: 4000, cache_write_tokens: 2000 },
68 output_tokens: 100,
69 }
70 expect(normalizeUsage(priced, usage)).toEqual({
71 input_tokens: 4000,
72 cache_read_tokens: 4000,
73 cache_write_tokens: 2000,
74 output_tokens: 100,
75 })
76 // 4000×1.25 + 4000×0.125 + 2000×1.5625 + 100×10, per 1M
77 expect(amount(priced, usage)).toBeCloseTo(0.009625, 10)
78 const chat = {
79 prompt_tokens: 10_000,
80 prompt_tokens_details: { cached_tokens: 4000, cache_write_tokens: 2000 },
81 completion_tokens: 100,
82 }
83 expect(normalizeUsage(priced, chat)).toEqual({
84 input_tokens: 4000,
85 cache_read_tokens: 4000,
86 cache_write_tokens: 2000,
87 output_tokens: 100,
88 })
89 // A zero write is not a lever. A write the card does not price is refused.
90 expect(
91 normalizeUsage(GPT_5, {
92 ...usage,
93 input_tokens_details: { cached_tokens: 4000, cache_write_tokens: 0 },
94 }),
95 ).toEqual({
96 input_tokens: 6000,
97 cache_read_tokens: 4000,
98 output_tokens: 100,
99 })
100 expect(() =>
101 priceDetailed(
102 GPT_5,
103 {},
104 normalizeUsage(GPT_5, {
105 input_tokens: 100,
106 input_tokens_details: { cache_write_tokens: 40 },
107 output_tokens: 10,
108 }),
109 ),
110 ).toThrow(expect.objectContaining({ code: 'unknown-usage-key' }))
111 })
112
113 it('subtracts OpenAI Chat cached tokens from prompt tokens', () => {
114 const usage = {
115 prompt_tokens: 12_000,
116 completion_tokens: 800,
117 total_tokens: 12_800,
118 prompt_tokens_details: { cached_tokens: 4000, audio_tokens: 0 },
119 completion_tokens_details: {
120 reasoning_tokens: 300,
121 audio_tokens: 0,
122 accepted_prediction_tokens: 0,
123 rejected_prediction_tokens: 0,
124 },
125 }
126 expect(usageFormat(GPT_5, usage)).toBe('openai-chat')
127 expect(amount(GPT_5, usage)).toBeCloseTo(0.0185, 10)
128 })
129
130 it('splits reasoning out of output only when the card prices it', () => {
131 const usage = {
132 prompt_tokens: 100,
133 completion_tokens: 50,
134 completion_tokens_details: { reasoning_tokens: 30 },
135 }
136 expect(normalizeUsage(GPT_5, usage)).toEqual({
137 input_tokens: 100,
138 output_tokens: 50,
139 })
140 const reasoning = card({
141 input_tokens: 1e-6,
142 output_tokens: 2e-6,
143 reasoning_tokens: 3e-6,
144 })
145 expect(normalizeUsage(reasoning, usage)).toEqual({
146 input_tokens: 100,
147 output_tokens: 20,
148 reasoning_tokens: 30,
149 })
150 })
151
152 it('maps Anthropic cache reads and writes, split by TTL', () => {
153 const usage = {
154 input_tokens: 1000,
155 cache_read_input_tokens: 1_000_000,
156 cache_creation_input_tokens: 3000,
157 cache_creation: {
158 ephemeral_5m_input_tokens: 1000,
159 ephemeral_1h_input_tokens: 2000,
160 },
161 output_tokens: 400,
162 service_tier: 'standard',
163 }
164 expect(usageFormat(CLAUDE, usage)).toBe('anthropic')
165 expect(normalizeUsage(CLAUDE, usage)).toEqual({
166 input_tokens: 1000,
167 cache_read_tokens: 1_000_000,
168 cache_write_tokens: 1000,
169 cache_write_1h_tokens: 2000,
170 output_tokens: 400,
171 })
172 // 1000 × 5 + 1M × 0.5 + 1000 × 6.25 + 2000 × 10 + 400 × 25, per 1M
173 expect(amount(CLAUDE, usage)).toBeCloseTo(0.54125, 10)
174 })
175
176 it('maps Gemini usageMetadata, thoughts billed as output', () => {
177 const usage = {
178 promptTokenCount: 12_000,
179 cachedContentTokenCount: 4000,
180 candidatesTokenCount: 500,
181 thoughtsTokenCount: 300,
182 totalTokenCount: 12_800,
183 promptTokensDetails: [{ modality: 'TEXT', tokenCount: 12_000 }],
184 cacheTokensDetails: [{ modality: 'TEXT', tokenCount: 4000 }],
185 }
186 expect(usageFormat(GPT_5, usage)).toBe('gemini')
187 expect(normalizeUsage(GPT_5, usage)).toEqual({
188 input_tokens: 8000,
189 cache_read_tokens: 4000,
190 output_tokens: 800,
191 })
192 })
193
194 it('splits Gemini audio only when the card prices audio', () => {
195 const usage = {
196 promptTokenCount: 1000,
197 candidatesTokenCount: 10,
198 promptTokensDetails: [
199 { modality: 'TEXT', tokenCount: 400 },
200 { modality: 'AUDIO', tokenCount: 600 },
201 ],
202 }
203 expect(normalizeUsage(GPT_5, usage)).toEqual({
204 input_tokens: 1000,
205 cache_read_tokens: 0,
206 output_tokens: 10,
207 })
208 const audio = card({
209 input_tokens: 1e-6,
210 audio_tokens: 3e-6,
211 output_tokens: 2e-6,
212 })
213 expect(normalizeUsage(audio, usage)).toEqual({
214 input_tokens: 400,
215 audio_tokens: 600,
216 output_tokens: 10,
217 })
218 })
219
220 it('passes card levers through unchanged', () => {
221 const usage = { input_tokens: 8000, cache_read_tokens: 4000 }
222 expect(usageFormat(GPT_5, usage)).toBe('card')
223 expect(normalizeUsage(GPT_5, usage)).toBe(usage)
224 })
225
226 it('reads a native key as a card lever when the card reads it', () => {
227 const video = card({ completion_tokens: 2.5e-6 })
228 expect(usageFormat(video, { completion_tokens: 1000 })).toBe('card')
229 expect(usageFormat(GPT_5, { completion_tokens: 1000 })).toBe('openai-chat')
230 expect(usageFormat(GPT_5, { input_tokens: 1, total_tokens: 1 })).toBe(
231 'card',
232 )
233 })
234
235 it('refuses a payload that mixes native fields and card levers', () => {
236 const mixed = () =>
237 normalizeUsage(GPT_5, {
238 input_tokens: 12_000,
239 input_tokens_details: { cached_tokens: 4000 },
240 cache_read_tokens: 4000,
241 })
242 expect(mixed).toThrow(expect.objectContaining({ code: 'mixed-usage' }))
243 expect(mixed).toThrow(
244 'usage.cache_read_tokens: not a field of openai-responses usage',
245 )
246 expect(() =>
247 normalizeUsage(CLAUDE, { prompt_tokens: 10, cache_read_input_tokens: 5 }),
248 ).toThrow(expect.objectContaining({ code: 'mixed-usage' }))
249 })
250
251 it('refuses a cache count the card does not price', () => {
252 const plain = card({ input_tokens: 1e-6, output_tokens: 2e-6 })
253 const usage = {
254 input_tokens: 100,
255 input_tokens_details: { cached_tokens: 40 },
256 output_tokens: 10,
257 }
258 expect(normalizeUsage(plain, usage)).toEqual({
259 input_tokens: 60,
260 cache_read_tokens: 40,
261 output_tokens: 10,
262 })
263 expect(() =>
264 priceDetailed(plain, {}, normalizeUsage(plain, usage)),
265 ).toThrow(expect.objectContaining({ code: 'unknown-usage-key' }))
266 })
267
268 it('refuses a native usage object that omits a format total', () => {
269 expect(() => normalizeUsage(GPT_5, { completion_tokens: 1000 })).toThrow(
270 /prompt_tokens/,
271 )
272 expect(() =>
273 normalizeUsage(GPT_5, {
274 input_tokens: 10,
275 input_tokens_details: { cached_tokens: 0 },
276 }),
277 ).toThrow(/output_tokens/)
278 expect(() =>
279 normalizeUsage(CLAUDE, { output_tokens: 10, cache_read_input_tokens: 5 }),
280 ).toThrow(/input_tokens/)
281 expect(() => normalizeUsage(GPT_5, { candidatesTokenCount: 10 })).toThrow(
282 /promptTokenCount/,
283 )
284 expect(
285 normalizeUsage(GPT_5, {
286 input_tokens: 0,
287 output_tokens: 0,
288 input_tokens_details: { cache_write_tokens: 0 },
289 }),
290 ).toEqual({ input_tokens: 0, output_tokens: 0 })
291 })
292
293 it('refuses counts that are not token counts or do not add up', () => {
294 expect(() =>
295 normalizeUsage(GPT_5, {
296 input_tokens: 10,
297 input_tokens_details: { cached_tokens: 40 },
298 }),
299 ).toThrow(expect.objectContaining({ code: 'bad-input' }))
300 expect(() =>
301 normalizeUsage(GPT_5, { prompt_tokens: '10', completion_tokens: 1 }),
302 ).toThrow(expect.objectContaining({ code: 'bad-input' }))
303 })
304
305 it('keeps Chat image tokens in the prompt total unless the card prices them', () => {
306 const usage = {
307 prompt_tokens: 10,
308 prompt_tokens_details: { image_tokens: 4, text_tokens: 6 },
309 completion_tokens: 1,
310 }
311 // text_tokens is already inside prompt_tokens, so it is not subtracted.
312 expect(normalizeUsage(GPT_5, usage)).toEqual({
313 input_tokens: 10,
314 output_tokens: 1,
315 })
316 const priced = card({
317 input_tokens: 1e-6,
318 image_tokens: 2e-6,
319 output_tokens: 1e-6,
320 })
321 expect(normalizeUsage(priced, usage)).toEqual({
322 input_tokens: 6,
323 image_tokens: 4,
324 output_tokens: 1,
325 })
326 })
327})