Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

opencode-pricing.test.ts

301 lines10.6 KB

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
44​
45/** The three live Image pricing rows (issue #100007), on the fixture page. */
46const IMAGE_PAGE = PAGE.replace(
47 '| Pickle | pickle |',
48 `| GPT Image 2.5 Sunburst | gpt-image-2.5-sunburst | \`https://opencode.ai/inference/openai/v1/images/generations\` | \`@ai-sdk/openai\` |
49| Nano Banana 2.1 | gemini-nano-banana-2.1 | \`https://opencode.ai/inference/google/v1beta/models/gemini-nano-banana-2.1:generateContent\` | \`@ai-sdk/google\` |
50| Grok Imagine Image 2.0 | grok-imagine-image-2.0 | \`https://opencode.ai/inference/xai/v1/images/generations\` | \`@ai-sdk/xai\` |
51| Pickle | pickle |`,
52).replace(
53 '\n## Privacy\n',
54 `
55### Image pricing
56​
57| Model | Price | Notes |
58| ---------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------- |
59| GPT Image 2.5 Sunburst | $1.00 / image | Plus $8 / $30 per 1M input / output tokens, and $2 per 1M cached input tokens. |
60| Nano Banana 2.1 | $1.50 / $7.50 / $30.00 per 1M tokens | Input / text output / image output. About $0.034 per 1K image, $0.050 per 2K, $0.113 per 4K. |
61| Grok Imagine Image 2.0 | $0.04 / image | $0.01 per input image; quality and resolution can raise the output price to $0.05\u2013$0.08. |
62​
63## Privacy
64`,
66​
67const doc = (text: string) => ({
68 text,
69 hash: 'a'.repeat(64),
70 extractedAt: '2026-10-18T00:00:00.000Z',
71})
72​
73type Card = RateCard & {
74 tables: { rate: Record<string, Record<string, number>> }
76​
77describe('parseOpenCodePricing', () => {
78 it('joins prices to ids, with tiers, discounts and free rows', () => {
79 const cards = parseOpenCodePricing(doc(PAGE))
80 expect([...cards.keys()].sort()).toEqual([
81 'gpt',
82 'jev',
83 'mistral',
84 'opus',
85 'pickle',
86 ])
87 const card = (id: string) => cards.get(id) as Card
88 expect(card('opus').source.url).toBe(OPENCODE_PRICING_URL)
89 expect(card('opus').tables.rate).toEqual({
90 base: {
91 input_tokens: 4 / 1e6,
92 output_tokens: 20 / 1e6,
93 cache_read_tokens: 0.2 / 1e6,
94 cache_write_tokens: 5 / 1e6,
95 },
96 })
97 expect(card('gpt').tables.rate).toEqual({
98 base: {
99 input_tokens: 5 / 1e6,
100 output_tokens: 30 / 1e6,
101 cache_read_tokens: 0.5 / 1e6,
102 },
103 '272000': {
104 input_tokens: 10 / 1e6,
105 output_tokens: 45 / 1e6,
106 cache_read_tokens: 1 / 1e6,
107 },
108 })
109 // The billed price, not the struck-through list price.
110 expect(card('mistral').tables.rate.base?.input_tokens).toBe(0.68 / 1e6)
111 expect(
112 price(card('jev'), {}, { input_tokens: 1_000_000, output_tokens: 5 }),
113 ).toBeCloseTo(0.042)
114 expect(card('pickle')).toMatchObject({ price: 0 })
115 expect(
116 price(card('pickle'), {}, { input_tokens: 10, output_tokens: 10 }),
117 ).toBe(0)
118 })
119​
120 it('prices stated image rows and stores no card for an unruled band', () => {
121 const cards = parseOpenCodePricing(doc(IMAGE_PAGE))
122 expect(cards.has('opus')).toBe(true)
123 const sun = cards.get('gpt-image-2.5-sunburst') as Card
124 const banana = cards.get('gemini-nano-banana-2.1') as Card
125 // $0.04 is the floor. Quality and resolution can raise it to $0.05–$0.08
126 // with no rule, so the row is unverified rather than billed at $0.04.
127 expect(cards.has('grok-imagine-image-2.0')).toBe(false)
128 expect(sun.tables.rate).toEqual({
129 base: {
130 input_tokens: 8 / 1e6,
131 output_tokens: 30 / 1e6,
132 cache_read_tokens: 2 / 1e6,
133 },
134 })
135 expect(sun.inputs.num_images).toEqual({
136 param: 'num_images',
137 bound: 'usage',
138 kind: 'number',
139 })
140 // $1.00 per image plus $8 / $30 / $2 per 1M tokens.
141 expect(
142 price(
143 sun,
144 {},
145 {
146 num_images: 1,
147 input_tokens: 1_000_000,
148 output_tokens: 1_000_000,
149 cache_read_tokens: 1_000_000,
150 },
151 ),
152 ).toBeCloseTo(41)
153 expect(
154 price(sun, {}, { num_images: 2, input_tokens: 0, output_tokens: 0 }),
155 ).toBeCloseTo(2)
156 expect(banana.tables.rate).toEqual({
157 base: {
158 input_tokens: 1.5 / 1e6,
159 output_tokens: 7.5 / 1e6,
160 image_output_tokens: 30 / 1e6,
161 },
162 })
163 // The "About $0.034 per 1K" clause is not a resolution price.
164 expect(banana.inputs).not.toHaveProperty('resolution')
165 expect(
166 price(
167 banana,
168 {},
169 {
170 input_tokens: 1_000_000,
171 output_tokens: 1_000_000,
172 image_output_tokens: 1_000_000,
173 },
174 ),
175 ).toBeCloseTo(39)
176 })
177​
178 it.each([
179 [
180 'an unreadable cell',
181 PAGE.replace('$4.00 ', 'TBD '),
182 /unreadable price TBD/,
183 ],
184 [
185 'a changed header',
186 PAGE.replace('Cached Write', 'Cache Write '),
187 /Pricing header changed/,
188 ],
189 [
190 'a row naming no id',
191 PAGE.replace('| Opus ', '| Opus 9 '),
192 /names no model id/,
193 ],
194 [
195 'a tier with no pair',
196 PAGE.replace(/\| GPT \(> 272K.*\n/, ''),
197 /unreadable context tiers/,
198 ],
199 [
200 'mismatched tier thresholds',
201 PAGE.replace('≤ 272K', '≤ 200K'),
202 /unreadable context tiers/,
203 ],
204 [
205 'a tier that drops a lever',
206 PAGE.replace(
207 '| $10.00 | $45.00 | $1.00 ',
208 '| $10.00 | $45.00 | - ',
209 ),
210 /changes the levers/,
211 ],
212 [
213 'a duplicate price row',
214 PAGE.replace(/^\| Opus {2}.*\$4\.00.*$/m, (row) => `${row}\n${row}`),
215 /duplicate price row/,
216 ],
217 [
218 'no output price',
219 PAGE.replace(
220 '| $4.00 | $20.00 ',
221 '| $4.00 | - ',
222 ),
223 /omits input or output/,
224 ],
225 [
226 'a price table that ends early',
227 PAGE.replace('\n| GPT (≤', '\n\n| GPT (≤'),
228 /has an endpoint but no price row/,
229 ],
230 [
231 'no Pricing section',
232 PAGE.replace('## Pricing', '## Prices'),
233 /has no Pricing/,
234 ],
235 [
236 'an image endpoint with no image price',
237 PAGE.replace(
238 '| Pickle | pickle |',
239 '| Sunburst | sunburst | `https://opencode.ai/inference/openai/v1/images/generations` | `@ai-sdk/openai` |\n| Pickle | pickle |',
240 ),
241 /Sunburst has an endpoint but no price row/,
242 ],
243 [
244 'an unreadable image price',
245 IMAGE_PAGE.replace('$1.00 / image', '$1.00 / picture'),
246 /unreadable image price row GPT Image 2.5 Sunburst/,
247 ],
248 [
249 'a resolution schedule instead of an approximation',
250 IMAGE_PAGE.replace(
251 'About $0.034 per 1K image, $0.050 per 2K, $0.113 per 4K.',
252 '1K is $0.034, 2K is $0.050 and 4K is $0.113.',
253 ),
254 /unreadable image price row Nano Banana 2.1/,
255 ],
256 [
257 'an unruled band removed from the image table',
258 IMAGE_PAGE.replace(/\| Grok Imagine Image 2\.0 \| \$0\.04.*\n/, ''),
259 /Grok Imagine Image 2\.0 has an endpoint but no price row/,
260 ],
261 [
262 'a quality rule instead of an unruled band',
263 IMAGE_PAGE.replace(
264 'quality and resolution can raise the output price to $0.05\u2013$0.08.',
265 'low quality is $0.05 and high quality is $0.08.',
266 ),
267 /unreadable image price row Grok Imagine Image 2.0/,
268 ],
269 [
270 'an image price naming no id',
271 IMAGE_PAGE.replace(
272 '| GPT Image 2.5 Sunburst | gpt-image-2.5-sunburst |',
273 '| GPT Image 9 | gpt-image-2.5-sunburst |',
274 ),
275 /price row GPT Image 2.5 Sunburst names no model id/,
276 ],
277 [
278 'an image model priced in both tables',
279 IMAGE_PAGE.replace(
280 '| Pickle | Free',
281 '| GPT Image 2.5 Sunburst | $1.00 | $1.00 | - | - |\n| Pickle | Free',
282 ),
283 /GPT Image 2.5 Sunburst is priced twice/,
284 ],
285 [
286 'a changed image header',
287 IMAGE_PAGE.replace(
288 '| Price |',
289 '| Cost |',
290 ),
291 /Image pricing header changed/,
292 ],
293 [
294 'an empty table',
295 PAGE.replace(/\| (Pickle|Jev|Mistral|Opus|GPT) .*\n/g, ''),
296 /unreadable/,
297 ],
298 ])('throws on %s', (_, text, message) => {
299 expect(() => parseOpenCodePricing(doc(text))).toThrow(message)
300 })
301})