Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { price, verifyExamples } from './evaluate.ts'
3import { GPT_4O } from './fixtures/gpt-4o.ts'
4import { NANO_BANANA_2 } from './fixtures/nano-banana-2.ts'
5import { compileOpenRouterPricing } from './openrouter.ts'
6import { rateCardSchema } from './rate-card.schema.ts'
7import type { RateCard } from './card.ts'
8
9const source = GPT_4O.source
10
11const compile = (listing: unknown): RateCard => {
12 const card = compileOpenRouterPricing(listing, source)
13 if (!card) throw new Error('did not compile')
14 expect(rateCardSchema.safeParse(card).success).toBe(true)
15 return card
16}
17
18describe('price', () => {
19 it('reads usage-bound levers from usage, not the request body', () => {
20 expect(
21 price(
22 GPT_4O,
23 { input_tokens: 999_999_999, model: 'gpt-4o' },
24 { input_tokens: 1_000_000, output_tokens: 0 },
25 ),
26 ).toBe(2.5)
27 })
28
29 it('refuses a token card without usage', () => {
30 expect(() => price(GPT_4O, { input_tokens: 1, output_tokens: 1 })).toThrow(
31 expect.objectContaining({ code: 'bad-input' }),
32 )
33 })
34
35 it('reads request-bound levers from the request body', () => {
36 expect(
37 price(NANO_BANANA_2, { resolution: '4K' }, { resolution: '0.5K' }),
38 ).toBe(0.16)
39 })
40})
41
42describe('compileOpenRouterPricing', () => {
43 it('gpt-4o listing reproduces the hand card’s worked examples', () => {
44 const card = compile({
45 prompt: '0.0000025',
46 completion: '0.00001',
47 input_cache_read: '0.00000125',
48 })
49 const results = verifyExamples({ ...card, examples: GPT_4O.examples })
50 expect(results.filter((r) => !r.ok)).toEqual([])
51 })
52
53 it('keeps extra keys as optional usage levers', () => {
54 const card = compile({
55 prompt: '0.000002',
56 completion: '0.000012',
57 image: '0.000002',
58 audio: '0.000002',
59 web_search: '0.014',
60 internal_reasoning: '0.000012',
61 })
62 expect(Object.keys(card.inputs).sort()).toEqual([
63 'audio_tokens',
64 'image_tokens',
65 'input_tokens',
66 'output_tokens',
67 'reasoning_tokens',
68 'web_searches',
69 ])
70 const usage = { input_tokens: 1000, output_tokens: 1000 }
71 expect(price(card, {}, usage)).toBeCloseTo(0.014, 9)
72 expect(
73 price(card, {}, { ...usage, web_searches: 2, reasoning_tokens: 1000 }),
74 ).toBeCloseTo(0.014 + 0.028 + 0.012, 9)
75 })
76
77 it('compiles min_prompt_tokens overrides to tiers on total prompt tokens', () => {
78 const card = compile({
79 prompt: '0.0000001',
80 completion: '0.0000004',
81 input_cache_read: '0.00000001',
82 overrides: [
83 {
84 min_prompt_tokens: 256000,
85 prompt: '0.0000002',
86 completion: '0.0000008',
87 },
88 {
89 min_prompt_tokens: 32000,
90 prompt: '0.00000015',
91 completion: '0.0000006',
92 },
93 ],
94 })
95 const at = (input_tokens: number, cache_read_tokens = 0) =>
96 price(card, {}, { input_tokens, output_tokens: 0, cache_read_tokens })
97 expect(at(10_000)).toBeCloseTo(0.001, 9)
98 // OpenRouter: tier applies when prompt tokens are strictly greater than
99 // min_prompt_tokens. Exactly 32_000 stays on the base rate.
100 expect(at(32_000)).toBeCloseTo(0.0032, 9)
101 expect(at(32_001)).toBeCloseTo(32_001 * 0.00000015, 9)
102 expect(at(100_000)).toBeCloseTo(0.015, 9)
103 expect(at(300_000)).toBeCloseTo(0.06, 9)
104 // Cached tokens count toward the threshold; the tier keeps the base cache rate.
105 expect(at(20_000, 20_000)).toBeCloseTo(0.003 + 0.0002, 9)
106 })
107
108 it('ignores listing.discount and prices the stated prompt/completion strings', () => {
109 const card = compile({
110 prompt: '0.0000025',
111 completion: '0.00001',
112 discount: 0.5,
113 })
114 expect(price(card, {}, { input_tokens: 1_000_000, output_tokens: 0 })).toBe(
115 2.5,
116 )
117 })
118
119 it('compiles when only prompt or only completion is a non-zero number', () => {
120 const promptOnly = compile({ prompt: '0.000001', completion: '0' })
121 expect(
122 price(promptOnly, {}, { input_tokens: 1_000_000, output_tokens: 0 }),
123 ).toBe(1)
124 const completionOnly = compile({ prompt: 0, completion: 0.000004 })
125 expect(
126 price(completionOnly, {}, { input_tokens: 0, output_tokens: 1_000_000 }),
127 ).toBe(4)
128 const promptKeyOnly = compile({ prompt: '0.000002' })
129 expect(Object.keys(promptKeyOnly.inputs)).toEqual(['input_tokens'])
130 expect(price(promptKeyOnly, {}, { input_tokens: 1_000_000 })).toBe(2)
131 })
132
133 it.each<[string, unknown]>([
134 ['Together-style all-zero listing', { prompt: '0', completion: '0' }],
135 ['*-free row', { prompt: '0', completion: '0', request: '0' }],
136 ['Lyria listing with no per-song unit', { prompt: '0', completion: '0' }],
137 ['router variable-price sentinel', { prompt: '-1', completion: '-1' }],
138 ['openrouter/auto', { prompt: '-1', completion: '-1' }],
139 ['non-numeric rate', { prompt: 'free', completion: '0' }],
140 [
141 'non-zero prompt with a bad completion',
142 { prompt: '0.000001', completion: 'free' },
143 ],
144 ['neither prompt nor completion', { image: '0.000002' }],
145 ['not an object', null],
146 ])('%s does not compile', (_name, listing) => {
147 expect(compileOpenRouterPricing(listing, source)).toBeNull()
148 })
149})