The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(usage): prefer bundled pricing before metadata fetch
fangliquanflq6064668c8fd2
diff --git a/agent/usage_pricing.py b/agent/usage_pricing.pyindex 9af08f6dc0e..6c354bab7a4 100644--- a/agent/usage_pricing.py+++ b/agent/usage_pricing.py@@ -1278,6 +1278,10 @@ def get_pricing_entry()if route.provider == "openrouter":return _openrouter_pricing_entry(route)++ bundled_entry = _lookup_official_docs_pricing(route)+ if bundled_entry:+ return bundled_entryif route.base_url:entry = _pricing_entry_from_metadata(fetch_endpoint_model_metadata(route.base_url, api_key=api_key or ""),@@ -1287,7 +1291,7 @@ def get_pricing_entry()if entry:return entry- return _lookup_official_docs_pricing(route)+ return Nonedef normalize_usage(diff --git a/tests/agent/test_usage_pricing.py b/tests/agent/test_usage_pricing.pyindex 1b6862aac6a..4d4e8a1ea88 100644--- a/tests/agent/test_usage_pricing.py+++ b/tests/agent/test_usage_pricing.py@@ -105,6 +105,48 @@ def test_deepseek_v4_pro_pricing_entry_exists():assert float(entry.cache_read_cost_per_million) == 0.003625+def test_bundled_pricing_skips_endpoint_metadata(monkeypatch):+ """An exact bundled price must not block on the provider's /models API."""+ monkeypatch.setattr(+ "agent.usage_pricing.fetch_endpoint_model_metadata",+ lambda *_args, **_kwargs: (_ for _ in ()).throw(+ AssertionError("endpoint metadata should not be fetched")+ ),+ )++ entry = get_pricing_entry(+ "deepseek-chat",+ provider="deepseek",+ base_url="https://api.deepseek.com/v1",+ )++ assert entry is not None+ assert entry.source == "official_docs_snapshot"+++def test_unknown_model_falls_back_to_endpoint_metadata(monkeypatch):+ """Models absent from the bundled table still use endpoint pricing."""+ monkeypatch.setattr(+ "agent.usage_pricing.fetch_endpoint_model_metadata",+ lambda *_args, **_kwargs: {+ "deepseek-future": {+ "pricing": {"prompt": "0.000001", "completion": "0.000002"}+ }+ },+ )++ entry = get_pricing_entry(+ "deepseek-future",+ provider="deepseek",+ base_url="https://api.deepseek.com/v1",+ )++ assert entry is not None+ assert entry.source == "provider_models_api"+ assert entry.input_cost_per_million == Decimal("1")+ assert entry.output_cost_per_million == Decimal("2")++def test_deepseek_deprecated_aliases_price_as_v4_flash():