Open History to see the last commit
1import { price } from '@modelschemas/rate-card'
2import type { RateCard } from '@modelschemas/rate-card'
3import { afterEach, describe, expect, it } from 'vitest'
4
5import {
6 parseVercelModels,
7 provider,
8 VERCEL_MODELS_URL,
9 vercelReasoning,
10} from './vercel.ts'
11
12/**
13 * Excerpt of https://ai-gateway.vercel.sh/v1/models (2026-10-08).
14 * `pricing` values are the gateway's USD-per-token strings.
15 */
16const FIXTURE = {
17 object: 'list',
18 data: [
19 {
20 id: 'alibaba/qwen-3-14b',
21 name: 'Qwen3-14B',
22 type: 'language',
23 context_window: 40960,
24 max_tokens: 16384,
25 modalities: { input: ['text'], output: ['text'] },
26 reasoning_options: [{ type: 'toggle' }],
27 supported_parameters: [
28 'max_tokens',
29 'temperature',
30 'tools',
31 'reasoning',
32 'not_a_flag',
33 ],
34 pricing: {
35 input: '0.00000012',
36 output: '0.00000024',
37 input_cache_read: '0.00000006',
38 input_cache_write: '0.00000015',
39 fast: { input: '0.000009', output: '0.000009' },
40 service_tiers: {
41 flex: { input: '0.00000001', output: '0.00000001' },
42 },
43 },
44 released: 1745798400,
45 },
46 {
47 id: 'alibaba/qwen-3-235b',
48 name: 'Qwen3-235B',
49 type: 'language',
50 modalities: { input: ['text'], output: ['text'] },
51 reasoning_options: [
52 { type: 'toggle' },
53 { type: 'effort', values: ['none', 'low', 'medium', 'high'] },
54 ],
55 supported_parameters: ['max_tokens', 'stop'],
56 pricing: { input: '0.00000018' },
57 },
58 {
59 id: 'google/imagen',
60 type: 'image',
61 modalities: { input: ['text'], output: ['image'] },
62 },
63 {
64 id: 'inclusionai/ling-3.1-flash',
65 type: 'language',
66 supported_parameters: ['max_tokens'],
67 pricing: { input: '0', output: '0' },
68 },
69 ],
70}
71
72const SOURCE: RateCard['source'] = {
73 url: VERCEL_MODELS_URL,
74 hash: 'abc',
75 extractedAt: '2026-10-08T00:00:00.000Z',
76}
77
78const originalFetch = globalThis.fetch
79
80afterEach(() => {
81 globalThis.fetch = originalFetch
82})
83
84describe('vercel', () => {
85 it('lists gateway models from the Vercel payload and skips the spec', async () => {
86 const urls: Array<string> = []
87 globalThis.fetch = ((url: string) => {
88 urls.push(String(url))
89 if (String(url) === VERCEL_MODELS_URL) {
90 return Promise.resolve(new Response(JSON.stringify(FIXTURE)))
91 }
92 return Promise.reject(new Error(`unexpected fetch: ${String(url)}`))
93 }) as typeof fetch
94
95 const listed = await provider.listModels({})
96 const spec = await provider.fetchSpec({})
97
98 expect(listed.skipped).toBeUndefined()
99 expect(listed.models.map((model) => model.rawId)).toEqual([
100 'alibaba/qwen-3-14b',
101 'alibaba/qwen-3-235b',
102 'google/imagen',
103 'inclusionai/ling-3.1-flash',
104 ])
105 expect(listed.models[0]).toMatchObject({
106 displayName: 'Qwen3-14B',
107 activity: 'chat',
108 contextWindow: 40960,
109 maxOutput: 16384,
110 modalities: { input: ['text'], output: ['text'] },
111 capabilities: ['max_tokens', 'temperature', 'tools', 'reasoning'],
112 exactCapabilities: true,
113 // A toggle and nothing else: "an on/off control".
114 reasoning: { mode: 'toggle', mandatory: false },
115 requestMap: {
116 thinking: {
117 on: { reasoning: { enabled: true } },
118 off: { reasoning: { enabled: false } },
119 levels: null,
120 },
121 maxTokensField: 'max_tokens',
122 developerRole: null,
123 reasoningEffort: null,
124 },
125 factSources: {
126 pricing: {
127 derivation: 'listing',
128 sourceUrl: VERCEL_MODELS_URL,
129 path: 'pricing',
130 },
131 capabilities: {
132 tools: {
133 derivation: 'listing',
134 sourceUrl: VERCEL_MODELS_URL,
135 path: 'supported_parameters.tools',
136 },
137 },
138 reasoning: {
139 derivation: 'listing',
140 sourceUrl: VERCEL_MODELS_URL,
141 sourceHash: expect.stringMatching(/^[0-9a-f]{64}$/) as string,
142 path: 'reasoning.enabled',
143 },
144 },
145 releasedAt: 1745798400,
146 })
147 expect(listed.models[0]?.pricing).toMatchObject({
148 tables: {
149 rate: {
150 base: {
151 input_tokens: 0.00000012,
152 output_tokens: 0.00000024,
153 cache_read_tokens: 0.00000006,
154 cache_write_tokens: 0.00000015,
155 },
156 },
157 },
158 })
159 // fast and flex are not the standard rate.
160 const base = (
161 listed.models[0]?.pricing as {
162 tables: { rate: { base: Record<string, number> } }
163 }
164 ).tables.rate.base
165 expect(base.input_tokens).toBe(0.00000012)
166 // An effort row's thinking body is the level list, not a control path.
167 expect(listed.models[1]?.factSources).not.toHaveProperty('reasoning')
168 expect(listed.models[1]).toMatchObject({
169 pricing: null,
170 capabilities: ['max_tokens', 'stop'],
171 exactCapabilities: true,
172 reasoning: {
173 mode: 'effort',
174 mandatory: false,
175 efforts: ['none', 'low', 'medium', 'high'],
176 },
177 requestMap: {
178 maxTokensField: 'max_tokens',
179 thinking: {
180 on: { reasoning: { effort: 'high' } },
181 off: { reasoning: { effort: 'none' } },
182 levels: {
183 off: 'none',
184 minimal: null,
185 low: 'low',
186 medium: 'medium',
187 high: 'high',
188 xhigh: null,
189 max: null,
190 },
191 },
192 },
193 })
194 expect(listed.models[2]).toMatchObject({
195 activity: 'image',
196 pricing: null,
197 })
198 expect(listed.models[2]).not.toHaveProperty('factSources')
199 expect(listed.models[2]).not.toHaveProperty('requestMap')
200 expect(listed.models[2]).not.toHaveProperty('capabilities')
201 // A published zero is free, not a card.
202 expect(listed.models[3]?.pricing).toBeNull()
203 expect(listed.models[3]?.requestMap).toMatchObject({
204 maxTokensField: 'max_tokens',
205 thinking: null,
206 })
207 expect(spec.skipped).toContain('skipped')
208 expect(spec.specs).toEqual([])
209 expect(urls).toEqual([VERCEL_MODELS_URL])
210 })
211
212 it('re-quotes the standard schedule above an inclusive context min', () => {
213 const [model] = parseVercelModels(
214 {
215 data: [
216 {
217 id: 'alibaba/qwen-3.6-max-preview',
218 type: 'language',
219 pricing: {
220 input: '0.000001',
221 output: '0.000002',
222 input_cache_read: '0.0000001',
223 input_tiers: [
224 { cost: '0.000001', min: 0, max: 128000 },
225 { cost: '0.000002', min: 128000 },
226 ],
227 output_tiers: [
228 { cost: '0.000002', max: 128000 },
229 { cost: '0.000004', min: 128000 },
230 ],
231 input_cache_read_tiers: [
232 { cost: '0.0000001', min: 0, max: 128000 },
233 { cost: '0.0000002', min: 128000 },
234 ],
235 regional: { eu: { input: '0.000009', output: '0.000009' } },
236 },
237 },
238 ],
239 },
240 SOURCE,
241 )
242 const card = model?.pricing as RateCard
243 expect(
244 price(card, {}, { input_tokens: 127999, output_tokens: 0 }),
245 ).toBeCloseTo(127999 * 0.000001)
246 expect(
247 price(card, {}, { input_tokens: 128000, output_tokens: 0 }),
248 ).toBeCloseTo(128000 * 0.000002)
249 expect(
250 price(
251 card,
252 {},
253 { input_tokens: 0, output_tokens: 0, cache_read_tokens: 128000 },
254 ),
255 ).toBeCloseTo(128000 * 0.0000002)
256 })
257
258 it('keeps the base cache rate below a tier that omits min 0', () => {
259 const [model] = parseVercelModels(
260 {
261 data: [
262 {
263 id: 'spacexai/grok-4.20-non-reasoning-beta',
264 type: 'language',
265 pricing: {
266 input: '0.000002',
267 output: '0.000006',
268 input_cache_read: '0.0000002',
269 input_cache_read_tiers: [{ cost: '0.0000004', min: 200001 }],
270 },
271 },
272 ],
273 },
274 SOURCE,
275 )
276 const card = model?.pricing as RateCard
277 const usage = (cache_read_tokens: number) => ({
278 input_tokens: 0,
279 output_tokens: 0,
280 cache_read_tokens,
281 })
282 expect(price(card, {}, usage(200000))).toBeCloseTo(200000 * 0.0000002)
283 expect(price(card, {}, usage(200001))).toBeCloseTo(200001 * 0.0000004)
284 })
285
286 it('uses the toggle when an effort list has no high', () => {
287 const [model] = parseVercelModels(
288 {
289 data: [
290 {
291 id: 'alibaba/qwen3.8-max',
292 type: 'language',
293 supported_parameters: ['max_completion_tokens'],
294 reasoning_options: [
295 { type: 'toggle' },
296 { type: 'effort', values: ['low', 'medium', 'xhigh'] },
297 ],
298 },
299 ],
300 },
301 SOURCE,
302 )
303 expect(model?.reasoning).toEqual({
304 mode: 'effort',
305 mandatory: false,
306 efforts: ['low', 'medium', 'xhigh'],
307 })
308 expect(model?.requestMap).toMatchObject({
309 maxTokensField: 'max_completion_tokens',
310 thinking: {
311 on: { reasoning: { enabled: true } },
312 off: { reasoning: { enabled: false } },
313 levels: null,
314 },
315 })
316 })
317
318 it('stores no thinking body for a budget with no high token count', () => {
319 const [model] = parseVercelModels(
320 {
321 data: [
322 {
323 id: 'alibaba/qwen3-235b-a22b-thinking',
324 type: 'language',
325 supported_parameters: ['max_tokens'],
326 reasoning_options: [{ type: 'budget_tokens', min: 1, max: 81920 }],
327 },
328 ],
329 },
330 SOURCE,
331 )
332 expect(model?.reasoning).toEqual({ mode: 'budget', mandatory: null })
333 expect(model?.requestMap).toMatchObject({
334 maxTokensField: 'max_tokens',
335 thinking: null,
336 })
337 expect(model?.factSources?.reasoning).toMatchObject({
338 path: 'reasoning.max_tokens',
339 sourceUrl: VERCEL_MODELS_URL,
340 })
341 })
342
343 it('throws when a tier schedule cannot be read', () => {
344 const row = (pricing: unknown) => ({
345 data: [{ id: 'openai/gpt-6-astra', type: 'language', pricing }],
346 })
347 expect(() =>
348 parseVercelModels(row({ input: '0.00001', input_tiers: 'nope' }), SOURCE),
349 ).toThrow(/input_tiers is not an array/)
350 expect(() =>
351 parseVercelModels(
352 row({
353 input: '0.00001',
354 output: '0.00002',
355 input_tiers: [{ cost: '0.00002', min: 0 }],
356 }),
357 SOURCE,
358 ),
359 ).toThrow(/disagrees with its first tier/)
360 expect(() =>
361 parseVercelModels(
362 row({
363 input: '0.00001',
364 output: '0.00002',
365 input_tiers: [
366 { cost: '0.00001', min: 0, max: 100 },
367 { cost: '0.00002', min: 200 },
368 ],
369 }),
370 SOURCE,
371 ),
372 ).toThrow(/ranges do not meet/)
373 expect(() =>
374 parseVercelModels(
375 {
376 data: [
377 {
378 id: 'openai/gpt-6-astra',
379 type: 'language',
380 supported_parameters: 'max_tokens',
381 },
382 ],
383 },
384 SOURCE,
385 ),
386 ).toThrow(/supported_parameters is not a string array/)
387 })
388
389 it('reads each control the row lists, and nothing it does not know', () => {
390 const options = (...reasoning_options: Array<unknown>) =>
391 vercelReasoning({ reasoning_options })
392 // anthropic/claude-sonnet-4.5: a switch and a budget.
393 expect(
394 options({ type: 'toggle' }, { type: 'budget_tokens', min: 1024 }),
395 ).toEqual({ mode: 'budget', mandatory: false })
396 // minimax/minimax-m3: a budget alone says nothing about turning it off.
397 expect(options({ type: 'budget_tokens' })).toEqual({
398 mode: 'budget',
399 mandatory: null,
400 })
401 // An effort entry keeps its reading whatever sits beside it.
402 expect(
403 options(
404 { type: 'toggle' },
405 { type: 'effort', values: ['low', 'medium', 'xhigh'] },
406 { type: 'budget_tokens' },
407 ),
408 ).toEqual({
409 mode: 'effort',
410 mandatory: false,
411 efforts: ['low', 'medium', 'xhigh'],
412 })
413 // A control type this does not know, an effort entry without values,
414 // or no controls: nothing is stored.
415 expect(options({ type: 'toggle' }, { type: 'auto' })).toBeNull()
416 expect(options({ type: 'switch' })).toBeNull()
417 expect(options({ type: 'toggle' }, { type: 'effort' })).toBeNull()
418 expect(
419 options({ type: 'toggle' }, { type: 'effort', values: [] }),
420 ).toBeNull()
421 expect(options()).toBeNull()
422 expect(vercelReasoning({})).toBeNull()
423 expect(vercelReasoning({ reasoning_options: 'toggle' })).toBeNull()
424 })
425
426 it('clears the price of a row whose price varies by route (#346)', () => {
427 // ai-gateway.vercel.sh/v1/models, 2026-10-22: the top-level price is
428 // one route's (beam: the free `reflection` route; qwen-3-235b: vertex,
429 // not the cheaper deepinfra), and the gateway picks the route.
430 const [beam, qwen, plain] = parseVercelModels(
431 {
432 data: [
433 {
434 id: 'reflection/beam',
435 type: 'language',
436 pricing: {
437 input: '0',
438 output: '0',
439 input_cache_read: '0',
440 varies_by_provider: true,
441 },
442 },
443 {
444 id: 'alibaba/qwen-3-235b',
445 type: 'language',
446 pricing: {
447 input: '0.00000022',
448 output: '0.00000088',
449 varies_by_provider: true,
450 },
451 },
452 {
453 id: 'alibaba/qwen-3-14b',
454 type: 'language',
455 pricing: { input: '0.00000012', output: '0.00000024' },
456 },
457 ],
458 },
459 SOURCE,
460 )
461 for (const row of [beam, qwen]) {
462 expect(row?.pricing).toBeNull()
463 expect(row?.absent).toEqual({ pricing: 'cleared' })
464 expect(row?.factSources?.pricing).toBeUndefined()
465 }
466 expect(plain?.pricing).not.toBeNull()
467 expect(plain?.absent).toBeUndefined()
468 })
469})