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
refactor(proxy): remove unreferenced and test-only helpers (#4074)
Tejas Choprae5cbec5f1a86
diff --git a/headroom/proxy/proxy_mode_policy.py b/headroom/proxy/proxy_mode_policy.pyindex bc93b145f..425813654 100644--- a/headroom/proxy/proxy_mode_policy.py+++ b/headroom/proxy/proxy_mode_policy.py@@ -56,12 +56,3 @@ def normalize_proxy_mode_decision(normalized=normalized,alias_used=key != normalized,)---def normalize_proxy_mode_value(- mode: str | None,- *,- default: str = PROXY_MODE_TOKEN,-) -> str:- """Return only the canonical proxy mode value."""- return normalize_proxy_mode_decision(mode, default=default).normalizeddiff --git a/headroom/proxy/rate_limit_policy.py b/headroom/proxy/rate_limit_policy.pyindex 9e57686c6..7e1654064 100644--- a/headroom/proxy/rate_limit_policy.py+++ b/headroom/proxy/rate_limit_policy.py@@ -36,14 +36,3 @@ def consume_from_bucket(wait_seconds = (required - available_tokens) * (60.0 / rate_per_minute)return False, available_tokens, wait_seconds---def stale_bucket_keys(- last_updates: dict[str, float],- *,- now: float,- stale_after_seconds: float,-) -> list[str]:- """Return bucket keys whose last update is older than the stale threshold."""- stale_before = now - stale_after_seconds- return [key for key, last_update in last_updates.items() if last_update < stale_before]diff --git a/headroom/proxy/tenant_key.py b/headroom/proxy/tenant_key.pyindex 62029d46a..772f566f5 100644--- a/headroom/proxy/tenant_key.py+++ b/headroom/proxy/tenant_key.py@@ -245,26 +245,6 @@ def _get_header(request: Any, name: str) -> str:return str(value)-def _get_auth_mode_str(request: Any) -> str:- """Read ``request.state.auth_mode`` as a string, or ``""`` if unset.-- F1 set this to an :class:`AuthMode` enum (a ``str`` subclass) on- every request. We don't import ``AuthMode`` here to keep this- module independent of the classifier — we only need the string- value, and the enum's ``str`` parent serializes transparently.- """- state = getattr(request, "state", None)- if state is None:- return ""- auth_mode = getattr(state, "auth_mode", None)- if auth_mode is None:- return ""- # AuthMode subclasses str so ``str(auth_mode)`` returns the enum- # name's value (``"payg"`` / ``"oauth"`` / ``"subscription"``) —- # not the Python repr.- return str(auth_mode)--def _sanitize_tenant_key(raw: str) -> str:"""Sanitize a tenant_key candidate. Returns ``""`` on rejection.diff --git a/headroom/proxy/tool_schema_compaction.py b/headroom/proxy/tool_schema_compaction.pyindex f8bad6f30..300300030 100644--- a/headroom/proxy/tool_schema_compaction.py+++ b/headroom/proxy/tool_schema_compaction.py@@ -193,12 +193,6 @@ def _cache_put(key: str, compacted: dict[str, Any], before: int, after: int) ->_compaction_cache[key] = (compacted, before, after)-def invalidate_cache() -> None:- """Clear the compaction cache (e.g. on config change)."""- with _cache_lock:- _compaction_cache.clear()--# ---------------------------------------------------------------------------# Layer 1: annotation-key compaction# ---------------------------------------------------------------------------diff --git a/tests/test_install/test_proxy_mode_default.py b/tests/test_install/test_proxy_mode_default.pyindex 1bbbf153c..61b0c00d0 100644--- a/tests/test_install/test_proxy_mode_default.py+++ b/tests/test_install/test_proxy_mode_default.py@@ -65,12 +65,12 @@ def test_token_mode_is_still_reachable() -> None:from headroom.cli.install import deploy, install_applyfrom headroom.proxy.proxy_mode_policy import (PROXY_MODE_TOKEN,- normalize_proxy_mode_value,+ normalize_proxy_mode_decision,)for command in (install_apply, deploy):param = next(p for p in command.params if p.name == "proxy_mode")assert param.type.name == "text", f"{command.name} --mode became restrictive"- assert normalize_proxy_mode_value("token") == PROXY_MODE_TOKEN- assert normalize_proxy_mode_value("token_headroom") == PROXY_MODE_TOKEN+ assert normalize_proxy_mode_decision("token").normalized == PROXY_MODE_TOKEN+ assert normalize_proxy_mode_decision("token_headroom").normalized == PROXY_MODE_TOKENdiff --git a/tests/test_proxy_mode_policy.py b/tests/test_proxy_mode_policy.pyindex 88f546069..bb6ab8476 100644--- a/tests/test_proxy_mode_policy.py+++ b/tests/test_proxy_mode_policy.py@@ -6,7 +6,6 @@ from headroom.proxy.proxy_mode_policy import (PROXY_MODE_CACHE,PROXY_MODE_TOKEN,normalize_proxy_mode_decision,- normalize_proxy_mode_value,)@@ -47,6 +46,6 @@ def test_decision_uses_default_and_marks_unknown_for_invalid_mode() -> None:assert decision.unknown is True-def test_value_helper_returns_only_canonical_mode() -> None:- assert normalize_proxy_mode_value("token_savings") == PROXY_MODE_TOKEN- assert normalize_proxy_mode_value("cache_mode") == PROXY_MODE_CACHE+def test_aliases_normalize_to_canonical_mode() -> None:+ assert normalize_proxy_mode_decision("token_savings").normalized == PROXY_MODE_TOKEN+ assert normalize_proxy_mode_decision("cache_mode").normalized == PROXY_MODE_CACHEdiff --git a/tests/test_rate_limit_policy.py b/tests/test_rate_limit_policy.pyindex 089f30028..5fdbd5148 100644--- a/tests/test_rate_limit_policy.py+++ b/tests/test_rate_limit_policy.py@@ -5,7 +5,6 @@ from __future__ import annotationsfrom headroom.proxy.rate_limit_policy import (consume_from_bucket,refilled_tokens,- stale_bucket_keys,)@@ -95,11 +94,3 @@ def test_consume_from_bucket_debt_delays_the_next_request() -> None:assert allowed is Falseassert remaining == -132_000assert wait_seconds == 79.8---def test_stale_bucket_keys_returns_only_old_buckets() -> None:- assert stale_bucket_keys(- {"fresh": 950, "edge": 400, "stale": 399},- now=1000,- stale_after_seconds=600,- ) == ["stale"]