Skip to content

The agent that grows with you

fix(auth): sk-ant-svc / sk-ant-admin Console keys also go out as x-api-key

kshitijk4poor82ef40ab2956
diff --git a/agent/anthropic_adapter.py b/agent/anthropic_adapter.py
index 1a6ce558765..afb08b4b774 100644
--- a/agent/anthropic_adapter.py
+++ b/agent/anthropic_adapter.py
@@ -1,8 +1,8 @@
"""Anthropic Messages API adapter: client construction + the Messages call for Hermes's
-OpenAI-style internals. Auth: API keys (``sk-ant-api*``, ``sk-ant-usr*``) -> x-api-key; OAuth
-setup-tokens (``sk-ant-oat*``) and Claude Code credentials -> Bearer + beta header. Endpoint
-predicates, payload conversion and credentials live in ``agent/anthropic_{endpoints,message_convert,
-credentials}.py``; import them from there."""
+OpenAI-style internals. Auth: Console API keys (``sk-ant-api*``/``-usr*``/``-svc*``/``-admin*``) ->
+x-api-key; OAuth setup-tokens (``sk-ant-oat*``) and Claude Code credentials -> Bearer + beta header.
+Endpoint predicates, payload conversion and credentials live in ``agent/anthropic_{endpoints,
+message_convert,credentials}.py``; import them from there."""
from pm import install_hint
import logging
diff --git a/agent/anthropic_credentials.py b/agent/anthropic_credentials.py
index 21690170d0f..8e25bba24b5 100644
--- a/agent/anthropic_credentials.py
+++ b/agent/anthropic_credentials.py
@@ -61,11 +61,11 @@ def _first_env(*names: str) -> str:
def _is_oauth_token(key: str) -> bool:
- """True for Anthropic OAuth/setup tokens (sk-ant-oat*, eyJ JWTs, cc-); False for sk-ant-api*
- Console keys and sk-ant-usr* user-scoped API keys (#133856) — the latter pay per token and
- authenticate as x-api-key, so the Claude Code identity must not ride along. Unknown sk-ant-*
- prefixes still classify as OAuth so OAuth prefixes Anthropic ships later keep working."""
- if not key or key.startswith(("sk-ant-api", "sk-ant-usr")):
+ """True for Anthropic OAuth/setup tokens (sk-ant-oat*, eyJ JWTs, cc-); False for the Console
+ API-key families sk-ant-api*, sk-ant-usr*, sk-ant-svc* and sk-ant-admin* (#133856) — they pay
+ per token and authenticate as x-api-key, so the Claude Code identity must not ride along.
+ Unknown sk-ant-* prefixes still classify as OAuth so OAuth prefixes Anthropic ships later keep working."""
+ if not key or key.startswith(("sk-ant-api", "sk-ant-usr", "sk-ant-svc", "sk-ant-admin")):
return False
return key.startswith(("sk-ant-", "eyJ", "cc-"))
diff --git a/tests/agent/test_anthropic_client_auth.py b/tests/agent/test_anthropic_client_auth.py
index 902f0f43bb0..b3f4842ac6d 100644
--- a/tests/agent/test_anthropic_client_auth.py
+++ b/tests/agent/test_anthropic_client_auth.py
@@ -65,3 +65,17 @@ def test_third_party_request_on_the_wire_carries_no_foreign_bearer(monkeypatch):
assert captured["headers"].get("x-api-key") == "third-party-provider-key"
assert "authorization" not in captured["headers"]
+
+
[email protected](("family", "oauth"), [("api03", False), ("usr01", False), ("svc01", False), ("admin01", False), ("oat01", True)])
+def test_console_key_families_carry_x_api_key_without_claude_code_identity(monkeypatch, family, oauth):
+ """#133856: Console key families must not ride Bearer + the Claude Code beta/user-agent (that request
+ shape bills the subscription bucket: 400 "credit balance too low"); OAuth setup tokens still do."""
+ pytest.importorskip("anthropic")
+ monkeypatch.setattr("agent.anthropic_adapter._claude_code_version_cache", "0.0.0") # skip the `claude --version` probe
+ key = "sk-" + "ant-" + family + "-" + "X" * 24
+ headers = {k.lower(): v for k, v in wire_headers(build_anthropic_client(key, "https://api.anthropic.com")).items()}
+ assert (headers.get("authorization") == "Bearer " + key) is oauth
+ assert (headers.get("x-api-key") == key) is (not oauth)
+ assert ("claude-code-20250219" in headers.get("anthropic-beta", "")) is oauth
+ assert ("claude-code/" in headers.get("user-agent", "")) is oauth