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(proxy): persist lifetime cache-read savings across restarts (#1665)
inix908997ef61d9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5c8d74125..793c9a97a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0contains the panic and treats the fragment as plain text, so the request iscompressed and forwarded normally instead of returning HTTP 500([#1547](https://github.com/headroomlabs-ai/headroom/issues/1547)).+* **proxy:** persist lifetime cache-read savings (tokens + USD) in `proxy_savings.json` (schema v4, additive) so cache-mode savings survive proxy restarts and upgrades. Previously prefix-cache read savings lived only in process memory and every restart reset the dashboard's cache figure to zero; the "Cache Reads (lifetime)" tile now reads the persisted value and the Prefix Cache Impact card renders after a restart with zero traffic, marking session-scoped tiles "no activity since restart".- Proactive expansion blocks injected into user turns are now wrapped in`<headroom_proactive_expansion>` XML tags, giving downstream consumers(LLMs, loggers, attribution parsers) a machine-readable provenancediff --git a/headroom/ccr/mcp_server.py b/headroom/ccr/mcp_server.pyindex 8e5264171..e016b6ca4 100644--- a/headroom/ccr/mcp_server.py+++ b/headroom/ccr/mcp_server.py@@ -165,9 +165,14 @@ def _format_session_summary(if isinstance(persistent_lifetime, dict):lifetime_tokens = persistent_lifetime.get("tokens_saved", 0) or 0lifetime_usd = persistent_lifetime.get("compression_savings_usd", 0.0) or 0.0+ lifetime_cache_reads = persistent_lifetime.get("cache_read_tokens", 0) or 0+ lifetime_cache_usd = persistent_lifetime.get("cache_savings_usd", 0.0) or 0.0lines.append("Lifetime Savings:")lines.append(f" Tokens saved: {lifetime_tokens:,}")lines.append(f" Compression savings: ${lifetime_usd:.2f}")+ if lifetime_cache_reads:+ lines.append(f" Cache-read tokens: {lifetime_cache_reads:,}")+ lines.append(f" Cache savings: ${lifetime_cache_usd:.2f}")lines.append("")# Tipdiff --git a/headroom/cli/doctor.py b/headroom/cli/doctor.pyindex e10ed11d2..cd07f8cf7 100644--- a/headroom/cli/doctor.py+++ b/headroom/cli/doctor.py@@ -93,7 +93,7 @@ def check_proxy_liveness(livez: dict[str, Any] | None, base_url: str) -> CheckRe)version = livez.get("version", "unknown")uptime = livez.get("uptime_seconds")- uptime_text = f"up {_format_uptime(uptime)}" if isinstance(uptime, (int, float)) else "up"+ uptime_text = f"up {_format_uptime(uptime)}" if isinstance(uptime, int | float) else "up"return CheckResult(name="proxy",status=PASS,@@ -314,7 +314,8 @@ def check_savings(stats: dict[str, Any] | None, savings_file: Path) -> CheckResulifetime = payload.get("lifetime") or {}tokens = lifetime.get("tokens_saved", 0) or 0usd = lifetime.get("compression_savings_usd", 0.0) or 0.0- if not tokens:+ cache_reads = lifetime.get("cache_read_tokens", 0) or 0+ if not tokens and not cache_reads:return CheckResult(name=name,status=WARN,@@ -328,6 +329,9 @@ def check_savings(stats: dict[str, Any] | None, savings_file: Path) -> CheckResuif isinstance(last_activity, str):freshness = _format_since(last_activity)summary = f"{tokens:,} tokens / ${usd:,.2f} saved lifetime"+ if cache_reads:+ cache_usd = lifetime.get("cache_savings_usd", 0.0) or 0.0+ summary += f"; {cache_reads:,} cache-read tokens / ${cache_usd:,.2f} cache savings"if freshness:summary += f" — last request {freshness}"return CheckResult(name=name, status=PASS, summary=f"{summary} ({source})")diff --git a/headroom/dashboard/templates/dashboard.html b/headroom/dashboard/templates/dashboard.htmlindex 0caea0b62..b038e9cfd 100644--- a/headroom/dashboard/templates/dashboard.html+++ b/headroom/dashboard/templates/dashboard.html@@ -755,42 +755,70 @@</div></template>- <template x-if="(stats.prefix_cache?.totals?.requests || 0) > 0">+ <!-- Card renders on live cache traffic OR persisted lifetime cache savings —+ the lifetime figure must survive a restart with zero traffic. -->+ <template x-if="cacheSessionActive || (stats.persistent_savings?.lifetime?.cache_read_tokens || 0) > 0"><div class="bg-surface rounded-lg p-4 border border-border mb-6"><div class="flex justify-between items-center mb-3"><div class="text-sm font-medium text-gray-300">Prefix Cache Impact</div>- <div class="text-xs text-emerald-400 font-mono" x-text="'Net savings: $' + formatCurrency(stats.prefix_cache?.totals?.net_savings_usd || 0)"></div>+ <div class="text-xs text-emerald-400 font-mono"+ x-show="cacheSessionActive"+ x-text="'Net savings: $' + formatCurrency(stats.prefix_cache?.totals?.net_savings_usd || 0)"></div>+ <div class="text-xs text-gray-500 font-mono"+ x-show="!cacheSessionActive">no activity since restart</div></div><!-- Totals row --><div class="grid grid-cols-2 md:grid-cols-5 gap-4 mb-4"><div>- <div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Cache Reads</div>- <div class="text-2xl font-light tabular-nums text-emerald-400" x-text="formatNumber(stats.prefix_cache?.totals?.cache_read_tokens || 0)"></div>- <div class="text-xs text-emerald-400/70" x-text="'$' + formatCurrency(stats.prefix_cache?.totals?.savings_usd || 0) + ' saved'"></div>+ <div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Cache Reads (lifetime)</div>+ <div class="text-2xl font-light tabular-nums text-emerald-400" x-text="formatNumber(stats.persistent_savings?.lifetime?.cache_read_tokens || 0)"></div>+ <!-- Three-way zero state mirrors the Proxy $ Saved tile: real value /+ unpriced-but-healthy / litellm unavailable. -->+ <div class="text-xs text-emerald-400/70"+ x-show="(stats.persistent_savings?.lifetime?.cache_savings_usd || 0) > 0"+ x-text="'$' + formatCurrency(stats.persistent_savings?.lifetime?.cache_savings_usd || 0) + ' saved'"></div>+ <div class="text-xs text-gray-500"+ x-show="!((stats.persistent_savings?.lifetime?.cache_savings_usd || 0) > 0) && stats.litellm_available !== false">savings not priced yet</div>+ <div class="text-xs text-gray-500"+ x-show="!((stats.persistent_savings?.lifetime?.cache_savings_usd || 0) > 0) && stats.litellm_available === false">pricing needs LiteLLM</div></div><div><div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Cache Writes</div>- <div class="text-2xl font-light tabular-nums text-amber-400" x-text="formatNumber(stats.prefix_cache?.totals?.cache_write_tokens || 0)"></div>- <div class="text-xs text-amber-400/70" x-text="'$' + formatCurrency(stats.prefix_cache?.totals?.write_premium_usd || 0) + ' write premium'"></div>+ <div class="text-2xl font-light tabular-nums text-amber-400"+ x-show="cacheSessionActive"+ x-text="formatNumber(stats.prefix_cache?.totals?.cache_write_tokens || 0)"></div>+ <div class="text-2xl font-light tabular-nums text-gray-600"+ x-show="!cacheSessionActive">—</div>+ <div class="text-xs text-amber-400/70"+ x-show="cacheSessionActive"+ x-text="'$' + formatCurrency(stats.prefix_cache?.totals?.write_premium_usd || 0) + ' write premium'"></div>+ <div class="text-xs text-gray-500"+ x-show="!cacheSessionActive">no activity since restart</div></div><div><div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Hit Rate</div>- <div class="text-2xl font-light tabular-nums" :class="(stats.prefix_cache?.totals?.hit_rate || 0) > 80 ? 'text-emerald-400' : (stats.prefix_cache?.totals?.hit_rate || 0) > 50 ? 'text-amber-400' : 'text-red-400'" x-text="(stats.prefix_cache?.totals?.hit_rate || 0).toFixed(0) + '%'"></div>- <div class="text-xs text-gray-500" x-text="(stats.prefix_cache?.totals?.hit_requests || 0) + ' / ' + (stats.prefix_cache?.totals?.requests || 0) + ' requests'"></div>+ <div class="text-2xl font-light tabular-nums" x-show="cacheSessionActive" :class="(stats.prefix_cache?.totals?.hit_rate || 0) > 80 ? 'text-emerald-400' : (stats.prefix_cache?.totals?.hit_rate || 0) > 50 ? 'text-amber-400' : 'text-red-400'" x-text="(stats.prefix_cache?.totals?.hit_rate || 0).toFixed(0) + '%'"></div>+ <div class="text-2xl font-light tabular-nums text-gray-600" x-show="!cacheSessionActive">—</div>+ <div class="text-xs text-gray-500" x-show="cacheSessionActive" x-text="(stats.prefix_cache?.totals?.hit_requests || 0) + ' / ' + (stats.prefix_cache?.totals?.requests || 0) + ' requests'"></div>+ <div class="text-xs text-gray-500" x-show="!cacheSessionActive">no activity since restart</div></div><div><div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Cache Busts</div>- <div class="text-2xl font-light tabular-nums" :class="(stats.prefix_cache?.totals?.bust_count || 0) > 5 ? 'text-red-400' : (stats.prefix_cache?.totals?.bust_count || 0) > 0 ? 'text-amber-400' : 'text-emerald-400'" x-text="stats.prefix_cache?.totals?.bust_count || 0"></div>- <div class="text-xs text-gray-500" x-text="formatNumber(stats.prefix_cache?.totals?.bust_write_tokens || 0) + ' tokens re-written'"></div>+ <div class="text-2xl font-light tabular-nums" x-show="cacheSessionActive" :class="(stats.prefix_cache?.totals?.bust_count || 0) > 5 ? 'text-red-400' : (stats.prefix_cache?.totals?.bust_count || 0) > 0 ? 'text-amber-400' : 'text-emerald-400'" x-text="stats.prefix_cache?.totals?.bust_count || 0"></div>+ <div class="text-2xl font-light tabular-nums text-gray-600" x-show="!cacheSessionActive">—</div>+ <div class="text-xs text-gray-500" x-show="cacheSessionActive" x-text="formatNumber(stats.prefix_cache?.totals?.bust_write_tokens || 0) + ' tokens re-written'"></div>+ <div class="text-xs text-gray-500" x-show="!cacheSessionActive">no activity since restart</div></div><div><div class="text-xs text-gray-500 uppercase tracking-wide mb-1">Providers</div>- <div class="text-2xl font-light tabular-nums text-gray-300" x-text="Object.keys(stats.prefix_cache?.by_provider || {}).length"></div>- <div class="text-xs text-gray-500">with cache data</div>+ <div class="text-2xl font-light tabular-nums text-gray-300" x-show="cacheSessionActive" x-text="Object.keys(stats.prefix_cache?.by_provider || {}).length"></div>+ <div class="text-2xl font-light tabular-nums text-gray-600" x-show="!cacheSessionActive">—</div>+ <div class="text-xs text-gray-500" x-show="cacheSessionActive">with cache data</div>+ <div class="text-xs text-gray-500" x-show="!cacheSessionActive">no activity since restart</div></div></div>- <!-- Cache efficiency bar -->- <div>+ <!-- Cache efficiency bar (session-scoped; hidden until traffic arrives) -->+ <div x-show="cacheSessionActive"><div class="flex justify-between text-xs text-gray-500 mb-1"><span>Cache Efficiency</span><span x-text="cacheSavingsPercent + '% of input served from cache'"></span>@@ -2393,6 +2421,10 @@// --- Prefix Cache ---+ get cacheSessionActive() {+ return (this.stats.prefix_cache?.totals?.requests || 0) > 0;+ },+get cacheSavingsPercent() {const t = this.stats.prefix_cache?.totals || {};const total = (t.cache_read_tokens || 0) + (t.cache_write_tokens || 0);diff --git a/headroom/proxy/savings_tracker.py b/headroom/proxy/savings_tracker.pyindex 926f9cd25..b5ac322bf 100644--- a/headroom/proxy/savings_tracker.py+++ b/headroom/proxy/savings_tracker.py@@ -28,7 +28,7 @@ logger = logging.getLogger(__name__)HEADROOM_SAVINGS_PATH_ENV_VAR = _paths.HEADROOM_SAVINGS_PATH_ENVDEFAULT_SAVINGS_DIR = ".headroom"DEFAULT_SAVINGS_FILE = "proxy_savings.json"-SCHEMA_VERSION = 3+SCHEMA_VERSION = 4DEFAULT_MAX_HISTORY_POINTS = 5000DEFAULT_MAX_PROJECTS = 50PROJECT_NAME_MAX_LENGTH = 128@@ -107,6 +107,8 @@ def _bucket_start(timestamp: datetime, bucket: str) -> datetime:def _coerce_int(value: Any, default: int = 0) -> int:+ # OverflowError: int(float("inf")) — json accepts bare Infinity, and a+ # corrupted state file must not crash proxy startup.try:return max(int(value), 0)except (TypeError, ValueError, OverflowError):@@ -114,12 +116,12 @@ def _coerce_int(value: Any, default: int = 0) -> int:def _coerce_float(value: Any, default: float = 0.0) -> float:+ # NaN is absorbing under += — one poisoned value would brick an+ # accumulator forever, so reject non-finite values outright.try:coerced = float(value)except (TypeError, ValueError, OverflowError):return default- # Reject NaN/inf -- float() accepts them, but they poison arithmetic and- # serialize to JSON the dashboard's JSON.parse rejects.if not math.isfinite(coerced):return defaultreturn max(coerced, 0.0)@@ -212,6 +214,36 @@ def _estimate_compression_savings_usd(model: str, tokens_saved: int) -> float:return float(tokens_saved) * float(DEFAULT_FALLBACK_INPUT_COST_PER_TOKEN)+def _estimate_cache_savings_usd(model: str, cache_read_tokens: int) -> float:+ """Estimate cache-read savings in USD — the discount delta vs list price.++ Cache reads bill at the provider's discounted rate, so the saving per token+ is ``input_cost_per_token - cache_read_input_token_cost``. Unknown models or+ an unavailable litellm price as 0.0 (fail open); tokens still accumulate.++ Deliberately diverges from ``proxy/cost.py``'s session-scoped provider+ multipliers (``_CACHE_ECONOMICS``): this lifetime figure follows the+ per-model litellm pricing the rest of this module already uses.+ """+ litellm = _get_litellm_module()+ if cache_read_tokens <= 0 or litellm is None:+ return 0.0++ try:+ resolved = _resolve_litellm_model(model)+ info = litellm.model_cost.get(resolved, {})+ input_cost_per_token = info.get("input_cost_per_token")+ if not input_cost_per_token:+ return 0.0+ cache_read_cost = info.get("cache_read_input_token_cost", input_cost_per_token)+ discount = float(input_cost_per_token) - float(cache_read_cost)+ if discount <= 0:+ return 0.0+ return float(cache_read_tokens) * discount+ except Exception:+ return 0.0++def _estimate_input_cost_usd(model: str,input_tokens: int,@@ -322,6 +354,8 @@ def _empty_display_session() -> dict[str, Any]:"requests": 0,"tokens_saved": 0,"compression_savings_usd": 0.0,+ "cache_read_tokens": 0,+ "cache_savings_usd": 0.0,"total_input_tokens": 0,"total_input_cost_usd": 0.0,"savings_percent": 0.0,@@ -416,6 +450,11 @@ def _normalize_display_session(entry: Any) -> dict[str, Any]:_coerce_float(entry.get("compression_savings_usd")),6,),+ "cache_read_tokens": _coerce_int(entry.get("cache_read_tokens")),+ "cache_savings_usd": round(+ _coerce_float(entry.get("cache_savings_usd")),+ 6,+ ),"total_input_tokens": total_input_tokens,"total_input_cost_usd": round(_coerce_float(entry.get("total_input_cost_usd")),@@ -567,6 +606,8 @@ class SavingsTracker:delta_tokens_saved = _coerce_int(tokens_saved)delta_input_tokens = _coerce_int(input_tokens)delta_savings_usd = _estimate_compression_savings_usd(model, delta_tokens_saved)+ delta_cache_read_tokens = _coerce_int(cache_read_tokens)+ delta_cache_savings_usd = _estimate_cache_savings_usd(model, delta_cache_read_tokens)delta_input_cost_usd = _estimate_input_cost_usd(model,delta_input_tokens,@@ -612,6 +653,11 @@ class SavingsTracker:lifetime["compression_savings_usd"] + delta_savings_usd,6,)+ lifetime["cache_read_tokens"] += delta_cache_read_tokens+ lifetime["cache_savings_usd"] = round(+ lifetime["cache_savings_usd"] + delta_cache_savings_usd,+ 6,+ )lifetime["total_input_tokens"] = next_total_input_tokenslifetime["total_input_cost_usd"] = next_total_input_cost_usd@@ -631,6 +677,11 @@ class SavingsTracker:session["compression_savings_usd"] + delta_savings_usd,6,)+ session["cache_read_tokens"] += delta_cache_read_tokens+ session["cache_savings_usd"] = round(+ session["cache_savings_usd"] + delta_cache_savings_usd,+ 6,+ )session["total_input_tokens"] += session_input_tokens_deltasession["total_input_cost_usd"] = round(session["total_input_cost_usd"] + session_input_cost_delta,@@ -849,6 +900,8 @@ class SavingsTracker:"requests": 0,"tokens_saved": 0,"compression_savings_usd": 0.0,+ "cache_read_tokens": 0,+ "cache_savings_usd": 0.0,"total_input_tokens": 0,"total_input_cost_usd": 0.0,},@@ -888,12 +941,16 @@ class SavingsTracker:lifetime_requests = 0lifetime_tokens_saved = 0lifetime_savings_usd = 0.0+ lifetime_cache_read_tokens = 0+ lifetime_cache_savings_usd = 0.0lifetime_input_tokens = 0lifetime_input_cost_usd = 0.0if isinstance(lifetime_raw, dict):lifetime_requests = _coerce_int(lifetime_raw.get("requests"))lifetime_tokens_saved = _coerce_int(lifetime_raw.get("tokens_saved"))lifetime_savings_usd = _coerce_float(lifetime_raw.get("compression_savings_usd"))+ lifetime_cache_read_tokens = _coerce_int(lifetime_raw.get("cache_read_tokens"))+ lifetime_cache_savings_usd = _coerce_float(lifetime_raw.get("cache_savings_usd"))lifetime_input_tokens = _coerce_int(lifetime_raw.get("total_input_tokens"))lifetime_input_cost_usd = _coerce_float(lifetime_raw.get("total_input_cost_usd"))@@ -922,6 +979,8 @@ class SavingsTracker:"requests": lifetime_requests,"tokens_saved": lifetime_tokens_saved,"compression_savings_usd": round(lifetime_savings_usd, 6),+ "cache_read_tokens": lifetime_cache_read_tokens,+ "cache_savings_usd": round(lifetime_cache_savings_usd, 6),"total_input_tokens": lifetime_input_tokens,"total_input_cost_usd": round(lifetime_input_cost_usd, 6),},diff --git a/tests/test_dashboard_cache_lifetime_playwright.py b/tests/test_dashboard_cache_lifetime_playwright.pynew file mode 100644index 000000000..a03fbe74d--- /dev/null+++ b/tests/test_dashboard_cache_lifetime_playwright.py@@ -0,0 +1,107 @@+"""Playwright validation for the persisted lifetime Cache Reads tile.++The Prefix Cache Impact card historically rendered only from in-memory+session counters, so every proxy restart blanked the operator's cache+savings. These tests pin the durable behavior: the card renders from+``persistent_savings.lifetime.cache_read_tokens`` alone after a restart+with zero traffic, session-scoped tiles read "no activity since restart",+and the card stays hidden when neither session nor lifetime data exists.+"""++from __future__ import annotations++import copy+import json+from urllib.parse import urlsplit++import pytest++from headroom.dashboard import get_dashboard_html+from tests.test_dashboard_cache_ttl_playwright import _sample_history, _sample_stats++playwright = pytest.importorskip("playwright.sync_api")+Page = playwright.Page+expect = playwright.expect+sync_playwright = playwright.sync_playwright+++def _stats_lifetime_only() -> dict:+ """Post-restart shape: zero session cache traffic, persisted lifetime present."""+ stats = copy.deepcopy(_sample_stats())+ totals = stats.setdefault("prefix_cache", {}).setdefault("totals", {})+ totals.update({"requests": 0, "cache_read_tokens": 0, "cache_write_tokens": 0})+ stats.setdefault("persistent_savings", {})["lifetime"] = {+ "requests": 6088,+ "tokens_saved": 42_181,+ "compression_savings_usd": 0.5,+ "cache_read_tokens": 629_537_547,+ "cache_savings_usd": 7.2,+ "total_input_tokens": 1_294_591_655,+ "total_input_cost_usd": 12.5,+ }+ return stats+++def _install_dashboard_routes(page: Page, stats: dict) -> None:+ history = _sample_history()+ health = {"status": "healthy", "version": "0.3.0"}+ dashboard_html = get_dashboard_html()++ def handler(route) -> None: # type: ignore[no-untyped-def]+ path = urlsplit(route.request.url).path+ if path in ("/dashboard", "/"):+ route.fulfill(status=200, content_type="text/html", body=dashboard_html)+ return+ if "/stats-history" in path:+ route.fulfill(status=200, content_type="application/json", body=json.dumps(history))+ return+ if path.endswith("/stats"):+ route.fulfill(status=200, content_type="application/json", body=json.dumps(stats))+ return+ if path.endswith("/health"):+ route.fulfill(status=200, content_type="application/json", body=json.dumps(health))+ return+ route.continue_()++ page.route("**/*", handler)+++def _open_dashboard(page: Page, stats: dict) -> None:+ _install_dashboard_routes(page, stats)+ page.goto("http://headroom.local/dashboard")+ page.wait_for_load_state("networkidle")+++def test_card_renders_lifetime_cache_reads_after_zero_traffic_restart() -> None:+ with sync_playwright() as p:+ browser = p.chromium.launch()+ page = browser.new_page(viewport={"width": 1440, "height": 1600})+ _open_dashboard(page, _stats_lifetime_only())++ expect(page.get_by_text("Prefix Cache Impact", exact=True)).to_be_visible()+ expect(page.get_by_text("Cache Reads (lifetime)", exact=True)).to_be_visible()+ expect(page.get_by_text("629.5M", exact=True)).to_be_visible()+ expect(page.get_by_text("$7.20 saved")).to_be_visible()+ # Session-scoped siblings read as inactive, not as literal zeros.+ expect(page.get_by_text("no activity since restart").first).to_be_visible()+ assert page.get_by_text("no activity since restart").count() >= 5+ # x-show hides via CSS (element stays in the DOM), so assert+ # visibility, not count — unlike the x-if card gate below.+ expect(page.get_by_text("Cache Efficiency", exact=True)).to_be_hidden()++ browser.close()+++def test_card_hidden_when_no_session_and_no_lifetime_data() -> None:+ stats = _stats_lifetime_only()+ stats["persistent_savings"]["lifetime"]["cache_read_tokens"] = 0+ stats["persistent_savings"]["lifetime"]["cache_savings_usd"] = 0.0++ with sync_playwright() as p:+ browser = p.chromium.launch()+ page = browser.new_page(viewport={"width": 1440, "height": 1600})+ _open_dashboard(page, stats)++ expect(page.get_by_text("Prefix Cache Impact", exact=True)).to_have_count(0)++ browser.close()diff --git a/tests/test_proxy_project_savings.py b/tests/test_proxy_project_savings.pyindex e04ddf634..2ea4c08a8 100644--- a/tests/test_proxy_project_savings.py+++ b/tests/test_proxy_project_savings.py@@ -256,7 +256,7 @@ def test_funnel_attributes_savings_from_context_and_stats_exposes_them(tmp_path,assert stats["persistent_savings"]["projects_limit"] == DEFAULT_MAX_PROJECTShistory = client.get("/stats-history").json()- assert history["schema_version"] == 3+ assert history["schema_version"] == 4assert history["projects"]["ctx-project"]["requests"] == 1diff --git a/tests/test_proxy_savings_history.py b/tests/test_proxy_savings_history.pyindex ffc18d40d..822a4c890 100644--- a/tests/test_proxy_savings_history.py+++ b/tests/test_proxy_savings_history.py@@ -114,11 +114,13 @@ def test_savings_tracker_sanitizes_legacy_state_and_applies_retention(tmp_path):)snapshot = tracker.snapshot()- assert snapshot["schema_version"] == 3+ assert snapshot["schema_version"] == 4assert snapshot["lifetime"] == {"requests": 0,"tokens_saved": 30,"compression_savings_usd": pytest.approx(0.03),+ "cache_read_tokens": 0,+ "cache_savings_usd": 0.0,"total_input_tokens": 0,"total_input_cost_usd": 0.0,}@@ -152,6 +154,8 @@ def test_non_dict_savings_state_resets_to_default(tmp_path):"requests": 0,"tokens_saved": 0,"compression_savings_usd": 0.0,+ "cache_read_tokens": 0,+ "cache_savings_usd": 0.0,"total_input_tokens": 0,"total_input_cost_usd": 0.0,}@@ -555,6 +559,8 @@ def test_display_session_rolls_after_inactivity_and_counts_zero_savings_requests"requests": 2,"tokens_saved": 20,"compression_savings_usd": pytest.approx(0.02),+ "cache_read_tokens": 0,+ "cache_savings_usd": 0.0,"total_input_tokens": 200,"total_input_cost_usd": pytest.approx(0.2),"savings_percent": pytest.approx(9.09),@@ -587,6 +593,8 @@ def test_display_session_rolls_after_inactivity_and_counts_zero_savings_requests"requests": 1,"tokens_saved": 5,"compression_savings_usd": pytest.approx(0.005),+ "cache_read_tokens": 0,+ "cache_savings_usd": 0.0,"total_input_tokens": 50,"total_input_cost_usd": pytest.approx(0.05),"savings_percent": pytest.approx(9.09),@@ -1017,7 +1025,7 @@ def test_stats_history_persists_across_restarts_and_stats_stays_compatible(tmp_phistory = client.get("/stats-history")assert history.status_code == 200history_data = history.json()- assert history_data["schema_version"] == 3+ assert history_data["schema_version"] == 4assert history_data["storage_path"] == str(savings_path)assert history_data["lifetime"]["tokens_saved"] == 40assert history_data["lifetime"]["total_input_tokens"] == 120@@ -1382,3 +1390,286 @@ def test_savings_tracker_loads_non_finite_persisted_state_without_crashing(tmp_passert math.isfinite(value), f"{key} is non-finite: {value}"assert lifetime["tokens_saved"] == 0assert lifetime["total_input_tokens"] == 0+++def test_cache_read_savings_accumulate_and_survive_restart(tmp_path, monkeypatch):+ path = tmp_path / "proxy_savings.json"+ monkeypatch.setattr(+ savings_tracker_module,+ "_estimate_cache_savings_usd",+ lambda model, cache_read_tokens: cache_read_tokens / 1_000_000.0,+ raising=False,+ )+ # Pin "now" just after the recorded timestamps so the display session+ # reads as active at snapshot time.+ monkeypatch.setattr(+ savings_tracker_module,+ "_utc_now",+ lambda: datetime(2026, 7, 1, 9, 5, tzinfo=timezone.utc),+ )++ tracker = SavingsTracker(path=str(path))+ tracker.record_request(+ model="claude-opus-4-8",+ input_tokens=1_000,+ tokens_saved=0,+ cache_read_tokens=800_000,+ timestamp="2026-07-01T09:00:00Z",+ )+ tracker.record_request(+ model="claude-opus-4-8",+ input_tokens=1_000,+ tokens_saved=0,+ cache_read_tokens=800_000,+ timestamp="2026-07-01T09:01:00Z",+ )++ snapshot = tracker.snapshot()+ assert snapshot["lifetime"]["cache_read_tokens"] == 1_600_000+ assert snapshot["lifetime"]["cache_savings_usd"] == pytest.approx(1.6)+ assert snapshot["display_session"]["cache_read_tokens"] == 1_600_000+ assert snapshot["display_session"]["cache_savings_usd"] == pytest.approx(1.6)++ # Restart: a fresh tracker on the same file sees the persisted totals (AE1).+ reloaded = SavingsTracker(path=str(path))+ assert reloaded.snapshot()["lifetime"]["cache_read_tokens"] == 1_600_000+ assert reloaded.snapshot()["lifetime"]["cache_savings_usd"] == pytest.approx(1.6)+ assert reloaded.stats_preview()["lifetime"]["cache_read_tokens"] == 1_600_000+ assert reloaded.history_response()["lifetime"]["cache_read_tokens"] == 1_600_000+++def test_v3_state_without_cache_fields_loads_clean_and_saves_v4(tmp_path):+ path = tmp_path / "proxy_savings.json"+ path.write_text(+ json.dumps(+ {+ "schema_version": 3,+ "lifetime": {+ "requests": 6088,+ "tokens_saved": 42181,+ "compression_savings_usd": 0.5,+ "total_input_tokens": 1_294_591_655,+ "total_input_cost_usd": 12.5,+ },+ "history": [],+ "projects": {},+ }+ ),+ encoding="utf-8",+ )++ tracker = SavingsTracker(path=str(path))+ snapshot = tracker.snapshot()++ # AE2: missing cache fields read as zero; compression data intact.+ assert snapshot["lifetime"]["cache_read_tokens"] == 0+ assert snapshot["lifetime"]["cache_savings_usd"] == 0.0+ assert snapshot["lifetime"]["tokens_saved"] == 42181+ assert snapshot["lifetime"]["total_input_tokens"] == 1_294_591_655++ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=5,+ timestamp="2026-07-02T00:00:00Z",+ )+ persisted = json.loads(path.read_text(encoding="utf-8"))+ assert persisted["schema_version"] == 4+ assert persisted["lifetime"]["cache_read_tokens"] == 5+ assert persisted["lifetime"]["tokens_saved"] == 42181+++def test_stateless_tracker_accumulates_cache_savings_in_memory_only(tmp_path):+ path = tmp_path / "proxy_savings.json"+ tracker = SavingsTracker(path=str(path), stateless=True)++ tracker.record_request(+ model="unknown-model",+ input_tokens=100,+ tokens_saved=0,+ cache_read_tokens=1_234,+ timestamp="2026-07-02T00:00:00Z",+ )++ # AE3: in-memory totals update; nothing is written.+ assert tracker.snapshot()["lifetime"]["cache_read_tokens"] == 1_234+ assert not path.exists()+++def test_active_display_session_without_cache_fields_reloads_safely(tmp_path, monkeypatch):+ # Pin "now" so the display session reads as active regardless of when the+ # suite runs (snapshot() expiry-checks against _utc_now).+ monkeypatch.setattr(+ savings_tracker_module,+ "_utc_now",+ lambda: datetime(2026, 7, 2, 0, 10, tzinfo=timezone.utc),+ )+ path = tmp_path / "proxy_savings.json"+ path.write_text(+ json.dumps(+ {+ "schema_version": 3,+ "lifetime": {+ "requests": 1,+ "tokens_saved": 0,+ "compression_savings_usd": 0.0,+ "total_input_tokens": 100,+ "total_input_cost_usd": 0.0,+ },+ "display_session": {+ "requests": 1,+ "tokens_saved": 0,+ "compression_savings_usd": 0.0,+ "total_input_tokens": 100,+ "total_input_cost_usd": 0.0,+ "savings_percent": 0.0,+ "started_at": "2026-07-02T00:00:00Z",+ "last_activity_at": "2026-07-02T00:00:00Z",+ },+ "history": [],+ "projects": {},+ }+ ),+ encoding="utf-8",+ )++ tracker = SavingsTracker(path=str(path))++ # Guards the _normalize_display_session whitelist rebuild (R2): a reload+ # within the inactivity window must not KeyError and must accumulate from 0.+ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=7,+ timestamp="2026-07-02T00:05:00Z",+ )+ session = tracker.snapshot()["display_session"]+ assert session["cache_read_tokens"] == 7+ assert session["requests"] == 2+++def test_cache_savings_edge_cases_zero_and_unpriced(tmp_path):+ path = tmp_path / "proxy_savings.json"+ tracker = SavingsTracker(path=str(path))++ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=0,+ timestamp="2026-07-02T00:00:00Z",+ )+ snapshot = tracker.snapshot()+ assert snapshot["lifetime"]["cache_read_tokens"] == 0+ assert snapshot["lifetime"]["cache_savings_usd"] == 0.0++ # Unpriced model: tokens accumulate, USD stays 0.0 (fail-open pricing).+ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=50,+ timestamp="2026-07-02T00:01:00Z",+ )+ snapshot = tracker.snapshot()+ assert snapshot["lifetime"]["cache_read_tokens"] == 50+ assert snapshot["lifetime"]["cache_savings_usd"] == 0.0+++def test_display_session_rollover_resets_cache_fields(tmp_path, monkeypatch):+ # Pin "now" just after the second request so the 1-minute window judges+ # the rolled session active regardless of when the suite runs.+ monkeypatch.setattr(+ savings_tracker_module,+ "_utc_now",+ lambda: datetime(2026, 7, 2, 2, 0, 30, tzinfo=timezone.utc),+ )+ path = tmp_path / "proxy_savings.json"+ tracker = SavingsTracker(path=str(path), display_session_inactivity_minutes=1)++ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=100,+ timestamp="2026-07-02T00:00:00Z",+ )+ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=25,+ timestamp="2026-07-02T02:00:00Z",+ )++ snapshot = tracker.snapshot()+ assert snapshot["display_session"]["cache_read_tokens"] == 25+ assert snapshot["lifetime"]["cache_read_tokens"] == 125+++def test_cache_savings_usd_uses_litellm_discount_delta(tmp_path, monkeypatch):+ fake_litellm = SimpleNamespace(+ model_cost={+ "priced-model": {+ "input_cost_per_token": 3e-06,+ "cache_read_input_token_cost": 3e-07,+ },+ "no-discount-model": {"input_cost_per_token": 3e-06},+ "inverted-model": {+ "input_cost_per_token": 3e-06,+ "cache_read_input_token_cost": 5e-06,+ },+ }+ )+ monkeypatch.setattr(savings_tracker_module, "_get_litellm_module", lambda: fake_litellm)+ monkeypatch.setattr(savings_tracker_module, "_resolve_litellm_model", lambda model: model)++ # Real discount delta: 1M reads x (3e-06 - 3e-07) = $2.70.+ assert savings_tracker_module._estimate_cache_savings_usd(+ "priced-model", 1_000_000+ ) == pytest.approx(2.7)+ # Missing cache_read_input_token_cost falls back to list price: discount 0.+ assert savings_tracker_module._estimate_cache_savings_usd("no-discount-model", 1_000_000) == 0.0+ # A non-positive discount never produces negative savings.+ assert savings_tracker_module._estimate_cache_savings_usd("inverted-model", 1_000_000) == 0.0++ tracker = SavingsTracker(path=str(tmp_path / "proxy_savings.json"))+ tracker.record_request(+ model="priced-model",+ input_tokens=1_000,+ tokens_saved=0,+ cache_read_tokens=1_000_000,+ timestamp="2026-07-02T00:00:00Z",+ )+ assert tracker.snapshot()["lifetime"]["cache_savings_usd"] == pytest.approx(2.7)+++def test_non_finite_state_values_coerce_to_defaults(tmp_path):+ path = tmp_path / "proxy_savings.json"+ # json accepts bare Infinity/NaN literals; a corrupted file must not crash+ # startup or poison accumulators (NaN is absorbing under +=).+ path.write_text(+ '{"schema_version": 4, "lifetime": {"requests": 1, "tokens_saved": 2, '+ '"compression_savings_usd": NaN, "cache_read_tokens": Infinity, '+ '"cache_savings_usd": NaN, "total_input_tokens": 100, '+ '"total_input_cost_usd": 0.5}, "history": [], "projects": {}}',+ encoding="utf-8",+ )++ tracker = SavingsTracker(path=str(path))+ lifetime = tracker.snapshot()["lifetime"]+ assert lifetime["cache_read_tokens"] == 0+ assert lifetime["cache_savings_usd"] == 0.0+ assert lifetime["compression_savings_usd"] == 0.0+ assert lifetime["tokens_saved"] == 2++ tracker.record_request(+ model="unknown-model",+ input_tokens=10,+ tokens_saved=0,+ cache_read_tokens=5,+ timestamp="2026-07-02T00:00:00Z",+ )+ assert tracker.snapshot()["lifetime"]["cache_read_tokens"] == 5