Open History to see the last commit
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 ],
90}
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 pinned row its route's price, limits and upstream", () => {
121 const groq = row('groq/gpt-oss-120b')
122 expect(
123 price(
124 groq?.pricing as RateCard,
125 {},
126 {
127 input_tokens: 1_000_000,
128 output_tokens: 1_000_000,
129 },
130 ),
131 ).toBeCloseTo(0.9)
132 expect(groq?.maxOutput).toBe(32766)
133 expect(groq?.contextWindow).toBeNull()
134 expect(groq?.reasoning).toEqual({
135 mode: 'effort',
136 mandatory: null,
137 efforts: ['low', 'medium', 'high'],
138 })
139 expect(groq?.upstream).toMatchObject({
140 providerNamespace: 'groq',
141 rawId: 'openai/gpt-oss-120b',
142 })
143 expect(row('cerebras/gpt-oss-120b')?.capabilities).toEqual([
144 'max_tokens',
145 'reasoning',
146 ])
147 })
148
149 it('prices a bare id whose routes all quote the same price', () => {
150 const bare = row('claude-sonnet-4-5')
151 expect(
152 price(
153 bare?.pricing as RateCard,
154 {},
155 {
156 input_tokens: 1_000_000,
157 output_tokens: 0,
158 cache_read_tokens: 1_000_000,
159 },
160 ),
161 ).toBeCloseTo(3.3)
162 expect(bare?.reasoning).toEqual({
163 mode: 'effort',
164 mandatory: false,
165 efforts: ['none', 'low', 'high'],
166 })
167 expect(bare?.requestMap?.thinking?.off).toEqual({
168 reasoning_effort: 'none',
169 })
170 expect(bare?.aliases).toEqual(['claude-sonnet-4.5'])
171 })
172
173 it('links a single-route bare id and refuses unreadable price keys', () => {
174 const bare = row('gpt-4o')
175 expect(bare?.upstream).toMatchObject({
176 providerNamespace: 'openai',
177 rawId: 'gpt-4o',
178 })
179 expect(bare?.pricing).toBeNull()
180 expect(row('openai/gpt-4o')?.pricing).toBeNull()
181 expect(bare?.deprecated).toBe(true)
182 })
183})
184
185describe('provider', () => {
186 it('classifies the gateway routes from openapi.json', () => {
187 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
188 expect(provider.classify('/v1/audio/transcriptions', {})).toBe('audio')
189 expect(provider.classify('/v1/messages', {})).toBeNull()
190 })
191})