From f22fd4cddd77bebd257ea98e3f2010120e0a3762 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 29 Sep 2025 18:16:52 -0700 Subject: [PATCH] Fix: Add /v1/messages/count_tokens to Anthropic routes for non-admin user access (#15034) * Initial plan * Fix: Add /v1/messages/count_tokens to Anthropic routes for user access Co-authored-by: ishaan-jaff <29436595+ishaan-jaff@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ishaan-jaff <29436595+ishaan-jaff@users.noreply.github.com> --- litellm/proxy/_types.py | 1 + .../proxy/auth/test_route_checks.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index c5b4ee0753e..c5370eb7d70 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -330,6 +330,7 @@ class LiteLLMRoutes(enum.Enum): anthropic_routes = [ "/v1/messages", + "/v1/messages/count_tokens", ] mcp_routes = [ diff --git a/tests/test_litellm/proxy/auth/test_route_checks.py b/tests/test_litellm/proxy/auth/test_route_checks.py index ac09917e4cd..539ee4a9ba8 100644 --- a/tests/test_litellm/proxy/auth/test_route_checks.py +++ b/tests/test_litellm/proxy/auth/test_route_checks.py @@ -228,3 +228,22 @@ def test_virtual_key_allowed_routes_with_no_member_names_only_explicit(): ) assert "Virtual key is not allowed to call this route" in str(exc_info.value) + + +def test_anthropic_count_tokens_route_is_llm_api_route(): + """Test that /v1/messages/count_tokens is recognized as an LLM API route for Anthropic""" + + # Test the core anthropic routes + assert RouteChecks.is_llm_api_route("/v1/messages") is True + assert RouteChecks.is_llm_api_route("/v1/messages/count_tokens") is True + + +def test_anthropic_count_tokens_route_accessible_to_internal_users(): + """Test that internal users can access the Anthropic count_tokens route""" + + # Test that the route is recognized as an LLM API route (which means it's accessible to internal users) + # This is the core check that was failing in the original issue + assert RouteChecks.is_llm_api_route("/v1/messages/count_tokens") is True + + # Also test that the regular messages route still works + assert RouteChecks.is_llm_api_route("/v1/messages") is True