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(proxy): reject Anthropic batch operations on Copilot (#3318)

Rod Boev871bbde37ae2
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.py
index c757ec9b7..7d67ce66f 100644
--- a/headroom/proxy/handlers/anthropic.py
+++ b/headroom/proxy/handlers/anthropic.py
@@ -31,7 +31,11 @@ import httpx
from headroom.agent_savings import proxy_pipeline_kwargs
from headroom.ccr.context_tracker import looks_like_claude_code_compact_summary
from headroom.ccr.marker_resolution import resolve_markers_in_response
-from headroom.copilot_auth import apply_copilot_api_auth, build_copilot_upstream_url
+from headroom.copilot_auth import (
+ apply_copilot_api_auth,
+ build_copilot_upstream_url,
+ is_copilot_upstream_url,
+)
from headroom.pipeline import PipelineStage, summarize_routing_markers
from headroom.proxy.auth_mode import (
classify_auth_mode,
@@ -5062,6 +5066,26 @@ class AnthropicHandlerMixin:
# permanently. The emit function is idempotent.
await _finalize_pre_upstream()
+ def _anthropic_batch_capability_error(self) -> Response | None:
+ """Return the stable client error for a Copilot batch target."""
+ if not is_copilot_upstream_url(self.ANTHROPIC_API_URL):
+ return None
+
+ from fastapi.responses import JSONResponse
+
+ return JSONResponse(
+ status_code=501,
+ content={
+ "type": "error",
+ "error": {
+ "type": "api_error",
+ "message": (
+ "Anthropic batch operations are not supported for Copilot targets."
+ ),
+ },
+ },
+ )
+
async def handle_anthropic_batch_create(
self,
request: Request,
@@ -5087,6 +5111,9 @@ class AnthropicHandlerMixin:
"""
from fastapi.responses import JSONResponse, Response
+ if (capability_error := self._anthropic_batch_capability_error()) is not None:
+ return capability_error
+
from headroom.ccr import CCRToolInjector
from headroom.proxy.helpers import MAX_REQUEST_BODY_SIZE, _read_request_json
from headroom.proxy.modes import is_cache_mode
@@ -5426,6 +5453,9 @@ class AnthropicHandlerMixin:
"""
from fastapi.responses import Response
+ if (capability_error := self._anthropic_batch_capability_error()) is not None:
+ return capability_error
+
request_id = await self._next_request_id()
start_time = time.time()
path = request.url.path
@@ -5566,6 +5596,9 @@ class AnthropicHandlerMixin:
"""
from fastapi.responses import Response
+ if (capability_error := self._anthropic_batch_capability_error()) is not None:
+ return capability_error
+
from headroom.ccr import BatchResultProcessor, get_batch_context_store
request_id = await self._next_request_id()
diff --git a/tests/test_copilot_auth.py b/tests/test_copilot_auth.py
index 797b70568..70190b862 100644
--- a/tests/test_copilot_auth.py
+++ b/tests/test_copilot_auth.py
@@ -901,6 +901,14 @@ def test_is_copilot_api_url_matches_ghe_copilot_hosts() -> None:
assert not copilot_auth.is_copilot_api_url("https://not-copilot-api.acme.ghe.com/v1/responses")
+def test_is_copilot_upstream_url_matches_public_and_enterprise_hosts() -> None:
+ assert copilot_auth.is_copilot_upstream_url("https://api.githubcopilot.com/v1/messages/batches")
+ assert copilot_auth.is_copilot_upstream_url(
+ "https://copilot-api.acme.ghe.com/v1/messages/batches"
+ )
+ assert not copilot_auth.is_copilot_upstream_url("https://api.anthropic.com/v1/messages/batches")
+
+
def test_is_copilot_api_url_trusts_configured_enterprise_api_url(
monkeypatch: pytest.MonkeyPatch,
) -> None:
diff --git a/tests/test_proxy_batch_integration.py b/tests/test_proxy_batch_integration.py
index fa4bc33f0..36d9e7f92 100644
--- a/tests/test_proxy_batch_integration.py
+++ b/tests/test_proxy_batch_integration.py
@@ -16,15 +16,16 @@ Run with:
import json
import os
+from unittest.mock import AsyncMock
import pytest
+httpx = pytest.importorskip("httpx")
pytest.importorskip("fastapi")
-pytest.importorskip("httpx")
-from fastapi.testclient import TestClient
+from fastapi.testclient import TestClient # noqa: E402
-from headroom.proxy.server import ProxyConfig, create_app
+from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
# =============================================================================
# Fixtures
@@ -520,3 +521,134 @@ class TestBatchErrorHandling:
content=b"not valid json",
)
assert response.status_code == 400
+
+
+def copilot_anthropic_batch_client():
+ """Create a client whose resolved Anthropic target is public Copilot."""
+ from headroom.proxy.server import HeadroomProxy
+
+ original_anthropic_api_url = HeadroomProxy.ANTHROPIC_API_URL
+ original_openai_api_url = HeadroomProxy.OPENAI_API_URL
+ config = ProxyConfig(
+ optimize=True,
+ cache_enabled=False,
+ rate_limit_enabled=False,
+ cost_tracking_enabled=False,
+ openai_api_url="https://api.githubcopilot.com",
+ )
+ app = create_app(config)
+ with TestClient(app) as client:
+ proxy = app.state.proxy
+ proxy.http_client = AsyncMock()
+ proxy.http_client.request.return_value = httpx.Response(
+ 404,
+ json={
+ "type": "error",
+ "error": {
+ "type": "not_found_error",
+ "message": "Batch endpoint not found",
+ },
+ },
+ )
+ try:
+ yield client, proxy.http_client
+ finally:
+ HeadroomProxy.ANTHROPIC_API_URL = original_anthropic_api_url
+ HeadroomProxy.OPENAI_API_URL = original_openai_api_url
+
+
+ ("method", "path", "kwargs"),
+ [
+ (
+ "post",
+ "/v1/messages/batches",
+ {
+ "json": {
+ "requests": [
+ {
+ "custom_id": "req-1",
+ "params": {
+ "model": "claude-3-5-sonnet-20241022",
+ "max_tokens": 1024,
+ "messages": [{"role": "user", "content": "Hello"}],
+ },
+ }
+ ]
+ }
+ },
+ ),
+ ("get", "/v1/messages/batches", {}),
+ ("get", "/v1/messages/batches/batch_123", {}),
+ ("post", "/v1/messages/batches/batch_123/cancel", {}),
+ ("get", "/v1/messages/batches/batch_123/results", {}),
+ ],
+)
+def test_copilot_anthropic_batch_routes_are_rejected_before_upstream(
+ copilot_anthropic_batch_client, method, path, kwargs
+):
+ """Every registered batch route returns one stable capability response."""
+ client, http_client = copilot_anthropic_batch_client
+
+ response = getattr(client, method)(path, **kwargs)
+
+ assert response.status_code == 501
+ assert response.json() == {
+ "type": "error",
+ "error": {
+ "type": "api_error",
+ "message": "Anthropic batch operations are not supported for Copilot targets.",
+ },
+ }
+ http_client.request.assert_not_called()
+ http_client.get.assert_not_called()
+ if method == "post":
+ http_client.post.assert_not_called()
+
+
+def test_copilot_anthropic_batch_rejection_precedes_body_parsing(copilot_anthropic_batch_client):
+ """Capability rejection wins even when the batch body is invalid."""
+ client, http_client = copilot_anthropic_batch_client
+
+ response = client.post("/v1/messages/batches", content=b"not json")
+
+ assert response.status_code == 501
+ assert response.json()["error"]["type"] == "api_error"
+ http_client.request.assert_not_called()
+ http_client.post.assert_not_called()
+
+
+def test_explicit_non_copilot_anthropic_target_forwards():
+ """An explicit Anthropic target remains outside the Copilot guard."""
+ from headroom.proxy.server import HeadroomProxy
+
+ original_anthropic_api_url = HeadroomProxy.ANTHROPIC_API_URL
+ original_openai_api_url = HeadroomProxy.OPENAI_API_URL
+ config = ProxyConfig(
+ optimize=False,
+ cache_enabled=False,
+ rate_limit_enabled=False,
+ cost_tracking_enabled=False,
+ openai_api_url="https://api.githubcopilot.com",
+ anthropic_api_url="https://api.anthropic.com",
+ )
+ app = create_app(config)
+ try:
+ with TestClient(app) as client:
+ proxy = app.state.proxy
+ proxy.http_client = AsyncMock()
+ proxy.http_client.request.return_value = httpx.Response(200, json={"data": []})
+ http_client = proxy.http_client
+
+ response = client.get("/v1/messages/batches")
+ finally:
+ HeadroomProxy.ANTHROPIC_API_URL = original_anthropic_api_url
+ HeadroomProxy.OPENAI_API_URL = original_openai_api_url
+
+ assert response.status_code == 200
+ http_client.request.assert_called_once()
+ assert http_client.request.call_args.kwargs["url"] == (
+ "https://api.anthropic.com/v1/messages/batches"
+ )
+ http_client.post.assert_not_called()