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): let HTTPException propagate past Anthropic catch-all
Adryan Eka Vandra63541051463c
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.pyindex 08778a6dd..4ce34ce5f 100644--- a/headroom/proxy/handlers/anthropic.py+++ b/headroom/proxy/handlers/anthropic.py@@ -1616,7 +1616,14 @@ class AnthropicHandlerMixin:status_code=response.status_code,headers=response_headers,)-+ except HTTPException:+ # FastAPI HTTPException carries its own status code, headers,+ # and client-facing message (e.g. 429 with Retry-After, 413 for+ # oversized bodies). Let FastAPI's own exception handler produce+ # the response — swallowing it into the 502 catch-all below+ # would regress rate-limit and budget responses to 502 with no+ # Retry-After header. The outer finally still runs.+ raiseexcept Exception as e:await self.metrics.record_failed(provider="anthropic")# Log full error details internally for debuggingdiff --git a/tests/test_anthropic_pre_upstream_backpressure.py b/tests/test_anthropic_pre_upstream_backpressure.pyindex 8867f79c3..456228f59 100644--- a/tests/test_anthropic_pre_upstream_backpressure.py+++ b/tests/test_anthropic_pre_upstream_backpressure.py@@ -709,16 +709,36 @@ def test_early_exit_paths_release_semaphore_under_contention(scenario):)# Drive several iterations to confirm each early-exit call fully- # releases the semaphore rather than leaking a permit.+ # releases the semaphore rather than leaking a permit AND that the+ # exception type or response status matches the contract for this+ # scenario. `except Exception: pass` would mask the 62d0a50 regression+ # where HTTPException got swallowed and turned into a 502 JSONResponse.+ from fastapi import HTTPException+for _ in range(3):+ raised: BaseException | None = None+ result = Nonetry:- await handler.handle_anthropic_messages(req)- except Exception:- # rate_limiter + cost_tracker raise HTTPException(429);+ result = await handler.handle_anthropic_messages(req)+ except HTTPException as exc:+ raised = exc++ if scenario in ("rate_limiter", "cost_tracker"):+ # These paths MUST surface HTTPException(429) so FastAPI's+ # exception handler emits the proper status + Retry-After.+ assert isinstance(raised, HTTPException), (+ f"{scenario}: expected HTTPException to propagate, got "+ f"raised={raised!r} result={result!r}"+ )+ assert raised.status_code == 429, (+ f"{scenario}: wrong status code — got {raised.status_code}"+ )+ else:# security returns a JSONResponse; cache returns a Response.- # Any exception that escapes is acceptable — what matters is- # the semaphore state after the handler returns.- pass+ assert raised is None, (+ f"{scenario}: unexpected exception {raised!r}"+ )+ assert result is not Noneassert sem._value == original_value, (f"{scenario}: semaphore leak "f"got={sem._value}, want={original_value}"