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(mcp): show lifetime totals and label rolling session scope in headroom_stats (#1428)
Rod Boev1c0e15243eda
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4ae691eb2..8d9e2b1bf 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Changed* **telemetry:** anonymous usage telemetry is now **opt-in** (off by default) instead of opt-out. Nothing is collected or sent unless you set `HEADROOM_TELEMETRY=on` or pass `--telemetry` to `headroom proxy` / `headroom install apply`. `is_telemetry_enabled()` is fail-closed — only explicit on-values (`on`/`true`/`1`/`yes`/`enable`/`enabled`) enable it; unset, empty, or unrecognized values stay disabled. The existing `--no-telemetry` flag and `HEADROOM_TELEMETRY=off` remain accepted for back-compat, and install manifests now write the `HEADROOM_TELEMETRY` value explicitly so generated deployments are unambiguous.+* **ccr:** `headroom_stats` now labels its formatted proxy output as a rolling/window-scoped session and adds a lifetime savings section from `/stats persistent_savings.lifetime` when present, while keeping existing summary structure and fallback JSON output behavior.### Featuresdiff --git a/headroom/ccr/mcp_server.py b/headroom/ccr/mcp_server.pyindex 5b75855f3..8e5264171 100644--- a/headroom/ccr/mcp_server.py+++ b/headroom/ccr/mcp_server.py@@ -87,10 +87,14 @@ _READ_ENABLED = os.environ.get("HEADROOM_MCP_READ", "off").lower().strip() in (DEFAULT_PROXY_URL = os.environ.get("HEADROOM_PROXY_URL", "http://127.0.0.1:8787")-def _format_session_summary(summary: dict[str, Any], local_stats: dict[str, Any]) -> str:+def _format_session_summary(+ summary: dict[str, Any],+ local_stats: dict[str, Any],+ persistent_lifetime: dict[str, Any] | None = None,+) -> str:"""Format the proxy summary + local MCP stats into clean readable text."""lines: list[str] = []- lines.append("Headroom Session Summary")+ lines.append("Headroom Window-Scoped Session Summary")lines.append("=" * 40)mode = summary.get("mode", "token")@@ -157,6 +161,15 @@ def _format_session_summary(summary: dict[str, Any], local_stats: dict[str, Any]lines.append(f"MCP Tool: {local_compressions} compressions, {local_saved:,} tokens saved")lines.append("")+ # Lifetime proxy savings (cross-session)+ if isinstance(persistent_lifetime, dict):+ lifetime_tokens = persistent_lifetime.get("tokens_saved", 0) or 0+ lifetime_usd = persistent_lifetime.get("compression_savings_usd", 0.0) or 0.0+ lines.append("Lifetime Savings:")+ lines.append(f" Tokens saved: {lifetime_tokens:,}")+ lines.append(f" Compression savings: ${lifetime_usd:.2f}")+ lines.append("")+# Tiptip = summary.get("tip")if tip:@@ -740,8 +753,14 @@ class HeadroomMCPServer:if proxy_data:summary = proxy_data.get("summary")if summary:+ lifetime = None+ persistent_savings = proxy_data.get("persistent_savings")+ if isinstance(persistent_savings, dict):+ lifetime_block = persistent_savings.get("lifetime")+ if isinstance(lifetime_block, dict):+ lifetime = lifetime_block# Return clean formatted summary instead of raw JSON- formatted = _format_session_summary(summary, stats)+ formatted = _format_session_summary(summary, stats, lifetime)return [TextContent(type="text", text=formatted)]# Fallback: add proxy stats to local statsproxy_stats = self._extract_proxy_stats(proxy_data)diff --git a/tests/test_ccr_mcp_server.py b/tests/test_ccr_mcp_server.pyindex bcdc13ae1..4b92715ff 100644--- a/tests/test_ccr_mcp_server.py+++ b/tests/test_ccr_mcp_server.py@@ -110,3 +110,94 @@ def test_mcp_retrieve_missing_hash_still_errors(fresh_store) -> None:server = mcp_server.HeadroomMCPServer(check_proxy=False)result = asyncio.run(server._retrieve_content("nonexistent_hash"))assert "Content not found" in result.get("error", "")+++def test_handle_stats_session_output_is_window_scoped() -> None:+ """window-scoped stats output should be explicitly labeled after this change."""++ async def fetch_stats() -> dict[str, object]:+ return {+ "summary": {+ "mode": "token",+ "api_requests": 3,+ "compression": {},+ }+ }++ server = mcp_server.HeadroomMCPServer(check_proxy=True)+ server._fetch_full_proxy_stats = fetch_stats+ response = asyncio.run(server._handle_stats())+ text = response[0].kwargs["text"]++ assert "Headroom Window-Scoped Session Summary" in text+ assert "Headroom Session Summary" not in text+++def test_handle_stats_includes_lifetime_totals_from_persistent_savings() -> None:+ """Lifetime savings are appended from /stats persistent_savings.lifetime."""++ async def fetch_stats() -> dict[str, object]:+ return {+ "summary": {+ "mode": "token",+ "api_requests": 3,+ "compression": {},+ },+ "persistent_savings": {+ "lifetime": {"tokens_saved": 12345, "compression_savings_usd": 7.25}+ },+ }++ server = mcp_server.HeadroomMCPServer(check_proxy=True)+ server._fetch_full_proxy_stats = fetch_stats+ response = asyncio.run(server._handle_stats())+ text = response[0].kwargs["text"]++ assert "Lifetime Savings:" in text+ assert "Tokens saved: 12,345" in text+ assert "Compression savings: $7.25" in text+++def test_handle_stats_falls_back_gracefully_without_persistent_lifetime() -> None:+ """Missing lifetime data should still return a valid session summary."""++ async def fetch_stats() -> dict[str, object]:+ return {+ "summary": {+ "mode": "token",+ "api_requests": 3,+ "compression": {},+ },+ "persistent_savings": {"lifetime": None},+ }++ server = mcp_server.HeadroomMCPServer(check_proxy=True)+ server._fetch_full_proxy_stats = fetch_stats+ response = asyncio.run(server._handle_stats())+ text = response[0].kwargs["text"]++ assert "Headroom Window-Scoped Session Summary" in text+ assert "Lifetime Savings:" not in text+++def test_handle_stats_shows_zero_lifetime_totals_when_present() -> None:+ """A present lifetime payload should still render explicit zero totals."""++ async def fetch_stats() -> dict[str, object]:+ return {+ "summary": {+ "mode": "token",+ "api_requests": 3,+ "compression": {},+ },+ "persistent_savings": {"lifetime": {"tokens_saved": 0, "compression_savings_usd": 0.0}},+ }++ server = mcp_server.HeadroomMCPServer(check_proxy=True)+ server._fetch_full_proxy_stats = fetch_stats+ response = asyncio.run(server._handle_stats())+ text = response[0].kwargs["text"]++ assert "Lifetime Savings:" in text+ assert "Tokens saved: 0" in text+ assert "Compression savings: $0.00" in text