From 25170d0f04c7116fe22721d46c139a36f115fd68 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Fri, 12 Jun 2026 12:30:20 +0530 Subject: [PATCH 01/12] feat(proxy): serve Anthropic-native /v1/models for Claude Code gateway discovery --- litellm/proxy/proxy_server.py | 20 ++++++++---- litellm/proxy/utils.py | 32 +++++++++++++++++++ .../proxy/proxy_server/test_routes_models.py | 22 +++++++++++++ tests/test_litellm/proxy/test_proxy_utils.py | 32 +++++++++++++++++++ 4 files changed, 100 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index a60ea2da0192..35212fb21b71 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8705,6 +8705,7 @@ def _init_pyroscope(cls): "/models", dependencies=[Depends(user_api_key_auth)], tags=["model management"] ) # if project requires model list async def model_list( + request: Request, user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth), return_wildcard_routes: Optional[bool] = False, team_id: Optional[str] = None, @@ -8745,10 +8746,15 @@ async def model_list( _user_has_admin_privileges, ) from litellm.proxy.utils import ( + create_anthropic_model_list_response, create_model_info_response, get_available_models_for_user, ) + # Claude Code's gateway discovery sends the same anthropic-version header it + # uses for /v1/messages and only parses the Anthropic-native models shape. + wants_anthropic_format = request.headers.get("anthropic-version") is not None + # Validate scope parameter if provided if scope is not None and scope != "expand": raise HTTPException( @@ -8816,9 +8822,10 @@ async def model_list( if hidden_names: all_models = [m for m in all_models if m not in hidden_names] - # Surface the public team name by default; legacy internal keys via flag. - # The internal routing key drives the metadata/fallback lookup, while the - # public name is what the client sees as the model id. + if wants_anthropic_format: + return create_anthropic_model_list_response(all_models) + + # Build response data with all proxy models model_data = [] for response_id, lookup_id in TeamModelNameTranslator.listing_entries(all_models, llm_router, settings): model_info = create_model_info_response( @@ -8856,9 +8863,10 @@ async def model_list( if hidden_names: all_models = [m for m in all_models if m not in hidden_names] - # Surface the public team name by default; legacy internal keys via flag. - # The internal routing key drives the metadata/fallback lookup, while the - # public name is what the client sees as the model id. + if wants_anthropic_format: + return create_anthropic_model_list_response(all_models) + + # Build response data model_data = [] for response_id, lookup_id in TeamModelNameTranslator.listing_entries(all_models, llm_router, settings): model_info = create_model_info_response( diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 2ca251a32119..1c8f7127e785 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -6496,6 +6496,38 @@ def create_model_info_response( return {**base, "metadata": {"fallbacks": fallbacks}} +def create_anthropic_model_list_response(model_ids: List[str]) -> dict: + """Build the Anthropic-native /v1/models envelope for Claude Code gateway discovery. + + Claude Code 2.1.126+ queries the gateway root's /v1/models with an + anthropic-version header and only parses the Anthropic Models API shape + (type/display_name/created_at plus has_more/first_id/last_id). It keeps + models whose id starts with claude/anthropic, so the full list is returned + here and the client filters. display_name falls back to the id, which is + the stable label a gateway can offer for arbitrary upstream models. + """ + created_at = ( + datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc) + .isoformat() + .replace("+00:00", "Z") + ) + data = [ + { + "type": "model", + "id": model_id, + "display_name": model_id, + "created_at": created_at, + } + for model_id in model_ids + ] + return { + "data": data, + "has_more": False, + "first_id": model_ids[0] if model_ids else None, + "last_id": model_ids[-1] if model_ids else None, + } + + def validate_model_access( model_id: str, available_models: List[str], diff --git a/tests/test_litellm/proxy/proxy_server/test_routes_models.py b/tests/test_litellm/proxy/proxy_server/test_routes_models.py index 381835fbc14f..2cd83c37e4b9 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,28 @@ def test_get_models_happy_path(client, auth_as, patched_models, path): } +@pytest.mark.parametrize("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") + + @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).""" diff --git a/tests/test_litellm/proxy/test_proxy_utils.py b/tests/test_litellm/proxy/test_proxy_utils.py index 3421751d9621..a713f8d8e624 100644 --- a/tests/test_litellm/proxy/test_proxy_utils.py +++ b/tests/test_litellm/proxy/test_proxy_utils.py @@ -1085,3 +1085,35 @@ async def test_post_mcp_call_hook_propagates_guardrail_block(restore_callbacks): request_data={"mcp_tool_name": "echo"}, user_api_key_dict=None, ) +from litellm.proxy.utils import create_anthropic_model_list_response + + +def test_create_anthropic_model_list_response_shape(): + response = create_anthropic_model_list_response( + ["claude-opus-4-6", "gpt-4o", "claude-haiku-4-5"] + ) + + 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"] + + +def test_create_anthropic_model_list_response_empty(): + 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 From 6898cff606b2157100411ba1a7e5e306c3d68b2d Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Fri, 12 Jun 2026 12:41:49 +0530 Subject: [PATCH 02/12] refactor(proxy): move Anthropic model-list formatter into llms/anthropic/common_utils --- litellm/llms/anthropic/common_utils.py | 34 +++++++++++++++++ litellm/proxy/proxy_server.py | 4 +- litellm/proxy/utils.py | 32 ---------------- .../anthropic/test_anthropic_common_utils.py | 37 +++++++++++++++++++ tests/test_litellm/proxy/test_proxy_utils.py | 34 +---------------- 5 files changed, 75 insertions(+), 66 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 256fee6b1663..6960d0ba24c2 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -4,11 +4,13 @@ import copy import re +from datetime import datetime, timezone from typing import Any, Dict, List, Optional, Union import httpx import litellm +from litellm.constants import DEFAULT_MODEL_CREATED_AT_TIME from litellm.litellm_core_utils.prompt_templates.common_utils import ( get_file_ids_from_messages, ) @@ -1073,3 +1075,35 @@ def process_anthropic_headers(headers: Union[httpx.Headers, dict]) -> dict: additional_headers = {**llm_response_headers, **openai_headers} return additional_headers + + +def create_anthropic_model_list_response(model_ids: List[str]) -> dict: + """Build the Anthropic-native /v1/models envelope for Claude Code gateway discovery. + + Claude Code 2.1.126+ queries the gateway root's /v1/models with an + anthropic-version header and only parses the Anthropic Models API shape + (type/display_name/created_at plus has_more/first_id/last_id). It keeps + models whose id starts with claude/anthropic, so the full list is returned + here and the client filters. display_name falls back to the id, which is + the stable label a gateway can offer for arbitrary upstream models. + """ + created_at = ( + datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc) + .isoformat() + .replace("+00:00", "Z") + ) + data = [ + { + "type": "model", + "id": model_id, + "display_name": model_id, + "created_at": created_at, + } + for model_id in model_ids + ] + return { + "data": data, + "has_more": False, + "first_id": model_ids[0] if model_ids else None, + "last_id": model_ids[-1] if model_ids else None, + } diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 35212fb21b71..b7421505fa1f 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8745,8 +8745,10 @@ async def model_list( from litellm.proxy.management_endpoints.common_utils import ( _user_has_admin_privileges, ) - from litellm.proxy.utils import ( + from litellm.llms.anthropic.common_utils import ( create_anthropic_model_list_response, + ) + from litellm.proxy.utils import ( create_model_info_response, get_available_models_for_user, ) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 1c8f7127e785..2ca251a32119 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -6496,38 +6496,6 @@ def create_model_info_response( return {**base, "metadata": {"fallbacks": fallbacks}} -def create_anthropic_model_list_response(model_ids: List[str]) -> dict: - """Build the Anthropic-native /v1/models envelope for Claude Code gateway discovery. - - Claude Code 2.1.126+ queries the gateway root's /v1/models with an - anthropic-version header and only parses the Anthropic Models API shape - (type/display_name/created_at plus has_more/first_id/last_id). It keeps - models whose id starts with claude/anthropic, so the full list is returned - here and the client filters. display_name falls back to the id, which is - the stable label a gateway can offer for arbitrary upstream models. - """ - created_at = ( - datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc) - .isoformat() - .replace("+00:00", "Z") - ) - data = [ - { - "type": "model", - "id": model_id, - "display_name": model_id, - "created_at": created_at, - } - for model_id in model_ids - ] - return { - "data": data, - "has_more": False, - "first_id": model_ids[0] if model_ids else None, - "last_id": model_ids[-1] if model_ids else None, - } - - def validate_model_access( model_id: str, available_models: List[str], diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py index 6ab0f2c08abe..7f51e834ba69 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -1842,3 +1842,40 @@ def test_native_anthropic_probe_still_reads_anthropic_entry( 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( + ["claude-opus-4-6", "gpt-4o", "claude-haiku-4-5"] + ) + + 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"] + + +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 file diff --git a/tests/test_litellm/proxy/test_proxy_utils.py b/tests/test_litellm/proxy/test_proxy_utils.py index a713f8d8e624..0438e500265f 100644 --- a/tests/test_litellm/proxy/test_proxy_utils.py +++ b/tests/test_litellm/proxy/test_proxy_utils.py @@ -1084,36 +1084,4 @@ async def test_post_mcp_call_hook_propagates_guardrail_block(restore_callbacks): response=result, request_data={"mcp_tool_name": "echo"}, user_api_key_dict=None, - ) -from litellm.proxy.utils import create_anthropic_model_list_response - - -def test_create_anthropic_model_list_response_shape(): - response = create_anthropic_model_list_response( - ["claude-opus-4-6", "gpt-4o", "claude-haiku-4-5"] - ) - - 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"] - - -def test_create_anthropic_model_list_response_empty(): - 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 file From 2ea0e34f3400a981ba7ef907df8aa5faf64888e3 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Fri, 12 Jun 2026 12:54:16 +0530 Subject: [PATCH 03/12] fix(proxy): make model_list request param optional for direct callers --- litellm/proxy/proxy_server.py | 14 ++++++++++---- tests/test_litellm/proxy/test_proxy_utils.py | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index b7421505fa1f..3cb85c45711b 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8705,7 +8705,7 @@ def _init_pyroscope(cls): "/models", dependencies=[Depends(user_api_key_auth)], tags=["model management"] ) # if project requires model list async def model_list( - request: Request, + request: Request = None, # type: ignore[assignment] user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth), return_wildcard_routes: Optional[bool] = False, team_id: Optional[str] = None, @@ -8755,7 +8755,9 @@ async def model_list( # Claude Code's gateway discovery sends the same anthropic-version header it # uses for /v1/messages and only parses the Anthropic-native models shape. - wants_anthropic_format = request.headers.get("anthropic-version") is not None + wants_anthropic_format = ( + request is not None and request.headers.get("anthropic-version") is not None + ) # Validate scope parameter if provided if scope is not None and scope != "expand": @@ -8827,7 +8829,9 @@ async def model_list( if wants_anthropic_format: return create_anthropic_model_list_response(all_models) - # Build response data with all proxy models + # Surface the public team name by default; legacy internal keys via flag. + # The internal routing key drives the metadata/fallback lookup, while the + # public name is what the client sees as the model id. model_data = [] for response_id, lookup_id in TeamModelNameTranslator.listing_entries(all_models, llm_router, settings): model_info = create_model_info_response( @@ -8868,7 +8872,9 @@ async def model_list( if wants_anthropic_format: return create_anthropic_model_list_response(all_models) - # Build response data + # Surface the public team name by default; legacy internal keys via flag. + # The internal routing key drives the metadata/fallback lookup, while the + # public name is what the client sees as the model id. model_data = [] for response_id, lookup_id in TeamModelNameTranslator.listing_entries(all_models, llm_router, settings): model_info = create_model_info_response( diff --git a/tests/test_litellm/proxy/test_proxy_utils.py b/tests/test_litellm/proxy/test_proxy_utils.py index 0438e500265f..3421751d9621 100644 --- a/tests/test_litellm/proxy/test_proxy_utils.py +++ b/tests/test_litellm/proxy/test_proxy_utils.py @@ -1084,4 +1084,4 @@ async def test_post_mcp_call_hook_propagates_guardrail_block(restore_callbacks): response=result, request_data={"mcp_tool_name": "echo"}, user_api_key_dict=None, - ) \ No newline at end of file + ) From cdf8b573047cd46fc40f00089bf56e901fb4177b Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Sat, 1 Aug 2026 20:48:18 +0530 Subject: [PATCH 04/12] style: apply ruff format to changed lines --- litellm/llms/anthropic/common_utils.py | 4 +--- litellm/proxy/proxy_server.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 6960d0ba24c2..2ecb98e77d98 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1088,9 +1088,7 @@ def create_anthropic_model_list_response(model_ids: List[str]) -> dict: the stable label a gateway can offer for arbitrary upstream models. """ created_at = ( - datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc) - .isoformat() - .replace("+00:00", "Z") + datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc).isoformat().replace("+00:00", "Z") ) data = [ { diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 3cb85c45711b..09c25afd5723 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8755,9 +8755,7 @@ async def model_list( # Claude Code's gateway discovery sends the same anthropic-version header it # uses for /v1/messages and only parses the Anthropic-native models shape. - wants_anthropic_format = ( - request is not None and request.headers.get("anthropic-version") is not None - ) + wants_anthropic_format = request is not None and request.headers.get("anthropic-version") is not None # Validate scope parameter if provided if scope is not None and scope != "expand": From 57b6481d68c6438b9384fb6636757ad6e95cfb6e Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Sat, 1 Aug 2026 21:01:05 +0530 Subject: [PATCH 05/12] style: satisfy ruff strict-rule budget (UP006, I001) --- litellm/llms/anthropic/common_utils.py | 2 +- litellm/proxy/proxy_server.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 2ecb98e77d98..e8511dacde86 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1077,7 +1077,7 @@ def process_anthropic_headers(headers: Union[httpx.Headers, dict]) -> dict: return additional_headers -def create_anthropic_model_list_response(model_ids: List[str]) -> dict: +def create_anthropic_model_list_response(model_ids: list[str]) -> dict: """Build the Anthropic-native /v1/models envelope for Claude Code gateway discovery. Claude Code 2.1.126+ queries the gateway root's /v1/models with an diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 09c25afd5723..322430d85283 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8742,12 +8742,12 @@ async def model_list( settings = cast(dict[str, object], general_settings) # any-ok: legacy settings - from litellm.proxy.management_endpoints.common_utils import ( - _user_has_admin_privileges, - ) from litellm.llms.anthropic.common_utils import ( create_anthropic_model_list_response, ) + from litellm.proxy.management_endpoints.common_utils import ( + _user_has_admin_privileges, + ) from litellm.proxy.utils import ( create_model_info_response, get_available_models_for_user, From 78cf8e598fa43eb49c52a2ecda12466d40d8a3be Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Sat, 1 Aug 2026 21:15:46 +0530 Subject: [PATCH 06/12] style: satisfy type-discipline budget (LIT002 mutable-ok, LIT009 pyright ignore) --- litellm/llms/anthropic/common_utils.py | 6 +++--- litellm/proxy/proxy_server.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index e8511dacde86..1377586fee13 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1090,8 +1090,8 @@ def create_anthropic_model_list_response(model_ids: list[str]) -> dict: created_at = ( datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc).isoformat().replace("+00:00", "Z") ) - data = [ - { + data = [ # mutable-ok: JSON response body, serialized by the route and never mutated + { # mutable-ok: JSON response body, serialized by the route and never mutated "type": "model", "id": model_id, "display_name": model_id, @@ -1099,7 +1099,7 @@ def create_anthropic_model_list_response(model_ids: list[str]) -> dict: } for model_id in model_ids ] - return { + return { # mutable-ok: JSON response body, serialized by the route and never mutated "data": data, "has_more": False, "first_id": model_ids[0] if model_ids else None, diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 322430d85283..1ea4448268c0 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8705,7 +8705,7 @@ def _init_pyroscope(cls): "/models", dependencies=[Depends(user_api_key_auth)], tags=["model management"] ) # if project requires model list async def model_list( - request: Request = None, # type: ignore[assignment] + request: Request = None, # pyright: ignore[reportArgumentType] # FastAPI always injects the Request; the None default only serves direct in-process callers user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth), return_wildcard_routes: Optional[bool] = False, team_id: Optional[str] = None, From a1bad1192f4e8e30ac9c8c1bc4acd9b8f1f657f0 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Sat, 8 Aug 2026 20:49:37 -0400 Subject: [PATCH 07/12] style: satisfy LIT001/LIT010 and drop explanatory comment per contributor rules --- litellm/llms/anthropic/common_utils.py | 19 ++++++++----------- litellm/proxy/proxy_server.py | 4 +--- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index e354edc951c9..47c3d792104c 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -1225,20 +1225,17 @@ def process_anthropic_headers(headers: httpx.Headers | dict) -> dict: return additional_headers -def create_anthropic_model_list_response(model_ids: list[str]) -> dict: - """Build the Anthropic-native /v1/models envelope for Claude Code gateway discovery. - - Claude Code 2.1.126+ queries the gateway root's /v1/models with an - anthropic-version header and only parses the Anthropic Models API shape - (type/display_name/created_at plus has_more/first_id/last_id). It keeps - models whose id starts with claude/anthropic, so the full list is returned - here and the client filters. display_name falls back to the id, which is - the stable label a gateway can offer for arbitrary upstream models. +def create_anthropic_model_list_response(model_ids: Sequence[str]) -> 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 id is returned here """ - created_at = ( + created_at: Final = ( datetime.fromtimestamp(DEFAULT_MODEL_CREATED_AT_TIME, tz=timezone.utc).isoformat().replace("+00:00", "Z") ) - data = [ # mutable-ok: JSON response body, serialized by the route and never mutated + data: Final = [ # mutable-ok: JSON response body, serialized by the route and never mutated { # mutable-ok: JSON response body, serialized by the route and never mutated "type": "model", "id": model_id, diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 780af519b5d6..f1632f268293 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -9036,9 +9036,7 @@ async def model_list( get_available_models_for_user, ) - # Claude Code's gateway discovery sends the same anthropic-version header it - # uses for /v1/messages and only parses the Anthropic-native models shape. - wants_anthropic_format = request is not None and request.headers.get("anthropic-version") is not None + wants_anthropic_format: Final = request is not None and request.headers.get("anthropic-version") is not None # Validate scope parameter if provided if scope is not None and scope != "expand": From 7dd58b57a26585debddfc5740c97ebeffec4ab7c Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Sun, 9 Aug 2026 14:40:09 -0400 Subject: [PATCH 08/12] fix(proxy): translate team model names in the Anthropic /v1/models response --- litellm/proxy/proxy_server.py | 8 +++- .../proxy/proxy_server/test_routes_models.py | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index f1632f268293..68527b34f15d 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -9106,7 +9106,9 @@ async def model_list( all_models = [m for m in all_models if m not in hidden_names] if wants_anthropic_format: - return create_anthropic_model_list_response(all_models) + return create_anthropic_model_list_response( + TeamModelNameTranslator.translate_listing(all_models, llm_router, settings) + ) # Surface the public team name by default; legacy internal keys via flag. # The internal routing key drives the metadata/fallback lookup, while the @@ -9149,7 +9151,9 @@ async def model_list( all_models = [m for m in all_models if m not in hidden_names] if wants_anthropic_format: - return create_anthropic_model_list_response(all_models) + return create_anthropic_model_list_response( + TeamModelNameTranslator.translate_listing(all_models, llm_router, settings) + ) # Surface the public team name by default; legacy internal keys via flag. # The internal routing key drives the metadata/fallback lookup, while the diff --git a/tests/test_litellm/proxy/proxy_server/test_routes_models.py b/tests/test_litellm/proxy/proxy_server/test_routes_models.py index 2cd83c37e4b9..687725c5cf98 100644 --- a/tests/test_litellm/proxy/proxy_server/test_routes_models.py +++ b/tests/test_litellm/proxy/proxy_server/test_routes_models.py @@ -152,3 +152,50 @@ def test_get_model_by_id_not_found(client, auth_as, patched_models, path): response = client.get(path) assert response.status_code == 404 assert "not found" in response.text.lower() + + +@pytest.mark.parametrize("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 From 492417f3ac699d541e80d0d03d97e6cf07f56a56 Mon Sep 17 00:00:00 2001 From: Yassin Kortam Date: Thu, 13 Aug 2026 20:41:23 -0700 Subject: [PATCH 09/12] ci: trigger buildkite status report From d3885909988a3dea9838168c800ea5b75c5beb39 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Fri, 14 Aug 2026 12:00:12 -0400 Subject: [PATCH 10/12] feat(proxy): carry token limits into the Anthropic-native /v1/models entries --- litellm/llms/anthropic/common_utils.py | 32 +++++++++----- litellm/proxy/proxy_server.py | 16 +++---- .../anthropic/test_anthropic_common_utils.py | 44 ++++++++++++++++++- .../proxy/proxy_server/test_routes_models.py | 34 ++++++++++++++ 4 files changed, 104 insertions(+), 22 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 47c3d792104c..b444c77d7180 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -30,6 +30,7 @@ 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+$") @@ -1225,28 +1226,37 @@ def process_anthropic_headers(headers: httpx.Headers | dict) -> dict: return additional_headers -def create_anthropic_model_list_response(model_ids: Sequence[str]) -> Mapping[str, object]: +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 id is returned here + 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 - { # 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, - } - for model_id in model_ids + _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": model_ids[0] if model_ids else None, - "last_id": model_ids[-1] if model_ids else None, + "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.py index 68527b34f15d..6a11f1b88d71 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -9105,11 +9105,6 @@ async def model_list( if hidden_names: all_models = [m for m in all_models if m not in hidden_names] - if wants_anthropic_format: - return create_anthropic_model_list_response( - TeamModelNameTranslator.translate_listing(all_models, llm_router, settings) - ) - # Surface the public team name by default; legacy internal keys via flag. # The internal routing key drives the metadata/fallback lookup, while the # public name is what the client sees as the model id. @@ -9125,6 +9120,9 @@ async def model_list( model_info["id"] = response_id model_data.append(model_info) + if wants_anthropic_format: + return create_anthropic_model_list_response(model_data) + return dict( data=model_data, object="list", @@ -9150,11 +9148,6 @@ async def model_list( if hidden_names: all_models = [m for m in all_models if m not in hidden_names] - if wants_anthropic_format: - return create_anthropic_model_list_response( - TeamModelNameTranslator.translate_listing(all_models, llm_router, settings) - ) - # Surface the public team name by default; legacy internal keys via flag. # The internal routing key drives the metadata/fallback lookup, while the # public name is what the client sees as the model id. @@ -9170,6 +9163,9 @@ async def model_list( model_info["id"] = response_id model_data.append(model_info) + if wants_anthropic_format: + return create_anthropic_model_list_response(model_data) + 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.py index 878bd2b67bc6..431030bcf2e2 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -2034,7 +2034,11 @@ def test_create_anthropic_model_list_response_shape(): ) response = create_anthropic_model_list_response( - ["claude-opus-4-6", "gpt-4o", "claude-haiku-4-5"] + [ + {"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 @@ -2052,6 +2056,44 @@ def test_create_anthropic_model_list_response_shape(): # 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(): diff --git a/tests/test_litellm/proxy/proxy_server/test_routes_models.py b/tests/test_litellm/proxy/proxy_server/test_routes_models.py index 687725c5cf98..f18c5998b8ce 100644 --- a/tests/test_litellm/proxy/proxy_server/test_routes_models.py +++ b/tests/test_litellm/proxy/proxy_server/test_routes_models.py @@ -121,6 +121,40 @@ def test_get_models_anthropic_format_when_header_present( assert entry["created_at"].endswith("Z") +@pytest.mark.parametrize("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).""" From 647034bc3739778d5bba481c06865fb918d36d1c Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Fri, 14 Aug 2026 12:14:22 -0400 Subject: [PATCH 11/12] fix(proxy): cast the injected request so the anthropic-version guard is a real comparison --- litellm/proxy/proxy_server.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 68a59f9d9eb9..72029b50f280 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -9541,7 +9541,10 @@ async def model_list( get_available_models_for_user, ) - wants_anthropic_format: Final = request is not None and request.headers.get("anthropic-version") is not None + http_request: Final = cast(Request | None, request) + wants_anthropic_format: Final = ( + http_request is not None and http_request.headers.get("anthropic-version") is not None + ) # Validate scope parameter if provided if scope is not None and scope != "expand": From b3248d713aecba59cc90251f267ab51a80028a82 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu Date: Fri, 14 Aug 2026 12:36:27 -0400 Subject: [PATCH 12/12] fix(proxy): explain the model listing casts so the type-discipline gate passes --- litellm/proxy/proxy_server.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 72029b50f280..359187f81cb5 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -9540,8 +9540,9 @@ 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) + 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 ) @@ -9629,7 +9630,8 @@ async def model_list( model_data.append(model_info) if wants_anthropic_format: - return create_anthropic_model_list_response(model_data) + 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, @@ -9672,7 +9674,8 @@ async def model_list( model_data.append(model_info) if wants_anthropic_format: - return create_anthropic_model_list_response(model_data) + listing: Final = cast(Sequence[ModelInfoResponse], model_data) # cast-ok: rows built above + return create_anthropic_model_list_response(listing) return dict( data=model_data,