Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import {
4 bedrockNameKey,
5 lookupBedrockPrice,
6 matchBedrockModelId,
7 matchBedrockOfferSlug,
8 parseBedrockOffer,
9 parseBedrockPricingPage,
10 BEDROCK_PRICE_LIST_URL,
11 BEDROCK_PRICING_PAGE_URL,
12} from './bedrock-pricing.ts'
13
14function dimension(usd: string, unit = '1K tokens') {
15 return {
16 unit,
17 beginRange: '0',
18 endRange: 'Inf',
19 pricePerUnit: { USD: usd },
20 }
21}
22
23function product(
24 usagetype: string,
25 inferenceType: string,
26 serviceTier: string,
27 feature: string,
28 model: string,
29) {
30 return {
31 attributes: {
32 usagetype,
33 inferenceType,
34 service_tier: serviceTier,
35 feature,
36 model,
37 },
38 }
39}
40
41const OFFER = {
42 products: {
43 kimiIn: product(
44 'USE1-moonshotai.kimi-k3-mantle-input-tokens-standard',
45 'Input tokens',
46 'standard',
47 '',
48 'Kimi K3',
49 ),
50 kimiOut: product(
51 'USE1-moonshotai.kimi-k3-mantle-output-tokens-standard',
52 'Output tokens',
53 'standard',
54 '',
55 'Kimi K3',
56 ),
57 kimiRead: product(
58 'USE1-moonshotai.kimi-k3-mantle-cache-read-tokens-standard',
59 'Cache read tokens',
60 'standard',
61 '',
62 'Kimi K3',
63 ),
64 kimiGlobal: product(
65 'USE1-moonshotai.kimi-k3-mantle-input-tokens-global-standard',
66 'Input tokens global',
67 'global-standard',
68 '',
69 'Kimi K3',
70 ),
71 kimiPriority: product(
72 'USE1-moonshotai.kimi-k3-mantle-output-tokens-priority',
73 'Output tokens priority',
74 'priority',
75 '',
76 'Kimi K3',
77 ),
78 qwenIn: product(
79 'USE1-Qwen3-32B-input-tokens',
80 'Input tokens',
81 '',
82 'On-demand Inference',
83 'Qwen3 32B',
84 ),
85 qwenOut: product(
86 'USE1-Qwen3-32B-output-tokens',
87 'Output tokens',
88 '',
89 'On-demand Inference',
90 'Qwen3 32B',
91 ),
92 qwenFlex: product(
93 'USE1-Qwen3-32B-input-tokens-flex',
94 'Input tokens flex',
95 '',
96 'On-demand Inference',
97 'Qwen3 32B',
98 ),
99 sonicTextIn: product(
100 'USE1-NovaSonic-text-input-tokens',
101 'Text Input Token',
102 '',
103 'On-demand Inference',
104 'Nova Sonic',
105 ),
106 sonicTextOut: product(
107 'USE1-NovaSonic-text-output-tokens',
108 'Text output token',
109 '',
110 'On-demand Inference',
111 'Nova Sonic',
112 ),
113 sonicSpeechIn: product(
114 'USE1-NovaSonic-speech-input-tokens',
115 'Speech Understanding input token',
116 '',
117 'On-demand Inference',
118 'Nova Sonic',
119 ),
120 sonicSpeechOut: product(
121 'USE1-NovaSonic-speech-output-tokens',
122 'Speech Understanding output token',
123 '',
124 'On-demand Inference',
125 'Nova Sonic',
126 ),
127 deepseekIn: product(
128 'USE1-deepseek.v3.1-mantle-input-tokens-standard',
129 'Input tokens',
130 'standard',
131 '',
132 'DeepSeek V3.1',
133 ),
134 deepseekOut: product(
135 'USE1-deepseek.v3.1-mantle-output-tokens-standard',
136 'Output tokens',
137 'standard',
138 '',
139 'DeepSeek V3.1',
140 ),
141 kimiWrite30: product(
142 'USE1-moonshotai.kimi-k3-mantle-cache-write-tokens-30m-standard',
143 'Cache write tokens',
144 'standard',
145 '',
146 'Kimi K3',
147 ),
148 qwenNextIn: product(
149 'USE1-qwen.qwen3-next-80b-a3b-instruct-mantle-input-tokens-standard',
150 'Input tokens',
151 'standard',
152 '',
153 'Qwen3 Next 80B A3B',
154 ),
155 qwenNextOut: product(
156 'USE1-qwen.qwen3-next-80b-a3b-instruct-mantle-output-tokens-standard',
157 'Output tokens',
158 'standard',
159 '',
160 'Qwen3 Next 80B A3B',
161 ),
162 },
163 terms: {
164 OnDemand: {
165 kimiIn: { t: { priceDimensions: { d: dimension('0.0033000000') } } },
166 kimiOut: { t: { priceDimensions: { d: dimension('0.0165000000') } } },
167 kimiRead: { t: { priceDimensions: { d: dimension('0.0003300000') } } },
168 kimiGlobal: { t: { priceDimensions: { d: dimension('0.0010000000') } } },
169 kimiPriority: {
170 t: { priceDimensions: { d: dimension('0.0280000000') } },
171 },
172 qwenIn: { t: { priceDimensions: { d: dimension('0.0001500000') } } },
173 qwenOut: { t: { priceDimensions: { d: dimension('0.0006000000') } } },
174 qwenFlex: { t: { priceDimensions: { d: dimension('0.0000750000') } } },
175 sonicTextIn: { t: { priceDimensions: { d: dimension('0.0000600000') } } },
176 sonicTextOut: {
177 t: { priceDimensions: { d: dimension('0.0002400000') } },
178 },
179 sonicSpeechIn: {
180 t: { priceDimensions: { d: dimension('0.0034000000') } },
181 },
182 sonicSpeechOut: {
183 t: { priceDimensions: { d: dimension('0.0136000000') } },
184 },
185 deepseekIn: { t: { priceDimensions: { d: dimension('0.0005500000') } } },
186 deepseekOut: { t: { priceDimensions: { d: dimension('0.0022000000') } } },
187 kimiWrite30: { t: { priceDimensions: { d: dimension('9.9900000000') } } },
188 qwenNextIn: { t: { priceDimensions: { d: dimension('0.0001400000') } } },
189 qwenNextOut: { t: { priceDimensions: { d: dimension('0.0014000000') } } },
190 },
191 },
192}
193
194const METER = {
195 regions: {
196 'US East (N. Virginia)': {
197 GEOIN: { price: '2.2000000000' },
198 GLOBAL: { price: '0.5000000000' },
199 },
200 },
201}
202
203const PAGE = `
204<h2>Global Cross-region Inference</h2>
205<table><thead><tr><th>Anthropic models</th><th>Price per 1M input tokens</th><th>Price per 1M output tokens</th></tr></thead>
206<tbody><tr><td>Claude Example</td><td>{priceOf!bedrockfoundationmodels/bedrockfoundationmodels!GLOBAL}</td><td>$1.00</td></tr></tbody></table>
207<h2>Geo and In-region Cross-region Inference</h2>
208<table><thead><tr>
209<th>Anthropic models</th>
210<th>Price per 1M input tokens</th>
211<th>Price per 1M output tokens</th>
212<th>Price per 1M input tokens (batch)</th>
213<th>Price per 1M input tokens (5m cache write)</th>
214<th>Price per 1M input tokens (1h cache write)</th>
215<th>Price per 1M input tokens (cache read)</th>
216</tr></thead>
217<tbody>
218<tr><td>Claude Example</td><td>{priceOf!bedrockfoundationmodels/bedrockfoundationmodels!GEOIN}</td><td>$11.00</td><td>$0.10</td><td>$2.75</td><td>$4.40</td><td>$0.22</td></tr>
219<tr><td>Claude Example - Long Context</td><td>$99.00</td><td>$99.00</td><td></td><td></td><td></td><td></td></tr>
220</tbody></table>
221<h2>Priority tier pricing</h2>
222<table><thead><tr><th>Models</th><th>Price per 1M input tokens</th><th>Price per 1M output tokens</th></tr></thead>
223<tbody><tr><td>Claude Example</td><td>$0.01</td><td>$0.01</td></tr></tbody></table>
224<h2>Cohere</h2>
225<table><thead><tr><th>Cohere models</th><th>Price per 1M input tokens</th><th>Price per 1M output tokens</th><th>Price per image</th></tr></thead>
226<tbody>
227<tr><td>Command</td><td>$1.00</td><td>$2.00</td><td>$0.001</td></tr>
228<tr><td>Command R+</td><td>$3.00</td><td>$15.00</td><td>N/A</td></tr>
229<tr><td>Command R</td><td>$0.50</td><td>$1.50</td><td>N/A</td></tr>
230<tr><td>Claude Haiku 4.5</td><td>$0.80</td><td>$4.00</td><td></td></tr>
231</tbody></table>
232<h2>Global example</h2>
233<table><thead><tr><th>Anthropic models</th><th>Price per 1M input tokens</th><th>Price per 1M output tokens</th></tr></thead>
234<tbody><tr><td>Claude 4.5 Haiku</td><td>$0.01</td><td>$0.02</td></tr></tbody></table>
235`
236
237describe('bedrock price list', () => {
238 const parsed = parseBedrockOffer(OFFER)
239
240 it('keeps standard on-demand rates and drops other tiers', () => {
241 expect(parsed.byId.get('moonshotai.kimi-k3')).toEqual({
242 input_tokens: 0.0033 / 1e3,
243 output_tokens: 0.0165 / 1e3,
244 cache_read_tokens: 0.00033 / 1e3,
245 })
246 expect(parsed.byName.get(bedrockNameKey('Qwen3 32B'))).toEqual({
247 input_tokens: 0.15 / 1e6,
248 output_tokens: 0.6 / 1e6,
249 })
250 expect(
251 parsed.byId.get('moonshotai.kimi-k3')?.cache_write_tokens,
252 ).toBeUndefined()
253 })
254
255 it('indexes an offer id by its model name when the card id differs', () => {
256 expect(parsed.byName.get(bedrockNameKey('DeepSeek V3.1'))).toEqual({
257 input_tokens: 0.00055 / 1e3,
258 output_tokens: 0.0022 / 1e3,
259 })
260 expect(
261 parsed.byName.get(bedrockNameKey('Qwen3 Next 80B A3B'))?.input_tokens,
262 ).toBe(0.00014 / 1e3)
263 expect(
264 matchBedrockModelId(parsed.byId.keys(), 'qwen.qwen3-next-80b-a3b'),
265 ).toBeNull()
266 expect(bedrockNameKey('Command R\\+')).toBe('command r+')
267 expect(bedrockNameKey('Claude Haiku 4.5')).toBe(
268 bedrockNameKey('Claude 4.5 Haiku'),
269 )
270 expect(bedrockNameKey('Ministral 3 8B')).not.toBe(
271 bedrockNameKey('Ministral 8B 3.0'),
272 )
273 })
274
275 it('drops a model priced as both speech and text', () => {
276 expect(parsed.byName.has(bedrockNameKey('Nova Sonic'))).toBe(false)
277 expect([...parsed.byId.keys()].some((id) => /sonic/i.test(id))).toBe(false)
278 })
279
280 it('joins a versioned model id on a boundary', () => {
281 expect(matchBedrockModelId(parsed.byId.keys(), 'moonshotai.kimi-k3')).toBe(
282 'moonshotai.kimi-k3',
283 )
284 expect(
285 matchBedrockModelId(
286 ['qwen.qwen3-32b', 'qwen.qwen3'],
287 'qwen.qwen3-32b-v1:0',
288 ),
289 ).toBe('qwen.qwen3-32b')
290 expect(matchBedrockModelId(['deepseek.v3'], 'deepseek.v3.2')).toBeNull()
291 })
292})
293
294describe('bedrock pricing page', () => {
295 const page = parseBedrockPricingPage(PAGE, METER)
296
297 it('uses the geo table, including cache columns, and ignores global, batch, priority, and long context', () => {
298 expect(page.get(bedrockNameKey('Claude Example'))).toEqual({
299 input_tokens: 2.2 / 1e6,
300 output_tokens: 11 / 1e6,
301 cache_write_tokens: 2.75 / 1e6,
302 cache_write_1h_tokens: 4.4 / 1e6,
303 cache_read_tokens: 0.22 / 1e6,
304 })
305 expect(page.has(bedrockNameKey('Claude Example - Long Context'))).toBe(
306 false,
307 )
308 })
309
310 it('extracts the explicit global table separately from geo prices', () => {
311 const global = parseBedrockPricingPage(PAGE, METER, 'global')
312 expect(global.get(bedrockNameKey('Claude Example'))).toEqual({
313 input_tokens: 0.5 / 1e6,
314 output_tokens: 1 / 1e6,
315 })
316 expect(global.has('command')).toBe(false)
317 expect(global.has('command r+')).toBe(false)
318 })
319
320 it('reads a literal dollar row and skips the per-image column', () => {
321 expect(page.get(bedrockNameKey('Command'))).toEqual({
322 input_tokens: 1 / 1e6,
323 output_tokens: 2 / 1e6,
324 })
325 expect(page.get('command r+')).toEqual({
326 input_tokens: 3 / 1e6,
327 output_tokens: 15 / 1e6,
328 })
329 expect(page.get('command r')).toEqual({
330 input_tokens: 0.5 / 1e6,
331 output_tokens: 1.5 / 1e6,
332 })
333 })
334
335 it('joins both Claude name orders and ignores the global spelling', () => {
336 expect(page.get(bedrockNameKey('Claude 4.5 Haiku'))).toEqual({
337 input_tokens: 0.8 / 1e6,
338 output_tokens: 4 / 1e6,
339 })
340 })
341
342 it('prefers the offer id over the page', () => {
343 const offer = parseBedrockOffer(OFFER)
344 const hit = lookupBedrockPrice(
345 {
346 offerById: offer.byId,
347 offerByName: offer.byName,
348 offerBySlug: offer.bySlug,
349 pageByName: page,
350 offerHash: 'offer',
351 pageHash: 'page',
352 },
353 'moonshotai.kimi-k3',
354 'Claude Example',
355 )
356 expect(hit?.url).toBe(BEDROCK_PRICE_LIST_URL)
357 expect(hit?.rates.input_tokens).toBeCloseTo(3.3 / 1e6)
358 })
359
360 it('uses the page when the offer has no row', () => {
361 const offer = parseBedrockOffer(OFFER)
362 const book = {
363 offerById: offer.byId,
364 offerByName: offer.byName,
365 offerBySlug: offer.bySlug,
366 pageByName: page,
367 offerHash: 'offer',
368 pageHash: 'page',
369 }
370 const hit = lookupBedrockPrice(
371 book,
372 'anthropic.claude-example',
373 'Claude Example',
374 )
375 expect(hit?.url).toBe(BEDROCK_PRICING_PAGE_URL)
376 expect(hit?.hash).toBe('page')
377 const command = lookupBedrockPrice(
378 book,
379 'cohere.command-r-plus',
380 'Command R\\+',
381 )
382 expect(command?.rates.input_tokens).toBeCloseTo(3 / 1e6)
383 const deepseek = lookupBedrockPrice(
384 book,
385 'deepseek.v3-v1:0',
386 'DeepSeek-V3.1',
387 )
388 expect(deepseek?.url).toBe(BEDROCK_PRICE_LIST_URL)
389 expect(deepseek?.rates.input_tokens).toBeCloseTo(0.00055 / 1e3)
390 const haiku = lookupBedrockPrice(
391 book,
392 'anthropic.claude-haiku-4-5',
393 'Claude Haiku 4.5',
394 )
395 expect(haiku?.rates.input_tokens).toBeCloseTo(0.8 / 1e6)
396 })
397})
398
399describe('bedrock offer slug', () => {
400 // us-east-1 On-demand Inference offer for Pixtral Large 25.02.
401 // Descriptions: $0.002 per 1K input, $0.006 per 1K output.
402 const offer = parseBedrockOffer({
403 products: {
404 pixtralIn: product(
405 'USE1-PixtralLarge2502-input-tokens',
406 'Input tokens',
407 '',
408 'On-demand Inference',
409 'Pixtral Large 25.02',
410 ),
411 pixtralOut: product(
412 'USE1-PixtralLarge2502-output-tokens',
413 'Output tokens',
414 '',
415 'On-demand Inference',
416 'Pixtral Large 25.02',
417 ),
418 pixtralBatch: product(
419 'USE1-PixtralLarge2502-input-tokens-batch',
420 'input tokens batch',
421 '',
422 'On-demand Inference',
423 'Pixtral Large 25.02',
424 ),
425 glmIn: product(
426 'USE1-zai.glm-5-input-tokens',
427 'Input tokens',
428 'standard',
429 '',
430 'GLM 5',
431 ),
432 glmOut: product(
433 'USE1-zai.glm-5-output-tokens',
434 'Output tokens',
435 'standard',
436 '',
437 'GLM 5',
438 ),
439 r1In: product(
440 'USE1-DeepSeekR1-input-tokens',
441 'Input tokens',
442 '',
443 'On-demand Inference',
444 'R1',
445 ),
446 r1Out: product(
447 'USE1-DeepSeekR1-output-tokens',
448 'Output tokens',
449 '',
450 'On-demand Inference',
451 'R1',
452 ),
453 },
454 terms: {
455 OnDemand: {
456 pixtralIn: { t: { priceDimensions: { d: dimension('0.0020000000') } } },
457 pixtralOut: {
458 t: { priceDimensions: { d: dimension('0.0060000000') } },
459 },
460 pixtralBatch: {
461 t: { priceDimensions: { d: dimension('0.0001000000') } },
462 },
463 glmIn: { t: { priceDimensions: { d: dimension('0.0010000000') } } },
464 glmOut: { t: { priceDimensions: { d: dimension('0.0032000000') } } },
465 r1In: { t: { priceDimensions: { d: dimension('0.0013500000') } } },
466 r1Out: { t: { priceDimensions: { d: dimension('0.0054000000') } } },
467 },
468 },
469 })
470
471 it('prices Pixtral Large from the on-demand offer, not the card display name', () => {
472 expect(offer.bySlug.get('pixtral-large-2502')).toEqual({
473 input_tokens: 0.002 / 1e3,
474 output_tokens: 0.006 / 1e3,
475 })
476 expect(offer.bySlug.has('glm-5')).toBe(false)
477 expect(offer.bySlug.has('r1')).toBe(false)
478 const book = {
479 offerById: offer.byId,
480 offerByName: offer.byName,
481 offerBySlug: offer.bySlug,
482 pageByName: new Map<string, Record<string, number>>(),
483 offerHash: 'offer',
484 pageHash: 'page',
485 }
486 const hit = lookupBedrockPrice(
487 book,
488 'mistral.pixtral-large-2502-v1:0',
489 'Pixtral Large',
490 )
491 expect(hit?.url).toBe(BEDROCK_PRICE_LIST_URL)
492 expect(hit?.rates).toEqual({
493 input_tokens: 0.002 / 1e3,
494 output_tokens: 0.006 / 1e3,
495 })
496 expect(lookupBedrockPrice(book, 'zai.glm-5.3', 'GLM 5.3')).toBeNull()
497 expect(
498 lookupBedrockPrice(book, 'deepseek.r1-v1:0', 'DeepSeek-R1'),
499 ).toBeNull()
500 expect(
501 matchBedrockOfferSlug(
502 ['pixtral-large-25', 'pixtral-large-2502'],
503 'mistral.pixtral-large-2502-v1:0',
504 ),
505 ).toBe('pixtral-large-2502')
506 expect(matchBedrockOfferSlug(['glm-5'], 'zai.glm-5.3')).toBeNull()
507 })
508})
509
510describe('bedrock pricing page inference options', () => {
511 // Z AI accordion on aws.amazon.com/bedrock/pricing/. US CRIS is the
512 // standard rate. The note after the table is what makes bare Input/Output
513 // columns on the Kimi table mean per 1M tokens.
514 const html = `
515<h2 id="Z_AI">Z AI</h2>
516<div class="lb-txt-none lb-txt">GLM 4.7 Flash</div>
517<div class="lb-txt-none lb-txt">GLM 5.3</div>
518<table>
519<tbody>
520<tr>
521<td><b>Inference option</b></td>
522<td><b>Price per 1M input tokens</b></td>
523<td><b>Price per 1M output tokens</b></td>
524<td><b>Cache read</b></td>
525<td><b>Cache write - 30m</b></td>
526</tr>
527<tr>
528<td><b>Global CRIS</b></td>
529<td>$ 1.68</td>
530<td>$ 5.28</td>
531<td>$ 0.312</td>
532<td>$ 2.10</td>
533</tr>
534<tr>
535<td><b>US CRIS</b></td>
536<td>$ 1.848</td>
537<td>$ 5.808</td>
538<td>$ 0.3432</td>
539<td>$ 2.31</td>
540</tr>
541</tbody>
542</table>
543<p><i>All prices are per 1 million tokens. Pricing shown is for the Standard tier.</i></p>
544<div class="lb-txt-none lb-txt">Kimi 2</div>
545<div class="lb-txt-none lb-txt">Kimi 3</div>
546<table>
547<tbody>
548<tr>
549<td><b>Inference option</b></td>
550<td><b>Input</b></td>
551<td><b>Output</b></td>
552<td><b>Cache read</b></td>
553<td><b>Cache write (30 min)</b></td>
554</tr>
555<tr>
556<td>Global CRIS</td>
557<td>$3.00</td>
558<td>$15.00</td>
559<td>$0.30</td>
560<td>$3.75</td>
561</tr>
562<tr>
563<td>US CRIS</td>
564<td>$3.30</td>
565<td>$16.50</td>
566<td>$0.33</td>
567<td>$4.125</td>
568</tr>
569</tbody>
570</table>
571<p><i>All prices are per 1 million tokens. Pricing shown is for the Standard tier.</i></p>
572`
573 const page = parseBedrockPricingPage(html, {
574 regions: { 'US East (N. Virginia)': {} },
575 })
576
577 it('keeps Standard US CRIS and drops the 30-minute write and Global', () => {
578 expect(page.get(bedrockNameKey('GLM 5.3'))).toEqual({
579 input_tokens: 1.848 / 1e6,
580 output_tokens: 5.808 / 1e6,
581 cache_read_tokens: 0.3432 / 1e6,
582 })
583 expect(page.has(bedrockNameKey('GLM 4.7 Flash'))).toBe(false)
584 expect(page.get(bedrockNameKey('Kimi 3'))).toEqual({
585 input_tokens: 3.3 / 1e6,
586 output_tokens: 16.5 / 1e6,
587 cache_read_tokens: 0.33 / 1e6,
588 })
589 })
590})
591
592// Unresolvable dynamic current meters are parser errors, not unknown pricing.
593it('rejects an unresolved published Global meter', () => {
594 const html = `<h2>Global cross-Region inference</h2><table>
595<tr><th>Model</th><th>Price per 1M input tokens</th><th>Price per 1M output tokens</th></tr>
596<tr><td>Synthetic model</td><td>{priceOf!bedrockfoundationmodels/bedrockfoundationmodels!missing}</td><td>$2</td></tr></table>`
597 expect(() =>
598 parseBedrockPricingPage(
599 html,
600 { regions: { 'US East (N. Virginia)': {} } },
601 'global',
602 ),
603 ).toThrow('amazon-bedrock global pricing: unreadable quote')
604})
605
606it('resolves Global prices from provider-published encoded public meter paths', () => {
607 const html = `<h2>Global cross-Region inference</h2><table>
608<tr><th>Model</th><th>Price per 1M input tokens</th><th>Price per 1M output tokens</th></tr>
609<tr><td>Synthetic model</td><td>{priceOf!bedrockfoundationmodels/cHVibGljL3N5bnRoZXRpYw!input}</td><td>{priceOf!bedrockfoundationmodels/cHVibGljL3N5bnRoZXRpYw!output!opt}</td></tr></table>`
610 const meter = {
611 regions: {
612 'US East (N. Virginia)': {
613 input: { price: '7' },
614 output: { price: '19' },
615 },
616 },
617 }
618 expect(
619 parseBedrockPricingPage(html, meter, 'global').get(
620 bedrockNameKey('Synthetic model'),
621 ),
622 ).toEqual({ input_tokens: 7 / 1e6, output_tokens: 19 / 1e6 })
623})