perf(usage): prefer bundled pricing before metadata fetch

This commit is contained in:
fangliquanflq
2026-09-02 15:00:48 +08:00
committed by kshitij
parent eab28066c3
commit 6064668c8f
2 changed files with 47 additions and 1 deletions

View File

@@ -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_entry
if 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 None
def normalize_usage(

View File

@@ -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():