The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(proxy): serve Anthropic-native /v1/models for Claude Code gateway discovery (#35455)
Armaan Sandhue1f3d6e15855
diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.pyindex 9aa5a4f465f..b444c77d718 100644--- a/litellm/llms/anthropic/common_utils.py+++ b/litellm/llms/anthropic/common_utils.py@@ -5,6 +5,7 @@ This file contains common utils for anthropic calls.import copyimport refrom collections.abc import Mapping, Sequence+from datetime import datetime, timezonefrom types import MappingProxyTypefrom typing import Any, Final, Literal@@ -12,6 +13,7 @@ import httpxfrom pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationErrorimport litellm+from litellm.constants import DEFAULT_MODEL_CREATED_AT_TIMEfrom litellm.litellm_core_utils.prompt_templates.common_utils import (get_file_ids_from_messages,)@@ -28,6 +30,7 @@ from litellm.types.llms.anthropic import (AnthropicMcpServerTool,)from litellm.types.llms.openai import AllMessageValues+from litellm.types.proxy.model_listing import ModelInfoResponse_BEDROCK_VERSION_SUFFIX_RE: Final = re.compile(r"-v\d+(?::\d+)?$")_INFERENCE_PROFILE_MINOR_RE: Final = re.compile(r":\d+$")@@ -1221,3 +1224,39 @@ def process_anthropic_headers(headers: httpx.Headers | dict) -> dict:additional_headers: Final = {**llm_response_headers, **openai_headers}return additional_headers+++def _anthropic_model_entry(model: ModelInfoResponse, created_at: str) -> Mapping[str, object]:+ token_limits: Final = (+ ("max_input_tokens", model.get("max_input_tokens")),+ ("max_tokens", model.get("max_output_tokens")),+ )+ return { # mutable-ok: JSON response body, serialized by the route and never mutated+ "type": "model",+ "id": model["id"],+ "display_name": model["id"],+ "created_at": created_at,+ **{name: limit for name, limit in token_limits if limit is not None}, # mutable-ok: merged into the body above+ }+++def create_anthropic_model_list_response(models: Sequence[ModelInfoResponse]) -> Mapping[str, object]:+ """Build the Anthropic-native /v1/models envelope.++ Clients that send an anthropic-version header parse the Anthropic Models API+ shape (type/display_name/created_at plus has_more/first_id/last_id) and filter+ the list themselves, so every model is returned here. The token limits carry+ over from the OpenAI-shaped listing, named as the Messages API names them+ """+ created_at: Final = (+ datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc).isoformat().replace("+00:00", "Z")+ )+ data: Final = [ # mutable-ok: JSON response body, serialized by the route and never mutated+ _anthropic_model_entry(model, created_at) for model in models+ ]+ return { # mutable-ok: JSON response body, serialized by the route and never mutated+ "data": data,+ "has_more": False,+ "first_id": models[0]["id"] if models else None,+ "last_id": models[-1]["id"] if models else None,+ }diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex b1b5a7ffbe5..359187f81cb 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -9493,6 +9493,7 @@ class ProxyStartupEvent:"/models", dependencies=[Depends(user_api_key_auth)], tags=["model management"]) # if project requires model listasync def model_list(+ request: Request = None, # pyright: ignore[reportArgumentType] # FastAPI always injects the Request; the None default only serves direct in-process callersuser_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),return_wildcard_routes: bool | None = False,team_id: str | None = None,@@ -9529,6 +9530,9 @@ async def model_list(settings: Final = cast(dict[str, object], general_settings) # any-ok: legacy settings+ from litellm.llms.anthropic.common_utils import (+ create_anthropic_model_list_response,+ )from litellm.proxy.management_endpoints.common_utils import (_user_has_admin_privileges,)@@ -9536,6 +9540,12 @@ async def model_list(create_model_info_response,get_available_models_for_user,)+ from litellm.types.proxy.model_listing import ModelInfoResponse++ http_request: Final = cast(Request | None, request) # cast-ok: in-process callers pass no request+ wants_anthropic_format: Final = (+ http_request is not None and http_request.headers.get("anthropic-version") is not None+ )# Validate scope parameter if providedif scope is not None and scope != "expand":@@ -9619,6 +9629,10 @@ async def model_list(model_info["id"] = response_idmodel_data.append(model_info)+ if wants_anthropic_format:+ admin_listing: Final = cast(Sequence[ModelInfoResponse], model_data) # cast-ok: rows built above+ return create_anthropic_model_list_response(admin_listing)+return dict(data=model_data,object="list",@@ -9659,6 +9673,10 @@ async def model_list(model_info["id"] = response_idmodel_data.append(model_info)+ if wants_anthropic_format:+ listing: Final = cast(Sequence[ModelInfoResponse], model_data) # cast-ok: rows built above+ return create_anthropic_model_list_response(listing)+return dict(data=model_data,object="list",diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.pyindex 9df72108332..431030bcf2e 100644--- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py+++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py@@ -2028,3 +2028,82 @@ class TestCapabilityProbeUsesCallerProvider:AnthropicModelInfo._is_adaptive_thinking_model("claude-opus-4-8", "anthropic")is True)+def test_create_anthropic_model_list_response_shape():+ from litellm.llms.anthropic.common_utils import (+ create_anthropic_model_list_response,+ )++ response = create_anthropic_model_list_response(+ [+ {"id": "claude-opus-4-6", "object": "model", "created": 0, "owned_by": "openai"},+ {"id": "gpt-4o", "object": "model", "created": 0, "owned_by": "openai"},+ {"id": "claude-haiku-4-5", "object": "model", "created": 0, "owned_by": "openai"},+ ]+ )++ assert "object" not in response+ assert response["has_more"] is False+ assert response["first_id"] == "claude-opus-4-6"+ assert response["last_id"] == "claude-haiku-4-5"+ assert [m["id"] for m in response["data"]] == [+ "claude-opus-4-6",+ "gpt-4o",+ "claude-haiku-4-5",+ ]+ for entry in response["data"]:+ assert entry["type"] == "model"+ assert entry["display_name"] == entry["id"]+ # ISO 8601 with a Z suffix, as the Anthropic Models API returns.+ assert entry["created_at"].endswith("Z")+ assert "+00:00" not in entry["created_at"]+ assert "max_input_tokens" not in entry+ assert "max_tokens" not in entry+++def test_create_anthropic_model_list_response_carries_token_limits():+ from litellm.llms.anthropic.common_utils import (+ create_anthropic_model_list_response,+ )++ response = create_anthropic_model_list_response(+ [+ {+ "id": "claude-opus-4-6",+ "object": "model",+ "created": 0,+ "owned_by": "openai",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ },+ {+ "id": "input-only",+ "object": "model",+ "created": 0,+ "owned_by": "openai",+ "max_input_tokens": 8192,+ },+ {"id": "unknown-limits", "object": "model", "created": 0, "owned_by": "openai"},+ ]+ )++ opus, input_only, unknown = response["data"]+ assert opus["max_input_tokens"] == 200000+ assert opus["max_tokens"] == 64000+ assert "max_output_tokens" not in opus+ assert input_only["max_input_tokens"] == 8192+ assert "max_tokens" not in input_only+ assert "max_input_tokens" not in unknown+ assert "max_tokens" not in unknown+++def test_create_anthropic_model_list_response_empty():+ from litellm.llms.anthropic.common_utils import (+ create_anthropic_model_list_response,+ )++ response = create_anthropic_model_list_response([])++ assert response["data"] == []+ assert response["has_more"] is False+ assert response["first_id"] is None+ assert response["last_id"] is None\ No newline at end of filediff --git a/tests/test_litellm/proxy/proxy_server/test_routes_models.py b/tests/test_litellm/proxy/proxy_server/test_routes_models.pyindex 381835fbc14..f18c5998b8c 100644--- a/tests/test_litellm/proxy/proxy_server/test_routes_models.py+++ b/tests/test_litellm/proxy/proxy_server/test_routes_models.py@@ -99,6 +99,62 @@ def test_get_models_happy_path(client, auth_as, patched_models, path):}[email protected]("path", ["/v1/models", "/models"])+def test_get_models_anthropic_format_when_header_present(+ client, auth_as, patched_models, path+):+ """Pins: ``GET /v1/models`` returns the Anthropic-native models shape when+ the caller sends an ``anthropic-version`` header (Claude Code gateway+ discovery), while the default OpenAI shape is unchanged without it."""+ with auth_as():+ response = client.get(path, headers={"anthropic-version": "2023-06-01"})+ assert response.status_code == 200+ body = response.json()+ assert "object" not in body+ assert body["has_more"] is False+ assert body["first_id"] == "gpt-4"+ assert body["last_id"] == "claude-sonnet"+ assert [m["id"] for m in body["data"]] == ["gpt-4", "claude-sonnet"]+ for entry in body["data"]:+ assert entry["type"] == "model"+ assert entry["display_name"] == entry["id"]+ assert entry["created_at"].endswith("Z")++[email protected]("path", ["/v1/models", "/models"])+def test_anthropic_format_exposes_token_limits(+ client, auth_as, patched_models, monkeypatch, path+):+ """Claude Code sizes requests off the listing, so the Anthropic-native entries+ carry the same token limits the OpenAI listing resolves, with the output budget+ named max_tokens as the Messages API names it."""+ from litellm.proxy import utils as proxy_utils++ def _create_model_info_response(model_id, provider="openai", **kwargs):+ if model_id != "claude-sonnet":+ return _stub_model_info_response(model_id=model_id, provider=provider)+ return {+ **_stub_model_info_response(model_id=model_id, provider=provider),+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ }++ monkeypatch.setattr(+ proxy_utils, "create_model_info_response", _create_model_info_response+ )++ with auth_as():+ response = client.get(path, headers={"anthropic-version": "2023-06-01"})++ assert response.status_code == 200+ gpt_4, claude = response.json()["data"]+ assert claude["max_input_tokens"] == 200000+ assert claude["max_tokens"] == 64000+ assert "max_output_tokens" not in claude+ assert "max_input_tokens" not in gpt_4+ assert "max_tokens" not in gpt_4++@pytest.mark.parametrize("path", ["/v1/models", "/models"])def test_get_models_invalid_scope_returns_400(client, auth_as, patched_models, path):"""Pins: ``GET /v1/models``, ``GET /models`` (error path: invalid scope)."""@@ -130,3 +186,50 @@ def test_get_model_by_id_not_found(client, auth_as, patched_models, path):response = client.get(path)assert response.status_code == 404assert "not found" in response.text.lower()++[email protected]("params", [{}, {"scope": "expand"}])+def test_anthropic_format_returns_public_team_model_name(+ client, auth_as, patched_models, monkeypatch, params+):+ """Regression: the Anthropic-native listing must go through the same team+ name translation as the OpenAI listing, so a caller never sees the internal+ ``model_name_{team_id}_{uuid}`` routing key."""+ from litellm.proxy import utils as proxy_utils+ from litellm.proxy.auth import model_checks++ internal_name = "model_name_team-1_c0ffee"++ patched_models.get_model_list = MagicMock(+ return_value=[+ {+ "model_name": internal_name,+ "model_info": {+ "team_id": "team-1",+ "team_public_model_name": "gpt-4-team",+ },+ }+ ]+ )+ patched_models.get_model_names = MagicMock(return_value=[internal_name])++ async def _fake_get_available_models_for_user(**kwargs):+ return [internal_name]++ monkeypatch.setattr(+ proxy_utils,+ "get_available_models_for_user",+ _fake_get_available_models_for_user,+ )+ monkeypatch.setattr(+ model_checks, "get_complete_model_list", lambda **kwargs: [internal_name]+ )++ with auth_as():+ response = client.get(+ "/v1/models", params=params, headers={"anthropic-version": "2023-06-01"}+ )++ assert response.status_code == 200+ assert [m["id"] for m in response.json()["data"]] == ["gpt-4-team"]+ assert internal_name not in response.text