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

fal-token-rate.test.ts

100 lines4.2 KB

1import { describe, expect, it } from 'vitest'
2​
3import { price, verifyExamples } from '@modelschemas/rate-card'
4import type { RateCard } from '@modelschemas/rate-card'
5​
6import { priceFalNamedSection } from './fal-unit-rate.ts'
7import { compileFalTokenCard } from './fal-token-rate.ts'
8​
9const SOURCE: RateCard['source'] = {
10 url: 'https://fal.ai/models/fal-ai/bytedance/seed/v2/mini/llms.txt',
11 hash: 'a'.repeat(64),
12 extractedAt: '2026-10-07T00:00:00.000Z',
14​
15/** Seed 2.0 Mini, https://fal.ai/models/fal-ai/bytedance/seed/v2/mini/llms.txt checked 2026-10-07. */
16const SEED = `Your request will cost **$0.0001** per 1000 units. For inputs under 128k tokens, the units per input token is 1. For inputs of over 128k tokens, 2 units will be charged per token. Similarly, each output token costs 4 units, provided the total output length (reasoning + output) is under 128k tokens, and 8 units per token otherwise.
17​
18For more details, see [fal.ai pricing](https://fal.ai/pricing).`
19​
20/** Nemotron 3 Nano Omni, https://fal.ai/models/nvidia/nemotron-3-nano-omni/llms.txt checked 2026-10-07. */
21const NEMOTRON = `- **Price**: $0.01 per 1000 tokens
22​
23For more details, see [fal.ai pricing](https://fal.ai/pricing).`
24​
25function base(card: RateCard | null): Record<string, number> | null {
26 const rate = card?.tables.rate
27 if (rate === undefined || typeof rate === 'number') return null
28 const row = rate.base
29 if (row === undefined || typeof row === 'number') return null
30 return row as Record<string, number>
32​
33describe('compileFalTokenCard', () => {
34 it('compiles the unit schedule as input and output token rates', () => {
35 const card = compileFalTokenCard(`## Pricing\n\n${SEED}`, SOURCE)
36 const perUnit = 0.0001 / 1000
37 expect(base(card)).toEqual({
38 input_tokens: perUnit,
39 output_tokens: perUnit * 4,
40 })
41 expect(card && verifyExamples(card).every((result) => result.ok)).toBe(true)
42 // Exactly 128k is not "over", so it stays on the under rate.
43 expect(
44 card && price(card, {}, { input_tokens: 128_000, output_tokens: 0 }),
45 ).toBeCloseTo(128_000 * perUnit, 10)
46 expect(
47 card && price(card, {}, { input_tokens: 128_001, output_tokens: 0 }),
48 ).toBeCloseTo(128_001 * perUnit * 2, 10)
49 // Output crosses on its own length, not because the prompt did.
50 expect(
51 card && price(card, {}, { input_tokens: 1, output_tokens: 128_001 }),
52 ).toBeCloseTo(perUnit + 128_001 * perUnit * 8, 10)
53 })
54​
55 it('bills one undifferentiated per-token price on input and output', () => {
56 const card = compileFalTokenCard(NEMOTRON, {
57 ...SOURCE,
58 url: 'https://fal.ai/models/nvidia/nemotron-3-nano-omni/llms.txt',
59 })
60 expect(base(card)).toEqual({
61 input_tokens: 0.01 / 1000,
62 output_tokens: 0.01 / 1000,
63 })
64 expect(
65 card && price(card, {}, { input_tokens: 412, output_tokens: 87 }),
66 ).toBeCloseTo(((412 + 87) / 1000) * 0.01, 10)
67 })
68​
69 it('returns null unless the section is one of those two shapes', () => {
70 const none = [
71 '',
72 'You will be charged based on the number of input and output tokens.\n\nFor more details, see [fal.ai pricing](https://fal.ai/pricing).',
73 '- **Price**: $0.001 per 1\n\nFor more details, see [fal.ai pricing](https://fal.ai/pricing).',
74 '- **Price**: $0.04 per megapixels',
75 '- **Price**: $0.01 per 1000 output tokens',
76 'Your request will cost **$0.0001** per 1000 units. Input is 1 unit and output is 4.',
77 `${SEED}\nPeak price is **$0.0002** per 1000 units.`,
78 '- **Price**: $0.01 per 1000 tokens. An additional $0.02 is charged for search.',
79 ]
80 for (const section of none) {
81 expect(compileFalTokenCard(section, SOURCE), section).toBeNull()
82 }
83 })
84})
85​
86describe('priceFalNamedSection token fallthrough', () => {
87 it('keeps a single image rate and compiles a token section the unit parser drops', () => {
88 const image = priceFalNamedSection(
89 'Your request will cost **$0.08** per image.',
90 new Set(),
91 SOURCE,
92 )
93 expect(image?.inputs.images).toMatchObject({ bound: 'usage' })
94 expect(base(image)).toBeNull()
95​
96 const seed = priceFalNamedSection(SEED, new Set(), SOURCE)
97 expect(base(seed)?.input_tokens).toBe(0.0001 / 1000)
98 expect(base(seed)?.output_tokens).toBe((0.0001 / 1000) * 4)
99 })
100})