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): honor bypass header and license gate in Anthropic batch create (#4411)
HetCreep4fd9cddfd2e9
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.pyindex 0f922a04e..c30010b33 100644--- a/headroom/proxy/handlers/anthropic.py+++ b/headroom/proxy/handlers/anthropic.py@@ -5771,7 +5771,11 @@ class AnthropicHandlerMixin:return capability_errorfrom headroom.ccr import CCRToolInjector- from headroom.proxy.helpers import MAX_REQUEST_BODY_SIZE, _read_request_json+ from headroom.proxy.helpers import (+ MAX_REQUEST_BODY_SIZE,+ _headroom_bypass_enabled,+ read_request_json_with_bytes,+ )from headroom.proxy.modes import is_cache_modefrom headroom.utils import extract_user_query@@ -5794,7 +5798,7 @@ class AnthropicHandlerMixin:# Parse requesttry:- body = await _read_request_json(request)+ body, original_body_bytes = await read_request_json_with_bytes(request)except (json.JSONDecodeError, ValueError) as e:return JSONResponse(status_code=400,@@ -5848,6 +5852,20 @@ class AnthropicHandlerMixin:request_id=request_id,)+ # Same two gates the OpenAI and Google batch paths apply: the client's+ # bypass/passthrough header, and the commercial gate (fails open when no+ # reporter is configured or no license info exists yet).+ _bypass = _headroom_bypass_enabled(request.headers)+ license_ok = (+ self.usage_reporter.should_compress if self.usage_reporter is not None else True+ )+ if _bypass:+ logger.info(f"[{request_id}] Compression skipped: reason=bypass_header")+ tags["passthrough_reason"] = "bypass_header"+ elif not license_ok:+ logger.info(f"[{request_id}] Compression skipped: reason=license_denied")+ tags["passthrough_reason"] = "license_denied"+# Track compression stats across all batch requeststotal_original_tokens = 0total_optimized_tokens = 0@@ -5875,6 +5893,10 @@ class AnthropicHandlerMixin:model, original_messages, params.get("system"))+ if _bypass or not license_ok:+ compressed_requests.append(batch_req)+ continue+if not messages or not self.config.optimize:# No messages or optimization disabled - pass through unchangedcompressed_requests.append(@@ -6024,14 +6046,15 @@ class AnthropicHandlerMixin:url = f"{self.ANTHROPIC_API_URL}/v1/messages/batches"try:- # Body is always mutated for batch (compressed requests).+ # Mutated unless bypassed: bypass forwards the client's exact bytes.response = await self._retry_request("POST",url,headers,body,- body_mutated=True,- mutation_reasons=["batch_compression"],+ original_body_bytes=original_body_bytes if _bypass else None,+ body_mutated=not _bypass,+ mutation_reasons=[] if _bypass else ["batch_compression"],request_id=request_id,forwarder_name="anthropic_batch",path_for_log="/v1/messages/batches",diff --git a/tests/test_proxy/test_anthropic_batch_bypass.py b/tests/test_proxy/test_anthropic_batch_bypass.pynew file mode 100755index 000000000..4baf504dc--- /dev/null+++ b/tests/test_proxy/test_anthropic_batch_bypass.py@@ -0,0 +1,102 @@+from __future__ import annotations++import json+from types import SimpleNamespace++import pytest++fastapi = pytest.importorskip("fastapi")+httpx = pytest.importorskip("httpx")++from fastapi.testclient import TestClient # noqa: E402++from headroom.proxy.server import ProxyConfig, create_app # noqa: E402++# Whitespace the canonical serializer would normalise away.+RAW_BODY = (+ b'{ "requests": [ { "custom_id": "req-1", "params": { "model": "claude-sonnet-4-6",'+ b' "max_tokens": 64, "messages": [ { "role": "user", "content": "hello" } ] } } ] }'+)+++def _post(*, headers: dict[str, str], license_ok: bool = True):+ config = ProxyConfig(+ optimize=True,+ mode="token",+ cache_enabled=False,+ rate_limit_enabled=False,+ cost_tracking_enabled=False,+ log_requests=False,+ ccr_inject_tool=False,+ ccr_handle_responses=False,+ ccr_context_tracking=False,+ image_optimize=False,+ )+ captured: dict[str, object] = {"pipeline_calls": 0}+ with TestClient(create_app(config)) as client:+ proxy = client.app.state.proxy+ real_reporter = proxy.usage_reporter+ if not license_ok:+ proxy.usage_reporter = SimpleNamespace(should_compress=False)++ def _apply(**kwargs): # noqa: ANN003+ captured["pipeline_calls"] += 1+ return SimpleNamespace(+ messages=kwargs["messages"],+ transforms_applied=[],+ timing={},+ tokens_before=100,+ tokens_after=100,+ waste_signals=None,+ )++ proxy.anthropic_pipeline.apply = _apply++ async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001+ captured["kwargs"] = kwargs+ captured["body"] = body+ return httpx.Response(200, json={"id": "msgbatch_1", "type": "message_batch"})++ proxy._retry_request = _fake_retry+ response = client.post(+ "/v1/messages/batches",+ headers={+ "x-api-key": "test-key",+ "anthropic-version": "2023-06-01",+ "content-type": "application/json",+ **headers,+ },+ content=RAW_BODY,+ )+ proxy.usage_reporter = real_reporter # shutdown calls the real reporter's stop()+ assert response.status_code == 200, response.text+ return captured+++ "bypass_headers",+ [{"x-headroom-bypass": "true"}, {"x-headroom-mode": "passthrough"}],+)+def test_batch_create_bypass_skips_compression_and_forwards_original_bytes(bypass_headers):+ captured = _post(headers=bypass_headers)+ assert captured["pipeline_calls"] == 0+ assert captured["kwargs"]["body_mutated"] is False+ assert captured["kwargs"]["original_body_bytes"] == RAW_BODY+ assert captured["body"]["requests"][0]["params"]["messages"] == [+ {"role": "user", "content": "hello"}+ ]+++def test_batch_create_license_denied_skips_compression():+ captured = _post(headers={}, license_ok=False)+ assert captured["pipeline_calls"] == 0+ assert captured["body"]["requests"][0]["params"]["messages"] == [+ {"role": "user", "content": "hello"}+ ]+++def test_batch_create_without_bypass_still_compresses():+ captured = _post(headers={})+ assert captured["pipeline_calls"] == 1+ assert captured["kwargs"]["body_mutated"] is True+ assert json.loads(RAW_BODY)["requests"][0]["custom_id"] == "req-1"