diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index 90e79d4d853..6277273a1b6 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -55,6 +55,7 @@ from litellm.types.llms.anthropic import ( AnthropicMessagesTextParam, AnthropicMessagesToolResultParam, AnthropicMessagesToolUseParam, + AnthropicSearchResultParam, ) from litellm.types.llms.openai import ( AllMessageValues, @@ -825,6 +826,7 @@ def _validate_anthropic_content(content: Mapping[str, object]) -> type: mapping: Final = { "tool_use": AnthropicMessagesToolUseParam, "tool_result": AnthropicMessagesToolResultParam, + "search_result": AnthropicSearchResultParam, } expected_cls: Final = mapping.get(content_type) if isinstance(content_type, str) else None @@ -1028,7 +1030,7 @@ def _count_content_list( count_function, use_default_image_token_count, ) - elif c["type"] in ("tool_use", "tool_result"): + elif c["type"] in ("tool_use", "tool_result", "search_result"): num_tokens += _count_anthropic_content( c, count_function, @@ -1058,7 +1060,7 @@ def _count_content_list( f"Invalid content item type: {content_type}. " f"Expected str or dict with 'type' field " f"(text, image_url, image, document, file, tool_use, tool_result, thinking, redacted_thinking, " - f"tool_reference)." + f"tool_reference, search_result)." ) return num_tokens except Exception as e: diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index ba422a0a901..a4717aa780d 100644 --- a/litellm/types/llms/anthropic.py +++ b/litellm/types/llms/anthropic.py @@ -209,6 +209,13 @@ class AnthropicMessagesTextParam(TypedDict, total=False): prompt_cache_breakpoint: ReadOnly[PromptCacheBreakpoint] +class AnthropicSearchResultParam(TypedDict, total=False): + type: Required[ReadOnly[Literal["search_result"]]] + source: Required[ReadOnly[str]] + title: Required[ReadOnly[str]] + content: Required[ReadOnly[Iterable[AnthropicMessagesTextParam]]] + + class AnthropicMessagesToolUseParam(TypedDict, total=False): type: Required[Literal["tool_use"]] id: str diff --git a/tests/unit/litellm_core_utils/test_token_counter.py b/tests/unit/litellm_core_utils/test_token_counter.py index 88af656ad2e..d796514cf04 100644 --- a/tests/unit/litellm_core_utils/test_token_counter.py +++ b/tests/unit/litellm_core_utils/test_token_counter.py @@ -1578,6 +1578,26 @@ def test_token_counter_with_thinking_content(): ), f"Expected minimal token count for empty thinking block, got {tokens_no_thinking}" +def test_token_counter_counts_anthropic_search_result_text() -> None: + search_result: Final = { + "type": "search_result", + "source": "https://example.com/paris", + "title": "Paris", + "content": [{"type": "text", "text": "Paris is the capital of France."}], + "citations": {"enabled": True}, + } + as_text_blocks: Final = [ + {"type": "text", "text": "https://example.com/paris"}, + {"type": "text", "text": "Paris"}, + {"type": "text", "text": "Paris is the capital of France."}, + ] + + def count(content: list[dict[str, object]]) -> int: + return token_counter(model="gpt-4o", messages=[{"role": "user", "content": content}]) + + assert count([search_result]) == count(as_text_blocks) + + def test_token_counter_with_redacted_thinking_content(): """ A replayed redacted_thinking block (Anthropic redacted reasoning, or the /v1/messages bridge's stand-in