Open History to see the last commit
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, by position. A row is priced only when
5 * every cell between its unit and its context window is a bare `$0.16`,
6 * so a struck or footnoted price leaves the row unpriced rather than
7 * shifting the others into its lever. `moonshotModelPricing` throws on a
8 * zero-row parse. The tables are read from the HTML page: its `.md`
9 * export renders them empty.
10 */
11import { compileTokenCard } from '@modelschemas/rate-card'
12
13import { parseKimiDocTables } from './kimi-doc-tables.ts'
14import type { KimiDocTable } from './kimi-doc-tables.ts'
15import { assertParsed, cachedDocs } from './model-facts.ts'
16import { fetchText, sha256Text } from './types.ts'
17import type { ModelInfo } from './types.ts'
18
19export const MOONSHOT_PRICING_URL = 'https://platform.kimi.ai/docs/pricing/chat'
20
21export interface MoonshotRates {
22 input: number
23 output: number
24 cacheRead: number
25 cacheWrite?: number
26 cacheWrite1h?: number
27}
28
29/** The amounts, or null when any cell is not a bare dollar amount. */
30function dollars(cells: Array<string | null>): Array<number> | null {
31 const amounts = cells.map((cell) => cell?.match(/^\$(\d+(?:\.\d+)?)$/)?.[1])
32 return amounts.every((amount) => amount !== undefined)
33 ? amounts.map(Number)
34 : null
35}
36
37/** Model id → USD per million tokens. */
38export function parseMoonshotPricing(
39 tables: Array<KimiDocTable>,
40): Map<string, MoonshotRates> {
41 const out = new Map<string, MoonshotRates>()
42 for (const [id, unit, ...cells] of tables.flatMap((table) => table.rows)) {
43 const context = /^[\d,]+ tokens$/.test(cells.at(-1) ?? '')
44 const amounts = dollars(context ? cells.slice(0, -1) : cells)
45 if (
46 !id?.startsWith('kimi-') ||
47 unit !== '1M tokens' ||
48 out.has(id) ||
49 amounts === null
50 ) {
51 continue
52 }
53 if (amounts.length === 3) {
54 const [cacheRead, input, output] = amounts
55 if (
56 cacheRead === undefined ||
57 input === undefined ||
58 output === undefined
59 ) {
60 continue
61 }
62 out.set(id, { cacheRead, input, output })
63 } else if (amounts.length === 5) {
64 const [cacheWrite, cacheWrite1h, cacheRead, input, output] = amounts
65 if (
66 cacheWrite === undefined ||
67 cacheWrite1h === undefined ||
68 cacheRead === undefined ||
69 input === undefined ||
70 output === undefined
71 ) {
72 continue
73 }
74 out.set(id, { cacheWrite, cacheWrite1h, cacheRead, input, output })
75 }
76 }
77 return out
78}
79
80type PricedFacts = Pick<ModelInfo, 'pricing'>
81
82export async function moonshotModelPricing(
83 kv?: KVNamespace,
84): Promise<(rawId: string) => PricedFacts> {
85 const doc = await cachedDocs(kv, MOONSHOT_PRICING_URL, async () => {
86 const tables = parseKimiDocTables(await fetchText(MOONSHOT_PRICING_URL))
87 const parsed = parseMoonshotPricing(tables)
88 assertParsed(parsed, 'moonshot pricing page')
89 return {
90 rates: Object.fromEntries(parsed),
91 // The tables, not the page: its HTML changes with every docs deploy.
92 hash: await sha256Text(JSON.stringify(tables)),
93 extractedAt: new Date().toISOString(),
94 }
95 })
96 return (rawId) => {
97 const row = doc.rates[rawId]
98 if (!row) return {}
99 const rates: Record<string, number> = {
100 input_tokens: row.input / 1e6,
101 output_tokens: row.output / 1e6,
102 cache_read_tokens: row.cacheRead / 1e6,
103 }
104 if (row.cacheWrite) rates.cache_write_tokens = row.cacheWrite / 1e6
105 if (row.cacheWrite1h) rates.cache_write_1h_tokens = row.cacheWrite1h / 1e6
106 const pricing = compileTokenCard(rates, [], {
107 url: MOONSHOT_PRICING_URL,
108 hash: doc.hash,
109 extractedAt: doc.extractedAt,
110 })
111 return pricing ? { pricing } : {}
112 }
113}