fix(anthropic): do not fetch caller-supplied image URLs when counting input tokens

This commit is contained in:
yetuge 2026-09-20 15:34:55 +08:00
parent 867c43a9d6
commit 5ce6b3168d
2 changed files with 33 additions and 3 deletions

View file

@ -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)

View file

@ -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 (