Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import type { RateCard } from '@modelschemas/rate-card'
4
5import { projectTokenPricing } from '#/server/rate-card.ts'
6
7import {
8 azureMetered,
9 azureModelPricing,
10 azureModelRates,
11 AZURE_PRICES_URL,
12 AZURE_PRICING_PAGE_URL,
13 parseAzureContextThresholds,
14 parseAzureMeters,
15 parseAzureSku,
16} from './azure-pricing.ts'
17
18const item = (skuName: string, unitOfMeasure: string, retailPrice: number) => ({
19 skuName,
20 unitOfMeasure,
21 retailPrice,
22})
23
24/** Items from the Retail Prices API, `eastus2` (2026-10-06). */
25const PRICE_ITEMS = [
26 item('5.4 mini Inp Gl', '1M', 0.75),
27 item('5.4 mini cd Inp Gl', '1M', 0.075),
28 item('5.4 mini Opt Gl', '1M', 4.5),
29 item('5.4 mini Inp Dz', '1M', 0.825),
30 item('5.4 mini Batch Inp Gl', '1M', 0.375),
31 item('5.4 mini pp Inp Gl', '1M', 1.5),
32 item('54 mini Inp Flex Gl', '1M', 0.375),
33 item('5.4 inp Gl', '1M', 2.5),
34 item('5.4 opt Gl', '1M', 15),
35 item('5.4 longco inp Gl', '1M', 5),
36 item('5.4 longco opt Gl', '1M', 22.5),
37 item('5.4 longco cd inp Gl', '1M', 0.5),
38 item('5.4 cd inp Gl', '1M', 0.25),
39 item('5.5 inp Gl', '1M', 5),
40 item('5.5 opt Gl', '1M', 30),
41 item('5.5 LongCo inp Gl', '1M', 10),
42 item('5.5 LongCo opt Gl', '1M', 45),
43 item('gpt 4.1 mini Inp glbl', '1K', 0.0004),
44 item('gpt 4.1 mini Outp glbl', '1K', 0.0016),
45 item('gpt 4.1 nano Inp glbl', '1K', 0.0001),
46 item('gpt 4.1 nano Outp glbl', '1K', 0.0004),
47 item('5 nano inp Gl', '1K', 0.00005),
48 item('5 nano opt Gl', '1K', 0.0004),
49 item('5.4 nano Inp Gl', '1K', 0.0002),
50 item('5.4 nano Opt Gl', '1K', 0.00125),
51 item('o3-deep research 0626-inp-glbl', '1M', 10),
52 item('6-sol ShortCo Inp Std Gl', '1M', 2),
53 item('6-sol ShortCo Cd Inp Std Gl', '1M', 0.2),
54 item('6-sol ShortCo Cd Wr Std Gl', '1M', 2.5),
55 item('6-sol ShortCo Opt Std Gl', '1M', 10),
56 item('6-sol ShortCo Inp PP Gl', '1M', 4),
57 item('gpt 4o 0513 Input global', '1K', 0.005),
58 item('gpt 4o 0513 Output global', '1K', 0.015),
59 item('gpt 4o 1120 Inp glbl', '1K', 0.0025),
60 item('gpt 4o 1120 cached Inp glbl', '1K', 0.00125),
61 item('gpt 4o 1120 Outp glbl', '1K', 0.01),
62 item('gpt-4o-aud-1217 Inp glbl', '1K', 0.04),
63 item('o3 0416 Inp glbl', '1K', 0.002),
64 item('o3 0416 Outp glbl', '1K', 0.008),
65 item('o1 mini input glbl', '1K', 0.0011),
66 item('o1 mini output glbl', '1K', 0.0044),
67 item('chat-latest 08062026 inp Gl', '1M', 5),
68 item('chat-latest 08062026 opt Gl', '1M', 30),
69 item('gpt-oss-120B Inp glbl', '1K', 0.00015),
70 item('o4 mini ft hosting Dz', '1 Hour', 1.7),
71 item('Code-Interpreter-global', '1', 0.03),
72]
73
74describe('azure SKU names', () => {
75 it('reads the lever, version, and model words', () => {
76 expect(parseAzureSku('5.4 mini cd Inp Gl')).toEqual({
77 name: '54mini',
78 version: null,
79 lever: 'cache_read_tokens',
80 long: false,
81 })
82 expect(parseAzureSku('gpt-4o-0806-Outp-glbl')).toEqual({
83 name: 'gpt4o',
84 version: '0806',
85 lever: 'output_tokens',
86 long: false,
87 })
88 expect(parseAzureSku('6-astra LongCo Cd Wr Std Gl')).toEqual({
89 name: '6astra',
90 version: null,
91 lever: 'cache_write_tokens',
92 long: true,
93 })
94 expect(parseAzureSku('chat-latest 08062026 inp Gl')).toMatchObject({
95 name: 'chatlatest',
96 version: '0806',
97 })
98 })
99
100 it('skips other deployments and SKUs with no direction', () => {
101 expect(parseAzureSku('5.4 mini Inp Dz')).toBeNull()
102 expect(parseAzureSku('o3 0416 Inp regnl')).toBeNull()
103 expect(parseAzureSku('Code-Interpreter-global')).toBeNull()
104 expect(parseAzureSku('text-embedding-3-small-glbl')).toBeNull()
105 })
106
107 it('keeps another tier in the model name so it matches nothing', () => {
108 expect(parseAzureSku('5.4 mini Batch Inp Gl')?.name).toBe('54minibatch')
109 expect(parseAzureSku('5.4 mini pp Inp Gl')?.name).toBe('54minipp')
110 expect(parseAzureSku('54 mini Inp Flex Gl')?.name).toBe('54miniflex')
111 expect(parseAzureSku('gpt-4o-aud-1217 Inp glbl')?.name).toBe('gpt4oaud')
112 expect(parseAzureSku('gpt-4-Turbo-Batch-128K Inp-glbl')?.name).toBe(
113 'gpt4turbobatch128k',
114 )
115 })
116
117 it('reads the gpt-4 turbo128k meter as gpt-4', () => {
118 expect(parseAzureSku('gpt-4-turbo128K Inp-glbl')).toEqual({
119 name: 'gpt4',
120 version: null,
121 lever: 'input_tokens',
122 long: false,
123 })
124 })
125})
126
127/** Row labels cut from the pricing page's static HTML (2026-10-06). */
128const PRICING_PAGE = `
129<td>GPT-5.4 (<272k context length) Global</td>
130<td>GPT-5.4 (<272k context length) Data Zone</td>
131<td>GPT-5.4 (>272k context length) Global</td>
132<td>GPT-5.4 Pro (<272k context length) Global</td>
133<td>GPT-5.4 Pro (>272k context length) Global</td>
134<td>GPT-5.5 Long Context Global</td>
135<td>GPT-6 Sol (short context) Global</td>
136<td>GPT-7 (<200k context length) Global</td>
137<td>GPT-8 (<200k context length) Global</td>
138<td>GPT-8 (>400k context length) Global</td>
139`
140
141describe('azure context thresholds', () => {
142 it('reads a threshold only from a matching pair of labels', () => {
143 expect(
144 Object.fromEntries(parseAzureContextThresholds(PRICING_PAGE)),
145 ).toEqual({ 'gpt-5.4': 272_000, 'gpt-5.4-pro': 272_000 })
146 expect(parseAzureContextThresholds('<td>GPT-5.5 Global</td>').size).toBe(0)
147 })
148})
149
150describe('azure model rates', () => {
151 const meters = parseAzureMeters(PRICE_ITEMS)
152 const doc = {
153 meters,
154 thresholds: Object.fromEntries(parseAzureContextThresholds(PRICING_PAGE)),
155 hash: 'h',
156 extractedAt: '2026-10-06T00:00:00.000Z',
157 }
158 const million = (rates: Record<string, number> | null | undefined) =>
159 rates &&
160 Object.fromEntries(
161 Object.entries(rates).map(([lever, rate]) => [
162 lever,
163 Number((rate * 1e6).toFixed(6)),
164 ]),
165 )
166 const perMillion = (id: string, version: string | null) =>
167 million(azureModelRates(meters, id, version)?.base) ?? null
168
169 it('prices the Global Standard tier per token', () => {
170 expect(perMillion('gpt-5.4-mini', '2026-03-17')).toEqual({
171 input_tokens: 0.75,
172 cache_read_tokens: 0.075,
173 output_tokens: 4.5,
174 })
175 expect(perMillion('o1-mini', '2024-09-12')).toEqual({
176 input_tokens: 1.1,
177 output_tokens: 4.4,
178 })
179 })
180
181 it('prices the listed version only', () => {
182 expect(perMillion('gpt-4o', '2024-11-20')).toEqual({
183 input_tokens: 2.5,
184 cache_read_tokens: 1.25,
185 output_tokens: 10,
186 })
187 expect(perMillion('gpt-4o', '2024-05-13')).toEqual({
188 input_tokens: 5,
189 output_tokens: 15,
190 })
191 expect(perMillion('gpt-4o', '2024-08-06')).toBeNull()
192 expect(perMillion('gpt-chat-latest', '2026-08-06')).toEqual({
193 input_tokens: 5,
194 output_tokens: 30,
195 })
196 expect(perMillion('o3', null)).toBeNull()
197 })
198
199 it('reads cache writes and ignores priority meters', () => {
200 expect(perMillion('gpt-6-sol', '2026-09-22')).toEqual({
201 input_tokens: 2,
202 cache_read_tokens: 0.2,
203 cache_write_tokens: 2.5,
204 output_tokens: 10,
205 })
206 })
207
208 it('refuses a model with no output meter or disagreeing meters', () => {
209 expect(perMillion('gpt-oss-120b', null)).toBeNull()
210 const split = parseAzureMeters([
211 item('o3 Inp glbl', '1K', 0.002),
212 item('o3 input global', '1K', 0.003),
213 item('o3 Outp glbl', '1K', 0.008),
214 ])
215 expect(azureModelRates(split, 'o3', null)).toBeNull()
216 })
217
218 it('serves the per-million figure the meter states', () => {
219 const served = (id: string) => {
220 const pricing = azureModelPricing(doc, id, null).pricing as
221 | RateCard
222 | undefined
223 if (!pricing) throw new Error(`fixture does not price ${id}`)
224 const { inputPerMillion, outputPerMillion } = projectTokenPricing(pricing)
225 return [inputPerMillion, outputPerMillion]
226 }
227 expect(served('gpt-4.1-mini')).toEqual([0.4, 1.6])
228 expect(served('gpt-4.1-nano')).toEqual([0.1, 0.4])
229 expect(served('gpt-5-nano')).toEqual([0.05, 0.4])
230 expect(served('gpt-5.4-nano')).toEqual([0.2, 1.25])
231 })
232
233 it('compiles a card that carries its source', () => {
234 const priced = azureModelPricing(doc, 'o3', '2025-04-16')
235 expect(priced.pricing).toMatchObject({
236 source: { url: AZURE_PRICES_URL, hash: 'h' },
237 })
238 expect(priced.factSources?.pricing).toEqual({
239 derivation: 'docs-derived',
240 sourceUrl: AZURE_PRICES_URL,
241 sourceHash: 'h',
242 path: 'Pricing',
243 })
244 })
245
246 it('tiers a long-context model at the threshold the pricing page labels', () => {
247 const rates = azureModelRates(meters, 'gpt-5.4', '2026-03-05')
248 expect(million(rates?.long)).toEqual({
249 input_tokens: 5,
250 cache_read_tokens: 0.5,
251 output_tokens: 22.5,
252 })
253 const priced = azureModelPricing(doc, 'gpt-5.4', '2026-03-05')
254 expect(priced.pricing).toMatchObject({
255 tables: {
256 rate: {
257 base: { input_tokens: 2.5e-6, output_tokens: 15e-6 },
258 '272000': { input_tokens: 5e-6, output_tokens: 22.5e-6 },
259 },
260 },
261 })
262 expect(priced.factSources?.pricing?.path).toBe(
263 `Pricing; long-context threshold: ${AZURE_PRICING_PAGE_URL}`,
264 )
265 })
266
267 it('never prices only the short band of a long-context model', () => {
268 expect(
269 azureModelRates(meters, 'gpt-5.5', '2026-04-24')?.long,
270 ).not.toBeNull()
271 expect(azureModelPricing(doc, 'gpt-5.5', '2026-04-24')).toEqual({})
272 })
273
274 it('clears the price of a version the list does not meter yet', () => {
275 expect(azureModelPricing(doc, 'gpt-chat-latest', '2026-10-07')).toEqual({
276 absent: { pricing: 'cleared' },
277 })
278 expect(azureModelPricing(doc, 'gpt-4o', '2024-08-06')).toEqual({
279 absent: { pricing: 'cleared' },
280 })
281 expect(
282 azureModelPricing(doc, 'gpt-chat-latest', '2026-08-06').pricing,
283 ).toBeDefined()
284 // No meter names it, or the row names no version: a parser miss, not a
285 // stated absence, so the stored card is kept.
286 expect(azureModelPricing(doc, 'gpt-5.6', '2026-09-01')).toEqual({})
287 expect(azureModelPricing(doc, 'o3', null)).toEqual({})
288 })
289
290 it('says whether the price list meters an id', () => {
291 expect(azureMetered(meters, 'o3-deep-research')).toBe(true)
292 expect(azureMetered(meters, 'gpt-5.6')).toBe(false)
293 expect(azureMetered(meters, 'chat-latest')).toBe(true)
294 expect(azureMetered(meters, 'chat-latest', ['gpt-chat-latest'])).toBe(false)
295 })
296
297 it('prices gpt-4 and computer-use-preview global meters', () => {
298 const extra = parseAzureMeters([
299 item('gpt-4-turbo128K Inp-glbl', '1K', 0.01),
300 item('gpt-4-turbo128K Outp-glbl', '1K', 0.03),
301 item('computer-use-inpt-glbl', '1K', 0.003),
302 item('computer-use-outp-glbl', '1K', 0.012),
303 item('computer-use-batch-inpt-glbl', '1K', 0.0015),
304 ])
305 expect(azureModelRates(extra, 'gpt-4', '2024-04-09')?.base).toEqual({
306 input_tokens: 1e-5,
307 output_tokens: 3e-5,
308 })
309 expect(azureModelRates(extra, 'computer-use-preview', null)?.base).toEqual({
310 input_tokens: 3e-6,
311 output_tokens: 1.2e-5,
312 })
313 })
314})