|
| 1 | +"""Per-model price tables and cost computation. |
| 2 | +
|
| 3 | +The runner uses this to fill `Trace.metrics.cost_usd` when an adapter reported |
| 4 | +token counts but no $ figure. Ships `DEFAULT_PRICE_TABLE` with a small set of |
| 5 | +current-generation models and a `freshness_date`; the runner emits a single |
| 6 | +warning per run when the default is in use so the staleness is visible. Users |
| 7 | +override via `eval.yaml > metrics.price_table_path`. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +from datetime import date |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import yaml |
| 17 | +from pydantic import BaseModel, ConfigDict, Field |
| 18 | + |
| 19 | +from eval_harness.core.errors import ConfigError |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | +_FORBID = ConfigDict(extra="forbid") |
| 24 | + |
| 25 | + |
| 26 | +class ModelPrice(BaseModel): |
| 27 | + """Per-million-token pricing for one model. |
| 28 | +
|
| 29 | + Thinking tokens (extended-thinking / reasoning) are billed separately on |
| 30 | + some providers; default to 0.0 when a model has no thinking surcharge. |
| 31 | + """ |
| 32 | + |
| 33 | + model_config = _FORBID |
| 34 | + input_per_million_tokens: float |
| 35 | + output_per_million_tokens: float |
| 36 | + thinking_per_million_tokens: float = 0.0 |
| 37 | + |
| 38 | + |
| 39 | +class PriceTable(BaseModel): |
| 40 | + """Versioned, dated price table. |
| 41 | +
|
| 42 | + `freshness_date` is the day prices were verified against provider |
| 43 | + documentation. The runner warns once per run when `DEFAULT_PRICE_TABLE` is |
| 44 | + in use so users know the cost figures may be stale. |
| 45 | + """ |
| 46 | + |
| 47 | + model_config = _FORBID |
| 48 | + table_version: str |
| 49 | + freshness_date: date |
| 50 | + models: dict[str, ModelPrice] = Field(default_factory=dict) |
| 51 | + |
| 52 | + |
| 53 | +# Sources verified 2026-05-12 against provider docs: |
| 54 | +# - Anthropic: https://www.anthropic.com/pricing#anthropic-api |
| 55 | +# - OpenAI: https://openai.com/api/pricing/ |
| 56 | +# Prices are USD per million tokens. Thinking-token pricing covers extended |
| 57 | +# reasoning where the provider bills a distinct rate (e.g. Anthropic 1M |
| 58 | +# extended thinking pricing matches output rate at time of capture). |
| 59 | +DEFAULT_PRICE_TABLE = PriceTable( |
| 60 | + table_version="2026-05-12", |
| 61 | + freshness_date=date(2026, 5, 12), |
| 62 | + models={ |
| 63 | + "claude-opus-4-7": ModelPrice( |
| 64 | + input_per_million_tokens=15.0, |
| 65 | + output_per_million_tokens=75.0, |
| 66 | + thinking_per_million_tokens=75.0, |
| 67 | + ), |
| 68 | + "claude-sonnet-4-6": ModelPrice( |
| 69 | + input_per_million_tokens=3.0, |
| 70 | + output_per_million_tokens=15.0, |
| 71 | + thinking_per_million_tokens=15.0, |
| 72 | + ), |
| 73 | + "claude-haiku-4-5-20251001": ModelPrice( |
| 74 | + input_per_million_tokens=0.25, |
| 75 | + output_per_million_tokens=1.25, |
| 76 | + ), |
| 77 | + "claude-4-7": ModelPrice( |
| 78 | + input_per_million_tokens=3.0, |
| 79 | + output_per_million_tokens=15.0, |
| 80 | + thinking_per_million_tokens=15.0, |
| 81 | + ), |
| 82 | + "gpt-5": ModelPrice( |
| 83 | + input_per_million_tokens=5.0, |
| 84 | + output_per_million_tokens=15.0, |
| 85 | + ), |
| 86 | + }, |
| 87 | +) |
| 88 | + |
| 89 | + |
| 90 | +def load_price_table(path: Path | None) -> PriceTable: |
| 91 | + """Load a price table from YAML; `None` returns `DEFAULT_PRICE_TABLE`.""" |
| 92 | + if path is None: |
| 93 | + return DEFAULT_PRICE_TABLE |
| 94 | + if not path.exists(): |
| 95 | + raise ConfigError(f"price_table_path does not exist: {path}") |
| 96 | + try: |
| 97 | + data = yaml.safe_load(path.read_text()) |
| 98 | + except yaml.YAMLError as e: |
| 99 | + raise ConfigError(f"price_table_path is not valid YAML ({path}): {e}") from e |
| 100 | + if not isinstance(data, dict): |
| 101 | + raise ConfigError( |
| 102 | + f"price_table_path must be a YAML mapping ({path}); got {type(data).__name__}" |
| 103 | + ) |
| 104 | + return PriceTable.model_validate(data) |
| 105 | + |
| 106 | + |
| 107 | +def compute_cost( |
| 108 | + table: PriceTable, |
| 109 | + model: str, |
| 110 | + token_input: int, |
| 111 | + token_output: int, |
| 112 | + token_thinking: int = 0, |
| 113 | +) -> float | None: |
| 114 | + """Compute the call's $ cost from token counts. Returns `None` when the |
| 115 | + model is not in the table — callers handle the gap (e.g. leave |
| 116 | + `Trace.metrics.cost_usd` as `None`).""" |
| 117 | + price = table.models.get(model) |
| 118 | + if price is None: |
| 119 | + return None |
| 120 | + return ( |
| 121 | + (token_input / 1_000_000.0) * price.input_per_million_tokens |
| 122 | + + (token_output / 1_000_000.0) * price.output_per_million_tokens |
| 123 | + + (token_thinking / 1_000_000.0) * price.thinking_per_million_tokens |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +def warn_default_table_in_use(table: PriceTable) -> None: |
| 128 | + """Emit a single warning when `DEFAULT_PRICE_TABLE` is the active table. |
| 129 | +
|
| 130 | + Idempotent: the runner calls this once at startup. Kept as a function so |
| 131 | + other entry points (e.g. ad-hoc tooling) can opt-in to the same warning. |
| 132 | + """ |
| 133 | + if table is DEFAULT_PRICE_TABLE: |
| 134 | + logger.warning( |
| 135 | + "Using DEFAULT_PRICE_TABLE (freshness_date=%s, table_version=%s). " |
| 136 | + "Prices may be stale. Override via eval.yaml > metrics.price_table_path.", |
| 137 | + table.freshness_date.isoformat(), |
| 138 | + table.table_version, |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +__all__ = [ |
| 143 | + "DEFAULT_PRICE_TABLE", |
| 144 | + "ModelPrice", |
| 145 | + "PriceTable", |
| 146 | + "compute_cost", |
| 147 | + "load_price_table", |
| 148 | + "warn_default_table_in_use", |
| 149 | +] |
0 commit comments