Repository navigation
Expand file tree
/
Copy pathcost.py
More file actions
121 lines (103 loc) · 4.61 KB
/
Copy pathcost.py
File metadata and controls
121 lines (103 loc) · 4.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Per-call cost estimation for Gemini Live.
Source of truth is Gemini's own `usageMetadata` (token counts, broken down by
modality), surfaced to us through LiveKit's `metrics_collected` event. We never
guess token counts — we only multiply Google-reported tokens by Google's
published per-1M-token price list. The result is an ESTIMATE for live
per-call clarity; reconcile the monthly total against aistudio.google.com/spend.
Because this is an audio-to-audio model, audio tokens dominate and are priced
very differently from text (audio input is 4x text input, audio output ~2.7x
text output). So we MUST keep the per-modality split — a single blended rate
would be wrong for a voice call.
"""
# Paid-tier price per 1,000,000 tokens, USD. Keyed by model.
# gemini-3.1-flash-live-preview — from Google AI pricing.
PRICING = {
"gemini-3.1-flash-live-preview": {
"input": {"text": 0.75, "audio": 3.00, "image": 1.00},
"output": {"text": 4.50, "audio": 12.00, "image": 0.00},
},
}
# Any model we don't have an explicit table for is priced with these rates.
_DEFAULT_MODEL = "gemini-3.1-flash-live-preview"
def _rates(model: str) -> dict:
return PRICING.get(model or _DEFAULT_MODEL, PRICING[_DEFAULT_MODEL])
def compute_cost(
model: str, *,
text_in: int = 0, audio_in: int = 0, image_in: int = 0,
text_out: int = 0, audio_out: int = 0, image_out: int = 0,
) -> float:
"""Cost in USD for one call, per-modality token counts × the price table.
Cost = ( text_in·0.75 + audio_in·3.00 + image_in·1.00
+ text_out·4.50 + audio_out·12.00 ) / 1,000,000
"""
r = _rates(model)
micros = (
text_in * r["input"]["text"]
+ audio_in * r["input"]["audio"]
+ image_in * r["input"]["image"]
+ text_out * r["output"]["text"]
+ audio_out * r["output"]["audio"]
+ image_out * r["output"]["image"]
)
return round(micros / 1_000_000, 6)
class CallUsage:
"""Accumulates per-modality token counts across a single call.
Fed one realtime/LLM metrics object at a time from the LiveKit
`metrics_collected` event (every model response emits one). At call end,
`.cost(model)` turns the totals into a dollar figure.
"""
def __init__(self) -> None:
self.text_in = self.audio_in = self.image_in = 0
self.text_out = self.audio_out = self.image_out = 0
def add(self, m) -> None:
"""Fold one metrics object into the running totals. Defensive against
LiveKit version differences — missing fields are treated as 0, and a
metric that reports only totals (no modality split) is attributed to the
dominant modality for its kind (audio for realtime, text for plain LLM)."""
idt = getattr(m, "input_token_details", None)
odt = getattr(m, "output_token_details", None)
had_in_detail = had_out_detail = False
if idt is not None:
ti = getattr(idt, "text_tokens", 0) or 0
ai = getattr(idt, "audio_tokens", 0) or 0
ii = getattr(idt, "image_tokens", 0) or 0
if ti or ai or ii:
self.text_in += ti
self.audio_in += ai
self.image_in += ii
had_in_detail = True
if odt is not None:
to = getattr(odt, "text_tokens", 0) or 0
ao = getattr(odt, "audio_tokens", 0) or 0
io = getattr(odt, "image_tokens", 0) or 0
if to or ao or io:
self.text_out += to
self.audio_out += ao
self.image_out += io
had_out_detail = True
# Fallback when the provider gave only aggregate token counts.
is_realtime = "realtime" in str(getattr(m, "type", "")).lower()
if not had_in_detail:
tin = getattr(m, "input_tokens", 0) or 0
if tin:
if is_realtime:
self.audio_in += tin
else:
self.text_in += tin
if not had_out_detail:
tout = getattr(m, "output_tokens", 0) or 0
if tout:
if is_realtime:
self.audio_out += tout
else:
self.text_out += tout
def total_tokens(self) -> int:
return (self.text_in + self.audio_in + self.image_in
+ self.text_out + self.audio_out + self.image_out)
def cost(self, model: str) -> float:
return compute_cost(
model,
text_in=self.text_in, audio_in=self.audio_in, image_in=self.image_in,
text_out=self.text_out, audio_out=self.audio_out, image_out=self.image_out,
)