-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpricing.ts
More file actions
64 lines (57 loc) · 1.79 KB
/
Copy pathpricing.ts
File metadata and controls
64 lines (57 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { TokenUsage } from './usage.types';
export interface ModelPricing {
readonly inputPerMTok: number;
readonly outputPerMTok: number;
readonly thoughtPerMTok: number;
readonly contextWindow: number;
}
// Per-million-token pricing in USD. Source: https://ai.google.dev/gemini-api/docs/pricing
export const PRICING: Readonly<Record<string, ModelPricing>> = {
'gemini-3.1-pro-preview': {
inputPerMTok: 2.0,
outputPerMTok: 12.0,
thoughtPerMTok: 12.0,
contextWindow: 1_000_000,
},
'gemini-3.5-flash': {
inputPerMTok: 1.5,
outputPerMTok: 9.0,
thoughtPerMTok: 9.0,
contextWindow: 1_000_000,
},
'gemini-3.1-flash-lite': {
inputPerMTok: 0.25,
outputPerMTok: 1.5,
thoughtPerMTok: 1.5,
contextWindow: 1_000_000,
},
} as const;
// Defensive fallback for unrecognised model ids — prefer overcount to undercount.
const FALLBACK_PRICING: ModelPricing = {
inputPerMTok: 2.0,
outputPerMTok: 12.0,
thoughtPerMTok: 12.0,
contextWindow: 1_000_000,
};
export function pricingFor(model: string): ModelPricing {
return PRICING[model] ?? FALLBACK_PRICING;
}
export function costUsd(usage: TokenUsage, model: string): number {
const p = pricingFor(model);
return (
(usage.inputTokens / 1_000_000) * p.inputPerMTok +
(usage.outputTokens / 1_000_000) * p.outputPerMTok +
(usage.thoughtTokens / 1_000_000) * p.thoughtPerMTok
);
}
export function formatUsd(amount: number): string {
if (amount === 0) return '$0';
if (amount < 0.01) return `$${amount.toFixed(4)}`;
if (amount < 1) return `$${amount.toFixed(3)}`;
return `$${amount.toFixed(2)}`;
}
export function formatTokens(n: number): string {
if (n < 1000) return n.toString();
if (n < 1_000_000) return `${(n / 1000).toFixed(1)}k`;
return `${(n / 1_000_000).toFixed(2)}M`;
}