Skip to content

The agent that grows with you

fix(auth): classify sk-ant-usr* API keys as x-api-key, not OAuth

liuhao1024332874323505
diff --git a/agent/anthropic_adapter.py b/agent/anthropic_adapter.py
index 043b34c9955..1a6ce558765 100644
--- a/agent/anthropic_adapter.py
+++ b/agent/anthropic_adapter.py
@@ -1,7 +1,7 @@
"""Anthropic Messages API adapter: client construction + the Messages call for Hermes's
-OpenAI-style internals. Auth: API keys (``sk-ant-api*``) -> 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,
+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."""
from pm import install_hint
diff --git a/agent/anthropic_credentials.py b/agent/anthropic_credentials.py
index 5f7b5cc9c24..75b1eaded75 100644
--- a/agent/anthropic_credentials.py
+++ b/agent/anthropic_credentials.py
@@ -61,8 +61,10 @@ def _first_env(*names: str) -> str:
def _is_oauth_token(key: str) -> bool:
- """True for Anthropic OAuth/setup tokens (sk-ant-*, eyJ JWTs, cc-); False for sk-ant-api* Console keys."""
- if not key or key.startswith("sk-ant-api"):
+ """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."""
+ if not key or key.startswith(("sk-ant-api", "sk-ant-usr")):
return False
return key.startswith(("sk-ant-", "eyJ", "cc-"))
diff --git a/tests/agent/test_anthropic_adapter.py b/tests/agent/test_anthropic_adapter.py
index ee94c93a147..132c84163c7 100644
--- a/tests/agent/test_anthropic_adapter.py
+++ b/tests/agent/test_anthropic_adapter.py
@@ -28,6 +28,11 @@ class TestIsOAuthToken:
def test_api_key(self):
assert _is_oauth_token("sk-ant-api03-abcdef1234567890") is False
+ def test_user_scoped_api_key(self):
+ # sk-ant-usr* is a pay-per-token API key accepted as x-api-key — not an OAuth/setup
+ # token (#133856): classifying it as OAuth rides the Claude Code identity on it.
+ assert _is_oauth_token("sk-ant-usr01-abcdef1234567890") is False
+
def test_missing_sdk_error_reports_why_the_lazy_install_did_not_land(monkeypatch):
"""A completed install that needs a restart must not tell the user to install it again."""
diff --git a/tests/agent/test_anthropic_route_oauth_identity.py b/tests/agent/test_anthropic_route_oauth_identity.py
index 350c1c35e2d..f53cb2f303d 100644
--- a/tests/agent/test_anthropic_route_oauth_identity.py
+++ b/tests/agent/test_anthropic_route_oauth_identity.py
@@ -16,6 +16,7 @@ from agent.anthropic_credentials import anthropic_route_is_oauth
OAUTH = "sk-ant-oat01-keycmd-token"
CONSOLE = "sk-ant-api03-console-key"
+USER_KEY = "sk-ant-usr01-user-scoped-key"
def test_route_identity_requires_native_host_and_oauth_shape():
@@ -33,6 +34,9 @@ def test_route_identity_requires_native_host_and_oauth_shape():
# Third-party hosts and Console keys never qualify; a callable that cannot mint is non-OAuth.
assert anthropic_route_is_oauth("https://api.minimax.io/anthropic", key_cmd) is False
assert anthropic_route_is_oauth("https://api.anthropic.com", CONSOLE) is False
+ # Neither do user-scoped sk-ant-usr* API keys: they pay per token, not via a Claude
+ # subscription, so the Claude Code identity must not attach (#133856).
+ assert anthropic_route_is_oauth("https://api.anthropic.com", USER_KEY) is False
assert anthropic_route_is_oauth("https://notapi.anthropic.com.evil.example", key_cmd) is False
assert anthropic_route_is_oauth("https://api.anthropic.com", lambda: (_ for _ in ()).throw(RuntimeError("no token"))) is False
assert len(calls) == 1 # the third-party route never materialised the callable