usage_pricing.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. from __future__ import annotations
  2. import logging
  3. import re
  4. from dataclasses import dataclass, fields, replace
  5. from datetime import datetime, timezone, UTC
  6. from decimal import Decimal
  7. from typing import Any, Dict, Literal, Optional
  8. from agent.model_metadata import fetch_endpoint_model_metadata, fetch_model_metadata, strip_codex_context_variant_suffix
  9. from utils import base_url_host_matches, base_url_hostname, base_url_origin
  10. logger = logging.getLogger(__name__)
  11. _ZERO = Decimal(0)
  12. _ONE_MILLION = Decimal(1000000)
  13. _NOUS_DEFAULT_BASE_URL = "https://inference-api.nousresearch.com/v1"
  14. # Pay-per-token first-party APIs whose models.dev rate card is the vendor's own
  15. # list price, keyed by billing-route provider -> API domain. A model missing from
  16. # the snapshot below is priced from models.dev only on HTTPS:443 to that domain
  17. # (or with no base URL, i.e. the provider default): a proxy, relay or custom
  18. # endpoint serving the same model id may bill differently, and subscription
  19. # routes (openai-codex, xai-oauth) keep their own policy.
  20. _MODELS_DEV_DIRECT_HOSTS = {
  21. "openai": "openai.com", "xai": "x.ai", "anthropic": "anthropic.com", "google": "googleapis.com",
  22. "deepseek": "deepseek.com", "xiaomi": "xiaomimimo.com",
  23. }
  24. # Below $0.01, render at 4 dp so cheap-model costs never display as $0.00.
  25. # Sub-cent cost threshold: below $0.01, render at 4 decimal places so the display is non-zero (e.g. $0.0046
  26. # instead of $0.00). See #79220.
  27. _SUBCENT_THRESHOLD = Decimal("0.01")
  28. # Attached to every CostResult with status="included" so consumers can
  29. # distinguish "free because subscription" from "free because $0 pricing".
  30. _INCLUDED_NOTE = "subscription-included; no provider invoice for usage"
  31. def format_cost_label(amount: Decimal) -> str:
  32. """Cost display label: zero → "$0.00"; sub-cent → "~$0.0046" (4 dp, or
  33. "~$<0.0001" when it rounds to 0.0000 so the label never reads as zero);
  34. else "~$1.23". Shared by per-response labels and insights cost buckets.
  35. This fixes #79220 where sub-cent per-turn costs on cheap models (DeepSeek, etc.) rendered as "$0.00"
  36. despite amount_usd carrying full Decimal precision.
  37. """
  38. if amount == _ZERO:
  39. return "$0.00"
  40. if amount < _SUBCENT_THRESHOLD:
  41. label = f"~${amount:.4f}"
  42. # Compare the rendered label: a naive `< 0.00005` threshold misses
  43. # the exact boundary under ROUND_HALF_EVEN.
  44. # A positive amount that rounds to 0.0000 at 4 dp would render "~$0.0000" — a zero-looking label,
  45. # the exact #79220 dishonesty.
  46. return label if label != "~$0.0000" else "~$<0.0001"
  47. return f"~${amount:.2f}"
  48. CostStatus = Literal["actual", "estimated", "included", "unknown"]
  49. CostSource = Literal[
  50. "provider_cost_api", "provider_generation_api", "provider_models_api", "official_docs_snapshot",
  51. "user_override", "custom_contract", "none",
  52. ]
  53. @dataclass(frozen=True)
  54. class CanonicalUsage:
  55. input_tokens: int = 0
  56. output_tokens: int = 0
  57. cache_read_tokens: int = 0
  58. cache_write_tokens: int = 0
  59. reasoning_tokens: int = 0
  60. request_count: int = 1
  61. raw_usage: Optional[dict[str, Any]] = None
  62. @property
  63. def prompt_tokens(self) -> int:
  64. return self.input_tokens + self.cache_read_tokens + self.cache_write_tokens
  65. @property
  66. def total_tokens(self) -> int:
  67. return self.prompt_tokens + self.output_tokens
  68. def __add__(self, other: CanonicalUsage) -> CanonicalUsage:
  69. """Sum two usage buckets. ``raw_usage`` (single-response detail) is
  70. dropped; ``request_count`` adds so callers see how many API calls a
  71. combined figure covers."""
  72. if not isinstance(other, CanonicalUsage):
  73. return NotImplemented
  74. return CanonicalUsage(**{
  75. f.name: getattr(self, f.name) + getattr(other, f.name)
  76. for f in fields(CanonicalUsage) if f.name != "raw_usage"
  77. })
  78. @dataclass(frozen=True)
  79. class BillingRoute:
  80. provider: str
  81. model: str
  82. base_url: str = ""
  83. billing_mode: str = "unknown"
  84. @dataclass(frozen=True)
  85. class PricingEntry:
  86. input_cost_per_million: Optional[Decimal] = None
  87. output_cost_per_million: Optional[Decimal] = None
  88. cache_read_cost_per_million: Optional[Decimal] = None
  89. cache_write_cost_per_million: Optional[Decimal] = None
  90. request_cost: Optional[Decimal] = None
  91. source: CostSource = "none"
  92. source_url: Optional[str] = None
  93. pricing_version: Optional[str] = None
  94. fetched_at: Optional[datetime] = None
  95. # Context-tiered pricing (e.g. Gemini Pro above 200k prompt tokens): when
  96. # ``usage.prompt_tokens`` exceeds ``tier_threshold_tokens`` the ``*_above``
  97. # rates replace the base rates for the WHOLE request (Google's semantics,
  98. # not marginal brackets). A None ``*_above`` falls back to its base rate.
  99. tier_threshold_tokens: Optional[int] = None
  100. input_cost_per_million_above: Optional[Decimal] = None
  101. output_cost_per_million_above: Optional[Decimal] = None
  102. cache_read_cost_per_million_above: Optional[Decimal] = None
  103. cache_write_cost_per_million_above: Optional[Decimal] = None
  104. @dataclass(frozen=True)
  105. class CostResult:
  106. amount_usd: Optional[Decimal]
  107. status: CostStatus
  108. source: CostSource
  109. label: str
  110. fetched_at: Optional[datetime] = None
  111. pricing_version: Optional[str] = None
  112. notes: tuple[str, ...] = ()
  113. _UTC_NOW = lambda: datetime.now(UTC)
  114. _INCLUDED_ENTRY = PricingEntry(
  115. input_cost_per_million=_ZERO, output_cost_per_million=_ZERO, cache_read_cost_per_million=_ZERO,
  116. cache_write_cost_per_million=_ZERO, source="none", pricing_version="included-route",
  117. )
  118. def _snap(
  119. inp: str, out: str, cache_read: Optional[str] = None, cache_write: Optional[str] = None, *,
  120. version: str, url: Optional[str] = None, **tiers: Any,
  121. ) -> PricingEntry:
  122. """Build an official-docs snapshot entry from per-million USD rate strings."""
  123. return PricingEntry(
  124. input_cost_per_million=Decimal(inp), output_cost_per_million=Decimal(out),
  125. cache_read_cost_per_million=Decimal(cache_read) if cache_read is not None else None,
  126. cache_write_cost_per_million=Decimal(cache_write) if cache_write is not None else None,
  127. source="official_docs_snapshot", source_url=url, pricing_version=version, **tiers,
  128. )
  129. # Official docs snapshot: models whose published pricing and cache semantics are
  130. # stable enough to encode exactly. Each snapshot is (provider, source_url,
  131. # pricing_version, {model-or-models: per-1M rates (input, output[, cache_read[,
  132. # cache_write]])}); a tuple key shares one rate row across several model ids.
  133. _BEDROCK_URL = "https://aws.amazon.com/bedrock/pricing/"
  134. _ANTHROPIC_URL = "https://platform.claude.com/docs/en/about-claude/pricing"
  135. _GOOGLE_URL = "https://ai.google.dev/pricing"
  136. _OPUS = ("5.00", "25.00", "0.50", "6.25")
  137. _SONNET = ("3.00", "15.00", "0.30", "3.75")
  138. _OPUS_5_5 = ("4.00", "20.00", "0.20", "5.00")
  139. _SNAPSHOTS: tuple[tuple[str, Optional[str], str, dict], ...] = (
  140. # OpenAI GPT-5.6 (Sol/Terra/Luna). Cache write = 1.25x input, cache read =
  141. # 0.10x input. "-pro" high-effort modes bill at the same per-token rates
  142. # (aliased below); "Sol Fast mode" is a separate tier, not covered.
  143. ("openai", "https://openai.com/index/previewing-gpt-5-6-sol/", "openai-gpt-5.6-2026-07", {
  144. "gpt-5.6-sol": ("5.00", "30.00", "0.50", "6.25"), "gpt-5.6-terra": ("2.50", "15.00", "0.25", "3.125"),
  145. "gpt-5.6-luna": ("1.00", "6.00", "0.10", "1.25"),
  146. }),
  147. # Claude 4.5/4.6/4.7/4.8 Opus share $5/$25 (new tokenizer, up to 35% more tokens).
  148. ("anthropic", _ANTHROPIC_URL, "anthropic-pricing-2026-05", {
  149. ("claude-opus-4-8", "claude-opus-4-7", "claude-opus-4-7-20250507", "claude-opus-4-6",
  150. "claude-opus-4-6-20250414", "claude-opus-4-5"): _OPUS,
  151. ("claude-sonnet-4-6", "claude-sonnet-4-6-20250414", "claude-sonnet-4-5", "claude-sonnet-4-20250514",
  152. "claude-3-5-sonnet-20241022"): _SONNET,
  153. "claude-haiku-4-5": ("1.00", "5.00", "0.10", "1.25"),
  154. ("claude-opus-4-20250514", "claude-3-opus-20240229"): ("15.00", "75.00", "1.50", "18.75"),
  155. "claude-3-5-haiku-20241022": ("0.80", "4.00", "0.08", "1.00"),
  156. "claude-3-haiku-20240307": ("0.25", "1.25", "0.03", "0.30"),
  157. }),
  158. # Fast mode is a separate model id at a 2x premium.
  159. ("anthropic", "https://openrouter.ai/anthropic/claude-opus-4.8-fast", "anthropic-pricing-2026-05", {
  160. "claude-opus-4-8-fast": ("10.00", "50.00", "1.00", "12.50"),
  161. }),
  162. # Claude Sonnet 5: introductory $2/$10 through 2026-08-31, then $3/$15
  163. # (matching Sonnet 4.6). Update this entry when the intro window closes.
  164. ("anthropic", _ANTHROPIC_URL, "anthropic-pricing-2026-06-intro", {
  165. "claude-sonnet-5": ("2.00", "10.00", "0.20", "2.50"),
  166. }),
  167. # Opus 5.5 cache hits are 0.05x input (every other Opus: 0.1x).
  168. ("anthropic", _ANTHROPIC_URL, "anthropic-pricing-2026-09", {
  169. "claude-opus-5": _OPUS,
  170. "claude-opus-5-5": _OPUS_5_5,
  171. }),
  172. ("openai", "https://openai.com/api/pricing/", "openai-pricing-2026-03-16", {
  173. "gpt-4o": ("2.50", "10.00", "1.25"), "gpt-4o-mini": ("0.15", "0.60", "0.075"),
  174. "gpt-4.1": ("2.00", "8.00", "0.50"), "gpt-4.1-mini": ("0.40", "1.60", "0.10"),
  175. "gpt-4.1-nano": ("0.10", "0.40", "0.025"), "o3": ("10.00", "40.00", "2.50"),
  176. "o3-mini": ("1.10", "4.40", "0.55"),
  177. }),
  178. # Off-peak USD rates (peak = 2x, Mon-Fri 01-04 + 06-10 UTC). ``deepseek-v4-flash`` and the
  179. # retired deepseek-chat / deepseek-reasoner aliases are served by V4.1-Flash at the Flash price.
  180. ("deepseek", "https://api-docs.deepseek.com/quick_start/pricing", "deepseek-pricing-2026-09-10", {
  181. ("deepseek-flash", "deepseek-v4-flash", "deepseek-chat", "deepseek-reasoner"): ("0.15", "0.60", "0.003"),
  182. "deepseek-v4-pro": ("0.66", "1.98", "0.022"),
  183. }),
  184. # Introductory rates through 2026-11-30; from 2026-12-01 they double ($4 / $20 / $0.20).
  185. ("google", "https://ai.google.dev/gemini-api/docs/pricing", "google-pricing-2026-10-13", {
  186. "gemini-4-argon": ("2.00", "10.00", "0.10"),
  187. }),
  188. ("google", "https://ai.google.dev/gemini-api/docs/pricing", "google-pricing-2026-09-02", {
  189. ("gemini-3.8-flash", "gemini-3.7-flash"): ("0.75", "3.75", "0.075"),
  190. }),
  191. ("google", "https://ai.google.dev/gemini-api/docs/pricing", "google-pricing-2026-07-28", {
  192. "gemini-3.6-flash": ("1.50", "7.50", "0.15"), "gemini-3.5-flash-lite": ("0.30", "2.50", "0.03"),
  193. }),
  194. ("google", _GOOGLE_URL, "google-pricing-2026-07-07", {
  195. "gemini-3.5-flash": ("1.50", "9.00", "0.15"), "gemini-3.1-flash-lite": ("0.25", "1.50", "0.025"),
  196. "gemini-3-pro-preview": ("2.00", "12.00", "0.20"), "gemini-3-flash-preview": ("0.50", "3.00", "0.05"),
  197. "gemini-2.5-flash": ("0.15", "0.60", "0.015"), "gemini-2.0-flash": ("0.10", "0.40", "0.01"),
  198. }),
  199. # AWS Bedrock on-demand: same per-token rates as the model provider, billed
  200. # through AWS. Current-gen Claude rows are commercial-list snapshots (the AWS
  201. # Price List API had not published these SKUs machine-readably).
  202. ("bedrock", _BEDROCK_URL, "anthropic-list-2026-07", {
  203. ("anthropic.claude-opus-4-8", "anthropic.claude-opus-4-7", "anthropic.claude-opus-4-6"): _OPUS,
  204. }),
  205. ("bedrock", _BEDROCK_URL, "anthropic-list-2026-09", {
  206. "anthropic.claude-opus-5": _OPUS, "anthropic.claude-opus-5-5": _OPUS_5_5,
  207. }),
  208. ("bedrock", _BEDROCK_URL, "bedrock-pricing-2026-06", {"anthropic.claude-sonnet-5": _SONNET}),
  209. ("bedrock", _BEDROCK_URL, "bedrock-pricing-2026-04", {
  210. ("anthropic.claude-sonnet-4-6", "anthropic.claude-sonnet-4-5"): _SONNET,
  211. "anthropic.claude-haiku-4-5": ("0.80", "4.00", "0.08", "1.00"),
  212. "amazon.nova-pro": ("0.80", "3.20"), "amazon.nova-lite": ("0.06", "0.24"), "amazon.nova-micro": ("0.035", "0.14"),
  213. }),
  214. ("minimax", None, "minimax-pricing-2026-04", {"minimax-m2.7": ("0.30", "1.20")}),
  215. ("minimax-cn", None, "minimax-pricing-2026-04", {"minimax-m2.7": ("0.30", "1.20")}),
  216. # Fireworks AI serverless (Standard tier) publishes a per-model cached_input
  217. # rate (→ cache_read) but no separate cache_write rate. Fast/turbo tiers are
  218. # exposed as accounts/fireworks/routers/<name>, so rsplit("/", 1) yields
  219. # these distinct ids with their own (higher) rates.
  220. ("fireworks", "https://docs.fireworks.ai/serverless/pricing", "fireworks-pricing-2026-07", {
  221. "kimi-k2p6": ("0.95", "4.00", "0.16"), "kimi-k2p7-code": ("0.95", "4.00", "0.19"),
  222. "glm-5p2": ("1.40", "4.40", "0.14"), "deepseek-v4-pro": ("1.74", "3.48", "0.145"),
  223. "deepseek-v4-flash": ("0.14", "0.28", "0.028"), "qwen3p7-plus": ("0.40", "1.60", "0.08"),
  224. "minimax-m3": ("0.30", "1.20", "0.06"), "gpt-oss-120b": ("0.15", "0.60", "0.015"),
  225. "gpt-oss-20b": ("0.07", "0.30", "0.035"), "glm-5p1": ("1.40", "4.40", "0.26"),
  226. "minimax-m2p7": ("0.30", "1.20", "0.06"),
  227. ("kimi-k2p6-fast", "kimi-k2p6-turbo"): ("2.00", "8.00", "0.30"),
  228. "kimi-k2p7-code-fast": ("1.90", "8.00", "0.38"), "glm-5p2-fast": ("2.10", "6.60", "0.21"),
  229. "glm-5p1-fast": ("2.80", "8.80", "0.52"),
  230. }),
  231. )
  232. _OFFICIAL_DOCS_PRICING: dict[tuple[str, str], PricingEntry] = {}
  233. for _provider, _url, _version, _rows in _SNAPSHOTS:
  234. for _models, _rates in _rows.items():
  235. _entry = _snap(*_rates, version=_version, url=_url)
  236. for _model in ((_models,) if isinstance(_models, str) else _models):
  237. _OFFICIAL_DOCS_PRICING[(_provider, _model)] = _entry
  238. del _SNAPSHOTS, _provider, _url, _version, _rows, _models, _rates, _entry, _model
  239. # GPT-6 Astra uses whole-request pricing above the 272K prompt tier. Keep this
  240. # account-gated model out of generic static catalogs, but retain published billing
  241. # metadata for an explicitly selected route.
  242. _OFFICIAL_DOCS_PRICING[("openai", "gpt-6-astra")] = _snap(
  243. "10.00", "50.00", "1.00", "12.50",
  244. url="https://developers.openai.com/api/docs/models/gpt-6-astra",
  245. version="openai-gpt-6-astra-2026-09",
  246. tier_threshold_tokens=272_000,
  247. input_cost_per_million_above=Decimal("20.00"),
  248. output_cost_per_million_above=Decimal("75.00"),
  249. cache_read_cost_per_million_above=Decimal("2.00"),
  250. cache_write_cost_per_million_above=Decimal("25.00"),
  251. )
  252. # GPT-6 Sol / Luna (the 5.6 Sol/Luna successors): same 272K whole-request tier as Astra
  253. # (2x input + cache, 1.5x output). Cache write = 1.25x input, cache read = 0.10x input.
  254. # Terra has no published model page yet, so it deliberately has no row.
  255. for _slug, _inp, _out, _read, _write, _inp_above, _out_above, _read_above, _write_above in (
  256. ("gpt-6-sol", "2.00", "10.00", "0.20", "2.50", "4.00", "15.00", "0.40", "5.00"),
  257. # 6.1 Sol: same input/output as 6 Sol, but cached input is 0.05x input (not 0.10x).
  258. ("gpt-6.1-sol", "2.00", "10.00", "0.10", "2.50", "4.00", "15.00", "0.20", "5.00"),
  259. ("gpt-6-luna", "0.10", "0.50", "0.01", "0.125", "0.20", "0.75", "0.02", "0.25"),
  260. ):
  261. _OFFICIAL_DOCS_PRICING[("openai", _slug)] = _snap(
  262. _inp, _out, _read, _write,
  263. url=f"https://developers.openai.com/api/docs/models/{_slug}",
  264. version="openai-gpt-6-tiers-2026-09",
  265. tier_threshold_tokens=272_000,
  266. input_cost_per_million_above=Decimal(_inp_above),
  267. output_cost_per_million_above=Decimal(_out_above),
  268. cache_read_cost_per_million_above=Decimal(_read_above),
  269. cache_write_cost_per_million_above=Decimal(_write_above),
  270. )
  271. del _slug, _inp, _out, _read, _write, _inp_above, _out_above, _read_above, _write_above
  272. # OpenAI Ultrafast (``service_tier: "ultrafast"``): 6x Standard on every bucket, same 272K
  273. # whole-request tier. Selected by the tier the response reports it was SERVED at (a request asking
  274. # for Ultrafast can be served at ``default``, and is then billed at Standard).
  275. _OPENAI_ULTRAFAST_PRICING: dict[str, PricingEntry] = {
  276. "gpt-6-astra": _snap(
  277. "60.00", "300.00", "6.00", "75.00",
  278. url="https://developers.openai.com/api/docs/pricing?latest-pricing=ultrafast",
  279. version="openai-ultrafast-2026-09",
  280. tier_threshold_tokens=272_000,
  281. input_cost_per_million_above=Decimal("120.00"),
  282. output_cost_per_million_above=Decimal("450.00"),
  283. cache_read_cost_per_million_above=Decimal("12.00"),
  284. cache_write_cost_per_million_above=Decimal("150.00"),
  285. ),
  286. "gpt-6.1-sol": _snap(
  287. "12.00", "60.00", "0.60", "15.00",
  288. url="https://developers.openai.com/api/docs/pricing?latest-pricing=ultrafast",
  289. version="openai-ultrafast-2026-10-08",
  290. tier_threshold_tokens=272_000,
  291. input_cost_per_million_above=Decimal("24.00"),
  292. output_cost_per_million_above=Decimal("90.00"),
  293. cache_read_cost_per_million_above=Decimal("1.20"),
  294. cache_write_cost_per_million_above=Decimal("30.00"),
  295. ),
  296. }
  297. # Context-tiered Gemini Pro: above 200k prompt tokens the *_above rates apply to
  298. # the whole request (see PricingEntry).
  299. _OFFICIAL_DOCS_PRICING[("google", "gemini-3.1-pro")] = _snap(
  300. "2.00", "12.00", "0.20", url=_GOOGLE_URL, version="google-pricing-2026-07-07",
  301. tier_threshold_tokens=200_000, input_cost_per_million_above=Decimal("4.00"),
  302. output_cost_per_million_above=Decimal("18.00"), cache_read_cost_per_million_above=Decimal("0.40"),
  303. )
  304. _OFFICIAL_DOCS_PRICING[("google", "gemini-2.5-pro")] = _snap(
  305. "1.25", "10.00", "0.125", url=_GOOGLE_URL, version="google-pricing-2026-07-07",
  306. tier_threshold_tokens=200_000, input_cost_per_million_above=Decimal("2.50"),
  307. output_cost_per_million_above=Decimal("15.00"),
  308. )
  309. # Anthropic fast mode (``speed: "fast"``): a premium on the whole context window, with the
  310. # prompt-caching multipliers applied on top. Selected per response by ``usage.speed``.
  311. _ANTHROPIC_FAST_MODE_PRICING: dict[str, PricingEntry] = {
  312. _model: _snap(*_rates, version="anthropic-fast-mode-2026-09", url=f"{_ANTHROPIC_URL}#fast-mode-pricing")
  313. for _models, _rates in (
  314. (("claude-opus-4-8", "claude-opus-5"), ("10.00", "50.00", "1.00", "12.50")),
  315. (("claude-opus-5-5",), ("8.00", "40.00", "0.40", "10.00")),
  316. )
  317. for _model in _models
  318. }
  319. del _BEDROCK_URL, _ANTHROPIC_URL, _GOOGLE_URL, _OPUS, _SONNET, _OPUS_5_5
  320. # GPT-5.6 / GPT-6 tier "-pro" high-effort variants bill at the base tier's per-token
  321. # rates (more tokens per task, not a higher rate); the Hermes-side "-900k" Codex
  322. # picker variants are the same model with the suffix stripped on the wire.
  323. # The direct Gemini provider emits preview IDs for two models; key the snapshot
  324. # by both the documented stable name and the emitted ID.
  325. for _provider, _alias, _canonical in (
  326. *(("openai", f"{m}-{suffix}", m)
  327. for m in ("gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna", "gpt-6-sol", "gpt-6-luna", "gpt-6.1-sol")
  328. for suffix in ("pro", "900k")),
  329. ("google", "gemini-3.1-pro-preview", "gemini-3.1-pro"),
  330. ("google", "gemini-3.1-flash-lite-preview", "gemini-3.1-flash-lite"),
  331. ):
  332. _OFFICIAL_DOCS_PRICING[(_provider, _alias)] = _OFFICIAL_DOCS_PRICING[(_provider, _canonical)]
  333. del _provider, _alias, _canonical
  334. def _to_decimal(value: Any) -> Optional[Decimal]:
  335. try:
  336. return None if value is None else Decimal(str(value))
  337. except Exception:
  338. return None
  339. def _usage_field(obj: Any, *path: str) -> int:
  340. """Non-negative int at ``obj.path[0].path[1]...``; 0 if any hop is falsy or
  341. non-numeric. Hops read dicts and attribute objects alike (the Responses API
  342. returns either); negative counters from providers are clamped so they cannot
  343. corrupt session accounting."""
  344. for hop in path:
  345. if not obj:
  346. return 0
  347. obj = obj.get(hop, 0) if isinstance(obj, dict) else getattr(obj, hop, 0)
  348. try:
  349. return max(0, int(obj or 0))
  350. except Exception:
  351. return 0
  352. def _first_nonzero(obj: Any, *paths: tuple[str, ...]) -> int:
  353. """First non-zero ``_usage_field`` across candidate paths, else 0."""
  354. return next((v for v in (_usage_field(obj, *path) for path in paths) if v), 0)
  355. # Picker slugs → snapshot provider key ("openai-api" is the slug for direct
  356. # api.openai.com). Google and Fireworks are matched by name OR host below.
  357. _SNAPSHOT_PROVIDER_ALIASES = {
  358. "anthropic": "anthropic", "openai": "openai", "openai-api": "openai", "minimax": "minimax", "minimax-cn": "minimax-cn",
  359. }
  360. # AI Studio and Vertex host the same Gemini models (the Vertex "google/" vendor
  361. # prefix is stripped with the rest of the path).
  362. _GOOGLE_PROVIDER_NAMES = {"google", "gemini", "vertex", "google-gemini", "google-ai-studio", "google-vertex", "vertex-ai"}
  363. def resolve_billing_route(
  364. model_name: str, provider: Optional[str] = None, base_url: Optional[str] = None
  365. ) -> BillingRoute:
  366. provider_name = (provider or "").strip().lower()
  367. base = (base_url or "").strip().lower()
  368. model = (model_name or "").strip()
  369. if not provider_name and "/" in model:
  370. inferred_provider, bare_model = model.split("/", 1)
  371. if inferred_provider in {"anthropic", "openai", "google"}:
  372. provider_name = inferred_provider
  373. model = bare_model
  374. url = base_url or ""
  375. # Fireworks ids look like accounts/fireworks/models/<name>; keys use <name>.
  376. # Every other snapshot provider keys on the last path segment as well.
  377. bare = model.split("/")[-1]
  378. def host(name: str) -> bool:
  379. return base_url_host_matches(url, name)
  380. if provider_name == "openai-codex":
  381. return BillingRoute(provider="openai-codex", model=model, base_url=url, billing_mode="subscription_included")
  382. if provider_name == "openrouter" or host("openrouter.ai"):
  383. return BillingRoute(provider="openrouter", model=model, base_url=url, billing_mode="official_models_api")
  384. if provider_name == "nous" or host("inference-api.nousresearch.com"):
  385. return BillingRoute(provider="nous", model=model, base_url=base_url or _NOUS_DEFAULT_BASE_URL, billing_mode="official_models_api")
  386. snapshot_provider = _SNAPSHOT_PROVIDER_ALIASES.get(provider_name)
  387. if snapshot_provider is None:
  388. if (
  389. provider_name in _GOOGLE_PROVIDER_NAMES
  390. or host("aiplatform.googleapis.com") or host("generativelanguage.googleapis.com")
  391. ):
  392. snapshot_provider = "google"
  393. elif provider_name == "fireworks" or host("api.fireworks.ai"):
  394. snapshot_provider = "fireworks"
  395. if snapshot_provider:
  396. if snapshot_provider == "openai" and (not url or host("api.openai.com")):
  397. bare = strip_codex_context_variant_suffix(bare)
  398. return BillingRoute(provider=snapshot_provider, model=bare, base_url=url, billing_mode="official_docs_snapshot")
  399. if provider_name in {"custom", "local"} or (base and base_url_hostname(base) in ("localhost", "127.0.0.1")):
  400. return BillingRoute(provider=provider_name or "custom", model=model, base_url=url, billing_mode="unknown")
  401. return BillingRoute(provider=provider_name or "unknown", model=bare if model else "", base_url=url, billing_mode="unknown")
  402. _BEDROCK_REGION_PREFIXES = ("global.", "us.", "eu.", "apac.", "ap.", "au.", "jp.", "ca.", "sa.", "me.", "af.")
  403. # Bedrock ids end in documented date/revision/profile components (``-20250514-v1:0``).
  404. _BEDROCK_TRAILERS = (r":\d+$", r"-v\d+$", r"-\d{8}$")
  405. def _strip_prefix(name: str, prefixes: tuple[str, ...]) -> str:
  406. """Drop the first matching prefix (at most one), else return ``name`` unchanged."""
  407. return next((name[len(p):] for p in prefixes if name.startswith(p)), name)
  408. def _normalize_bedrock_model_name(model: str) -> str:
  409. """Bare foundation-model id: strip the cross-region inference-profile scope
  410. (``us.``/``global.``/...), map dotted versions (``4.7`` → ``4-7``), then
  411. strip the trailing date/revision/profile components."""
  412. name = re.sub(r"(\d+)\.(\d+)", r"\1-\2", _strip_prefix(model.lower().strip(), _BEDROCK_REGION_PREFIXES))
  413. for pattern in _BEDROCK_TRAILERS:
  414. name = re.sub(pattern, "", name)
  415. return name
  416. def _normalize_anthropic_model_name(model: str) -> str:
  417. """Strip an ``anthropic/`` prefix and map dotted versions (4.7 → 4-7)."""
  418. return re.sub(r"(\d+)\.(\d+)", r"\1-\2", _strip_prefix(model.lower().strip(), ("anthropic/",)))
  419. # Anthropic dot-notation (opus-4.7) and Bedrock region-prefixed ids need
  420. # normalizing before a second lookup.
  421. _MODEL_NORMALIZERS = {"anthropic": _normalize_anthropic_model_name, "bedrock": _normalize_bedrock_model_name}
  422. def _lookup_official_docs_pricing(route: BillingRoute) -> Optional[PricingEntry]:
  423. model = route.model.lower()
  424. entry = _OFFICIAL_DOCS_PRICING.get((route.provider, model))
  425. if entry:
  426. return entry
  427. normalize = _MODEL_NORMALIZERS.get(route.provider)
  428. normalized = normalize(model) if normalize else model
  429. return _OFFICIAL_DOCS_PRICING.get((route.provider, normalized)) if normalized != model else None
  430. def with_served_service_tier(usage: CanonicalUsage, response: Any) -> CanonicalUsage:
  431. """``usage`` with the response's served ``service_tier`` folded into ``raw_usage``. OpenAI reports
  432. the tier on the response, not inside ``usage``, and pricing reads it from ``raw_usage``."""
  433. tier = getattr(response, "service_tier", None)
  434. if not isinstance(tier, str) or not tier.strip():
  435. return usage
  436. return replace(usage, raw_usage={**(usage.raw_usage or {}), "service_tier": tier.strip().lower()})
  437. def _served_openai_tier(usage: CanonicalUsage) -> Optional[str]:
  438. return usage.raw_usage.get("service_tier") if isinstance(usage.raw_usage, dict) else None
  439. def _served_fast(usage: CanonicalUsage) -> bool:
  440. """Anthropic names the speed that served a fast-mode request in ``usage.speed``."""
  441. return isinstance(usage.raw_usage, dict) and usage.raw_usage.get("speed") == "fast"
  442. def _anthropic_fast_mode_entry(model: str) -> Optional[PricingEntry]:
  443. name = model.lower()
  444. return _ANTHROPIC_FAST_MODE_PRICING.get(name) or _ANTHROPIC_FAST_MODE_PRICING.get(
  445. _normalize_anthropic_model_name(name))
  446. def _openrouter_pricing_entry(route: BillingRoute) -> Optional[PricingEntry]:
  447. return _pricing_entry_from_metadata(
  448. fetch_model_metadata(), route.model,
  449. source_url="https://openrouter.ai/docs/api/api-reference/models/get-models",
  450. pricing_version="openrouter-models-api",
  451. )
  452. def _pricing_entry_from_metadata(
  453. metadata: dict[str, dict[str, Any]], model_id: str, *, source_url: str, pricing_version: str
  454. ) -> Optional[PricingEntry]:
  455. if model_id not in metadata:
  456. return None
  457. pricing = metadata[model_id].get("pricing") or {}
  458. rejected: set[str] = set()
  459. def rate(key: str, *aliases: str) -> Optional[Decimal]:
  460. raw = pricing.get(key)
  461. for alias in aliases: # alias chain is truthiness-based (``a or b or c``)
  462. raw = raw or pricing.get(alias)
  463. value = _to_decimal(raw)
  464. # A negative rate is a "pricing varies" sentinel (OpenRouter publishes "-1" for
  465. # openrouter/auto), not a price: priced through, it books huge negative session costs.
  466. # NaN raises in the cost sum and Infinity books an infinite cost, so neither is a price.
  467. if value is not None and (not value.is_finite() or value < 0):
  468. logger.debug("Ignoring unusable %s rate %s for %s (variable-pricing sentinel)", key, raw, model_id)
  469. rejected.add(key)
  470. return None
  471. return value
  472. def per_million(key: str, *aliases: str) -> Optional[Decimal]:
  473. value = rate(key, *aliases)
  474. return None if value is None else value * _ONE_MILLION
  475. prompt = per_million("prompt")
  476. completion = per_million("completion")
  477. request = rate("request")
  478. # A missing request rate means "no per-request fee"; a sentinel one means the fee is unknown,
  479. # and dropping it would under-count, so the whole entry is unknown.
  480. if "request" in rejected or (prompt is None and completion is None and request is None):
  481. return None
  482. return PricingEntry(
  483. input_cost_per_million=prompt, output_cost_per_million=completion,
  484. cache_read_cost_per_million=per_million("cache_read", "cached_prompt", "input_cache_read"),
  485. cache_write_cost_per_million=per_million("cache_write", "cache_creation", "input_cache_write"),
  486. request_cost=request, source="provider_models_api", source_url=source_url,
  487. pricing_version=pricing_version, fetched_at=_UTC_NOW(),
  488. )
  489. def _models_dev_pricing_entry(route: BillingRoute) -> Optional[PricingEntry]:
  490. """models.dev list price for a direct first-party route (see ``_MODELS_DEV_DIRECT_HOSTS``)."""
  491. domain = _MODELS_DEV_DIRECT_HOSTS.get(route.provider)
  492. if not domain or not route.model:
  493. return None
  494. if route.base_url:
  495. scheme, host, port = base_url_origin(route.base_url)
  496. if (scheme, port) != ("https", 443) or not (host == domain or host.endswith("." + domain)):
  497. return None
  498. from agent.models_dev import get_model_info
  499. model_info = get_model_info(route.provider, route.model)
  500. if model_info is None or not model_info.has_cost_data():
  501. return None
  502. return PricingEntry(
  503. input_cost_per_million=_to_decimal(model_info.cost_input),
  504. output_cost_per_million=_to_decimal(model_info.cost_output),
  505. cache_read_cost_per_million=_to_decimal(model_info.cost_cache_read),
  506. cache_write_cost_per_million=_to_decimal(model_info.cost_cache_write),
  507. source="provider_models_api", source_url="https://models.dev", pricing_version="models.dev",
  508. fetched_at=_UTC_NOW(),
  509. )
  510. def get_pricing_entry(
  511. model_name: str, provider: Optional[str] = None, base_url: Optional[str] = None,
  512. api_key: Optional[str] = None,
  513. ) -> Optional[PricingEntry]:
  514. route = resolve_billing_route(model_name, provider=provider, base_url=base_url)
  515. if route.billing_mode == "subscription_included":
  516. return _INCLUDED_ENTRY
  517. if route.provider == "openrouter":
  518. return _openrouter_pricing_entry(route)
  519. bundled_entry = _lookup_official_docs_pricing(route)
  520. if bundled_entry:
  521. return bundled_entry
  522. if route.base_url:
  523. entry = _pricing_entry_from_metadata(
  524. fetch_endpoint_model_metadata(route.base_url, api_key=api_key or ""), route.model,
  525. source_url=f"{route.base_url.rstrip('/')}/models",
  526. pricing_version="openai-compatible-models-api",
  527. )
  528. if entry:
  529. return entry
  530. return _models_dev_pricing_entry(route)
  531. # Usage-field candidate paths per API shape: (input/prompt total, output, cache
  532. # read, cache write); the first non-zero path wins.
  533. _ANTHROPIC_USAGE_SHAPE = (
  534. (("input_tokens",),), (("output_tokens",),), (("cache_read_input_tokens",),), (("cache_creation_input_tokens",),)
  535. )
  536. # OpenAI's documented GPT-5.6+ field is `cache_write_tokens` (billed at 1.25x);
  537. # `cache_creation_tokens` is a fallback for older endpoints.
  538. _CODEX_USAGE_SHAPE = (
  539. (("input_tokens",),), (("output_tokens",),), (("input_tokens_details", "cached_tokens"),),
  540. (("input_tokens_details", "cache_write_tokens"), ("input_tokens_details", "cache_creation_tokens")),
  541. )
  542. # OpenAI-style names first, then Anthropic-style: local OpenAI-compatible
  543. # servers (e.g. mlx_vlm.server) emit input_tokens/output_tokens and the OpenAI
  544. # client preserves them as extra attributes. Cache reads: nested OpenAI shape,
  545. # then Anthropic-style top-level fields exposed by proxies routing Claude
  546. # (OpenRouter, Vercel AI Gateway, Cline), then DeepSeek's prompt_cache_hit_tokens,
  547. # then Kimi/Moonshot's cached_tokens — without these, direct sessions show 0
  548. # hits and bill hits at the full input rate.
  549. _CHAT_USAGE_SHAPE = (
  550. (("prompt_tokens",), ("input_tokens",)),
  551. (("completion_tokens",), ("output_tokens",)),
  552. (("prompt_tokens_details", "cached_tokens"), ("cache_read_input_tokens",), ("prompt_cache_hit_tokens",), ("cached_tokens",)),
  553. (("prompt_tokens_details", "cache_write_tokens"), ("prompt_tokens_details", "cache_creation_input_tokens"),
  554. ("cache_creation_input_tokens",), ("cache_write_tokens",)),
  555. )
  556. def normalize_usage(
  557. response_usage: Any, *, provider: Optional[str] = None, api_mode: Optional[str] = None
  558. ) -> CanonicalUsage:
  559. """Normalize raw API response usage into canonical token buckets (Anthropic,
  560. Codex Responses, or OpenAI Chat Completions shape)."""
  561. if not response_usage:
  562. return CanonicalUsage()
  563. provider_name = (provider or "").strip().lower()
  564. mode = (api_mode or "").strip().lower()
  565. u = response_usage
  566. if mode == "anthropic_messages" or provider_name == "anthropic":
  567. shape = _ANTHROPIC_USAGE_SHAPE
  568. elif mode == "codex_responses":
  569. shape = _CODEX_USAGE_SHAPE
  570. else:
  571. shape = _CHAT_USAGE_SHAPE
  572. prompt_total, output_tokens, cache_read_tokens, cache_write_tokens = (
  573. _first_nonzero(u, *paths) for paths in shape
  574. )
  575. # Anthropic reports uncached input directly; Codex/Chat totals INCLUDE
  576. # cached tokens, so the cache buckets are subtracted back out.
  577. input_tokens = prompt_total if shape is _ANTHROPIC_USAGE_SHAPE else max(
  578. 0, prompt_total - cache_read_tokens - cache_write_tokens
  579. )
  580. # Responses API: output_tokens_details.reasoning_tokens. Chat Completions
  581. # (OpenAI, OpenRouter, DeepSeek, ...): completion_tokens_details.reasoning_tokens.
  582. # Hidden thinking dominates output spend on reasoning models, so read both.
  583. reasoning_tokens = _first_nonzero(
  584. u, ("output_tokens_details", "reasoning_tokens"), ("completion_tokens_details", "reasoning_tokens")
  585. )
  586. # On MiniMax-M3's Anthropic wire, cache_read_input_tokens carries a constant
  587. # +128 floor and cache_creation is always 0, so cache_read is not a reliable
  588. # hit signal; the input_tokens drop between consecutive calls is.
  589. # Docs: https://platform.minimax.io/docs/api-reference/text-prompt-caching
  590. if provider_name in {"minimax", "minimax-cn"} and mode == "anthropic_messages":
  591. logger.debug(
  592. "cache_observability provider=%s mode=%s input_tokens=%s "
  593. "output_tokens=%s cache_read_tokens=%s cache_write_tokens=%s "
  594. "(note: on MiniMax-M3 cache_read carries a +128 constant "
  595. "floor and is not a reliable hit signal — track input_tokens "
  596. "drops across calls instead)",
  597. provider_name, mode, input_tokens, output_tokens,
  598. cache_read_tokens, cache_write_tokens,
  599. )
  600. return CanonicalUsage(
  601. input_tokens=input_tokens, output_tokens=output_tokens, cache_read_tokens=cache_read_tokens,
  602. cache_write_tokens=cache_write_tokens, reasoning_tokens=reasoning_tokens,
  603. raw_usage=dict(u) if isinstance(u, dict) else (u.model_dump() if callable(getattr(u, 'model_dump', None)) else None),
  604. )
  605. def _unknown_cost(source: CostSource, *notes: str) -> CostResult:
  606. return CostResult(amount_usd=None, status="unknown", source=source, label="n/a", notes=notes)
  607. def estimate_usage_cost(
  608. model_name: str, usage: CanonicalUsage, *, provider: Optional[str] = None,
  609. base_url: Optional[str] = None, api_key: Optional[str] = None,
  610. ) -> CostResult:
  611. from providers import get_provider_profile
  612. profile = get_provider_profile(provider or '')
  613. reported = profile.get_usage_cost(model_name, usage) if profile else None
  614. if reported is not None:
  615. return reported
  616. route = resolve_billing_route(model_name, provider=provider, base_url=base_url)
  617. if route.billing_mode == "subscription_included":
  618. return CostResult(
  619. amount_usd=_ZERO, status="included", source="none", label="included",
  620. pricing_version="included-route", notes=(_INCLUDED_NOTE,),
  621. )
  622. entry = get_pricing_entry(model_name, provider=provider, base_url=base_url, api_key=api_key)
  623. if route.provider == "anthropic" and _served_fast(usage):
  624. entry = _anthropic_fast_mode_entry(route.model)
  625. if not entry:
  626. return _unknown_cost("official_docs_snapshot", "fast-mode pricing unavailable for model")
  627. if route.provider == "openai" and _served_openai_tier(usage) == "ultrafast":
  628. entry = _OPENAI_ULTRAFAST_PRICING.get(route.model)
  629. if not entry:
  630. return _unknown_cost("official_docs_snapshot", "ultrafast pricing unavailable for model")
  631. if not entry:
  632. return _unknown_cost("none")
  633. # Whole-request context tier (e.g. Gemini Pro >200k prompts): above the
  634. # threshold the *_above rates apply to the entire request; None falls back.
  635. above = entry.tier_threshold_tokens is not None and usage.prompt_tokens > entry.tier_threshold_tokens
  636. amount = _ZERO
  637. for tokens, rate, rate_above, note in (
  638. (usage.input_tokens, entry.input_cost_per_million, entry.input_cost_per_million_above, ()),
  639. (usage.output_tokens, entry.output_cost_per_million, entry.output_cost_per_million_above, ()),
  640. (usage.cache_read_tokens, entry.cache_read_cost_per_million, entry.cache_read_cost_per_million_above,
  641. ("cache-read pricing unavailable for route",)),
  642. (usage.cache_write_tokens, entry.cache_write_cost_per_million, entry.cache_write_cost_per_million_above,
  643. ("cache-write pricing unavailable for route",)),
  644. ):
  645. if above and rate_above is not None:
  646. rate = rate_above
  647. if rate is None:
  648. if tokens:
  649. return _unknown_cost(entry.source, *note)
  650. continue
  651. amount += Decimal(tokens) * rate / _ONE_MILLION
  652. if entry.request_cost is not None and usage.request_count:
  653. amount += Decimal(usage.request_count) * entry.request_cost
  654. notes: list[str] = []
  655. status: CostStatus = "estimated"
  656. label = format_cost_label(amount)
  657. if entry.source == "none" and amount == _ZERO:
  658. status = "included"
  659. label = "included"
  660. notes.append(_INCLUDED_NOTE)
  661. if route.provider == "openrouter":
  662. notes.append("OpenRouter cost is estimated from the models API until reconciled.")
  663. return CostResult(
  664. amount_usd=amount, status=status, source=entry.source, label=label,
  665. fetched_at=entry.fetched_at, pricing_version=entry.pricing_version, notes=tuple(notes),
  666. )
  667. def has_known_pricing(
  668. model_name: str, provider: Optional[str] = None, base_url: Optional[str] = None,
  669. api_key: Optional[str] = None,
  670. ) -> bool:
  671. """True if pricing data exists for this model+route (direct lookup, no dummy usage)."""
  672. return get_pricing_entry(model_name, provider=provider, base_url=base_url, api_key=api_key) is not None
  673. def format_duration_compact(seconds: float) -> str:
  674. if seconds < 60:
  675. return f"{seconds:.0f}s"
  676. minutes = seconds / 60
  677. if minutes < 60:
  678. return f"{minutes:.0f}m"
  679. hours = minutes / 60
  680. if hours < 24:
  681. remaining_min = int(minutes % 60)
  682. return f"{int(hours)}h {remaining_min}m" if remaining_min else f"{int(hours)}h"
  683. return f"{hours / 24:.1f}d"
  684. def format_token_count_compact(value: int) -> str:
  685. abs_value = abs(int(value))
  686. if abs_value < 1_000:
  687. return str(int(value))
  688. sign = "-" if value < 0 else ""
  689. threshold, suffix = next((t, sfx) for t, sfx in ((1_000_000_000, "B"), (1_000_000, "M"), (1_000, "K")) if abs_value >= t)
  690. scaled = abs_value / threshold
  691. text = f"{scaled:.2f}" if scaled < 10 else f"{scaled:.1f}" if scaled < 100 else f"{scaled:.0f}"
  692. if "." in text:
  693. text = text.rstrip("0").rstrip(".")
  694. return f"{sign}{text}{suffix}"