llmtap-proxy 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
llmtap/pricing.py ADDED
@@ -0,0 +1,379 @@
1
+ """Model price table and cost calculation.
2
+
3
+ Prices are **published list prices in USD per 1M tokens** as of ``PRICES_AS_OF``
4
+ (standard tier: no batch discount, no long-context surcharge, no regional uplift).
5
+ They drift; override or extend them with the config ``pricing_overrides`` table
6
+ (``[llmtap.pricing_overrides."my-model"] input = 1.0 output = 2.0``), which wins
7
+ over this table. Entries marked ``# est.`` are best estimates.
8
+
9
+ Lookup (``price_for``):
10
+ 1. normalize the name: lowercase; drop routing prefixes (``anthropic/``, ``openai/``,
11
+ ``google/``, ``meta-llama/`` ... - everything up to the last ``/``); drop Bedrock
12
+ ``[us.]anthropic.`` style prefixes; drop ``@version`` suffixes (Vertex); drop
13
+ ``ft:`` fine-tune wrappers; Claude ``3.5`` -> ``3-5`` (OpenRouter spelling).
14
+ 2. ``:tag`` suffixes: numeric (Bedrock ``-v2:0``) and OpenRouter variants
15
+ (``:thinking``, ``:beta``) are dropped; ``:free`` is free; any other tag on a bare
16
+ name (``llama3:8b``, ``qwen2.5:7b``) is an Ollama local model -> cost 0.
17
+ 3. exact match, then the LONGEST key that is a prefix of the name on a word boundary
18
+ (``gpt-4o-mini-2024-07-18`` -> ``gpt-4o-mini``, never ``gpt-4o``).
19
+ Overrides are matched the same way and win ties.
20
+
21
+ Cost (``cost_usd``) - the providers report cached tokens differently:
22
+ * Anthropic (``claude-*``): ``input_tokens`` EXCLUDES cache tokens, so
23
+ ``cost = input*in + cache_read*cache_read + cache_write*cache_write + output*out``.
24
+ * Everyone else (OpenAI and OpenAI-compatible APIs, Gemini, DeepSeek, ...): cached
25
+ tokens are a SUBSET of ``prompt_tokens``, so
26
+ ``cost = (input - cache_read)*in + cache_read*cache_read + output*out``
27
+ (cache writes are not billed). If ``cache_read > input`` the numbers are clearly
28
+ exclusive, so the input is charged in full.
29
+ Missing cache prices default to 10% of input (Anthropic) / 50% (others); cache-write
30
+ price defaults to 125% of input.
31
+ """
32
+ from __future__ import annotations
33
+
34
+ import re
35
+ from typing import Any
36
+
37
+ from .models import Usage
38
+
39
+ PRICES_AS_OF = "2026-11"
40
+
41
+ _ZERO: dict[str, float] = {"input": 0.0, "output": 0.0, "cache_read": 0.0, "cache_write": 0.0}
42
+
43
+
44
+ def _p(inp: float, out: float, cr: float | None = None, cw: float | None = None) -> dict[str, float]:
45
+ d = {"input": float(inp), "output": float(out)}
46
+ if cr is not None:
47
+ d["cache_read"] = float(cr)
48
+ if cw is not None:
49
+ d["cache_write"] = float(cw)
50
+ return d
51
+
52
+
53
+ _RAW: dict[str, dict[str, float]] = {
54
+ # ------------------------------------------------------------------ Anthropic
55
+ # cache_read = 0.1x input, cache_write (5-min TTL) = 1.25x input unless noted.
56
+ "claude-instant": _p(0.8, 2.4),
57
+ "claude-2": _p(8, 24),
58
+ "claude-3-haiku": _p(0.25, 1.25, 0.03, 0.30),
59
+ "claude-3-sonnet": _p(3, 15),
60
+ "claude-3-opus": _p(15, 75),
61
+ "claude-3-5-haiku": _p(0.8, 4, 0.08, 1.0),
62
+ "claude-3-5-sonnet": _p(3, 15),
63
+ "claude-3-7-sonnet": _p(3, 15),
64
+ "claude-sonnet-4": _p(3, 15),
65
+ "claude-sonnet-4-5": _p(3, 15),
66
+ "claude-sonnet-4-6": _p(3, 15),
67
+ "claude-opus-4": _p(15, 75),
68
+ "claude-opus-4-1": _p(15, 75),
69
+ "claude-opus-4-5": _p(5, 25),
70
+ "claude-opus-4-6": _p(5, 25),
71
+ "claude-opus-4-7": _p(5, 25),
72
+ "claude-opus-4-8": _p(5, 25),
73
+ "claude-haiku-4-5": _p(1, 5),
74
+ # Claude 5 family (newer than most published tables; best-known list prices).
75
+ "claude-opus-5": _p(5, 25),
76
+ "claude-opus-5-5": _p(4, 20, 0.20),
77
+ "claude-sonnet-5": _p(2, 10),
78
+ "claude-sonnet-5-5": _p(2, 10, 0.10),
79
+ "claude-haiku-5-5": _p(0.10, 0.50), # <=100K-token prompts
80
+ "claude-fable-5": _p(10, 50, 1.0),
81
+ "claude-fable-5-1": _p(10, 50, 0.25),
82
+ "claude-mythos-5": _p(10, 50, 1.0),
83
+ "claude-mythos-5-1": _p(10, 50, 0.25),
84
+ "claude-fable-5-2": _p(8, 40, 0.20),
85
+ "claude-mythos-5-2": _p(8, 40, 0.20),
86
+ # Aliases without a minor version.
87
+ "claude-opus": _p(4, 20, 0.20), # est. (latest opus)
88
+ "claude-sonnet": _p(2, 10, 0.10), # est. (latest sonnet)
89
+ "claude-haiku": _p(0.10, 0.50), # est. (latest haiku)
90
+ # ------------------------------------------------------------------ OpenAI
91
+ "gpt-4o": _p(2.5, 10, 1.25),
92
+ "gpt-4o-2024-05-13": _p(5, 15),
93
+ "chatgpt-4o-latest": _p(5, 15),
94
+ "gpt-4o-mini": _p(0.15, 0.6, 0.075),
95
+ "gpt-4o-audio-preview": _p(2.5, 10),
96
+ "gpt-4o-mini-audio-preview": _p(0.15, 0.6),
97
+ "gpt-4o-realtime-preview": _p(5, 20, 2.5),
98
+ "gpt-4o-mini-realtime-preview": _p(0.6, 2.4, 0.3),
99
+ "gpt-4o-search-preview": _p(2.5, 10),
100
+ "gpt-4o-mini-search-preview": _p(0.15, 0.6),
101
+ "gpt-4.1": _p(2, 8, 0.5),
102
+ "gpt-4.1-mini": _p(0.4, 1.6, 0.1),
103
+ "gpt-4.1-nano": _p(0.1, 0.4, 0.025),
104
+ "gpt-4.5-preview": _p(75, 150, 37.5),
105
+ "gpt-4-turbo": _p(10, 30),
106
+ "gpt-4-1106-preview": _p(10, 30),
107
+ "gpt-4-0125-preview": _p(10, 30),
108
+ "gpt-4-vision-preview": _p(10, 30),
109
+ "gpt-4": _p(30, 60),
110
+ "gpt-4-32k": _p(60, 120),
111
+ "gpt-3.5-turbo": _p(0.5, 1.5),
112
+ "gpt-3.5-turbo-instruct": _p(1.5, 2),
113
+ "o1": _p(15, 60, 7.5),
114
+ "o1-preview": _p(15, 60, 7.5),
115
+ "o1-mini": _p(1.1, 4.4, 0.55),
116
+ "o1-pro": _p(150, 600),
117
+ "o3": _p(2, 8, 0.5),
118
+ "o3-pro": _p(20, 80),
119
+ "o3-mini": _p(1.1, 4.4, 0.55),
120
+ "o3-deep-research": _p(10, 40, 2.5),
121
+ "o4-mini": _p(1.1, 4.4, 0.275),
122
+ "o4-mini-deep-research": _p(2, 8, 0.5),
123
+ "codex-mini-latest": _p(1.5, 6, 0.375),
124
+ "gpt-5": _p(1.25, 10, 0.125),
125
+ "gpt-5-mini": _p(0.25, 2, 0.025),
126
+ "gpt-5-nano": _p(0.05, 0.4, 0.005),
127
+ "gpt-5-pro": _p(15, 120),
128
+ "gpt-5-codex": _p(1.25, 10, 0.125),
129
+ "gpt-5-chat": _p(1.25, 10, 0.125),
130
+ "gpt-5.1": _p(1.25, 10, 0.125),
131
+ "gpt-5.1-codex": _p(1.25, 10, 0.125),
132
+ "gpt-5.1-codex-mini": _p(0.25, 2, 0.025),
133
+ "gpt-5.2": _p(1.75, 14, 0.175), # est.
134
+ "gpt-oss-120b": _p(0.15, 0.6), # hosted (groq/together), est.
135
+ "gpt-oss-20b": _p(0.075, 0.3), # hosted, est.
136
+ "text-embedding-3-small": _p(0.02, 0),
137
+ "text-embedding-3-large": _p(0.13, 0),
138
+ "text-embedding-ada-002": _p(0.10, 0),
139
+ "omni-moderation": _p(0, 0),
140
+ "text-moderation": _p(0, 0),
141
+ "davinci-002": _p(2, 2),
142
+ "babbage-002": _p(0.4, 0.4),
143
+ # ------------------------------------------------------------------ Google
144
+ # (<=200K-token prompt tier; cache_read = context-cache price)
145
+ "gemini-3-pro": _p(2, 12, 0.2), # est.
146
+ "gemini-2.5-pro": _p(1.25, 10, 0.125),
147
+ "gemini-2.5-flash": _p(0.30, 2.50, 0.03),
148
+ "gemini-2.5-flash-lite": _p(0.10, 0.40, 0.01),
149
+ "gemini-2.0-flash": _p(0.10, 0.40, 0.025),
150
+ "gemini-2.0-flash-lite": _p(0.075, 0.30),
151
+ "gemini-1.5-pro": _p(1.25, 5, 0.3125),
152
+ "gemini-1.5-flash": _p(0.075, 0.30, 0.01875),
153
+ "gemini-1.5-flash-8b": _p(0.0375, 0.15, 0.01),
154
+ "gemini-embedding-001": _p(0.15, 0),
155
+ "text-embedding-004": _p(0, 0),
156
+ # ------------------------------------------------------------------ Mistral
157
+ "mistral-large": _p(2, 6),
158
+ "mistral-medium": _p(0.4, 2),
159
+ "mistral-small": _p(0.1, 0.3),
160
+ "mistral-tiny": _p(0.25, 0.25),
161
+ "mistral-saba": _p(0.2, 0.6),
162
+ "mistral-embed": _p(0.1, 0),
163
+ "mistral-7b-instruct": _p(0.03, 0.055), # openrouter, est.
164
+ "open-mistral-7b": _p(0.25, 0.25),
165
+ "open-mistral-nemo": _p(0.15, 0.15),
166
+ "mistral-nemo": _p(0.15, 0.15),
167
+ "open-mixtral-8x7b": _p(0.7, 0.7),
168
+ "open-mixtral-8x22b": _p(2, 6),
169
+ "mixtral-8x7b": _p(0.24, 0.24), # groq / together
170
+ "mixtral-8x22b": _p(1.2, 1.2), # together
171
+ "codestral": _p(0.3, 0.9),
172
+ "devstral-small": _p(0.1, 0.3),
173
+ "devstral-medium": _p(0.4, 2),
174
+ "magistral-medium": _p(2, 5),
175
+ "magistral-small": _p(0.5, 1.5),
176
+ "ministral-8b": _p(0.1, 0.1),
177
+ "ministral-3b": _p(0.04, 0.04),
178
+ "pixtral-large": _p(2, 6),
179
+ "pixtral-12b": _p(0.15, 0.15),
180
+ # ------------------------------------------------------------------ DeepSeek
181
+ # Official API (V3.2 pricing for both aliases; cache_read = cache-hit price).
182
+ "deepseek-chat": _p(0.28, 0.42, 0.028),
183
+ "deepseek-reasoner": _p(0.28, 0.42, 0.028),
184
+ "deepseek-v3": _p(0.27, 1.10, 0.07), # openrouter-style names
185
+ "deepseek-v3.1": _p(0.27, 1.10, 0.07), # est.
186
+ "deepseek-v3.2": _p(0.28, 0.42, 0.028),
187
+ "deepseek-r1": _p(0.55, 2.19, 0.14),
188
+ "deepseek-r1-distill-llama-70b": _p(0.75, 0.99), # groq
189
+ # ------------------------------------------------------------------ Meta Llama (hosted)
190
+ # groq / openrouter spellings
191
+ "llama-3.1-8b": _p(0.05, 0.08),
192
+ "llama-3.1-70b": _p(0.59, 0.79),
193
+ "llama-3.1-405b": _p(3.5, 3.5),
194
+ "llama-3.2-1b": _p(0.04, 0.04),
195
+ "llama-3.2-3b": _p(0.06, 0.06),
196
+ "llama-3.2-11b": _p(0.18, 0.18),
197
+ "llama-3.2-90b": _p(0.9, 0.9),
198
+ "llama-3.3-70b": _p(0.59, 0.79),
199
+ "llama-4-scout": _p(0.11, 0.34),
200
+ "llama-4-maverick": _p(0.20, 0.60),
201
+ "llama3-8b": _p(0.05, 0.08), # groq legacy llama3-8b-8192
202
+ "llama3-70b": _p(0.59, 0.79), # groq legacy llama3-70b-8192
203
+ "llama-guard": _p(0.2, 0.2),
204
+ # together spellings (meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo)
205
+ "meta-llama-3.1-8b": _p(0.18, 0.18),
206
+ "meta-llama-3.1-70b": _p(0.88, 0.88),
207
+ "meta-llama-3.1-405b": _p(3.5, 3.5),
208
+ "meta-llama-3-8b": _p(0.2, 0.2),
209
+ "meta-llama-3-70b": _p(0.88, 0.88),
210
+ "llama-3.3-70b-instruct-turbo": _p(0.88, 0.88),
211
+ "llama-4-scout-17b-16e-instruct": _p(0.18, 0.59), # together, est.
212
+ "llama-4-maverick-17b-128e-instruct-fp8": _p(0.27, 0.85), # together, est.
213
+ # ------------------------------------------------------------------ Qwen (hosted)
214
+ "qwen-max": _p(1.6, 6.4),
215
+ "qwen-plus": _p(0.4, 1.2),
216
+ "qwen-turbo": _p(0.05, 0.2),
217
+ "qwen-2.5-72b": _p(0.35, 0.40), # openrouter, est.
218
+ "qwen2.5-72b": _p(1.2, 1.2), # together
219
+ "qwen2.5-7b": _p(0.3, 0.3), # together
220
+ "qwen2.5-coder-32b": _p(0.8, 0.8), # together
221
+ "qwen-2.5-coder-32b": _p(0.8, 0.8),
222
+ "qwq-32b": _p(1.2, 1.2), # together, est.
223
+ "qwen3-235b": _p(0.2, 0.6), # est.
224
+ "qwen3-32b": _p(0.29, 0.59), # groq, est.
225
+ "qwen3-coder": _p(0.4, 1.6), # est.
226
+ # ------------------------------------------------------------------ others
227
+ "grok-4": _p(3, 15, 0.75),
228
+ "grok-4-fast": _p(0.2, 0.5, 0.05),
229
+ "grok-3": _p(3, 15, 0.75),
230
+ "grok-3-mini": _p(0.3, 0.5, 0.075),
231
+ "grok-code-fast": _p(0.2, 1.5, 0.02),
232
+ "command-a": _p(2.5, 10),
233
+ "command-r-plus": _p(2.5, 10),
234
+ "command-r": _p(0.15, 0.6),
235
+ "command-r7b": _p(0.0375, 0.15),
236
+ "kimi-k2": _p(0.6, 2.5, 0.15), # est.
237
+ "glm-4.6": _p(0.6, 2.2, 0.11), # est.
238
+ # ------------------------------------------------------------------ local (Ollama etc.) = free
239
+ "llama2": _ZERO, "llama3": _ZERO, "llama4": _ZERO, "codellama": _ZERO, "llava": _ZERO,
240
+ "qwen2": _ZERO, "qwen2.5": _ZERO, "qwen3": _ZERO, "qwq": _ZERO,
241
+ "gemma2": _ZERO, "gemma3": _ZERO, "phi3": _ZERO, "phi4": _ZERO,
242
+ "tinyllama": _ZERO, "starcoder2": _ZERO, "deepseek-coder-v2": _ZERO,
243
+ "nomic-embed-text": _ZERO, "mxbai-embed-large": _ZERO, "all-minilm": _ZERO,
244
+ "dolphin-mixtral": _ZERO, "neural-chat": _ZERO, "openchat": _ZERO, "zephyr": _ZERO,
245
+ "vicuna": _ZERO, "orca-mini": _ZERO, "smollm": _ZERO, "smollm2": _ZERO,
246
+ "mistral": _ZERO, "mixtral": _ZERO, "qwen": _ZERO, "phi": _ZERO, "gemma": _ZERO,
247
+ }
248
+
249
+ # Bare local names that must only match exactly (as prefixes they'd swallow paid
250
+ # models such as "mistral-large" or "qwen-max").
251
+ _EXACT_ONLY: frozenset[str] = frozenset({"mistral", "mixtral", "qwen", "phi", "gemma"})
252
+
253
+
254
+ def _is_anthropic(key: str) -> bool:
255
+ return key.startswith("claude")
256
+
257
+
258
+ def _complete(key: str, entry: dict[str, Any]) -> dict[str, float]:
259
+ """Fill in default cache prices for one table entry."""
260
+ inp = float(entry.get("input", 0.0) or 0.0)
261
+ out = float(entry.get("output", 0.0) or 0.0)
262
+ cr = entry.get("cache_read")
263
+ cw = entry.get("cache_write")
264
+ if cr is None:
265
+ cr = inp * (0.10 if _is_anthropic(key) else 0.50)
266
+ if cw is None:
267
+ cw = inp * 1.25
268
+ return {"input": inp, "output": out, "cache_read": float(cr), "cache_write": float(cw)}
269
+
270
+
271
+ PRICES: dict[str, dict[str, float]] = {k: _complete(k, v) for k, v in _RAW.items()}
272
+
273
+ _BEDROCK_PREFIX = re.compile(
274
+ r"^(?:(?:us|eu|apac|au|ca|jp|global)\.)?(?:anthropic|meta|mistral|amazon|cohere|ai21|deepseek|openai|qwen)\."
275
+ )
276
+ _CLAUDE_DOT = re.compile(r"(\d)\.(\d)")
277
+
278
+
279
+ def _normalize(model: str) -> tuple[str, bool]:
280
+ """Return (normalized name, is_free_local)."""
281
+ name = model.strip().lower()
282
+ if name.startswith("ft:"): # ft:gpt-4o-mini-2024-07-18:org::id
283
+ name = name[3:].split(":", 1)[0]
284
+ had_slash = "/" in name
285
+ if had_slash:
286
+ name = name.rstrip("/").rsplit("/", 1)[-1]
287
+ name = _BEDROCK_PREFIX.sub("", name)
288
+ if "@" in name:
289
+ name = name.split("@", 1)[0]
290
+ if ":" in name:
291
+ base, tag = name.split(":", 1)
292
+ if tag == "free":
293
+ return base, True
294
+ if tag.isdigit() or had_slash:
295
+ name = base
296
+ else:
297
+ return base, True # ollama "llama3:8b" -> local
298
+ if name.startswith("claude"):
299
+ name = _CLAUDE_DOT.sub(r"\1-\2", name)
300
+ return name, False
301
+
302
+
303
+ def normalize_model(model: str) -> str:
304
+ """Canonical lookup form of a model name (lowercase, prefixes/suffixes stripped)."""
305
+ return _normalize(model)[0]
306
+
307
+
308
+ def _best_key(name: str, table: dict[str, Any]) -> str | None:
309
+ """Exact key, else the longest key that prefixes `name` at a word boundary."""
310
+ if name in table:
311
+ return name
312
+ best: str | None = None
313
+ for key in table:
314
+ if key in _EXACT_ONLY or not name.startswith(key):
315
+ continue
316
+ nxt = name[len(key):len(key) + 1]
317
+ if nxt and nxt.isalnum():
318
+ continue
319
+ if best is None or len(key) > len(best):
320
+ best = key
321
+ return best
322
+
323
+
324
+ def price_for(model: str | None, overrides: dict | None = None) -> dict[str, float] | None:
325
+ """USD-per-1M-token prices for `model` ({"input","output","cache_read","cache_write"}), or None."""
326
+ if not model or not isinstance(model, str) or not model.strip():
327
+ return None
328
+ name, free = _normalize(model)
329
+ builtin_key = _best_key(name, PRICES)
330
+ if overrides:
331
+ norm_over: dict[str, dict[str, Any]] = {}
332
+ for k, v in overrides.items():
333
+ if isinstance(k, str) and isinstance(v, dict):
334
+ norm_over[normalize_model(k)] = v
335
+ okey = _best_key(name, norm_over) if norm_over else None
336
+ if okey is not None and (builtin_key is None or len(okey) >= len(builtin_key) or free):
337
+ base: dict[str, Any] = {}
338
+ if builtin_key is not None and okey.startswith(builtin_key) and not free:
339
+ base = dict(_RAW[builtin_key])
340
+ base.update({k: v for k, v in norm_over[okey].items() if v is not None})
341
+ return _complete(okey, base)
342
+ if free:
343
+ return dict(_ZERO)
344
+ if builtin_key is None:
345
+ return None
346
+ return dict(PRICES[builtin_key])
347
+
348
+
349
+ def cost_usd(model: str | None, usage: Usage, overrides: dict | None = None) -> float:
350
+ """Dollar cost of one call; 0.0 for unknown models. See module docstring for the formula."""
351
+ price = price_for(model, overrides)
352
+ if price is None:
353
+ return 0.0
354
+ inp = max(int(usage.input_tokens or 0), 0)
355
+ out = max(int(usage.output_tokens or 0), 0)
356
+ cr = max(int(usage.cache_read_tokens or 0), 0)
357
+ cw = max(int(usage.cache_write_tokens or 0), 0)
358
+ name = normalize_model(model or "")
359
+ if _is_anthropic(name):
360
+ total = (inp * price["input"] + cr * price["cache_read"]
361
+ + cw * price["cache_write"] + out * price["output"])
362
+ else:
363
+ uncached = inp - cr if cr <= inp else inp
364
+ total = uncached * price["input"] + cr * price["cache_read"] + out * price["output"]
365
+ return round(total / 1_000_000, 10)
366
+
367
+
368
+ def format_usd(x: float) -> str:
369
+ """'$0.0042' (4 decimals under $1), '$1.23', '$0.00' for zero."""
370
+ x = float(x or 0.0)
371
+ sign = "-" if x < 0 else ""
372
+ a = abs(x)
373
+ if a == 0:
374
+ return "$0.00"
375
+ if a < 0.00005:
376
+ return f"{sign}<$0.0001"
377
+ if a < 1:
378
+ return f"{sign}${a:.4f}"
379
+ return f"{sign}${a:.2f}"