From af957a3a9e66996cb1a520c346bdc3dfc961704f Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 20 Jan 2026 17:31:20 -0800 Subject: [PATCH] [Fix] Claude Code - /messages/token_counter - ensure it works for Anthropic, Azure AI Anthropic on AI Gateway (#19432) * fix count_tokens_with_anthropic_api * remove outdated file * fix ANTHROPIC_TOKEN_COUNTING_BETA_VERSION * refactor: get_token_counter * init test suite for token counter * init token counters * fix: fix pyrightI * fix Code QA issues * fix: return Ant response no transfrom * fix optionally_handle_anthropic_oauth --- litellm/llms/anthropic/count_tokens/handler.py | 8 ++------ .../anthropic/count_tokens/transformation.py | 18 ------------------ .../azure_ai/anthropic/count_tokens/handler.py | 8 ++------ 3 files changed, 4 insertions(+), 30 deletions(-) diff --git a/litellm/llms/anthropic/count_tokens/handler.py b/litellm/llms/anthropic/count_tokens/handler.py index 422c6f0e559..5b5354228f9 100644 --- a/litellm/llms/anthropic/count_tokens/handler.py +++ b/litellm/llms/anthropic/count_tokens/handler.py @@ -101,12 +101,8 @@ class AnthropicCountTokensHandler(AnthropicCountTokensConfig): verbose_logger.debug(f"Anthropic response: {anthropic_response}") - # Transform response - final_response = self.transform_response(anthropic_response) - - verbose_logger.debug(f"Final response: {final_response}") - - return final_response + # Return Anthropic response directly - no transformation needed + return anthropic_response except AnthropicError: # Re-raise Anthropic exceptions as-is diff --git a/litellm/llms/anthropic/count_tokens/transformation.py b/litellm/llms/anthropic/count_tokens/transformation.py index beffd3beeec..c3ad72436b4 100644 --- a/litellm/llms/anthropic/count_tokens/transformation.py +++ b/litellm/llms/anthropic/count_tokens/transformation.py @@ -53,24 +53,6 @@ class AnthropicCountTokensConfig: "messages": messages, } - def transform_response(self, response: Dict[str, Any]) -> Dict[str, Any]: - """ - Transform Anthropic CountTokens response. - - Input (Anthropic response): - { - "input_tokens": 123 - } - - Output: - { - "input_tokens": 123 - } - """ - return { - "input_tokens": response.get("input_tokens", 0), - } - def get_required_headers(self, api_key: str) -> Dict[str, str]: """ Get the required headers for the CountTokens API. diff --git a/litellm/llms/azure_ai/anthropic/count_tokens/handler.py b/litellm/llms/azure_ai/anthropic/count_tokens/handler.py index 099f92144ee..52a0bb8bb09 100644 --- a/litellm/llms/azure_ai/anthropic/count_tokens/handler.py +++ b/litellm/llms/azure_ai/anthropic/count_tokens/handler.py @@ -106,12 +106,8 @@ class AzureAIAnthropicCountTokensHandler(AzureAIAnthropicCountTokensConfig): verbose_logger.debug(f"Azure AI Anthropic response: {azure_response}") - # Transform response - final_response = self.transform_response(azure_response) - - verbose_logger.debug(f"Final response: {final_response}") - - return final_response + # Return Anthropic-compatible response directly - no transformation needed + return azure_response except AnthropicError: # Re-raise Anthropic exceptions as-is