Skip to content

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.

fix: Vertex AI support for Claude Code with ANTHROPIC_VERTEX_BASE_URL (#1393)

vladgrishcff7247efd6f
diff --git a/headroom/cli/wrap.py b/headroom/cli/wrap.py
index 0daf7a432..77e53f4d6 100644
--- a/headroom/cli/wrap.py
+++ b/headroom/cli/wrap.py
@@ -447,6 +447,10 @@ def _start_proxy(
# Ensure proxy subprocess uses UTF-8 (Windows defaults to cp1252)
proxy_env = os.environ.copy()
proxy_env["PYTHONIOENCODING"] = "utf-8"
+ # Vertex AI RST_STREAMs HTTP/2 connections (error_code:2). Force HTTP/1.1
+ # when wrapping a Vertex-mode client so upstream requests succeed.
+ if os.environ.get("CLAUDE_CODE_USE_VERTEX") or os.environ.get("ANTHROPIC_VERTEX_PROJECT_ID"):
+ proxy_env.setdefault("HEADROOM_HTTP2", "false")
# Tell the proxy which agent is being wrapped (for traffic learning output)
if agent_type != "unknown":
proxy_env["HEADROOM_AGENT_TYPE"] = agent_type
diff --git a/headroom/providers/proxy_routes.py b/headroom/providers/proxy_routes.py
index 40a4faea5..1760e0d1d 100644
--- a/headroom/providers/proxy_routes.py
+++ b/headroom/providers/proxy_routes.py
@@ -733,22 +733,25 @@ def register_provider_routes(app: FastAPI, proxy: Any) -> None:
return await vertex_publisher_passthrough(request, publisher, "rawPredict")
@app.post(
- "/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:rawPredict"
+ "/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:rawPredict"
)
async def vertex_raw_predict_no_version(
request: Request,
project: str,
location: str,
+ publisher: str,
model: str,
):
- del project
- target = _vertex_target_for_location(proxy, location).rstrip("/") + "/v1"
- return await proxy.handle_anthropic_messages(
- request,
- target,
- "vertex:anthropic",
- model,
- )
+ if publisher == "anthropic":
+ del project
+ target = _vertex_target_for_location(proxy, location).rstrip("/") + "/v1"
+ return await proxy.handle_anthropic_messages(
+ request,
+ target,
+ "vertex:anthropic",
+ model,
+ )
+ return await vertex_publisher_passthrough(request, publisher, "rawPredict")
@app.post(
"/{api_version}/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:streamRawPredict"
@@ -773,23 +776,26 @@ def register_provider_routes(app: FastAPI, proxy: Any) -> None:
return await vertex_publisher_passthrough(request, publisher, "streamRawPredict")
@app.post(
- "/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:streamRawPredict"
+ "/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:streamRawPredict"
)
async def vertex_stream_raw_predict_no_version(
request: Request,
project: str,
location: str,
+ publisher: str,
model: str,
):
- del project
- target = _vertex_target_for_location(proxy, location).rstrip("/") + "/v1"
- return await proxy.handle_anthropic_messages(
- request,
- target,
- "vertex:anthropic",
- model,
- True,
- )
+ if publisher == "anthropic":
+ del project
+ target = _vertex_target_for_location(proxy, location).rstrip("/") + "/v1"
+ return await proxy.handle_anthropic_messages(
+ request,
+ target,
+ "vertex:anthropic",
+ model,
+ True,
+ )
+ return await vertex_publisher_passthrough(request, publisher, "streamRawPredict")
@app.get("/v1/models")
async def list_models(request: Request):
diff --git a/tests/test_vertex_claude_compression.py b/tests/test_vertex_claude_compression.py
index 0396f78aa..bc39a2e75 100644
--- a/tests/test_vertex_claude_compression.py
+++ b/tests/test_vertex_claude_compression.py
@@ -138,3 +138,92 @@ def test_vertex_rawpredict_anthropic_runs_compression_handler(monkeypatch) -> No
assert captured["provider"] == "vertex:anthropic"
assert captured["base_url"] == "https://europe-west1-aiplatform.googleapis.com"
assert captured["model"] == "claude-sonnet-4-6"
+
+
+def test_vertex_rawpredict_versionless_anthropic_rewrites_to_v1(monkeypatch) -> None:
+ captured: dict[str, Any] = {}
+
+ async def fake(
+ self,
+ request,
+ base_url,
+ provider,
+ model,
+ force_stream=False,
+ ): # type: ignore[no-untyped-def]
+ captured.update(
+ path=request.url.path,
+ raw_path=request.scope.get("raw_path"),
+ base_url=str(base_url),
+ provider=str(provider),
+ model=str(model),
+ force_stream=force_stream,
+ )
+ return JSONResponse({"ok": True})
+
+ monkeypatch.setattr(HeadroomProxy, "handle_anthropic_messages", fake)
+
+ with TestClient(_default_vertex_app()) as client:
+ resp = client.post(
+ "/projects/p/locations/europe-west1/publishers/anthropic/models/"
+ "claude-sonnet-4-6:rawPredict",
+ json={"anthropic_version": "vertex-2023-10-16", "messages": []},
+ )
+
+ assert resp.status_code == 200
+ assert captured == {
+ "path": (
+ "/projects/p/locations/europe-west1/publishers/anthropic/models/"
+ "claude-sonnet-4-6:rawPredict"
+ ),
+ "raw_path": (
+ b"/projects/p/locations/europe-west1/publishers/anthropic/models/"
+ b"claude-sonnet-4-6:rawPredict"
+ ),
+ "base_url": "https://europe-west1-aiplatform.googleapis.com/v1",
+ "provider": "vertex:anthropic",
+ "model": "claude-sonnet-4-6",
+ "force_stream": False,
+ }
+
+
+def test_vertex_stream_rawpredict_versionless_anthropic_forces_stream(monkeypatch) -> None:
+ captured: dict[str, Any] = {}
+
+ async def fake(
+ self,
+ request,
+ base_url,
+ provider,
+ model,
+ force_stream=False,
+ ): # type: ignore[no-untyped-def]
+ captured.update(
+ path=request.url.path,
+ base_url=str(base_url),
+ provider=str(provider),
+ model=str(model),
+ force_stream=force_stream,
+ )
+ return JSONResponse({"ok": True})
+
+ monkeypatch.setattr(HeadroomProxy, "handle_anthropic_messages", fake)
+
+ with TestClient(_default_vertex_app()) as client:
+ resp = client.post(
+ "/projects/p/locations/europe-west1/publishers/anthropic/models/"
+ "claude-sonnet-4-6:streamRawPredict",
+ json={"anthropic_version": "vertex-2023-10-16", "messages": []},
+ )
+
+ assert resp.status_code == 200
+ assert captured == {
+ "path": (
+ "/projects/p/locations/europe-west1/publishers/anthropic/models/"
+ "claude-sonnet-4-6:streamRawPredict"
+ ),
+ "base_url": "https://europe-west1-aiplatform.googleapis.com/v1",
+ "provider": "vertex:anthropic",
+ "model": "claude-sonnet-4-6",
+ "force_stream": True,
+ }