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(anthropic): stop the spurious compaction warning and drop dead handler code (#3678)
trs-806e712672cb5c
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.pyindex 7aeb457dd..b1f0a8869 100644--- a/headroom/proxy/handlers/anthropic.py+++ b/headroom/proxy/handlers/anthropic.py@@ -1032,7 +1032,6 @@ class AnthropicHandlerMixin:# Check request body sizecontent_length = request.headers.get("content-length")if content_length and int(content_length) > MAX_REQUEST_BODY_SIZE:- await _finalize_pre_upstream()return JSONResponse(status_code=413,content={@@ -1055,7 +1054,6 @@ class AnthropicHandlerMixin:async with stage_timer.measure("read_request_json"):body, original_body_bytes = await read_request_json_with_bytes(request)except (json.JSONDecodeError, ValueError) as e:- await _finalize_pre_upstream()return JSONResponse(status_code=400,content={@@ -1132,7 +1130,6 @@ class AnthropicHandlerMixin:# Validate message array sizeif len(messages) > MAX_MESSAGE_ARRAY_LENGTH:- await _finalize_pre_upstream()return JSONResponse(status_code=400,content={@@ -1424,9 +1421,6 @@ class AnthropicHandlerMixin:f"hits={cached.hit_count}")- # Unit 4: release the pre-upstream semaphore on cache- # hit — no upstream call will happen.- await _finalize_pre_upstream()return Response(content=cached.response_body,headers=response_headers,@@ -1455,9 +1449,6 @@ class AnthropicHandlerMixin:if hasattr(e, "reason"):from fastapi.responses import JSONResponse as _JSONResp- # Unit 4: release the pre-upstream semaphore on- # security block — no upstream call will happen.- await _finalize_pre_upstream()return _JSONResp(status_code=403,content={@@ -2969,15 +2960,15 @@ class AnthropicHandlerMixin:model=model,request_id=request_id,)- if _sys_modified:- transforms_applied.append("anthropic:system_prompt_compaction")- logger.debug(- "[%s] system prompt compaction: %d -> %d bytes (%.0f%% saved)",- request_id,- _sys_before,- _sys_after,- (1 - _sys_after / max(_sys_before, 1)) * 100,- )+ if _sys_modified:+ transforms_applied.append("anthropic:system_prompt_compaction")+ logger.debug(+ "[%s] system prompt compaction: %d -> %d bytes (%.0f%% saved)",+ request_id,+ _sys_before,+ _sys_after,+ (1 - _sys_after / max(_sys_before, 1)) * 100,+ )except Exception as _sys_compaction_exc:_sys_modified = Falselogger.warning(@@ -3683,8 +3674,6 @@ class AnthropicHandlerMixin:)except Exception as e:logger.error(f"[{request_id}] Bedrock backend error: {e}")- # Unit 4: release the pre-upstream semaphore on error.- await _finalize_pre_upstream()return JSONResponse(status_code=500,content={@@ -5036,12 +5025,6 @@ class AnthropicHandlerMixin:# Log full error details internally for debugginglogger.error(f"[{request_id}] Request failed: {type(e).__name__}: {e}")- # Try fallback if enabled- if self.config.fallback_enabled and self.config.fallback_provider == "openai":- logger.info(f"[{request_id}] Attempting fallback to OpenAI")- # Convert to OpenAI format and retry- # (simplified - would need message format conversion)-# Return sanitized error message to client (don't expose internal details)return JSONResponse(status_code=502,diff --git a/headroom/proxy/models.py b/headroom/proxy/models.pyindex 4aa7163c1..5957f5773 100644--- a/headroom/proxy/models.py+++ b/headroom/proxy/models.py@@ -371,10 +371,6 @@ class ProxyConfig:# (repeatable); env: HEADROOM_COMPRESSORS.compressors: set[str] | None = None- # Fallback- fallback_enabled: bool = False- fallback_provider: str | None = None-# Timeoutsrequest_timeout_seconds: int = 300connect_timeout_seconds: int = 10diff --git a/tests/test_anthropic_pre_upstream_backpressure.py b/tests/test_anthropic_pre_upstream_backpressure.pyindex 6524c4af9..6447e65c2 100644--- a/tests/test_anthropic_pre_upstream_backpressure.py+++ b/tests/test_anthropic_pre_upstream_backpressure.py@@ -39,6 +39,7 @@ from fastapi.testclient import TestClientfrom headroom.cli.proxy import proxy as proxy_clifrom headroom.proxy.handlers.anthropic import AnthropicHandlerMixin+from headroom.proxy.helpers import MAX_MESSAGE_ARRAY_LENGTH, MAX_REQUEST_BODY_SIZEfrom headroom.proxy.models import CacheEntry, ProxyConfigfrom headroom.proxy.server import HeadroomProxy, create_app@@ -136,8 +137,6 @@ class _DummyAnthropicHandler(AnthropicHandlerMixin):mode="token",cache_enabled=False,rate_limit_enabled=False,- fallback_enabled=False,- fallback_provider=None,prefix_freeze_enabled=False,memory_enabled=False,)@@ -291,8 +290,8 @@ class _DummyAnthropicHandler(AnthropicHandlerMixin):)-def _build_request(body: dict, headers: dict[str, str]) -> Request:- payload = json.dumps(body).encode("utf-8")+def _build_request(body: dict | bytes, headers: dict[str, str]) -> Request:+ payload = body if isinstance(body, bytes) else json.dumps(body).encode("utf-8")async def receive():return {"type": "http.request", "body": payload, "more_body": False}@@ -723,8 +722,6 @@ def test_compression_is_not_bypassed_when_gated(stage_log_capture):mode="token",cache_enabled=False,rate_limit_enabled=False,- fallback_enabled=False,- fallback_provider=None,prefix_freeze_enabled=False,memory_enabled=False,anthropic_pre_upstream_concurrency=2,@@ -848,11 +845,12 @@ def test_auto_computed_default_on_this_machine():# --------------------------------------------------------------------------- ## Semaphore released on HTTPException / early-exit paths even with an #-# already-held permit. Explicitly covers the 4 pre-upstream early exits: #+# already-held permit. Explicitly covers the pre-upstream early exits: ## - rate_limiter deny (429) ## - cost_tracker block (429) ## - security scan block (403) ## - cache hit (200) #+# - oversized body (413), invalid JSON (400), too many messages (400) ## Each test holds 1 permit of a Semaphore(2) with a concurrent request, ## then verifies the handler restores ``_value`` to the original after ## the early return. #@@ -910,7 +908,15 @@ class _CacheHit:@pytest.mark.parametrize("scenario",- ["rate_limiter", "cost_tracker", "security", "cache"],+ [+ "rate_limiter",+ "cost_tracker",+ "security",+ "cache",+ "oversized_body",+ "invalid_json",+ "too_many_messages",+ ],)def test_early_exit_paths_release_semaphore_under_contention(scenario):"""Hold one permit of a Semaphore(1) with a concurrent request, trigger@@ -931,13 +937,19 @@ def test_early_exit_paths_release_semaphore_under_contention(scenario):elif scenario == "cache":handler.cache = _CacheHit()- req = _build_request(- {- "model": "claude-3-5-sonnet-latest",- "messages": [{"role": "user", "content": "hello"}],- },- {"authorization": "Bearer sk-ant-api-test"},- )+ body: dict | bytes = {+ "model": "claude-3-5-sonnet-latest",+ "messages": [{"role": "user", "content": "hello"}],+ }+ headers = {"authorization": "Bearer sk-ant-api-test"}+ if scenario == "oversized_body":+ headers["content-length"] = str(MAX_REQUEST_BODY_SIZE + 1)+ elif scenario == "invalid_json":+ body = b"{not json"+ elif scenario == "too_many_messages":+ body["messages"] = [{"role": "user", "content": "hi"}] * (MAX_MESSAGE_ARRAY_LENGTH + 1)+ req = _build_request(body, headers)+ expected_status = {"oversized_body": 413, "invalid_json": 400, "too_many_messages": 400}# Drive several iterations to confirm each early-exit call fully# releases the semaphore rather than leaking a permit AND that the@@ -968,6 +980,8 @@ def test_early_exit_paths_release_semaphore_under_contention(scenario):# security returns a JSONResponse; cache returns a Response.assert raised is None, f"{scenario}: unexpected exception {raised!r}"assert result is not None+ if scenario in expected_status:+ assert result.status_code == expected_status[scenario], result.bodyassert sem._value == original_value, (f"{scenario}: semaphore leak got={sem._value}, want={original_value}")diff --git a/tests/test_anthropic_stage_timings.py b/tests/test_anthropic_stage_timings.pyindex db8a2d3a1..58dfe7400 100644--- a/tests/test_anthropic_stage_timings.py+++ b/tests/test_anthropic_stage_timings.py@@ -68,8 +68,6 @@ class _DummyAnthropicHandler(AnthropicHandlerMixin):mode="token",cache_enabled=False,rate_limit_enabled=False,- fallback_enabled=False,- fallback_provider=None,prefix_freeze_enabled=False,memory_enabled=False,)diff --git a/tests/test_cache_mode_cold_start.py b/tests/test_cache_mode_cold_start.pyindex 3acc75bcd..4b85c7479 100644--- a/tests/test_cache_mode_cold_start.py+++ b/tests/test_cache_mode_cold_start.py@@ -107,8 +107,6 @@ class _DummyAnthropicHandler(AnthropicHandlerMixin):mode="cache",cache_enabled=False,rate_limit_enabled=False,- fallback_enabled=False,- fallback_provider=None,prefix_freeze_enabled=False,memory_enabled=False,)diff --git a/tests/test_cold_start_fast_pass.py b/tests/test_cold_start_fast_pass.pyindex a230facb4..242612cd1 100644--- a/tests/test_cold_start_fast_pass.py+++ b/tests/test_cold_start_fast_pass.py@@ -105,8 +105,6 @@ class _DummyAnthropicHandler(AnthropicHandlerMixin):mode="token",cache_enabled=False,rate_limit_enabled=False,- fallback_enabled=False,- fallback_provider=None,prefix_freeze_enabled=False,memory_enabled=False,)diff --git a/tests/test_system_compaction_handler.py b/tests/test_system_compaction_handler.pynew file mode 100644index 000000000..724626872--- /dev/null+++ b/tests/test_system_compaction_handler.py@@ -0,0 +1,81 @@+"""The Anthropic handler's system-prompt compaction must tolerate a pipeline+with no ContentRouter instead of logging a spurious failure, and must record+the transform when a router does rewrite the prompt."""++from __future__ import annotations++import logging+from typing import Any++import pytest++pytest.importorskip("fastapi")++from fastapi.testclient import TestClient # noqa: E402++from headroom.proxy.server import ProxyConfig, create_app # noqa: E402+++def _post_with_system_compaction(+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+) -> str:+ """POST one /v1/messages request with HEADROOM_SYSTEM_COMPACT on; return the log."""+ monkeypatch.setenv("HEADROOM_SYSTEM_COMPACT", "1")+ app = create_app(+ ProxyConfig(+ cache_enabled=False,+ rate_limit_enabled=False,+ cost_tracking_enabled=False,+ anthropic_api_url="http://127.0.0.1:9",+ )+ )+ # The headroom logger does not propagate to root, so capture it directly.+ proxy_logger = logging.getLogger("headroom.proxy")+ proxy_logger.addHandler(caplog.handler)+ caplog.set_level(logging.DEBUG, logger="headroom.proxy")+ try:+ with TestClient(app) as client:+ client.post(+ "/v1/messages",+ headers={"x-api-key": "sk-ant-test"},+ json={+ "model": "claude-sonnet-4-5",+ "max_tokens": 8,+ "system": "You are terse.",+ "messages": [{"role": "user", "content": "hi"}],+ },+ )+ finally:+ proxy_logger.removeHandler(caplog.handler)+ assert "Request failed" in caplog.text # reached the (dead) upstream, so compaction ran+ return caplog.text+++def test_system_compaction_without_content_router_is_not_a_failure(+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+) -> None:+ monkeypatch.setattr(+ "headroom.transforms.compression_units.find_content_router", lambda _pipeline: None+ )+ log = _post_with_system_compaction(monkeypatch, caplog)+ assert "system prompt compaction FAILED" not in log+++def test_system_compaction_with_content_router_records_the_transform(+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+) -> None:+ router = object()+ monkeypatch.setattr(+ "headroom.transforms.compression_units.find_content_router", lambda _pipeline: router+ )++ def fake_compact(+ payload: dict[str, Any], **kwargs: Any+ ) -> tuple[dict[str, Any], bool, int, int]:+ assert kwargs["router"] is router+ return payload, True, 100, 40++ monkeypatch.setattr("headroom.proxy.system_compaction.compact_system_prompt", fake_compact)+ log = _post_with_system_compaction(monkeypatch, caplog)+ assert "system prompt compaction: 100 -> 40 bytes (60% saved)" in log+ assert "system prompt compaction FAILED" not in log