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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(savings): count cache-read tokens in input cost estimate (#1429)
gglucass72ade3711211
diff --git a/headroom/proxy/savings_tracker.py b/headroom/proxy/savings_tracker.pyindex 1a6fdd945..a3549db9e 100644--- a/headroom/proxy/savings_tracker.py+++ b/headroom/proxy/savings_tracker.py@@ -217,13 +217,17 @@ def _estimate_input_cost_usd(otherwise falls back to list-price input tokens."""total_input_tokens = _coerce_int(input_tokens)- litellm = _get_litellm_module()- if total_input_tokens <= 0 or litellm is None:- return 0.0-cache_read = _coerce_int(cache_read_tokens)cache_write = _coerce_int(cache_write_tokens)uncached = _coerce_int(uncached_input_tokens)+ litellm = _get_litellm_module()+ # Gate on tokens actually sent. Providers like Anthropic report cache+ # reads/writes separately from `input_tokens` (the uncached portion), so a+ # fully prefix-cached request has input_tokens == 0 while cache_read > 0.+ # Bailing on `input_tokens <= 0` alone dropped the real cache-read cost,+ # leaving days with compression savings but zero recorded spend.+ if total_input_tokens + cache_read + cache_write + uncached <= 0 or litellm is None:+ return 0.0try:resolved = _resolve_litellm_model(model)diff --git a/tests/test_proxy_savings_history.py b/tests/test_proxy_savings_history.pyindex 6b582638b..6bcc56bbd 100644--- a/tests/test_proxy_savings_history.py+++ b/tests/test_proxy_savings_history.py@@ -343,6 +343,37 @@ def test_litellm_resolution_and_savings_estimation_fallbacks(monkeypatch):assert savings_tracker_module._estimate_input_cost_usd("gpt-4o", 100) == 0.0+def test_input_cost_counts_cache_reads_when_uncached_input_is_zero(monkeypatch):+ # Anthropic reports cache reads/writes separately from `input_tokens` (the+ # uncached portion). A fully prefix-cached request has input_tokens == 0 but+ # cache_read_tokens > 0 -- it still cost money and must not be priced at 0,+ # otherwise the day shows compression savings with zero recorded spend.+ def fake_cost_per_token(*, model, prompt_tokens, completion_tokens):+ if model == "anthropic/claude-sonnet-4-6":+ return {"model": model}+ raise RuntimeError("unknown model")++ fake_litellm = SimpleNamespace(+ cost_per_token=fake_cost_per_token,+ model_cost={+ "anthropic/claude-sonnet-4-6": {+ "input_cost_per_token": 0.003,+ "cache_read_input_token_cost": 0.0003,+ "cache_creation_input_token_cost": 0.00375,+ },+ },+ )+ monkeypatch.setattr(savings_tracker_module, "LITELLM_AVAILABLE", True)+ monkeypatch.setattr(savings_tracker_module, "litellm", fake_litellm)++ cost = savings_tracker_module._estimate_input_cost_usd(+ "claude-sonnet-4-6",+ 0,+ cache_read_tokens=1000,+ )+ assert cost == pytest.approx(0.3)++def test_display_session_rolls_after_inactivity_and_counts_zero_savings_requests(tmp_path, monkeypatch):