diff --git a/litellm/llms/anthropic/count_tokens/transformation.py b/litellm/llms/anthropic/count_tokens/transformation.py index 81dea02a02b..e9d33ac2b15 100644 --- a/litellm/llms/anthropic/count_tokens/transformation.py +++ b/litellm/llms/anthropic/count_tokens/transformation.py @@ -34,6 +34,8 @@ class AnthropicCountTokensConfig: return base if base.endswith("/v1/messages"): return f"{base}/count_tokens" + if base.endswith("/v1"): + return f"{base}/messages/count_tokens" return f"{base}{self._COUNT_TOKENS_PATH}" def transform_request_to_count_tokens( diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py b/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py index d3c6272429b..7369935b7a2 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py @@ -140,3 +140,12 @@ def test_build_count_tokens_url_trailing_slash_stripped(): config._build_count_tokens_url("https://my-proxy.example.com/") == "https://my-proxy.example.com/v1/messages/count_tokens" ) + + +def test_build_count_tokens_url_base_with_v1_only(): + """/v1-suffixed base gets /messages/count_tokens appended (not doubled /v1).""" + config = AnthropicCountTokensConfig() + assert ( + config._build_count_tokens_url("https://my-proxy.example.com/v1") + == "https://my-proxy.example.com/v1/messages/count_tokens" + )