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
style: ruff format parser.py, content_router.py, smart_crusher.py
Tejas Chopra7fd1f6f5f135
diff --git a/headroom/parser.py b/headroom/parser.pyindex 731dcd333..28a9db9cb 100644--- a/headroom/parser.py+++ b/headroom/parser.py@@ -70,7 +70,9 @@ def detect_waste_signals(text: str, tokenizer: Tokenizer) -> WasteSignals:# Count tokens that could be saved by normalizing whitespace to single spacesws_text = "".join(ws_matches)normalized_text = " ".join(ws_matches)- signals.whitespace_tokens = max(0, tokenizer.count_text(ws_text) - tokenizer.count_text(normalized_text))+ signals.whitespace_tokens = max(+ 0, tokenizer.count_text(ws_text) - tokenizer.count_text(normalized_text)+ )# Large JSON blocksjson_matches = JSON_BLOCK_PATTERN.findall(text)diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.pyindex e4759b137..4eb13bc04 100644--- a/headroom/transforms/content_router.py+++ b/headroom/transforms/content_router.py@@ -288,7 +288,9 @@ class ContentRouterConfig:# compression. At 10 msgs, protects ~5 Reads. At 100 msgs, protects ~10.# Old Reads beyond this window become compressible even though they are# in DEFAULT_EXCLUDE_TOOLS. 0.0 = always exclude all (old behavior).- protect_recent_reads_fraction: float = 0.0 # 0.0 = protect ALL excluded-tool outputs (safest for coding agents)+ protect_recent_reads_fraction: float = (+ 0.0 # 0.0 = protect ALL excluded-tool outputs (safest for coding agents)+ )# Adaptive compression ratio: scales with context pressure.# At low pressure (<30% full), use the relaxed threshold (reject marginal).diff --git a/headroom/transforms/smart_crusher.py b/headroom/transforms/smart_crusher.pyindex 0f29796f1..6d42f37e4 100644--- a/headroom/transforms/smart_crusher.py+++ b/headroom/transforms/smart_crusher.py@@ -506,9 +506,7 @@ def _detect_score_field_statistically(stats: FieldStats, items: list[dict]) -> t# Check for descending sortnum_pairs = len(values_in_order) - 1descending_count = sum(- 1- for i in range(num_pairs)- if values_in_order[i] >= values_in_order[i + 1]+ 1 for i in range(num_pairs) if values_in_order[i] >= values_in_order[i + 1])if num_pairs > 0 and descending_count / num_pairs > 0.7:confidence += 0.3