The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(token-counter): count Anthropic native image content blocks
Fazeel Usmani1cce589aa008
diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.pyindex 858b078d626..ccfce0e4133 100644--- a/litellm/litellm_core_utils/token_counter.py+++ b/litellm/litellm_core_utils/token_counter.py@@ -646,6 +646,24 @@ def _validate_anthropic_content(content: Mapping[str, Any]) -> type:return expected_cls+def _anthropic_image_source_data(source: Mapping[str, str]) -> str:+ """+ Resolve an Anthropic image `source` to the data string `calculate_img_tokens` prices.++ Returns "" for a `file` source, whose bytes the proxy cannot resolve locally.+ """+ source_type: Final = source.get("type")+ if source_type == "base64":+ data: Final = source.get("data")+ if not data:+ return ""+ media_type: Final = source.get("media_type") or "image/png"+ return f"data:{media_type};base64,{data}"+ if source_type == "url":+ return source.get("url") or ""+ return ""++def _count_anthropic_content(content: Mapping[str, Any],count_function: TokenCounterFunction,@@ -714,6 +732,13 @@ def _count_content_list(elif c["type"] == "image_url":image_url = c.get("image_url")num_tokens += _count_image_tokens(image_url, use_default_image_token_count)+ elif c["type"] == "image":+ source = c.get("source")+ num_tokens += calculate_img_tokens(+ data=_anthropic_image_source_data(source) if isinstance(source, dict) else "",+ mode="auto",+ use_default_image_token_count=use_default_image_token_count,+ )elif c["type"] in ("tool_use", "tool_result"):num_tokens += _count_anthropic_content(c,@@ -742,7 +767,8 @@ def _count_content_list(content_type = c.get("type", type(c).__name__) if isinstance(c, dict) else type(c).__name__raise ValueError(f"Invalid content item type: {content_type}. "- f"Expected str or dict with 'type' field (text, image_url, tool_use, tool_result, thinking, tool_reference)."+ f"Expected str or dict with 'type' field "+ f"(text, image_url, image, tool_use, tool_result, thinking, tool_reference).")return num_tokensexcept Exception as e:diff --git a/tests/test_litellm/litellm_core_utils/test_token_counter.py b/tests/test_litellm/litellm_core_utils/test_token_counter.pyindex a2590dbca2d..701a1accd5f 100644--- a/tests/test_litellm/litellm_core_utils/test_token_counter.py+++ b/tests/test_litellm/litellm_core_utils/test_token_counter.py@@ -1160,3 +1160,123 @@ def test_count_content_list_rejects_unknown_type():message = str(exc_info.value)assert "Invalid content item type: totally_unknown_block" in messageassert "tool_reference" in message+++ "source",+ [+ {"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo="},+ {"type": "url", "url": "https://example.com/image.png"},+ {"type": "file", "file_id": "file-abc123"},+ ],+ ids=["base64", "url", "file"],+)+def test_token_counter_with_anthropic_image_block(source):+ """+ Anthropic-native `image` blocks must NOT raise, for every source variant.++ Before this fix `_count_content_list` raised+ `Invalid content item type: image`. That 500s /v1/messages/count_tokens and+ /utils/token_counter, and it makes the router's context-window pre-call+ check swallow the error and return every deployment unfiltered, so an+ oversized prompt carrying an image is dispatched upstream instead of being+ rejected locally.+ """+ from litellm.constants import DEFAULT_IMAGE_TOKEN_COUNT++ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "What is in this image?"},+ {"type": "image", "source": source},+ ],+ }+ ]++ tokens = token_counter(+ model="anthropic/claude-sonnet-4-5-20250929",+ messages=messages,+ use_default_image_token_count=True,+ )+ assert tokens > DEFAULT_IMAGE_TOKEN_COUNT, (+ f"Expected the image block to contribute tokens, got {tokens}"+ )+++def test_anthropic_image_block_matches_equivalent_image_url():+ """+ An Anthropic `image` block must price identically to the OpenAI `image_url`+ block carrying the same bytes, so the count does not depend on which+ endpoint shape the caller used.+ """+ anthropic_messages = [+ {+ "role": "user",+ "content": [+ {+ "type": "image",+ "source": {+ "type": "base64",+ "media_type": "image/png",+ "data": "iVBORw0KGgo=",+ },+ }+ ],+ }+ ]+ openai_messages = [+ {+ "role": "user",+ "content": [+ {+ "type": "image_url",+ "image_url": {"url": "data:image/png;base64,iVBORw0KGgo="},+ }+ ],+ }+ ]++ anthropic_tokens = token_counter(+ model="anthropic/claude-sonnet-4-5-20250929", messages=anthropic_messages+ )+ openai_tokens = token_counter(+ model="anthropic/claude-sonnet-4-5-20250929", messages=openai_messages+ )+ assert anthropic_tokens == openai_tokens+++def test_anthropic_image_block_nested_in_tool_result():+ """+ An `image` block nested inside a `tool_result.content` list must be counted+ too. `_count_anthropic_content` recurses back into `_count_content_list`, so+ the nested case failed for the same reason the top-level one did.+ """+ messages = [+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "toolu_01",+ "content": [+ {+ "type": "image",+ "source": {+ "type": "base64",+ "media_type": "image/png",+ "data": "iVBORw0KGgo=",+ },+ }+ ],+ }+ ],+ }+ ]++ tokens = token_counter(+ model="anthropic/claude-sonnet-4-5-20250929",+ messages=messages,+ use_default_image_token_count=True,+ )+ assert tokens > 0