Open History to see the last commit
1import { evaluateCard } from '@modelschemas/rate-card/evaluator'
2import { describe, expect, it } from 'vitest'
3
4import {
5 compileReplicateBilling,
6 replicateBillingFromHtml,
7 replicatePagePricing,
8} from './replicate-pricing.ts'
9
10const SOURCE = {
11 url: 'https://replicate.com/black-forest-labs/flux-schnell',
12 hash: 'abc',
13 extractedAt: '2026-10-03T00:00:00.000Z',
14}
15
16describe('replicate prediction prices', () => {
17 it('compiles a per-thousand output image price', () => {
18 const card = compileReplicateBilling(
19 {
20 current_tiers: [
21 {
22 criteria: [],
23 prices: [
24 {
25 metric: 'image_output_count',
26 price: '$3',
27 title: 'per thousand output images',
28 type: 'per-unit',
29 },
30 ],
31 },
32 ],
33 },
34 SOURCE,
35 )
36 expect(card?.price).toEqual({
37 '*': [{ var: 'output_images' }, 3 / 1000],
38 })
39 expect(card?.inputs.output_images).toMatchObject({ bound: 'usage' })
40 })
41
42 it('compiles input and output token prices', () => {
43 const card = compileReplicateBilling(
44 {
45 current_tiers: [
46 {
47 criteria: [],
48 prices: [
49 {
50 metric: 'token_output_count',
51 price: '$0.01',
52 title: 'per thousand output tokens',
53 type: 'per-unit',
54 },
55 {
56 metric: 'token_input_count',
57 price: '$3.75',
58 title: 'per million input tokens',
59 type: 'per-unit',
60 },
61 ],
62 },
63 ],
64 },
65 SOURCE,
66 )
67 expect(card?.tables.rate).toEqual({
68 base: {
69 output_tokens: 0.01 / 1000,
70 input_tokens: 3.75 / 1_000_000,
71 },
72 })
73 })
74
75 it('compiles a resolution tier as a usage-bound lookup', () => {
76 const card = compileReplicateBilling(
77 {
78 current_tiers: [
79 {
80 criteria: [
81 { title: 'target resolution', type: 'equals', value: '480p' },
82 ],
83 prices: [
84 {
85 metric: 'video_output_duration_seconds',
86 price: '$0.03',
87 title: 'per second of output video',
88 type: 'per-unit',
89 },
90 ],
91 },
92 {
93 criteria: [
94 { title: 'target resolution', type: 'equals', value: '720p' },
95 ],
96 prices: [
97 {
98 metric: 'video_output_duration_seconds',
99 price: '$0.06',
100 title: 'per second of output video',
101 type: 'per-unit',
102 },
103 ],
104 },
105 ],
106 },
107 SOURCE,
108 )
109 expect(card?.tables.rate).toEqual({ '480p': 0.03, '720p': 0.06 })
110 expect(card?.inputs.target_resolution).toMatchObject({
111 bound: 'usage',
112 values: ['480p', '720p'],
113 })
114 })
115
116 it('sums each metric a two-criterion tier quotes', () => {
117 // xai/grok-imagine-image-2's billingConfig, 2026-10-31.
118 const tier = (variant: string, resolution: string, output: string) => ({
119 criteria: [
120 { title: 'model variant', type: 'equals', value: variant },
121 { title: 'target resolution', type: 'equals', value: resolution },
122 ],
123 prices: [
124 {
125 metric: 'image_output_count',
126 price: output,
127 title: 'per output image',
128 type: 'per-unit',
129 },
130 {
131 metric: 'image_input_megapixel_count',
132 price: '$0.01',
133 title: 'per input image megapixel',
134 type: 'per-unit',
135 },
136 ],
137 })
138 const card = compileReplicateBilling(
139 {
140 current_tiers: [
141 tier('low', '1k', '$0.04'),
142 tier('low', '2k', '$0.06'),
143 tier('medium', '1k', '$0.06'),
144 tier('medium', '2k', '$0.08'),
145 ],
146 },
147 SOURCE,
148 )
149 expect(card?.tables.output_images).toEqual({
150 low: { '1k': 0.04, '2k': 0.06 },
151 medium: { '1k': 0.06, '2k': 0.08 },
152 })
153 if (!card) throw new Error('no card')
154 const priced = evaluateCard(card, {
155 usage: {
156 model_variant: 'medium',
157 target_resolution: '2k',
158 output_images: 2,
159 input_megapixels: 3,
160 },
161 })
162 expect(priced.amount).toBeCloseTo(2 * 0.08 + 3 * 0.01, 10)
163 // Unkeyed tiers name nothing to choose between them.
164 expect(
165 compileReplicateBilling(
166 {
167 current_tiers: [
168 { criteria: [], prices: tier('low', '1k', '$0.04').prices },
169 { criteria: [], prices: tier('low', '1k', '$0.09').prices },
170 ],
171 },
172 SOURCE,
173 ),
174 ).toBeNull()
175 // A tier missing from the grid is not a card.
176 expect(
177 compileReplicateBilling(
178 {
179 current_tiers: [
180 tier('low', '1k', '$0.04'),
181 tier('low', '2k', '$0.06'),
182 tier('medium', '1k', '$0.06'),
183 ],
184 },
185 SOURCE,
186 ),
187 ).toBeNull()
188 })
189
190 it('leaves an empty billing config and an unknown unit null', () => {
191 expect(compileReplicateBilling({ current_tiers: [] }, SOURCE)).toBeNull()
192 expect(
193 compileReplicateBilling(
194 {
195 current_tiers: [
196 {
197 criteria: [],
198 prices: [
199 {
200 metric: 'image_output_count',
201 price: '$1',
202 title: 'per request',
203 type: 'per-unit',
204 },
205 ],
206 },
207 ],
208 },
209 SOURCE,
210 ),
211 ).toBeNull()
212 })
213
214 it('leaves out the tier for a request that names no value (#600)', () => {
215 // https://replicate.com/xai/grok-imagine-video billingConfig.
216 const tier = (value: string | null, price: string) => ({
217 criteria: [{ title: 'target resolution', type: 'equals', value }],
218 prices: [
219 {
220 metric: 'video_output_duration_seconds',
221 price,
222 title: 'per second of output video',
223 type: 'per-unit',
224 },
225 ],
226 })
227 const card = compileReplicateBilling(
228 {
229 current_tiers: [
230 tier('720p', '$0.07'),
231 tier('480p', '$0.05'),
232 tier(null, '$0.05'),
233 ],
234 },
235 SOURCE,
236 )
237 expect(card?.tables.rate).toEqual({ '720p': 0.07, '480p': 0.05 })
238 if (!card) throw new Error('no card')
239 const priced = (params: Record<string, unknown>) =>
240 evaluateCard(card, { usage: params }).amount
241 expect(
242 priced({ target_resolution: '720p', video_seconds: 10 }),
243 ).toBeCloseTo(0.7, 10)
244 expect(() => priced({ video_seconds: 10 })).toThrow(/required/)
245 })
246
247 it('leaves out variants billed by tokens or an unnamed metric (#600)', () => {
248 // https://replicate.com/openai/gpt-image-2.6-flare billingConfig.
249 const variant = (value: string, prices: Array<object>) => ({
250 criteria: [{ title: 'model variant', type: 'equals', value }],
251 prices,
252 })
253 const tokens = [
254 {
255 metric: 'token_output_count',
256 price: '$0.000035',
257 title: 'Image output tokens',
258 type: 'per-unit',
259 },
260 {
261 metric: 'token_input_count',
262 price: '$0.00001',
263 title: 'Input tokens',
264 type: 'per-unit',
265 },
266 ]
267 const image = (price: string) => [
268 {
269 metric: 'image_output_count',
270 price,
271 title: 'per output image',
272 type: 'per-unit',
273 },
274 ]
275 const card = compileReplicateBilling(
276 {
277 current_tiers: [
278 variant('svg-gpt-5.5', [
279 ...tokens,
280 {
281 metric: 'unspecified_billing_metric',
282 price: '$0.000036',
283 title: 'gpt-5.5 output tokens',
284 type: 'per-unit',
285 },
286 ]),
287 variant('gif', tokens),
288 variant('low', image('$0.05')),
289 variant('max', image('$0.50')),
290 ],
291 },
292 SOURCE,
293 )
294 expect(card?.inputs.model_variant).toMatchObject({
295 values: ['low', 'max'],
296 })
297 expect(card?.tables.rate).toEqual({ low: 0.05, max: 0.5 })
298 if (!card) throw new Error('no card')
299 expect(() =>
300 evaluateCard(card, {
301 usage: { model_variant: 'gif', output_images: 1 },
302 }),
303 ).toThrow(/not one of/)
304 })
305
306 it('still refuses a keyed tier billed by a metric it has never seen', () => {
307 const variant = (value: string, metric: string) => ({
308 criteria: [{ title: 'model variant', type: 'equals', value }],
309 prices: [{ metric, price: '$0.05', title: 'x', type: 'per-unit' }],
310 })
311 expect(
312 compileReplicateBilling(
313 {
314 current_tiers: [
315 variant('low', 'image_output_count'),
316 variant('new', 'brand_new_metric'),
317 ],
318 },
319 SOURCE,
320 ),
321 ).toBeNull()
322 })
323
324 it('reads billingConfig from the model page and ignores a page with none', () => {
325 const html = `<section id="pricing"><script type="application/json">{"billingConfig":{"current_tiers":[{"criteria":[],"prices":[{"metric":"image_output_count","price":"$0.04","title":"per output image","type":"per-unit"}]}]}}</script>`
326 expect(replicateBillingFromHtml(html)?.current_tiers).toHaveLength(1)
327 const priced = replicatePagePricing(html, SOURCE)
328 expect(priced.pricing).toMatchObject({
329 price: { '*': [{ var: 'output_images' }, 0.04] },
330 })
331 expect(replicatePagePricing('<p>no price</p>', SOURCE)).toEqual({})
332 })
333})
334
335describe('replicate prompt-length token tiers', () => {
336 // https://replicate.com/google/gemini-3-pro billingConfig.
337 const tier = (
338 range: [number | null, number | null],
339 subtype: string,
340 input: string,
341 output: string,
342 ) => ({
343 criteria: [{ title: 'input token', type: 'range', subtype, value: range }],
344 prices: [
345 {
346 metric: 'token_input_count',
347 price: input,
348 title: 'per million input tokens',
349 type: 'per-unit',
350 },
351 {
352 metric: 'token_output_count',
353 price: output,
354 title: 'per thousand output tokens',
355 type: 'per-unit',
356 },
357 ],
358 })
359 const low = tier([null, 200000], 'open-closed', '$2', '$0.012')
360 const high = tier([200000, null], 'open', '$4', '$0.018')
361
362 it('compiles the base tier and the re-quote above the threshold', () => {
363 const card = compileReplicateBilling({ current_tiers: [low, high] }, SOURCE)
364 expect(card?.tables.rate).toEqual({
365 base: { input_tokens: 2e-6, output_tokens: 1.2e-5 },
366 '200000': { input_tokens: 4e-6, output_tokens: 1.8e-5 },
367 })
368 })
369
370 it('refuses any other range shape', () => {
371 const compile = (...tiers: Array<ReturnType<typeof tier>>) =>
372 compileReplicateBilling({ current_tiers: tiers }, SOURCE)
373 // One tier alone, a gap between the tiers, a closed upper tier.
374 expect(compile(low)).toBeNull()
375 expect(compile(high)).toBeNull()
376 expect(compile(low, tier([256000, null], 'open', '$4', '$0.018'))).toBe(
377 null,
378 )
379 expect(
380 compile(low, tier([200000, 400000], 'open-closed', '$4', '$0.018')),
381 ).toBeNull()
382 expect(compile(low, { ...high, criteria: [] })).toBeNull()
383 // A range over something other than prompt tokens.
384 const seconds = structuredClone(high)
385 if (seconds.criteria[0]) seconds.criteria[0].title = 'output second'
386 expect(compile(low, seconds)).toBeNull()
387 // A tier that prices only input tokens.
388 expect(compile(low, { ...high, prices: high.prices.slice(0, 1) })).toBe(
389 null,
390 )
391 })
392})