Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

test: fix ONNX and xAI fixtures blocking main CI (#4050)

JD Davisa63b23fe812e
diff --git a/tests/test_onnx_dependency_contract.py b/tests/test_onnx_dependency_contract.py
index 993660e10..359e6debf 100644
--- a/tests/test_onnx_dependency_contract.py
+++ b/tests/test_onnx_dependency_contract.py
@@ -110,13 +110,13 @@ def test_proxy_dependency_gate_matches_onnx_platform_markers(
monkeypatch.setattr(platform, "machine", lambda: platform_machine)
requested = []
- def import_dependency(name):
+ def find_dependency(name):
requested.append(name)
if name in {"magika", "onnxruntime"}:
- raise ImportError(f"No module named '{name}'")
+ return None
return object()
- monkeypatch.setattr(proxy, "import_module", import_dependency)
+ monkeypatch.setattr(proxy, "find_spec", find_dependency)
if optional:
proxy.ensure_proxy_dependencies()
assert not {"magika", "onnxruntime"} & set(requested)
diff --git a/tests/test_xai_cache_economics.py b/tests/test_xai_cache_economics.py
index 1177855d3..8f0314025 100644
--- a/tests/test_xai_cache_economics.py
+++ b/tests/test_xai_cache_economics.py
@@ -1,4 +1,4 @@
-"""xAI cached input must have the same price in savings and cost totals."""
+"""xAI inferred writes and cache estimates honor billing provenance."""
import pytest
@@ -17,6 +17,9 @@ def test_xai_inferred_writes_are_not_billed_twice(monkeypatch, inferred, expecte
{
"input_cost_per_token": 1e-6,
"output_cost_per_token": 2e-6,
+ # Keep the double-charge regression about writes, with known rates.
+ "cache_read_input_token_cost": 0.16e-6,
+ "cache_creation_input_token_cost": 1e-6,
"litellm_provider": "xai",
"mode": "chat",
},
@@ -38,13 +41,29 @@ def test_xai_inferred_writes_are_not_billed_twice(monkeypatch, inferred, expecte
@pytest.mark.parametrize("catalog_read", [None, 0.05e-6])
-def test_xai_cache_read_pricing_matches_cost_totals(monkeypatch, catalog_read):
+def test_xai_cache_savings_and_billing_use_their_respective_price_sources(
+ monkeypatch, catalog_read
+):
import litellm
- from headroom.pricing.counterfactual import CacheMix, Region, price_savings, resolve_rates
+ from headroom.pricing.counterfactual import (
+ BASIS_CATALOG,
+ BASIS_PROVIDER_RATIO,
+ CacheMix,
+ Region,
+ price_savings,
+ resolve_rates,
+ )
from headroom.proxy.cost import CostTracker
- model = "xai/grok-review-fallback" if catalog_read is None else "xai/grok-review-catalog"
+ # Provider dispatch uses LiteLLM's registered model set independently of
+ # model_cost. A synthetic catalog key can therefore be rejected by older
+ # supported LiteLLM versions before the canonical billing probe runs.
+ model = next(
+ model
+ for model in sorted(litellm.xai_models)
+ if model.startswith("xai/grok") and model in litellm.model_cost
+ )
row = {
"input_cost_per_token": 1e-6,
"output_cost_per_token": 2e-6,
@@ -60,9 +79,23 @@ def test_xai_cache_read_pricing_matches_cost_totals(monkeypatch, catalog_read):
mix = CacheMix.from_usage(cache_read_tokens=10_000)
saving = price_savings(10_000, model=model, mix=mix, provider="xai", region=Region.PREFIX)
assert saving.usd == pytest.approx(10_000 * expected_read)
+ assert saving.basis == (BASIS_PROVIDER_RATIO if catalog_read is None else BASIS_CATALOG)
+
+ # A provider-wide counterfactual estimate is not a billed cache rate.
+ # Use the real calculator for the same reported usage, as billing does.
+ if catalog_read is None:
+ canonical_input, _ = litellm.cost_per_token(
+ model=model,
+ prompt_tokens=10_000,
+ completion_tokens=0,
+ cache_read_input_tokens=10_000,
+ )
+ else:
+ canonical_input = 10_000 * catalog_read
tracker = CostTracker()
tracker.record_tokens(model, tokens_saved=0, tokens_sent=10_000, cache_read_tokens=10_000)
- assert tracker.stats()["total_input_cost_usd"] == pytest.approx(10_000 * expected_read)
+ assert tracker.totals()[1] == pytest.approx(canonical_input)
+ assert tracker.stats()["total_input_cost_usd"] == pytest.approx(canonical_input)
finally:
resolve_rates.cache_clear()