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): hoist compression_policy outside is_token_mode branch (F2.1 c5 followup)
chopratejas708b87a19fbe
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.pyindex 45251393b..b1c2b5570 100644--- a/headroom/proxy/handlers/anthropic.py+++ b/headroom/proxy/handlers/anthropic.py@@ -846,6 +846,18 @@ class AnthropicHandlerMixin:else None)+ # F2.1 c5/5: derive the per-request CompressionPolicy+ # from the auth_mode classified at request entry. The+ # policy short-circuits CacheAligner for subscription+ # users (closes the cache-instability complaints in+ # #327/#388). When the enforcement env var is off, the+ # policy collapses to PAYG so behaviour is unchanged.+ # Hoisted here so all three pipeline.apply call sites+ # (token / non-cache / cache-delta) see the same policy.+ from headroom.transforms.compression_policy import resolve_policy++ compression_policy = resolve_policy(getattr(request.state, "auth_mode", None))+if is_token_mode(self.config.mode):comp_cache = self._get_compression_cache(session_id)@@ -880,17 +892,6 @@ class AnthropicHandlerMixin:# Record all tool_results in the verified frozen prefix as stablecomp_cache.mark_stable_from_messages(messages, frozen_message_count)- # F2.1 c5/5: derive the per-request CompressionPolicy- # from the auth_mode classified at request entry. The- # policy short-circuits CacheAligner for subscription- # users (closes the cache-instability complaints in- # #327/#388). When the enforcement env var is off, the- # policy collapses to PAYG so behaviour is unchanged.- from headroom.transforms.compression_policy import resolve_policy-- compression_policy = resolve_policy(- getattr(request.state, "auth_mode", None)- )async with stage_timer.measure("compression_first_stage"):result = await self._run_compression_in_executor(lambda: self.anthropic_pipeline.apply(@@ -941,6 +942,7 @@ class AnthropicHandlerMixin:frozen_message_count=frozen_message_count,biases=biases,request_id=request_id,+ compression_policy=compression_policy,),timeout=COMPRESSION_TIMEOUT_SECONDS,)@@ -971,6 +973,7 @@ class AnthropicHandlerMixin:frozen_message_count=0,biases=biases,request_id=request_id,+ compression_policy=compression_policy,),timeout=COMPRESSION_TIMEOUT_SECONDS,)diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex 2b2805b5b..ab77b9913 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -422,6 +422,14 @@ class OpenAIHandlerMixin:try:context_limit = self.openai_provider.get_context_limit(model)+ # F2.1 c5/5: per-request CompressionPolicy. Hoisted out of+ # the is_token_mode branch so the else (non-token) branch+ # below can pass it through too. See the equivalent block+ # in handlers/anthropic.py.+ from headroom.transforms.compression_policy import resolve_policy++ compression_policy = resolve_policy(getattr(request.state, "auth_mode", None))+if is_token_mode(self.config.mode):comp_cache = self._get_compression_cache(openai_session_id)@@ -431,11 +439,6 @@ class OpenAIHandlerMixin:# Re-freeze boundaryopenai_frozen_count = comp_cache.compute_frozen_count(messages)- # F2.1 c5/5: per-request CompressionPolicy. See the- # equivalent block in handlers/anthropic.py.- from headroom.transforms.compression_policy import resolve_policy-- compression_policy = resolve_policy(getattr(request.state, "auth_mode", None))result = await self._run_compression_in_executor(lambda: self.openai_pipeline.apply(messages=working_messages,