Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

llmgateway.test.ts

373 lines10.8 KB

1import { price } from '@modelschemas/rate-card'
2import type { RateCard } from '@modelschemas/rate-card'
3import { describe, expect, it } from 'vitest'
4​
5import { parseLlmGatewayModels, provider } from './llmgateway.ts'
6​
7const SOURCE = { url: 'u', hash: 'h', extractedAt: 'x' }
8const ZERO = { image: '0', request: '0', input_cache_write: '0' }
9​
10/** Excerpt of https://api.llmgateway.io/v1/models (2026-10-22). */
11const FIXTURE = {
12 data: [
13 {
14 id: 'auto',
15 name: 'Auto Route',
16 architecture: { input_modalities: ['text'], output_modalities: ['text'] },
17 providers: [{ providerId: 'llmgateway', externalId: 'auto' }],
18 context_length: 0,
19 supported_parameters: ['temperature'],
20 },
21 {
22 id: 'gpt-oss-120b',
23 name: 'GPT OSS 120B',
24 created: 1754352000,
25 architecture: { input_modalities: ['text'], output_modalities: ['text'] },
26 providers: [
27 {
28 providerId: 'groq',
29 externalId: 'openai/gpt-oss-120b',
30 pricing: { prompt: '0.15e-6', completion: '0.75e-6', ...ZERO },
31 reasoning: true,
32 reasoning_efforts: ['low', 'medium', 'high'],
33 max_output: 32766,
34 },
35 {
36 providerId: 'cerebras',
37 externalId: 'gpt-oss-120b',
38 pricing: { prompt: '0.35e-6', completion: '0.75e-6', ...ZERO },
39 reasoning: true,
40 max_output: 32768,
41 tools: false,
42 },
43 ],
44 // Undocumented cheapest-route price: never stored.
45 pricing: { prompt: '0.032e-6', completion: '0.14e-6' },
46 context_length: 131072,
47 max_output: 32000,
48 supported_parameters: ['max_tokens', 'tools', 'tool_choice', 'reasoning'],
49 },
50 {
51 id: 'claude-sonnet-4-5',
52 name: 'Claude Sonnet 4.5',
53 architecture: {
54 input_modalities: ['text', 'image'],
55 output_modalities: ['text'],
56 },
57 providers: ['anthropic', 'aws-bedrock'].map((providerId) => ({
58 providerId,
59 externalId: 'claude-sonnet-4-5',
60 pricing: {
61 prompt: '3.0e-6',
62 completion: '15.0e-6',
63 input_cache_read: '0.3e-6',
64 image: '0',
65 },
66 reasoning: true,
67 reasoning_efforts: ['none', 'low', 'high'],
68 })),
69 context_length: 200000,
70 supported_parameters: ['max_tokens'],
71 aliases: ['claude-sonnet-4.5'],
72 },
73 {
74 id: 'gpt-4o',
75 name: 'GPT-4o',
76 architecture: { input_modalities: ['text'], output_modalities: ['text'] },
77 providers: [
78 {
79 providerId: 'openai',
80 externalId: 'gpt-4o',
81 // A nonzero key whose unit is unstated: no card.
82 pricing: { prompt: '2.5e-6', completion: '10e-6', image: '0.00553' },
83 },
84 ],
85 context_length: 128000,
86 supported_parameters: [],
87 deactivated_at: '2020-01-01T00:00:00.000Z',
88 },
89 ],
91​
92const rows = parseLlmGatewayModels(FIXTURE, SOURCE, Date.UTC(2026, 9, 22))
93const row = (rawId: string) => rows.find((r) => r.rawId === rawId)
94​
95describe('parseLlmGatewayModels', () => {
96 it('lists bare ids plus one pinned row per upstream route', () => {
97 expect(rows.map((r) => r.rawId)).toEqual([
98 'auto',
99 'gpt-oss-120b',
100 'groq/gpt-oss-120b',
101 'cerebras/gpt-oss-120b',
102 'claude-sonnet-4-5',
103 'anthropic/claude-sonnet-4-5',
104 'aws-bedrock/claude-sonnet-4-5',
105 'gpt-4o',
106 'openai/gpt-4o',
107 ])
108 expect(row('auto')?.upstream).toBeNull()
109 })
110​
111 it('leaves a routed bare id unpriced and unlinked when routes differ', () => {
112 const bare = row('gpt-oss-120b')
113 expect(bare?.pricing).toBeNull()
114 expect(bare?.reasoning).toBeNull()
115 expect(bare?.upstream).toBeNull()
116 expect(bare?.contextWindow).toBe(131072)
117 expect(bare?.maxOutput).toBe(32000)
118 })
119​
120 it('gives a bare id whose routes differ a range over them, not a price (#406)', () => {
121 const bare = row('gpt-oss-120b')
122 expect(bare?.absent).toEqual({ pricing: 'cleared' })
123 expect(bare?.pricingRange).toEqual({
124 currency: 'USD',
125 routes: [
126 {
127 model: 'groq/gpt-oss-120b',
128 pricing: {
129 currency: 'USD',
130 per: 'token',
131 inputPerMillion: 0.15,
132 outputPerMillion: 0.75,
133 },
134 },
135 {
136 model: 'cerebras/gpt-oss-120b',
137 pricing: {
138 currency: 'USD',
139 per: 'token',
140 inputPerMillion: 0.35,
141 outputPerMillion: 0.75,
142 },
143 },
144 ],
145 inputPerMillion: { min: 0.15, max: 0.35 },
146 outputPerMillion: { min: 0.75, max: 0.75 },
147 })
148 expect(bare?.factSources?.pricingRange?.path).toBe(
149 'data[].providers[].pricing',
150 )
151 // One price on every route, or a single route: no range.
152 expect(row('claude-sonnet-4-5')?.pricingRange).toBeNull()
153 expect(row('claude-sonnet-4-5')?.absent).toBeUndefined()
154 expect(row('gpt-4o')?.pricingRange).toBeNull()
155 })
156​
157 it('gives no range when a route has no readable card', () => {
158 const [bare] = parseLlmGatewayModels(
159 {
160 data: [
161 {
162 id: 'm',
163 architecture: { output_modalities: ['text'] },
164 providers: [
165 {
166 providerId: 'a',
167 externalId: 'm',
168 pricing: { prompt: '1e-6', completion: '2e-6' },
169 },
170 {
171 providerId: 'b',
172 externalId: 'm',
173 pricing: { prompt: '1e-6', image: '0.01' },
174 },
175 ],
176 },
177 ],
178 },
179 SOURCE,
180 )
181 expect(bare?.pricing).toBeNull()
182 expect(bare?.pricingRange).toBeNull()
183 // Unreadable is not "no single price": a plain omission.
184 expect(bare?.absent).toBeUndefined()
185 })
186​
187 it("gives a pinned row its route's price, limits and upstream", () => {
188 const groq = row('groq/gpt-oss-120b')
189 expect(
190 price(
191 groq?.pricing as RateCard,
192 {},
193 {
194 input_tokens: 1_000_000,
195 output_tokens: 1_000_000,
196 },
197 ),
198 ).toBeCloseTo(0.9)
199 expect(groq?.maxOutput).toBe(32766)
200 expect(groq?.contextWindow).toBeNull()
201 expect(groq?.reasoning).toEqual({
202 mode: 'effort',
203 mandatory: null,
204 efforts: ['low', 'medium', 'high'],
205 })
206 expect(groq?.upstream).toMatchObject({
207 providerNamespace: 'groq',
208 rawId: 'openai/gpt-oss-120b',
209 })
210 expect(row('cerebras/gpt-oss-120b')?.capabilities).toEqual([
211 'max_tokens',
212 'reasoning',
213 ])
214 })
215​
216 it('prices a bare id whose routes all quote the same price', () => {
217 const bare = row('claude-sonnet-4-5')
218 expect(
219 price(
220 bare?.pricing as RateCard,
221 {},
222 {
223 input_tokens: 1_000_000,
224 output_tokens: 0,
225 cache_read_tokens: 1_000_000,
226 },
227 ),
228 ).toBeCloseTo(3.3)
229 expect(bare?.reasoning).toEqual({
230 mode: 'effort',
231 mandatory: false,
232 efforts: ['none', 'low', 'high'],
233 })
234 expect(bare?.requestMap?.thinking?.off).toEqual({
235 reasoning_effort: 'none',
236 })
237 expect(bare?.aliases).toEqual(['claude-sonnet-4.5'])
238 })
239​
240 it('links a single-route bare id and refuses unreadable price keys', () => {
241 const bare = row('gpt-4o')
242 expect(bare?.upstream).toMatchObject({
243 providerNamespace: 'openai',
244 rawId: 'gpt-4o',
245 })
246 expect(bare?.pricing).toBeNull()
247 expect(row('openai/gpt-4o')?.pricing).toBeNull()
248 expect(bare?.deprecated).toBe(true)
249 })
250})
251​
252describe('route price shapes (2026-11-04 listing, #580)', () => {
253 /** Excerpt of https://api.llmgateway.io/v1/models (2026-11-08). */
254 const [qwen] = parseLlmGatewayModels(
255 {
256 data: [
257 {
258 id: 'qwen-plus',
259 architecture: {
260 input_modalities: ['text'],
261 output_modalities: ['text'],
262 },
263 providers: [
264 {
265 providerId: 'alibaba',
266 externalId: 'qwen-plus',
267 pricing: {
268 prompt: '0.4e-6',
269 completion: '1.2e-6',
270 image: '0',
271 request: '0',
272 input_cache_read: '0.08e-6',
273 input_cache_read_explicit: '0.04e-6',
274 input_cache_write: '0.5e-6',
275 input_cache_write_1h: '0',
276 web_search: '0.01',
277 content_filter: '0.05',
278 tiers: [
279 {
280 name: 'Up to 256K',
281 up_to_tokens: 256000,
282 prompt: '0.4e-6',
283 completion: '1.2e-6',
284 },
285 {
286 name: 'Over 256K',
287 up_to_tokens: null,
288 prompt: '1.2e-6',
289 completion: '3.6e-6',
290 input_cache_read_explicit: '0.12e-6',
291 },
292 ],
293 },
294 },
295 ],
296 supported_parameters: [],
297 },
298 ],
299 },
300 SOURCE,
301 )
302 const card = qwen?.pricing as RateCard
303​
304 it('bills web search, content filter and explicit cache reads', () => {
305 expect(
306 price(
307 card,
308 {},
309 {
310 input_tokens: 0,
311 output_tokens: 0,
312 web_searches: 2,
313 content_filtered_requests: 1,
314 explicit_cache_read_tokens: 100_000,
315 },
316 ),
317 ).toBeCloseTo(0.02 + 0.05 + 0.004)
318 })
319​
320 it('picks the band whose up_to_tokens covers the prompt', () => {
321 const at = (input_tokens: number, explicit = 0) =>
322 price(
323 card,
324 {},
325 {
326 input_tokens,
327 output_tokens: 1_000_000,
328 explicit_cache_read_tokens: explicit,
329 },
330 )
331 expect(at(256_000)).toBeCloseTo(0.1024 + 1.2)
332 expect(at(256_001)).toBeCloseTo(256_001 * 1.2e-6 + 3.6)
333 // Explicit cache reads count toward the prompt size, as in costs.ts.
334 expect(at(1, 256_000)).toBeCloseTo(1.2e-6 + 3.6 + 256_000 * 0.12e-6)
335 })
336​
337 it('refuses a band list it cannot read', () => {
338 const [bad] = parseLlmGatewayModels(
339 {
340 data: [
341 {
342 id: 'x',
343 architecture: {
344 input_modalities: ['text'],
345 output_modalities: ['text'],
346 },
347 providers: [
348 {
349 providerId: 'p',
350 externalId: 'x',
351 pricing: {
352 prompt: '1e-6',
353 completion: '1e-6',
354 tiers: [{ up_to_tokens: 1000, prompt: '1e-6' }],
355 },
356 },
357 ],
358 },
359 ],
360 },
361 SOURCE,
362 )
363 expect(bad?.pricing).toBeNull()
364 })
365})
366​
367describe('provider', () => {
368 it('classifies the gateway routes from openapi.json', () => {
369 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
370 expect(provider.classify('/v1/audio/transcriptions', {})).toBe('audio')
371 expect(provider.classify('/v1/messages', {})).toBeNull()
372 })
373})