|
| 1 | +"""Model pricing table for estimating trial cost from token counts. |
| 2 | +
|
| 3 | +Harbor does not populate ``cost_usd`` on AgentContext for every provider, so |
| 4 | +we fall back to a static pricing table to estimate cost from the captured |
| 5 | +input / cache / output token counts. Prices are per-token (not per-million) |
| 6 | +and sourced from LiteLLM's ``model_prices_and_context_window.json`` reference. |
| 7 | +
|
| 8 | +Model-name matching uses substring patterns because trajectory / agent data |
| 9 | +uses varying formats (e.g. ``claude-sonnet-4-5-20250929``, |
| 10 | +``anthropic/claude-opus-4-5``, ``bedrock/global.anthropic.claude-sonnet-4-5``, |
| 11 | +``gpt-5.1-codex``). More-specific patterns MUST appear before less-specific |
| 12 | +ones to avoid false substring matches (e.g. ``gpt-5-mini`` before ``gpt-5``). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | + |
| 19 | + |
| 20 | +@dataclass(frozen=True) |
| 21 | +class ModelPricing: |
| 22 | + """Per-token pricing for a single model family.""" |
| 23 | + |
| 24 | + input: float |
| 25 | + output: float |
| 26 | + cache_read: float | None = None |
| 27 | + |
| 28 | + |
| 29 | +PRICING_TABLE: list[tuple[str, ModelPricing]] = [ |
| 30 | + # Anthropic — Claude 4.x family. Opus 4.5 dropped in price vs 4 / 4.1. |
| 31 | + ("claude-opus-4-5", ModelPricing(input=5e-6, output=25e-6, cache_read=5e-7)), |
| 32 | + ("claude-opus-4-1", ModelPricing(input=15e-6, output=75e-6, cache_read=1.5e-6)), |
| 33 | + ("claude-opus-4", ModelPricing(input=15e-6, output=75e-6, cache_read=1.5e-6)), |
| 34 | + ("claude-sonnet-4-5", ModelPricing(input=3e-6, output=15e-6, cache_read=3e-7)), |
| 35 | + ("claude-sonnet-4", ModelPricing(input=3e-6, output=15e-6, cache_read=3e-7)), |
| 36 | + ("claude-haiku-4-5", ModelPricing(input=1e-6, output=5e-6, cache_read=1e-7)), |
| 37 | + ("claude-haiku-4", ModelPricing(input=1e-6, output=5e-6, cache_read=1e-7)), |
| 38 | + # Anthropic — older Claude 3.x still showing up in legacy runs. |
| 39 | + ("claude-3-7-sonnet", ModelPricing(input=3e-6, output=15e-6, cache_read=3e-7)), |
| 40 | + ("claude-3-5-sonnet", ModelPricing(input=3e-6, output=15e-6, cache_read=3e-7)), |
| 41 | + ("claude-3-5-haiku", ModelPricing(input=8e-7, output=4e-6, cache_read=8e-8)), |
| 42 | + ("claude-3.5-sonnet", ModelPricing(input=3e-6, output=15e-6, cache_read=3e-7)), |
| 43 | + ("claude-3.5-haiku", ModelPricing(input=8e-7, output=4e-6, cache_read=8e-8)), |
| 44 | + ("claude-3-opus", ModelPricing(input=15e-6, output=75e-6, cache_read=1.5e-6)), |
| 45 | + # Google — Gemini 3.x |
| 46 | + ("gemini-3-pro", ModelPricing(input=2e-6, output=12e-6, cache_read=2e-7)), |
| 47 | + ("gemini-3-flash", ModelPricing(input=5e-7, output=3e-6, cache_read=5e-8)), |
| 48 | + # Google — Gemini 2.5 |
| 49 | + ("gemini-2.5-flash-lite", ModelPricing(input=1e-7, output=4e-7, cache_read=1e-8)), |
| 50 | + ("gemini-2.5-flash", ModelPricing(input=3e-7, output=2.5e-6, cache_read=3e-8)), |
| 51 | + ("gemini-2.5-pro", ModelPricing(input=1.25e-6, output=10e-6, cache_read=1.25e-7)), |
| 52 | + # OpenAI — GPT-5.x |
| 53 | + ("gpt-5.1-codex-mini", ModelPricing(input=2.5e-7, output=2e-6, cache_read=2.5e-8)), |
| 54 | + ("gpt-5.1-codex", ModelPricing(input=1.25e-6, output=10e-6, cache_read=1.25e-7)), |
| 55 | + ("gpt-5.1", ModelPricing(input=1.25e-6, output=10e-6, cache_read=1.25e-7)), |
| 56 | + ("gpt-5-codex", ModelPricing(input=1.25e-6, output=10e-6, cache_read=1.25e-7)), |
| 57 | + ("gpt-5-mini", ModelPricing(input=2.5e-7, output=2e-6, cache_read=2.5e-8)), |
| 58 | + ("gpt-5-nano", ModelPricing(input=5e-8, output=4e-7, cache_read=5e-9)), |
| 59 | + ("gpt-5-pro", ModelPricing(input=15e-6, output=120e-6)), |
| 60 | + ("gpt-5", ModelPricing(input=1.25e-6, output=10e-6, cache_read=1.25e-7)), |
| 61 | + # OpenAI — GPT-4.1 / 4o. |
| 62 | + ("gpt-4.1-mini", ModelPricing(input=4e-7, output=1.6e-6, cache_read=1e-7)), |
| 63 | + ("gpt-4.1-nano", ModelPricing(input=1e-7, output=4e-7, cache_read=2.5e-8)), |
| 64 | + ("gpt-4.1", ModelPricing(input=2e-6, output=8e-6, cache_read=5e-7)), |
| 65 | + ("gpt-4o-mini", ModelPricing(input=1.5e-7, output=6e-7, cache_read=7.5e-8)), |
| 66 | + ("gpt-4o", ModelPricing(input=2.5e-6, output=10e-6, cache_read=1.25e-6)), |
| 67 | + # OpenAI — reasoning. |
| 68 | + ("o4-mini", ModelPricing(input=1.1e-6, output=4.4e-6, cache_read=2.75e-7)), |
| 69 | + ("o3-pro", ModelPricing(input=20e-6, output=80e-6)), |
| 70 | + ("o3-mini", ModelPricing(input=1.1e-6, output=4.4e-6, cache_read=5.5e-7)), |
| 71 | + ("o3", ModelPricing(input=2e-6, output=8e-6, cache_read=5e-7)), |
| 72 | + # OpenAI — standalone codex endpoint. |
| 73 | + ("codex-mini", ModelPricing(input=1.5e-6, output=6e-6, cache_read=3.75e-7)), |
| 74 | +] |
| 75 | + |
| 76 | + |
| 77 | +def _find_pricing(model_name: str) -> ModelPricing | None: |
| 78 | + lower = model_name.lower() |
| 79 | + for pattern, pricing in PRICING_TABLE: |
| 80 | + if pattern in lower: |
| 81 | + return pricing |
| 82 | + return None |
| 83 | + |
| 84 | + |
| 85 | +def has_pricing(model_name: str | None) -> bool: |
| 86 | + """Return True iff the pricing table has an entry for this model.""" |
| 87 | + if not model_name: |
| 88 | + return False |
| 89 | + return _find_pricing(model_name) is not None |
| 90 | + |
| 91 | + |
| 92 | +def estimate_cost_usd( |
| 93 | + model_name: str | None, |
| 94 | + input_tokens: int | None, |
| 95 | + output_tokens: int | None, |
| 96 | + cached_tokens: int | None = None, |
| 97 | +) -> float | None: |
| 98 | + """Estimate USD cost from token counts and a model name. |
| 99 | +
|
| 100 | + ``cached_tokens`` is the subset of ``input_tokens`` that were served from |
| 101 | + prompt cache; those tokens are billed at ``cache_read`` instead of the |
| 102 | + full input rate. Returns ``None`` when the model is not in the pricing |
| 103 | + table or there are no tokens to price. |
| 104 | + """ |
| 105 | + if not model_name: |
| 106 | + return None |
| 107 | + input_total = int(input_tokens or 0) |
| 108 | + output_total = int(output_tokens or 0) |
| 109 | + if input_total == 0 and output_total == 0: |
| 110 | + return None |
| 111 | + pricing = _find_pricing(model_name) |
| 112 | + if pricing is None: |
| 113 | + return None |
| 114 | + |
| 115 | + cached = int(cached_tokens or 0) |
| 116 | + uncached_input = max(0, input_total - cached) |
| 117 | + cache_rate = pricing.cache_read if pricing.cache_read is not None else pricing.input |
| 118 | + |
| 119 | + return ( |
| 120 | + uncached_input * pricing.input |
| 121 | + + cached * cache_rate |
| 122 | + + output_total * pricing.output |
| 123 | + ) |
0 commit comments