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 Bedrock/Vertex auth regression: don't forward api_key for env-auth providers (#105)
chopratejasfeb163d8c803
diff --git a/headroom/backends/litellm.py b/headroom/backends/litellm.pyindex 8cc57e3b1..14ba30658 100644--- a/headroom/backends/litellm.py+++ b/headroom/backends/litellm.py@@ -669,12 +669,16 @@ class LiteLLMBackend(Backend):elif self.provider in ("vertex_ai", "vertex_ai_beta"):kwargs["vertex_location"] = self.region- # Forward API key from request headers if present- auth_header = headers.get("authorization", headers.get("Authorization", ""))- if auth_header.startswith("Bearer "):- kwargs["api_key"] = auth_header[7:]- elif headers.get("x-api-key"):- kwargs["api_key"] = headers["x-api-key"]+ # Forward API key from request headers if present.+ # Skip for Bedrock/Vertex: they use env-based auth (AWS SigV4 / Google ADC).+ # Forwarding x-api-key (e.g. sk-ant-dummy) would override their credentials.+ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if self.provider not in _env_auth_providers:+ auth_header = headers.get("authorization", headers.get("Authorization", ""))+ if auth_header.startswith("Bearer "):+ kwargs["api_key"] = auth_header[7:]+ elif headers.get("x-api-key"):+ kwargs["api_key"] = headers["x-api-key"]logger.debug(f"LiteLLM request: model={litellm_model}")@@ -769,12 +773,16 @@ class LiteLLMBackend(Backend):elif self.provider in ("vertex_ai", "vertex_ai_beta"):kwargs["vertex_location"] = self.region- # Forward API key from request headers if present- auth_header = headers.get("authorization", headers.get("Authorization", ""))- if auth_header.startswith("Bearer "):- kwargs["api_key"] = auth_header[7:]- elif headers.get("x-api-key"):- kwargs["api_key"] = headers["x-api-key"]+ # Forward API key from request headers if present.+ # Skip for Bedrock/Vertex: they use env-based auth (AWS SigV4 / Google ADC).+ # Forwarding x-api-key (e.g. sk-ant-dummy) would override their credentials.+ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if self.provider not in _env_auth_providers:+ auth_header = headers.get("authorization", headers.get("Authorization", ""))+ if auth_header.startswith("Bearer "):+ kwargs["api_key"] = auth_header[7:]+ elif headers.get("x-api-key"):+ kwargs["api_key"] = headers["x-api-key"]msg_id = f"msg_{uuid.uuid4().hex[:24]}"@@ -989,12 +997,16 @@ class LiteLLMBackend(Backend):elif self.provider in ("vertex_ai", "vertex_ai_beta"):kwargs["vertex_location"] = self.region- # Forward API key from request headers if present- auth_header = headers.get("authorization", headers.get("Authorization", ""))- if auth_header.startswith("Bearer "):- kwargs["api_key"] = auth_header[7:]- elif headers.get("x-api-key"):- kwargs["api_key"] = headers["x-api-key"]+ # Forward API key from request headers if present.+ # Skip for Bedrock/Vertex: they use env-based auth (AWS SigV4 / Google ADC).+ # Forwarding x-api-key (e.g. sk-ant-dummy) would override their credentials.+ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if self.provider not in _env_auth_providers:+ auth_header = headers.get("authorization", headers.get("Authorization", ""))+ if auth_header.startswith("Bearer "):+ kwargs["api_key"] = auth_header[7:]+ elif headers.get("x-api-key"):+ kwargs["api_key"] = headers["x-api-key"]logger.debug(f"LiteLLM OpenAI request: model={litellm_model}")@@ -1121,12 +1133,16 @@ class LiteLLMBackend(Backend):elif self.provider in ("vertex_ai", "vertex_ai_beta"):kwargs["vertex_location"] = self.region- # Forward API key from request headers if present- auth_header = headers.get("authorization", headers.get("Authorization", ""))- if auth_header.startswith("Bearer "):- kwargs["api_key"] = auth_header[7:]- elif headers.get("x-api-key"):- kwargs["api_key"] = headers["x-api-key"]+ # Forward API key from request headers if present.+ # Skip for Bedrock/Vertex: they use env-based auth (AWS SigV4 / Google ADC).+ # Forwarding x-api-key (e.g. sk-ant-dummy) would override their credentials.+ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if self.provider not in _env_auth_providers:+ auth_header = headers.get("authorization", headers.get("Authorization", ""))+ if auth_header.startswith("Bearer "):+ kwargs["api_key"] = auth_header[7:]+ elif headers.get("x-api-key"):+ kwargs["api_key"] = headers["x-api-key"]response = await acompletion(**kwargs)diff --git a/tests/test_backend_bugs.py b/tests/test_backend_bugs.pyindex b04f27e1f..30297ef58 100644--- a/tests/test_backend_bugs.py+++ b/tests/test_backend_bugs.py@@ -753,3 +753,68 @@ class TestOpenAIURLNormalization:assert proxy.OPENAI_API_URL == "http://localhost:4000"finally:HeadroomProxy.OPENAI_API_URL = original+++# =============================================================================+# Bedrock API Key Forwarding Regression (#105)+# =============================================================================+++class TestBedrockApiKeyNotForwarded:+ """Bedrock uses AWS SigV4 auth, not API keys.++ Forwarding x-api-key (e.g. sk-ant-dummy) to LiteLLM overrides+ AWS credentials and breaks Bedrock auth.+ """++ def test_bedrock_does_not_forward_api_key(self):+ """api_key should NOT be in kwargs for Bedrock provider."""+ backend = LiteLLMBackend(provider="bedrock", region="us-west-2")++ kwargs = {}+ headers = {+ "x-api-key": "sk-ant-dummy-key",+ "authorization": "Bearer sk-ant-dummy-key",+ }++ # Simulate what the handler does: build kwargs then check+ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if backend.provider not in _env_auth_providers:+ auth_header = headers.get("authorization", headers.get("Authorization", ""))+ if auth_header.startswith("Bearer "):+ kwargs["api_key"] = auth_header[7:]+ elif headers.get("x-api-key"):+ kwargs["api_key"] = headers["x-api-key"]++ assert "api_key" not in kwargs, (+ f"Bedrock should not have api_key in kwargs, got: {kwargs.get('api_key')}"+ )++ def test_openai_does_forward_api_key(self):+ """api_key SHOULD be in kwargs for non-Bedrock providers."""+ backend = LiteLLMBackend(provider="openai")++ kwargs = {}+ headers = {"authorization": "Bearer sk-real-key-123"}++ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if backend.provider not in _env_auth_providers:+ auth_header = headers.get("authorization", headers.get("Authorization", ""))+ if auth_header.startswith("Bearer "):+ kwargs["api_key"] = auth_header[7:]++ assert kwargs.get("api_key") == "sk-real-key-123"++ def test_vertex_does_not_forward_api_key(self):+ """Vertex AI also uses env-based auth (Google ADC)."""+ backend = LiteLLMBackend(provider="vertex_ai")++ kwargs = {}+ headers = {"x-api-key": "sk-ant-dummy"}++ _env_auth_providers = ("bedrock", "vertex_ai", "vertex_ai_beta", "sagemaker")+ if backend.provider not in _env_auth_providers:+ if headers.get("x-api-key"):+ kwargs["api_key"] = headers["x-api-key"]++ assert "api_key" not in kwargs