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 {
6 OPENCODE_PRICING_URL,
7 parseOpenCodePricing,
8} from './opencode-pricing.ts'
9
10/** Excerpt-shaped page. Names and prices are fixtures, not a live snapshot. */
11const PAGE = `---
12title: "Models"
13---
14
15## Endpoints
16
17| Model | Model ID | Endpoint | AI SDK Package |
18| --------------- | ------------ | ----------------------------------------------------- | ------------------- |
19| Opus | opus | \`https://opencode.ai/inference/anthropic/v1/messages\` | \`@ai-sdk/anthropic\` |
20| GPT | gpt | \`https://opencode.ai/inference/openai/v1/responses\` | \`@ai-sdk/openai\` |
21| Mistral | mistral | \`https://opencode.ai/inference/openai/v1/chat\` | \`@ai-sdk/openai\` |
22| Jev | jev | \`https://opencode.ai/inference/systemone/v1/systemone\` | - |
23| Pickle | pickle | \`https://opencode.ai/inference/openai/v1/chat\` | \`@ai-sdk/openai\` |
24
25These packages are for applications.
26
27## Pricing
28
29Below are the prices **per 1M tokens**.
30
31| Model | Input | Output | Cached Read | Cached Write |
32| --------------------------------- | --------------- | --------------- | --------------- | ------------ |
33| Pickle | Free | Free | Free | - |
34| Jev | $0.042 | Free | - | - |
35| Mistral (50% off) | ~~$1.36~~ $0.68 | ~~$4.18~~ $2.09 | ~~$0.14~~ $0.07 | - |
36| Opus | $4.00 | $20.00 | $0.20 | $5.00 |
37| GPT (≤ 272K tokens) | $5.00 | $30.00 | $0.50 | - |
38| GPT (> 272K tokens) | $10.00 | $45.00 | $1.00 | - |
39
40**Mistral:** The 50% discount applies through November 30, 2026.
41
42## Privacy
43`
44
45const doc = (text: string) => ({
46 text,
47 hash: 'a'.repeat(64),
48 extractedAt: '2026-10-18T00:00:00.000Z',
49})
50
51type Card = RateCard & {
52 tables: { rate: Record<string, Record<string, number>> }
53}
54
55describe('parseOpenCodePricing', () => {
56 it('joins prices to ids, with tiers, discounts and free rows', () => {
57 const cards = parseOpenCodePricing(doc(PAGE))
58 expect([...cards.keys()].sort()).toEqual([
59 'gpt',
60 'jev',
61 'mistral',
62 'opus',
63 'pickle',
64 ])
65 const card = (id: string) => cards.get(id) as Card
66 expect(card('opus').source.url).toBe(OPENCODE_PRICING_URL)
67 expect(card('opus').tables.rate).toEqual({
68 base: {
69 input_tokens: 4 / 1e6,
70 output_tokens: 20 / 1e6,
71 cache_read_tokens: 0.2 / 1e6,
72 cache_write_tokens: 5 / 1e6,
73 },
74 })
75 expect(card('gpt').tables.rate).toEqual({
76 base: {
77 input_tokens: 5 / 1e6,
78 output_tokens: 30 / 1e6,
79 cache_read_tokens: 0.5 / 1e6,
80 },
81 '272000': {
82 input_tokens: 10 / 1e6,
83 output_tokens: 45 / 1e6,
84 cache_read_tokens: 1 / 1e6,
85 },
86 })
87 // The billed price, not the struck-through list price.
88 expect(card('mistral').tables.rate.base?.input_tokens).toBe(0.68 / 1e6)
89 expect(
90 price(card('jev'), {}, { input_tokens: 1_000_000, output_tokens: 5 }),
91 ).toBeCloseTo(0.042)
92 expect(card('pickle')).toMatchObject({ price: 0 })
93 expect(
94 price(card('pickle'), {}, { input_tokens: 10, output_tokens: 10 }),
95 ).toBe(0)
96 })
97
98 it.each([
99 [
100 'an unreadable cell',
101 PAGE.replace('$4.00 ', 'TBD '),
102 /unreadable price TBD/,
103 ],
104 [
105 'a changed header',
106 PAGE.replace('Cached Write', 'Cache Write '),
107 /Pricing header changed/,
108 ],
109 [
110 'a row naming no id',
111 PAGE.replace('| Opus ', '| Opus 9 '),
112 /names no model id/,
113 ],
114 [
115 'a tier with no pair',
116 PAGE.replace(/\| GPT \(> 272K.*\n/, ''),
117 /unreadable context tiers/,
118 ],
119 [
120 'mismatched tier thresholds',
121 PAGE.replace('≤ 272K', '≤ 200K'),
122 /unreadable context tiers/,
123 ],
124 [
125 'a tier that drops a lever',
126 PAGE.replace(
127 '| $10.00 | $45.00 | $1.00 ',
128 '| $10.00 | $45.00 | - ',
129 ),
130 /changes the levers/,
131 ],
132 [
133 'a duplicate price row',
134 PAGE.replace(/^\| Opus {2}.*\$4\.00.*$/m, (row) => `${row}\n${row}`),
135 /duplicate price row/,
136 ],
137 [
138 'no output price',
139 PAGE.replace(
140 '| $4.00 | $20.00 ',
141 '| $4.00 | - ',
142 ),
143 /omits input or output/,
144 ],
145 [
146 'a price table that ends early',
147 PAGE.replace('\n| GPT (≤', '\n\n| GPT (≤'),
148 /has an endpoint but no price row/,
149 ],
150 [
151 'no Pricing section',
152 PAGE.replace('## Pricing', '## Prices'),
153 /has no Pricing/,
154 ],
155 [
156 'an empty table',
157 PAGE.replace(/\| (Pickle|Jev|Mistral|Opus|GPT) .*\n/g, ''),
158 /unreadable/,
159 ],
160 ])('throws on %s', (_, text, message) => {
161 expect(() => parseOpenCodePricing(doc(text))).toThrow(message)
162 })
163})