Open History to see the last commit
1/**
2 * DeepSeek prices from the models page (issue #109). Each model publishes
3 * off-peak and peak rates. There is no request lever for the clock, so the
4 * card is the off-peak rate (the rate outside the named peak windows).
5 * Peak is not stored. A page that does not match this table throws.
6 */
7import { compileTokenCard } from '@modelschemas/rate-card'
8
9import { assertParsed, cachedDocs } from './model-facts.ts'
10import { fetchText, sha256Text } from './types.ts'
11import type { ModelInfo } from './types.ts'
12
13export const DEEPSEEK_PRICING_URL =
14 'https://api-docs.deepseek.com/quick_start/pricing'
15
16export interface DeepseekRates {
17 input: number
18 cacheRead: number
19 output: number
20}
21
22function money(line: string): number | null {
23 const match = line.match(/^\$(\d+(?:\.\d+)?)$/)
24 return match?.[1] ? Number(match[1]) : null
25}
26
27/** Off-peak USD per million tokens, keyed by API id. */
28export function parseDeepseekPricing(html: string): Map<string, DeepseekRates> {
29 const text = html
30 .replace(/<script[\s\S]*?<\/script>/gi, ' ')
31 .replace(/<[^>]+>/g, '\n')
32 .replace(/&/g, '&')
33 const lines = text
34 .split('\n')
35 .map((line) => line.trim())
36 .filter((line) => line.length > 0)
37 const modelAt = lines.indexOf('MODEL')
38 const pricingAt = lines.indexOf('PRICING')
39 if (modelAt < 0 || pricingAt < modelAt) return new Map()
40 const ids: Array<string> = []
41 for (const line of lines.slice(modelAt + 1, pricingAt)) {
42 if (/^deepseek-[a-z0-9.-]+$/.test(line)) ids.push(line)
43 else if (line.startsWith('BASE URL')) break
44 }
45 const prices: Array<number> = []
46 for (const line of lines.slice(pricingAt + 1)) {
47 const value = money(line)
48 if (value !== null) prices.push(value)
49 else if (prices.length > 0 && line.startsWith('Concurrency')) break
50 }
51 if (ids.length === 0 || prices.length !== ids.length * 6) return new Map()
52 const out = new Map<string, DeepseekRates>()
53 ids.forEach((id, index) => {
54 const at = (row: number) => prices[row * ids.length + index]
55 const cacheRead = at(0)
56 const input = at(2)
57 const output = at(4)
58 if (
59 cacheRead === undefined ||
60 input === undefined ||
61 output === undefined
62 ) {
63 return
64 }
65 out.set(id, { input, cacheRead, output })
66 })
67 return out
68}
69
70type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
71
72export async function deepseekModelPricing(
73 kv?: KVNamespace,
74): Promise<(rawId: string) => PricedFacts> {
75 const doc = await cachedDocs(kv, DEEPSEEK_PRICING_URL, async () => {
76 const html = await fetchText(DEEPSEEK_PRICING_URL)
77 const parsed = parseDeepseekPricing(html)
78 assertParsed(parsed, 'deepseek pricing page')
79 return {
80 rates: Object.fromEntries(parsed),
81 hash: await sha256Text(html),
82 extractedAt: new Date().toISOString(),
83 }
84 })
85 return (rawId) => {
86 const row = doc.rates[rawId]
87 if (!row) return {}
88 const pricing = compileTokenCard(
89 {
90 input_tokens: row.input / 1e6,
91 output_tokens: row.output / 1e6,
92 cache_read_tokens: row.cacheRead / 1e6,
93 },
94 [],
95 {
96 url: DEEPSEEK_PRICING_URL,
97 hash: doc.hash,
98 extractedAt: doc.extractedAt,
99 },
100 )
101 if (!pricing) return {}
102 return { pricing }
103 }
104}