From ca73e3de44b2e1cf3481df715fa12106d0e014c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lodri=20P=C3=A9ter?= <267054113+peterlodri-sec@users.noreply.github.com> Date: Wed, 24 Jun 2026 23:00:40 +0200 Subject: [PATCH] fix(anthropic): handle /v1-suffixed api_base in _build_count_tokens_url api_base values ending in /v1 fell through to the catch-all branch and produced a doubled path (/v1/v1/messages/count_tokens). Add an explicit branch to append /messages/count_tokens in that case. --- litellm/llms/anthropic/count_tokens/transformation.py | 2 ++ .../test_anthropic_count_tokens_transformation.py | 9 +++++++++ 2 files changed, 11 insertions(+) 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" + )