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): reject ping-only SSE from buffered CCR (#3682)
iuiuiu000fefceaa26
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.pyindex 33ce52171..7aeb457dd 100644--- a/headroom/proxy/handlers/anthropic.py+++ b/headroom/proxy/handlers/anthropic.py@@ -4171,9 +4171,13 @@ class AnthropicHandlerMixin:# ``stream`` is what the *client* asked for, not what# went upstream: a buffered CCR turn deliberately# requests JSON on behalf of a streaming client and- # re-emits SSE further down, and must keep doing so.+ # re-emits SSE further down. That same flip means the+ # upstream's stream contract is JSON, so its SSE reply+ # must still be reconstructed before CCR processing;+ # otherwise a keepalive-only stream can be relayed as a+ # successful but contentless response (#3266).if should_recover_sse_reply(- client_requested_stream=bool(stream),+ client_requested_stream=bool(stream and not buffered_stream_ccr),status_code=response.status_code,content_type=response.headers.get("content-type"),body_is_event_stream=_looks_like_sse_response(response),diff --git a/tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py b/tests/test_proxy/test_anthropic_streaming_ccr_retrieve.pyindex 70889b647..e36e2ab09 100644--- a/tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py+++ b/tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py@@ -628,6 +628,42 @@ def test_buffered_ccr_rejects_malformed_success_as_502() -> None:assert b"gateway timeout" not in response.content+def test_buffered_ccr_rejects_ping_only_sse_as_502() -> None:+ """A 200 SSE body is not a successful answer to a stream:false request.++ A provider that ignores the buffered flip can return only keepalives. The+ client asked for SSE, but the upstream did not, so relaying the shell as a+ successful stream loses the turn behind a valid-looking 200 (#3266).+ """+ config = _make_config()+ with patch("headroom.proxy.server.AnyLLMBackend"):+ app = create_app(config)+ with TestClient(app) as client:+ proxy = app.state.proxy+ proxy._retry_request = AsyncMock(+ return_value=httpx.Response(+ 200,+ content=b'event: ping\ndata: {"type":"ping"}\n\n',+ headers={"content-type": "text/event-stream"},+ )+ )+ response = client.post(+ "/v1/messages",+ headers={"x-api-key": "test-key", "anthropic-version": "2023-06-01"},+ json={+ "model": "claude-sonnet-4-6",+ "max_tokens": 64,+ "stream": True,+ "tools": [create_ccr_tool_definition("anthropic")],+ "messages": [{"role": "user", "content": _buffered("ping only")}],+ },+ )++ assert response.status_code == 502+ assert "text/event-stream" not in response.headers["content-type"]+ assert response.json()["error"]["type"] == "upstream_protocol_error"++@pytest.mark.asyncioasync def test_buffered_ccr_late_failure_returns_sanitized_json_error() -> None:"""A slow crash gets the same 502 the fast one does, not a downgraded 200."""