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

moonshot-pricing.ts

96 lines2.9 KB

1/**
2 * Kimi prices from the international pricing page (issue #109). Amounts
3 * are USD per 1M tokens. K2 rows are cache-hit / cache-miss / output.
4 * K3 rows add cache-write TTLs. A zero-row parse throws.
5 */
6import { compileTokenCard } from '@modelschemas/rate-card'
7​
8import { assertParsed, cachedDocs } from './model-facts.ts'
9import { fetchText, sha256Text } from './types.ts'
10import type { ModelInfo } from './types.ts'
11​
12export const MOONSHOT_PRICING_URL =
13 'https://platform.kimi.ai/docs/pricing/chat.md'
14​
15export interface MoonshotRates {
16 input: number
17 output: number
18 cacheRead: number
19 cacheWrite?: number
20 cacheWrite1h?: number
22​
23function dollars(row: string): Array<number> {
24 return [...row.matchAll(/\{"\$"\}([\d.]+)/g)].map((match) => Number(match[1]))
26​
27/** Model id → USD per million tokens. */
28export function parseMoonshotPricing(
29 markdown: string,
30): Map<string, MoonshotRates> {
31 const out = new Map<string, MoonshotRates>()
32 const rows = markdown.matchAll(/\["(kimi-[^"]+)",\s*"1M tokens",[\s\S]*?\]/g)
33 for (const match of rows) {
34 const id = match[1]
35 const amounts = dollars(match[0])
36 if (!id || out.has(id)) continue
37 if (amounts.length === 3) {
38 const [cacheRead, input, output] = amounts
39 if (
40 cacheRead === undefined ||
41 input === undefined ||
42 output === undefined
43 ) {
44 continue
45 }
46 out.set(id, { cacheRead, input, output })
47 } else if (amounts.length === 5) {
48 const [cacheWrite, cacheWrite1h, cacheRead, input, output] = amounts
49 if (
50 cacheWrite === undefined ||
51 cacheWrite1h === undefined ||
52 cacheRead === undefined ||
53 input === undefined ||
54 output === undefined
55 ) {
56 continue
57 }
58 out.set(id, { cacheWrite, cacheWrite1h, cacheRead, input, output })
59 }
60 }
61 return out
63​
64type PricedFacts = Pick<ModelInfo, 'pricing'>
65​
66export async function moonshotModelPricing(
67 kv?: KVNamespace,
68): Promise<(rawId: string) => PricedFacts> {
69 const doc = await cachedDocs(kv, MOONSHOT_PRICING_URL, async () => {
70 const markdown = await fetchText(MOONSHOT_PRICING_URL)
71 const parsed = parseMoonshotPricing(markdown)
72 assertParsed(parsed, 'moonshot pricing page')
73 return {
74 rates: Object.fromEntries(parsed),
75 hash: await sha256Text(markdown),
76 extractedAt: new Date().toISOString(),
77 }
78 })
79 return (rawId) => {
80 const row = doc.rates[rawId]
81 if (!row) return {}
82 const rates: Record<string, number> = {
83 input_tokens: row.input / 1e6,
84 output_tokens: row.output / 1e6,
85 cache_read_tokens: row.cacheRead / 1e6,
86 }
87 if (row.cacheWrite) rates.cache_write_tokens = row.cacheWrite / 1e6
88 if (row.cacheWrite1h) rates.cache_write_1h_tokens = row.cacheWrite1h / 1e6
89 const pricing = compileTokenCard(rates, [], {
90 url: MOONSHOT_PRICING_URL,
91 hash: doc.hash,
92 extractedAt: doc.extractedAt,
93 })
94 return pricing ? { pricing } : {}
95 }