Open History to see the last commit
1import { readFileSync } from 'node:fs'
2
3import { describe, expect, it } from 'vitest'
4
5import { compileTokenCard } from '@modelschemas/rate-card'
6
7import { parseCoherePricing } from './cohere-pricing.ts'
8
9// The pricing groups of https://cohere.com/pricing as its data payload
10// carries them, and the legacy FAQ, fetched 2026-10-07.
11const LIVE_PAGE = readFileSync(
12 new URL('./fixtures/cohere-pricing.html.txt', import.meta.url),
13 'utf8',
14)
15const LIVE_IDS = [
16 'command-a-plus-05-2026',
17 'command-a-03-2025',
18 'command-r7b-12-2024',
19 'command-r-08-2024',
20 'command-r-plus-08-2024',
21 'north-mini-code-1-0',
22]
23
24describe('cohere pricing cards', () => {
25 it('prices the one live dated id under a card name', () => {
26 const rates = parseCoherePricing(LIVE_PAGE, '', LIVE_IDS)
27 expect(rates.get('command-r-08-2024')).toEqual({
28 input_tokens: 1.5e-7,
29 output_tokens: 6e-7,
30 })
31 expect(rates.get('command-r7b-12-2024')).toEqual({
32 input_tokens: 3.75e-8,
33 output_tokens: 1.5e-7,
34 })
35 // The FAQ sentence, not a card.
36 expect(rates.get('command-r-plus-08-2024')?.input_tokens).toBe(2.5e-6)
37 // "Free" cards, search and page prices, and Embed's one "Cost" figure
38 // are not token prices for a chat id.
39 expect([...rates.keys()].sort()).toEqual([
40 'command-r-03-2024',
41 'command-r-08-2024',
42 'command-r-plus-04-2024',
43 'command-r-plus-08-2024',
44 'command-r7b-12-2024',
45 ])
46 })
47
48 it('gives no card when the name fits no live id, or several', () => {
49 expect(
50 parseCoherePricing(LIVE_PAGE, '', ['command-r-plus-08-2024']).has(
51 'command-r-08-2024',
52 ),
53 ).toBe(false)
54 const two = parseCoherePricing(LIVE_PAGE, '', [
55 'command-r-08-2024',
56 'command-r-02-2027',
57 ])
58 expect(two.has('command-r-08-2024')).toBe(false)
59 expect(two.has('command-r-02-2027')).toBe(false)
60 })
61
62 it('drops a card whose price block is reshaped', () => {
63 const ids = ['command-r7b-12-2024']
64 const mutated = (from: string, to: string) => {
65 expect(LIVE_PAGE).toContain(from)
66 return parseCoherePricing(LIVE_PAGE.replace(from, to), '', ids)
67 }
68 expect(mutated('', '').has('command-r7b-12-2024')).toBe(true)
69 for (const [from, to] of [
70 // A different unit.
71 [
72 '\\"modelName\\":\\"Command R7B\\",\\"per\\":\\"1M tokens\\"',
73 '\\"modelName\\":\\"Command R7B\\",\\"per\\":\\"1K tokens\\"',
74 ],
75 // A thousands separator must not parse as its leading digits.
76 ['\\"inputPrice\\":0.0375,', '\\"inputPrice\\":\\"1,037.50\\",'],
77 // A batch or cached label is not the standard input price.
78 [
79 '\\"inputLabel\\":\\"Input\\",\\"inputPrice\\":0.0375',
80 '\\"inputLabel\\":\\"Batch input\\",\\"inputPrice\\":0.0375',
81 ],
82 // A per-unit override on the price row.
83 [
84 '\\"inputPrice\\":0.0375,\\"outputLabel\\":\\"Output\\",\\"outputPrice\\":0.15}',
85 '\\"inputPrice\\":0.0375,\\"outputLabel\\":\\"Output\\",\\"outputPrice\\":0.15,\\"overridePer\\":\\"1K tokens\\"}',
86 ],
87 // A second price row (a tier) leaves no single standard price.
88 [
89 '\\"outputPrice\\":0.15}]',
90 '\\"outputPrice\\":0.15},{\\"_key\\":\\"x\\",\\"_type\\":\\"pricing\\",\\"inputLabel\\":\\"Input\\",\\"inputPrice\\":1,\\"outputLabel\\":\\"Output\\",\\"outputPrice\\":2}]',
91 ],
92 ['\\"inputPrice\\":0.0375,', '\\"inputPrice\\":0,'],
93 ] as const) {
94 expect(mutated(from, to).has('command-r7b-12-2024'), from).toBe(false)
95 }
96 })
97})
98
99const PAGE = `<p>For existing customers:</p>
100<ul>
101<li>Command pricing is $1.00/1M tokens for input and $2.00/1M tokens for output</li>
102<li>Command-light pricing is $0.30/1M tokens for input and $0.60/1M tokens for output</li>
103<li>Command R 03-2024 pricing is $0.50/1M tokens for input and $1.50/1M tokens for output</li>
104<li>Command R+ 04-2024 pricing is $3.00/1M tokens for input and $15.00/1M tokens for output</li>
105</ul>
106<p>Command R+ 08-2024 pricing is $2.50/1M tokens for input and $10.00/1M tokens for output</p>
107<p>Aya Expanse models (8B and 32B) on the API are charged at $0.50/1M tokens for input and $1.50/1M tokens for output.</p>
108<p>Command A+ is free until rate limits are reached.</p>`
109
110const AYA = `| \`c4ai-aya-expanse-32b\` | Aya Expanse 32B |`
111
112describe('cohere pricing page', () => {
113 it('prices dated Command R rows and the Aya id the model page names', () => {
114 const rates = parseCoherePricing(PAGE, AYA)
115 expect(rates.get('command-r-plus-08-2024')).toEqual({
116 input_tokens: 2.5 / 1e6,
117 output_tokens: 10 / 1e6,
118 })
119 expect(rates.get('command-r-03-2024')?.input_tokens).toBe(0.5 / 1e6)
120 expect(rates.get('c4ai-aya-expanse-32b')?.output_tokens).toBe(1.5 / 1e6)
121 const card = compileTokenCard(
122 rates.get('command-r-plus-08-2024') ?? {},
123 [],
124 {
125 url: 'https://cohere.com/pricing',
126 hash: 'abc',
127 extractedAt: '2026-10-03',
128 },
129 )
130 expect(card).not.toBeNull()
131 // No API id in the sentence, and Command A+ publishes no token price.
132 expect(rates.has('command')).toBe(false)
133 expect(rates.has('command-light')).toBe(false)
134 expect(rates.has('command-a-03-2025')).toBe(false)
135 expect(rates.has('command-a-plus-05-2026')).toBe(false)
136 expect(rates.has('c4ai-aya-expanse-8b')).toBe(false)
137 })
138})