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): keep json_bloat waste signals out of the catch-all lifetime bucket (#3430)
Chris Flippene59cf1012a2b
diff --git a/headroom/proxy/persistent_metrics.py b/headroom/proxy/persistent_metrics.pyindex 2aeab96e7..6028fe72c 100644--- a/headroom/proxy/persistent_metrics.py+++ b/headroom/proxy/persistent_metrics.py@@ -16,9 +16,12 @@ MAX_EXPOSED_MODELS = 100MAX_LABEL_LENGTH = 128KNOWN_MISS_REASONS = frozenset({"ttl_expiry", "prefix_change", "unknown"})+# Names must match ``WasteSignals.to_dict()`` in headroom/config.py — the+# parser emits ``json_bloat``; an allowlist that says ``json_noise`` silently+# shoves the largest waste category into the catch-all bucket.KNOWN_WASTE_SIGNALS = frozenset({- "json_noise",+ "json_bloat","html_noise","base64","whitespace",@@ -174,8 +177,12 @@ class PersistentMetricsState:raw_cost = _dict_or_empty(source.get("cost"))for key in ("input_usd", "compression_savings_usd", "cache_savings_usd"):result["cost"][key] = round(_coerce_float(raw_cost.get(key)), 6)+ # Record-time puts unrecognised names in ``other`` (see+ # ``record_request``); load-time must do the same, or every restart+ # relabels the whole ``other`` bucket as ``unknown`` and the two+ # grow side by side.result["waste_signals"] = self._normalize_enum_map(- source.get("waste_signals"), KNOWN_WASTE_SIGNALS+ source.get("waste_signals"), KNOWN_WASTE_SIGNALS, fallback="other")raw_models = _dict_or_empty(source.get("models"))@@ -204,12 +211,14 @@ class PersistentMetricsState:return result@staticmethod- def _normalize_enum_map(raw: Any, allowed: frozenset[str]) -> dict[str, int]:+ def _normalize_enum_map(+ raw: Any, allowed: frozenset[str], *, fallback: str = "unknown"+ ) -> dict[str, int]:result: dict[str, int] = {}if not isinstance(raw, dict):return resultfor key, value in raw.items():- label = key if isinstance(key, str) and key in allowed else "unknown"+ label = key if isinstance(key, str) and key in allowed | {fallback} else fallbackresult[label] = result.get(label, 0) + _coerce_int(value)return resultdiff --git a/tests/test_persistent_metrics.py b/tests/test_persistent_metrics.pyindex 23a1d45ff..2f517c23e 100644--- a/tests/test_persistent_metrics.py+++ b/tests/test_persistent_metrics.py@@ -153,3 +153,48 @@ def test_state_normalizes_invalid_values_and_unknown_dimension_labels() -> None:assert snapshot["requests"]["by_stack"] == {"other": 1}assert snapshot["by_model"]["other"]["input_tokens"] == 7assert snapshot["waste_signals"] == {"other": 9}+++def test_json_bloat_waste_signal_is_a_named_bucket() -> None:+ """The parser emits ``json_bloat`` (WasteSignals.to_dict); it must not fall to ``other``."""+ state = _new_state()+ state.record_request(+ provider="anthropic",+ stack="claude",+ model="claude-sonnet-5",+ waste_signals={"json_bloat": 500, "html_noise": 20},+ )++ snapshot = state.snapshot(persistence={"enabled": True, "healthy": True})++ assert snapshot["waste_signals"] == {"json_bloat": 500, "html_noise": 20}+++def test_waste_signal_other_bucket_survives_reload() -> None:+ """Reloading persisted state must keep ``other`` as ``other``, not relabel it ``unknown``.++ Before this fix, unrecognised names went to ``other`` at record time but to+ ``unknown`` at load time, so every restart moved the whole ``other`` bucket+ into a new ``unknown`` bucket and the two grew side by side.+ """+ state = PersistentMetricsState(+ {+ "waste_signals": {"other": 40, "unknown": 60, "html_noise": 5, "bogus": 3},+ },+ now=lambda: FIXED_NOW,+ )++ snapshot = state.snapshot(persistence={"enabled": True, "healthy": True})++ assert snapshot["waste_signals"] == {"other": 103, "html_noise": 5}+++def test_miss_reasons_still_fall_back_to_unknown_on_reload() -> None:+ state = PersistentMetricsState(+ {"prefix_cache": {"misses_by_reason": {"ttl_expiry": 2, "bogus": 1}}},+ now=lambda: FIXED_NOW,+ )++ snapshot = state.snapshot(persistence={"enabled": True, "healthy": True})++ assert snapshot["prefix_cache"]["misses_by_reason"] == {"ttl_expiry": 2, "unknown": 1}