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

deepgram-pricing.ts

268 lines7.8 KB

1/**
2 * Deepgram Pay As You Go prices from https://deepgram.com/pricing (issue #121).
3 * JSON-LD offers name the product; the page text must show the same number
4 * with `/min` or `/1k`. Growth is a prepaid discount, not the list price.
5 * Add-ons and Voice Agent tiers are not model rates. Nova-2, Enhanced, and
6 * Base are absent from the current offers, so those rows stay null.
7 * Whisper Large is priced for pre-recorded only while the model also streams,
8 * so that id stays null rather than quoting one mode for both.
9 */
10import { compileUnitCard } from '@modelschemas/rate-card'
11import type { RateCard } from '@modelschemas/rate-card'
12​
13import { tagDocsFacts } from './fact-sources.ts'
14import { assertParsed, cachedDocs } from './model-facts.ts'
15import { fetchText, sha256Text } from './types.ts'
16import type { ModelInfo } from './types.ts'
17​
18export const DEEPGRAM_PRICING_URL = 'https://deepgram.com/pricing'
19​
20const PAYG = 'Pay As You Go'
21​
22const REQUIRED = [
23 'Streaming - Flux English',
24 'Streaming - Flux Multilingual',
25 'Streaming - Nova-3 Monolingual',
26 'Streaming - Nova-3 Multilingual',
27 'Pre-Recorded - Nova-3 Monolingual',
28 'Pre-Recorded - Nova-3 Multilingual',
29 'Flux TTS',
30 'Aura-2',
31 'Aura-1',
32] as const
33​
34const OFFER =
35 /Deepgram Voice AI Platform Pricing - (.+?) - (Pay As You Go|Growth)(?:\\)?"\s*,\s*(?:\\)?"price(?:\\)?"\s*:\s*(?:\\)?"(\d+(?:\.\d+)?)/g
36​
37function unitFor(product: string): '/min' | '/1k' | null {
38 if (
39 product.startsWith('Streaming - ') ||
40 product.startsWith('Pre-Recorded - ')
41 ) {
42 return '/min'
43 }
44 if (product === 'Flux TTS' || product === 'Aura-2' || product === 'Aura-1') {
45 return '/1k'
46 }
47 return null
49​
50function pageShows(
51 html: string,
52 amount: number,
53 unit: '/min' | '/1k',
54): boolean {
55 const text = html.replace(/\\u002f/gi, '/')
56 const forms = new Set<string>([
57 String(amount),
58 amount.toFixed(4).replace(/0+$/, '').replace(/\.$/, ''),
59 amount.toFixed(4),
60 amount.toFixed(3),
61 amount.toFixed(2),
62 ])
63 for (const form of forms) {
64 if (form.length > 0 && text.includes(`$${form}${unit}`)) return true
65 }
66 return false
68​
69/** Pay As You Go product → published amount (`/min` or `/1k`). Empty on a bad page. */
70export function parseDeepgramPricing(html: string): Map<string, number> {
71 const out = new Map<string, number>()
72 for (const match of html.matchAll(OFFER)) {
73 const product = match[1]
74 const plan = match[2]
75 const amount = match[3] ? Number(match[3]) : Number.NaN
76 if (!product || plan !== PAYG || !Number.isFinite(amount) || amount <= 0) {
77 continue
78 }
79 const unit = unitFor(product)
80 if (!unit || !pageShows(html, amount, unit)) continue
81 const prior = out.get(product)
82 if (prior !== undefined && prior !== amount) return new Map()
83 out.set(product, amount)
84 }
85 if (REQUIRED.some((product) => !out.has(product))) return new Map()
86 return out
88​
89function perMinute(rates: Map<string, number>, product: string): number | null {
90 const amount = rates.get(product)
91 return amount === undefined ? null : amount / 60
93​
94function perCharacter(
95 rates: Map<string, number>,
96 product: string,
97): number | null {
98 const amount = rates.get(product)
99 return amount === undefined ? null : amount / 1000
101​
102/**
103 * One shared card for a model family. Nova-3 general bills monolingual and
104 * multilingual, and streaming and pre-recorded, at different published rates.
105 * The caller supplies those usage levers; they are not request fields.
106 */
107export function deepgramCard(
108 rawId: string,
109 rates: Map<string, number>,
110 source: RateCard['source'],
111): RateCard | null {
112 const listen = (
113 streaming: number | null,
114 preRecorded: number | null,
115 ): RateCard | null => {
116 if (streaming === null || preRecorded === null) return null
117 return compileUnitCard(
118 {
119 quantity: { param: 'audio_seconds', bound: 'usage' },
120 keys: [
121 {
122 param: 'listen_mode',
123 values: ['streaming', 'pre_recorded'],
124 bound: 'usage',
125 },
126 ],
127 rates: { streaming, pre_recorded: preRecorded },
128 },
129 source,
130 )
131 }
132​
133 if (rawId === 'flux-general-en') {
134 const streaming = perMinute(rates, 'Streaming - Flux English')
135 if (streaming === null) return null
136 return compileUnitCard(
137 {
138 quantity: { param: 'audio_seconds', bound: 'usage' },
139 rates: streaming,
140 },
141 source,
142 )
143 }
144 if (rawId === 'flux-general-multi') {
145 const streaming = perMinute(rates, 'Streaming - Flux Multilingual')
146 if (streaming === null) return null
147 return compileUnitCard(
148 {
149 quantity: { param: 'audio_seconds', bound: 'usage' },
150 rates: streaming,
151 },
152 source,
153 )
154 }
155 if (rawId.startsWith('flux-')) {
156 const characters = perCharacter(rates, 'Flux TTS')
157 if (characters === null) return null
158 return compileUnitCard(
159 {
160 quantity: { param: 'characters', bound: 'usage' },
161 rates: characters,
162 },
163 source,
164 )
165 }
166 if (rawId.startsWith('aura-2-')) {
167 const characters = perCharacter(rates, 'Aura-2')
168 if (characters === null) return null
169 return compileUnitCard(
170 {
171 quantity: { param: 'characters', bound: 'usage' },
172 rates: characters,
173 },
174 source,
175 )
176 }
177 if (rawId.startsWith('aura-')) {
178 const characters = perCharacter(rates, 'Aura-1')
179 if (characters === null) return null
180 return compileUnitCard(
181 {
182 quantity: { param: 'characters', bound: 'usage' },
183 rates: characters,
184 },
185 source,
186 )
187 }
188 if (rawId === 'nova-3' || rawId === 'nova-3-general') {
189 const streamingMono = perMinute(rates, 'Streaming - Nova-3 Monolingual')
190 const streamingMulti = perMinute(rates, 'Streaming - Nova-3 Multilingual')
191 const batchMono = perMinute(rates, 'Pre-Recorded - Nova-3 Monolingual')
192 const batchMulti = perMinute(rates, 'Pre-Recorded - Nova-3 Multilingual')
193 if (
194 streamingMono === null ||
195 streamingMulti === null ||
196 batchMono === null ||
197 batchMulti === null
198 ) {
199 return null
200 }
201 return compileUnitCard(
202 {
203 quantity: { param: 'audio_seconds', bound: 'usage' },
204 keys: [
205 {
206 param: 'listen_mode',
207 values: ['streaming', 'pre_recorded'],
208 bound: 'usage',
209 },
210 {
211 param: 'language_scope',
212 values: ['monolingual', 'multilingual'],
213 bound: 'usage',
214 },
215 ],
216 rates: {
217 streaming: {
218 monolingual: streamingMono,
219 multilingual: streamingMulti,
220 },
221 pre_recorded: {
222 monolingual: batchMono,
223 multilingual: batchMulti,
224 },
225 },
226 },
227 source,
228 )
229 }
230 if (rawId.startsWith('nova-3-')) {
231 return listen(
232 perMinute(rates, 'Streaming - Nova-3 Monolingual'),
233 perMinute(rates, 'Pre-Recorded - Nova-3 Monolingual'),
234 )
235 }
236 return null
238​
239type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
240​
241export async function deepgramModelPricing(
242 kv?: KVNamespace,
243): Promise<(rawId: string) => PricedFacts> {
244 const doc = await cachedDocs(kv, DEEPGRAM_PRICING_URL, async () => {
245 const html = await fetchText(DEEPGRAM_PRICING_URL)
246 const parsed = parseDeepgramPricing(html)
247 assertParsed(parsed, 'deepgram pricing page')
248 return {
249 rates: Object.fromEntries(parsed),
250 hash: await sha256Text(html),
251 extractedAt: new Date().toISOString(),
252 }
253 })
254 const rates = new Map(Object.entries(doc.rates))
255 const source = {
256 url: DEEPGRAM_PRICING_URL,
257 hash: doc.hash,
258 extractedAt: doc.extractedAt,
259 }
260 return (rawId) => {
261 const pricing = deepgramCard(rawId, rates, source)
262 if (!pricing) return {}
263 return {
264 pricing,
265 factSources: tagDocsFacts({ pricing }, DEEPGRAM_PRICING_URL, doc.hash),
266 }
267 }