Skip to content

The agent that grows with you

fix(pricing): custom-provider /models prices already per-million are no longer inflated 1e6x

teknium1d54ae026066c
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index 97a00499a72..495e0b5384f 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -789,6 +789,33 @@ def _context_length_from_model_payload(payload: Dict[str, Any]) -> Optional[int]
return int(raw) if isinstance(raw, (int, float)) and int(raw) > 0 else None
+# Generic ``/models`` pricing: an explicit ``unit`` beside the rates wins; without one, a token rate
+# at or above $0.001/token ($1,000/MTok — no real model charges that) can only be a per-million quote.
+_PRICING_UNIT_DIVISORS = {
+ "per_token": 1, "per_1k_tokens": 1_000, "per_thousand_tokens": 1_000,
+ "per_1m_tokens": 1_000_000, "per_million_tokens": 1_000_000,
+}
+_PER_MILLION_QUOTE_MIN = 0.001
+_TOKEN_RATE_FIELDS = ("prompt", "completion", "cache_read", "cache_write")
+
+
+def _normalize_token_rates(pricing: Dict[str, Any], unit: Any) -> Dict[str, Any]:
+ """Rescale the generic path's token rates to per-token strings (the contract usage_pricing
+ multiplies by 1e6), the way the Novita/DeepInfra branches already do for their known units."""
+ rates: Dict[str, float] = {}
+ for key in _TOKEN_RATE_FIELDS:
+ try:
+ rates[key] = float(pricing[key])
+ except (KeyError, TypeError, ValueError):
+ continue
+ divisor = _PRICING_UNIT_DIVISORS.get(str(unit or "").strip().lower())
+ if divisor is None:
+ divisor = 1_000_000 if any(v >= _PER_MILLION_QUOTE_MIN for v in rates.values()) else 1
+ if divisor != 1:
+ pricing.update({key: str(value / divisor) for key, value in rates.items()})
+ return pricing
+
+
def _extract_pricing(payload: Dict[str, Any]) -> Dict[str, Any]:
def _per_token(source: Dict[str, Any], fields: Dict[str, str], scale) -> Dict[str, Any]:
# Provider $/MTok (or Novita's 1/10_000-$ per M) -> per-token strings, the same path usage_pricing uses for OpenRouter.
@@ -818,7 +845,7 @@ def _extract_pricing(payload: Dict[str, Any]) -> Dict[str, Any]:
pricing[target] = normalized[alias]
break
if pricing:
- return pricing
+ return _normalize_token_rates(pricing, normalized.get("unit"))
return {}
diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py
index 38a4e13f0be..9b4360f3e65 100644
--- a/tests/agent/test_model_metadata.py
+++ b/tests/agent/test_model_metadata.py
@@ -1990,3 +1990,38 @@ class TestOpenRouterRoutingVariantContextLength:
assert variant_ctx == base_ctx == 2_000_000
assert variant_ctx != DEFAULT_CONTEXT_LENGTHS.get("grok")
assert get_model_context_length("thinkingmachines/inkling:free", provider="openrouter") == 64_000
+
+
+def test_endpoint_pricing_already_per_million_is_not_inflated():
+ """#112018 / #34256 / #79174: a /models catalog quoting USD per 1M tokens (with or without an explicit
+ ``unit``) must reach usage_pricing as per-token rates, so the cost estimate is $0.60/M — not $600,000/M."""
+ from agent import model_metadata as mm
+ from agent import usage_pricing as up
+
+ catalog = {
+ "minimax-m3": {"id": "minimax-m3", "pricing": {"currency": "USD", "prompt": 0.6, "completion": 1.2, "cache_read": 0.12}},
+ "glm-x": {"id": "glm-x", "pricing": {"currency": "CNY", "unit": "per_1m_tokens", "prompt": 1, "completion": 2}},
+ "crof-a": {"id": "crof-a", "cost": {"input": 0.04, "output": 0.15}},
+ }
+ meta = {mid: mm._endpoint_model_entry(model, mid, None) for mid, model in catalog.items()}
+ dollars_per_million = {
+ mid: up._pricing_entry_from_metadata(meta, mid, source_url="x", pricing_version="openai-compatible-models-api")
+ for mid in catalog
+ }
+ assert float(dollars_per_million["minimax-m3"].input_cost_per_million) == pytest.approx(0.6)
+ assert float(dollars_per_million["minimax-m3"].cache_read_cost_per_million) == pytest.approx(0.12)
+ assert float(dollars_per_million["glm-x"].output_cost_per_million) == pytest.approx(2.0)
+ assert float(dollars_per_million["crof-a"].input_cost_per_million) == pytest.approx(0.04)
+
+
+def test_endpoint_pricing_per_token_quotes_pass_through_unchanged():
+ """Control: per-token quotes (the OpenRouter convention) and per-request fees are left alone."""
+ from agent import model_metadata as mm
+ from agent import usage_pricing as up
+
+ model = {"id": "m", "pricing": {"prompt": "0.0000006", "completion": "0.0000012", "request": "0.005"}}
+ meta = {"m": mm._endpoint_model_entry(model, "m", None)}
+ entry = up._pricing_entry_from_metadata(meta, "m", source_url="x", pricing_version="openai-compatible-models-api")
+ assert float(entry.input_cost_per_million) == pytest.approx(0.6)
+ assert float(entry.output_cost_per_million) == pytest.approx(1.2)
+ assert float(entry.request_cost) == pytest.approx(0.005)