From 5ce6b3168dbe084fc8b2384a4d068229f674ecc5 Mon Sep 17 00:00:00 2001 From: yetuge <2219677952@qq.com> Date: Sun, 20 Sep 2026 15:34:55 +0800 Subject: [PATCH] fix(anthropic): do not fetch caller-supplied image URLs when counting input tokens --- .../responses_adapters/handler.py | 1 + ...t_responses_adapters_streaming_iterator.py | 35 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py index a58acb1b018..3097079d5a8 100644 --- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py +++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py @@ -70,6 +70,7 @@ def _count_anthropic_input_tokens( model=local_model_name(model, provider), messages=prompt_messages, tools=tools, + use_default_image_token_count=True, ) except Exception: # noqa: BLE001 # counting must not block a request over an informational field verbose_logger.debug("Could not count input tokens for message_start", exc_info=True) diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py index 8765ae8a05b..e5c209d6a4c 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py @@ -533,13 +533,42 @@ class TestCountAnthropicInputTokens: messages = [{"role": "user", "content": "hello world"}] bare = _count_anthropic_input_tokens(messages=messages, model="gpt-4o", system=None, tools=None) - prefixed = _count_anthropic_input_tokens( - messages=messages, model="openai/gpt-4o", system=None, tools=None - ) + prefixed = _count_anthropic_input_tokens(messages=messages, model="openai/gpt-4o", system=None, tools=None) assert bare is not None and prefixed is not None assert prefixed == bare + def test_image_urls_are_not_fetched(self, monkeypatch): + import requests + from litellm.llms.anthropic.experimental_pass_through.responses_adapters.handler import ( + _count_anthropic_input_tokens, + ) + + def fail(*args, **kwargs): + raise AssertionError("token counting must not fetch a caller-supplied URL") + + monkeypatch.setattr(requests, "get", fail) + + counted = _count_anthropic_input_tokens( + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "what is in this image?"}, + { + "type": "image_url", + "image_url": {"url": "https://attacker.example/x.png", "detail": "high"}, + }, + ], + } + ], + model="gpt-4o", + system=None, + tools=None, + ) + + assert isinstance(counted, int) and counted > 0 + def test_counting_failure_returns_none(self, monkeypatch): import litellm from litellm.llms.anthropic.experimental_pass_through.responses_adapters.handler import (