Open History to see the last commit
1/**
2 * Groq prices from GroqCloud's supported-models page (issue #73). The
3 * markdown mirror is one table per lifecycle (production, preview) whose
4 * price cell is `$0.15 input$0.60 output`, `$0.111 per hour`, or
5 * `$40.00 per 1M characters`. "Contact Sales" publishes no number, so that
6 * row gets no card. The models table has no cached-input column, and the
7 * prompt-caching doc names no dollar rate, so `cache_read_tokens` stays
8 * unset. A price cell in any other shape refuses that model rather than
9 * quoting part of it. A page that prices nothing throws.
10 */
11import { compileTokenCard, compileUnitCard } from '@modelschemas/rate-card'
12import type { UnitCardSpec } from '@modelschemas/rate-card'
13
14import { tagDocsFacts } from './fact-sources.ts'
15import { assertParsed, cachedDocs, markdownTableRows } from './model-facts.ts'
16import { fetchText, sha256Text } from './types.ts'
17import type { ModelInfo } from './types.ts'
18
19export const GROQ_PRICING_URL = 'https://console.groq.com/docs/models.md'
20
21export type GroqPrice =
22 | { kind: 'tokens'; rates: Record<string, number> }
23 | { kind: 'unit'; unit: UnitCardSpec }
24
25/** API id glued on after the docs link. `Enterprise` is a badge, not the id. */
26export function groqModelId(cell: string): string | null {
27 // The cell leads with an image link; the model id follows the docs link.
28 const matches = [
29 ...cell.matchAll(/\]\([^)]+\)([A-Za-z0-9][A-Za-z0-9./_-]*)/g),
30 ]
31 const id = matches.at(-1)?.[1]?.replace(/^Enterprise(?=[a-z0-9])/, '')
32 return id && id.length > 0 ? id : null
33}
34
35/**
36 * One price cell. `null` skips a row with no public price. A cell that
37 * quotes `$` in a shape this does not know refuses the model (`'refuse'`).
38 */
39export function parseGroqPrice(cell: string): GroqPrice | null | 'refuse' {
40 const text = cell.replace(/\\/g, '').replace(/\s+/g, '')
41 if (text === '' || text === '-' || /^contactsales$/i.test(text)) {
42 return null
43 }
44 const tokens = text.match(/^\$(\d+(?:\.\d+)?)input\$(\d+(?:\.\d+)?)output$/)
45 if (tokens?.[1] && tokens[2]) {
46 return {
47 kind: 'tokens',
48 rates: {
49 input_tokens: Number(tokens[1]) / 1e6,
50 output_tokens: Number(tokens[2]) / 1e6,
51 },
52 }
53 }
54 const hour = text.match(/^\$(\d+(?:\.\d+)?)perhour$/)
55 if (hour?.[1]) {
56 return {
57 kind: 'unit',
58 unit: {
59 quantity: { param: 'audio_seconds', bound: 'usage' },
60 rates: Number(hour[1]) / 3600,
61 },
62 }
63 }
64 const characters = text.match(/^\$(\d+(?:\.\d+)?)per1Mcharacters$/)
65 if (characters?.[1]) {
66 return {
67 kind: 'unit',
68 unit: {
69 quantity: { param: 'characters', bound: 'usage' },
70 rates: Number(characters[1]) / 1e6,
71 },
72 }
73 }
74 return text.includes('$') ? 'refuse' : null
75}
76
77/** Model id → the price the models page states. Refused rows are absent. */
78export function parseGroqPricing(markdown: string): Map<string, GroqPrice> {
79 const out = new Map<string, GroqPrice>()
80 for (const cells of markdownTableRows(markdown)) {
81 const id = groqModelId(cells[0] ?? '')
82 const price = parseGroqPrice(cells[2] ?? '')
83 if (!id || price === null || price === 'refuse' || out.has(id)) continue
84 out.set(id, price)
85 }
86 return out
87}
88
89type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
90
91/** Card lookup by listed model id. */
92export async function groqModelPricing(
93 kv?: KVNamespace,
94): Promise<(rawId: string) => PricedFacts> {
95 const doc = await cachedDocs(kv, GROQ_PRICING_URL, async () => {
96 const markdown = await fetchText(GROQ_PRICING_URL)
97 const parsed = parseGroqPricing(markdown)
98 assertParsed(parsed, 'groq models page')
99 return {
100 rates: Object.fromEntries(parsed),
101 hash: await sha256Text(markdown),
102 extractedAt: new Date().toISOString(),
103 }
104 })
105 return (rawId) => {
106 const row = doc.rates[rawId]
107 if (!row) return {}
108 const source = {
109 url: GROQ_PRICING_URL,
110 hash: doc.hash,
111 extractedAt: doc.extractedAt,
112 }
113 const pricing =
114 row.kind === 'unit'
115 ? compileUnitCard(row.unit, source)
116 : compileTokenCard(row.rates, [], source)
117 if (!pricing) return {}
118 return {
119 pricing,
120 factSources: tagDocsFacts({ pricing }, GROQ_PRICING_URL, doc.hash),
121 }
122 }
123}