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 x-headroom-base-url on provider passthrough routes (#4596)
Chen Yufeiyang5c8772b7210c
diff --git a/headroom/providers/proxy_routes.py b/headroom/providers/proxy_routes.pyindex cba7eccfd..a86887822 100644--- a/headroom/providers/proxy_routes.py+++ b/headroom/providers/proxy_routes.py@@ -108,6 +108,25 @@ def _register_provider_passthrough_route(spec: ProviderPassthroughRoute,) -> None:async def provider_passthrough(request: Request):+ # Honor the per-request upstream override like /v1/messages and the+ # catch-all do. Clients that speak one wire format but authenticate+ # against a custom OpenAI-compatible gateway must reach that gateway+ # for embeddings/audio/metadata too, not the default provider target+ # (the same gap as the model-metadata routes, #4302). Telemetry keeps+ # the route spec labels: they are bounded by this module, so the+ # request-controlled host never becomes a stats label (#3759), while+ # `handle_passthrough` still records the outcome for these known routes.+ custom_base = request.headers.get("x-headroom-base-url", "").strip()+ if custom_base:+ if not await is_safe_upstream_url_async(custom_base):+ logger.warning("rejecting unsafe x-headroom-base-url: %r", custom_base)+ raise HTTPException(status_code=400, detail="Rejected unsafe upstream base URL")+ return await proxy.handle_passthrough(+ request,+ custom_base.rstrip("/"),+ spec.sub_path,+ spec.provider_name,+ )return await proxy.handle_passthrough(request,_api_target(proxy, spec.provider_name),diff --git a/tests/test_provider_proxy_routes.py b/tests/test_provider_proxy_routes.pyindex b8287f8d8..855d6a564 100644--- a/tests/test_provider_proxy_routes.py+++ b/tests/test_provider_proxy_routes.py@@ -144,6 +144,41 @@ def test_provider_passthrough_routes_forward_expected_targets(monkeypatch) -> Noassert client.get("/v1beta/models").json()["provider"] == "gemini"assert client.get("/v1beta/models/demo").json()["sub_path"] == "models"assert client.post("/v1beta/models/demo:embedContent").json()["sub_path"] == "embedContent"+ # Provider passthrough routes must honor the per-request upstream like+ # /v1/messages and the catch-all do. Without this, a custom+ # OpenAI-compatible provider's embeddings/audio/metadata requests are+ # forwarded to the default provider target and fail with 401.+ custom_embeddings = client.post(+ "/v1/embeddings",+ headers={"x-headroom-base-url": "https://custom.example/base/"},+ ).json()+ assert custom_embeddings["base_url"] == "https://custom.example/base"+ assert custom_embeddings["path"] == "/v1/embeddings"+ # Known provider routes keep their bounded labels so handle_passthrough+ # records the outcome for the overridden upstream; the custom host is+ # never turned into a label.+ assert custom_embeddings["sub_path"] == "embeddings"+ assert custom_embeddings["provider"] == "openai"+ custom_count_tokens = client.post(+ "/v1/messages/count_tokens",+ headers={"x-headroom-base-url": "https://custom.example/anthropic/"},+ ).json()+ assert custom_count_tokens["base_url"] == "https://custom.example/anthropic"+ assert custom_count_tokens["sub_path"] == "count_tokens"+ assert custom_count_tokens["provider"] == "anthropic"+ custom_gemini = client.get(+ "/v1beta/models",+ headers={"x-headroom-base-url": "https://custom.example/gemini/"},+ ).json()+ assert custom_gemini["base_url"] == "https://custom.example/gemini"+ assert custom_gemini["sub_path"] == "models"+ assert custom_gemini["provider"] == "gemini"+ # An unsafe override is rejected instead of being relayed.+ unsafe_embeddings = client.post(+ "/v1/embeddings",+ headers={"x-headroom-base-url": "http://127.0.0.1:9"},+ )+ assert unsafe_embeddings.status_code == 400assert client.post("/v1/projects/p/locations/us-central1/publishers/google/models/gemini-2.0-flash:generateContent").json() == {