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
Apply Ruff formatting to rebased stack
Garmd88c6e205fee
diff --git a/headroom/proxy/savings_tracker.py b/headroom/proxy/savings_tracker.pyindex 67a88d9ca..544904c53 100644--- a/headroom/proxy/savings_tracker.py+++ b/headroom/proxy/savings_tracker.py@@ -49,12 +49,7 @@ def _utc_now() -> datetime:def _to_utc_iso(dt: datetime) -> str:- return (- dt.astimezone(timezone.utc)- .replace(microsecond=0)- .isoformat()- .replace("+00:00", "Z")- )+ return dt.astimezone(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")def _parse_timestamp(value: Any) -> datetime | None:@@ -293,9 +288,7 @@ class SavingsTracker:path: str | None = None,max_history_points: int = DEFAULT_MAX_HISTORY_POINTS,max_history_age_days: int = DEFAULT_MAX_HISTORY_AGE_DAYS,- display_session_inactivity_minutes: int = (- DEFAULT_DISPLAY_SESSION_INACTIVITY_MINUTES- ),+ display_session_inactivity_minutes: int = (DEFAULT_DISPLAY_SESSION_INACTIVITY_MINUTES),) -> None:self._path = Path(path or get_default_savings_storage_path())self._max_history_points = max_history_points@@ -472,9 +465,7 @@ class SavingsTracker:)total_before = session["tokens_saved"] + session["total_input_tokens"]session["savings_percent"] = round(- (session["tokens_saved"] / total_before * 100)- if total_before > 0- else 0.0,+ (session["tokens_saved"] / total_before * 100) if total_before > 0 else 0.0,2,)session["last_activity_at"] = _to_utc_iso(timestamp_dt)@@ -584,9 +575,7 @@ class SavingsTracker:"lifetime": dict(self._state["lifetime"]),"display_session": self._display_session_snapshot_locked(),"display_session_policy": {- "rollover_inactivity_minutes": (- self._display_session_inactivity_minutes- ),+ "rollover_inactivity_minutes": (self._display_session_inactivity_minutes),},"history": history,"retention": {@@ -645,13 +634,9 @@ class SavingsTracker:if 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_savings_usd = _coerce_float(lifetime_raw.get("compression_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")- )+ lifetime_input_cost_usd = _coerce_float(lifetime_raw.get("total_input_cost_usd"))if normalized_history:last = normalized_history[-1]@@ -686,9 +671,7 @@ class SavingsTracker:}if normalized_history:- reference_time = (- _parse_timestamp(normalized_history[-1]["timestamp"]) or _utc_now()- )+ reference_time = _parse_timestamp(normalized_history[-1]["timestamp"]) or _utc_now()original_state = self._state if hasattr(self, "_state") else Noneself._state = statetry:@@ -706,9 +689,7 @@ class SavingsTracker:returnif self._max_history_age_days > 0:- cutoff = (reference_time or _utc_now()) - timedelta(- days=self._max_history_age_days- )+ cutoff = (reference_time or _utc_now()) - timedelta(days=self._max_history_age_days)filtered = [itemfor item in historydiff --git a/tests/test_proxy_savings_history.py b/tests/test_proxy_savings_history.pyindex 77eb2fbee..c5b559111 100644--- a/tests/test_proxy_savings_history.py+++ b/tests/test_proxy_savings_history.py@@ -43,9 +43,7 @@ def _record_request(def test_savings_tracker_helpers_normalize_inputs_and_paths(tmp_path, monkeypatch):override_path = tmp_path / "custom-savings.json"monkeypatch.setenv(HEADROOM_SAVINGS_PATH_ENV_VAR, str(override_path))- assert savings_tracker_module.get_default_savings_storage_path() == str(- override_path- )+ assert savings_tracker_module.get_default_savings_storage_path() == str(override_path)monkeypatch.delenv(HEADROOM_SAVINGS_PATH_ENV_VAR, raising=False)default_path = savings_tracker_module.get_default_savings_storage_path()@@ -119,10 +117,7 @@ def test_savings_tracker_sanitizes_legacy_state_and_applies_retention(tmp_path):"total_input_tokens": 0,"total_input_cost_usd": 0.0,}- assert (- snapshot["display_session"]- == savings_tracker_module._empty_display_session()- )+ assert snapshot["display_session"] == savings_tracker_module._empty_display_session()assert snapshot["history"] == [{"timestamp": "2026-03-27T09:00:00Z",@@ -152,10 +147,7 @@ def test_non_dict_savings_state_resets_to_default(tmp_path):"total_input_tokens": 0,"total_input_cost_usd": 0.0,}- assert (- snapshot["display_session"]- == savings_tracker_module._empty_display_session()- )+ assert snapshot["display_session"] == savings_tracker_module._empty_display_session()assert snapshot["history"] == []@@ -254,9 +246,7 @@ def test_litellm_resolution_and_savings_estimation_fallbacks(monkeypatch):) == pytest.approx(0.2)fake_litellm.model_cost = {}- assert (- savings_tracker_module._estimate_compression_savings_usd("gpt-4o", 100) == 0.0- )+ assert savings_tracker_module._estimate_compression_savings_usd("gpt-4o", 100) == 0.0assert savings_tracker_module._estimate_input_cost_usd("gpt-4o", 100) == 0.0monkeypatch.setattr(@@ -264,19 +254,11 @@ def test_litellm_resolution_and_savings_estimation_fallbacks(monkeypatch):"cost_per_token",lambda **kwargs: (_ for _ in ()).throw(RuntimeError("boom")),)- assert (- savings_tracker_module._resolve_litellm_model("mystery-model")- == "mystery-model"- )- assert (- savings_tracker_module._estimate_compression_savings_usd("mystery-model", 100)- == 0.0- )+ assert savings_tracker_module._resolve_litellm_model("mystery-model") == "mystery-model"+ assert savings_tracker_module._estimate_compression_savings_usd("mystery-model", 100) == 0.0monkeypatch.setattr(savings_tracker_module, "LITELLM_AVAILABLE", False)- assert (- savings_tracker_module._estimate_compression_savings_usd("gpt-4o", 100) == 0.0- )+ assert savings_tracker_module._estimate_compression_savings_usd("gpt-4o", 100) == 0.0assert savings_tracker_module._estimate_input_cost_usd("gpt-4o", 100) == 0.0@@ -331,10 +313,7 @@ def test_display_session_rolls_after_inactivity_and_counts_zero_savings_requests"_utc_now",lambda: datetime(2026, 3, 27, 9, 45, tzinfo=timezone.utc),)- assert (- tracker.snapshot()["display_session"]- == savings_tracker_module._empty_display_session()- )+ assert tracker.snapshot()["display_session"] == savings_tracker_module._empty_display_session()tracker.record_request(model="gpt-4o",@@ -362,9 +341,7 @@ def test_display_session_rolls_after_inactivity_and_counts_zero_savings_requests}-def test_savings_tracker_rollups_preserve_spend_and_input_history(- tmp_path, monkeypatch-):+def test_savings_tracker_rollups_preserve_spend_and_input_history(tmp_path, monkeypatch):path = tmp_path / "proxy_savings.json"tracker = SavingsTracker(path=str(path),@@ -507,9 +484,7 @@ def test_savings_tracker_rollups_preserve_spend_and_input_history(]-def test_stats_history_persists_across_restarts_and_stats_stays_compatible(- tmp_path, monkeypatch-):+def test_stats_history_persists_across_restarts_and_stats_stays_compatible(tmp_path, monkeypatch):savings_path = tmp_path / "proxy_savings.json"monkeypatch.setenv("HEADROOM_SAVINGS_PATH", str(savings_path))monkeypatch.setattr(@@ -555,14 +530,13 @@ def test_stats_history_persists_across_restarts_and_stats_stays_compatible(]assert history_data["exports"]["available_series"][-2:] == ["weekly", "monthly"]assert history_data["series"]["hourly"][0]["total_input_tokens_delta"] == 120- assert history_data["series"]["hourly"][0][- "total_input_cost_usd_delta"- ] == pytest.approx(0.24)+ assert history_data["series"]["hourly"][0]["total_input_cost_usd_delta"] == pytest.approx(+ 0.24+ )assert stats_data["display_session"] == history_data["display_session"]assert (- stats_data["persistent_savings"]["display_session"]- == history_data["display_session"]+ stats_data["persistent_savings"]["display_session"] == history_data["display_session"])with TestClient(create_app(config)) as client:@@ -584,9 +558,7 @@ def test_stats_history_persists_across_restarts_and_stats_stays_compatible(assert updated["display_session"]["total_input_tokens"] == 240assert updated["display_session"]["savings_percent"] == pytest.approx(18.64)assert updated["series"]["daily"][0]["total_input_tokens_delta"] == 240- assert updated["series"]["daily"][0][- "total_input_cost_usd_delta"- ] == pytest.approx(0.48)+ assert updated["series"]["daily"][0]["total_input_cost_usd_delta"] == pytest.approx(0.48)persisted = json.loads(savings_path.read_text())assert persisted["lifetime"]["tokens_saved"] == 55