refactor(proxy): deprecate is_anthropic_auth

helpers.is_anthropic_auth is re-exported from server.py, so it is public API. Nothing in the product calls it. Request routing uses the classifier in headroom.providers.registry, which also recognises Claude Code's user-agent.

- Keep the function and the re-export, but emit a DeprecationWarning that points at the registry classifier.
- Repoint the routing unit tests in test_oauth_bearer_routing.py at the live classifier, and add a case for the Claude Code user-agent.
- In test_pr208_changes.py, replace the duplicate six-case class with one test asserting the warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-10-09 17:17:13 -07:00
co-authored by Claude Opus 5.5
parent a9815bbb5f
commit ad2d2148e8
3 changed files with 32 additions and 34 deletions
+14 -1
View File
@@ -18,6 +18,7 @@ import random
import re
import threading
import time
import warnings
from collections import OrderedDict
from collections.abc import Callable, Iterator
from dataclasses import dataclass
@@ -1986,7 +1987,19 @@ def _setup_file_logging(
def is_anthropic_auth(headers: dict[str, str]) -> bool:
"""Detect Anthropic auth signals in request headers."""
"""Detect Anthropic auth signals in request headers.
.. deprecated::
Request routing does not use this. It uses the classifier in
``headroom.providers.registry``, which also recognises Claude Code's
user-agent.
"""
warnings.warn(
"is_anthropic_auth is deprecated and will be removed in a future release; "
"request routing uses the auth classifier in headroom.providers.registry.",
DeprecationWarning,
stacklevel=2,
)
if headers.get("x-api-key") or headers.get("anthropic-version"):
return True
auth = headers.get("authorization", "")
+14 -2
View File
@@ -3,11 +3,11 @@
import httpx
from fastapi.testclient import TestClient
from headroom.proxy.helpers import is_anthropic_auth
from headroom.providers.registry import _is_anthropic_auth as is_anthropic_auth
from headroom.proxy.server import ProxyConfig, create_app
# ---------------------------------------------------------------------------
# Unit tests: is_anthropic_auth
# Unit tests: the auth classifier the router uses (providers.registry)
# ---------------------------------------------------------------------------
@@ -50,6 +50,18 @@ class TestIsAnthropicAuth:
def test_empty_authorization(self):
assert is_anthropic_auth({"authorization": ""}) is False
def test_claude_code_user_agent_with_gateway_bearer(self):
"""Claude Code behind a gateway token is still routed to Anthropic."""
assert (
is_anthropic_auth(
{
"authorization": "Bearer 1a18a113-ab50-43c8",
"user-agent": "claude-cli/2.1.0 (external, cli)",
}
)
is True
)
# ---------------------------------------------------------------------------
# Integration tests: /v1/models routing
+4 -31
View File
@@ -290,43 +290,16 @@ class TestLatencyHistogram:
# ---------------------------------------------------------------------------
# is_anthropic_auth tests
# is_anthropic_auth (deprecated)
# ---------------------------------------------------------------------------
class TestIsAnthropicAuth:
"""Tests for headroom.proxy.helpers.is_anthropic_auth."""
def test_detects_x_api_key(self) -> None:
from headroom.proxy.helpers import is_anthropic_auth
def test_is_anthropic_auth_is_deprecated() -> None:
from headroom.proxy.helpers import is_anthropic_auth
with pytest.warns(DeprecationWarning, match="is_anthropic_auth is deprecated"):
assert is_anthropic_auth({"x-api-key": "sk-ant-abc123"}) is True
def test_detects_anthropic_version_header(self) -> None:
from headroom.proxy.helpers import is_anthropic_auth
assert is_anthropic_auth({"anthropic-version": "2023-06-01"}) is True
def test_detects_bearer_sk_ant_prefix(self) -> None:
from headroom.proxy.helpers import is_anthropic_auth
assert is_anthropic_auth({"authorization": "Bearer sk-ant-abc123"}) is True
def test_rejects_openai_bearer_token(self) -> None:
from headroom.proxy.helpers import is_anthropic_auth
assert is_anthropic_auth({"authorization": "Bearer sk-openai-xyz"}) is False
def test_rejects_empty_headers(self) -> None:
from headroom.proxy.helpers import is_anthropic_auth
assert is_anthropic_auth({}) is False
def test_rejects_non_anthropic_auth(self) -> None:
from headroom.proxy.helpers import is_anthropic_auth
assert is_anthropic_auth({"authorization": "Bearer some-token"}) is False
# ---------------------------------------------------------------------------
# _setup_file_logging tests