Skip to content

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]

fix(anthropic_adapter): carry web search usage into /v1/messages cost breakdown

mateo-berri815fa0ff0811
diff --git a/basedpyright-code-budget.json b/basedpyright-code-budget.json
index 3357212a6c8..e9b5afba9ea 100644
--- a/basedpyright-code-budget.json
+++ b/basedpyright-code-budget.json
@@ -84,7 +84,7 @@
"limit": 56
},
"reportPrivateUsage": {
- "limit": 1810
+ "limit": 1808
},
"reportRedeclaration": {
"limit": 8
@@ -135,7 +135,7 @@
"limit": 21
},
"reportUnusedFunction": {
- "limit": 139
+ "limit": 138
},
"reportUnusedImport": {
"limit": 544
diff --git a/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py b/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py
index 875e4e156c7..9a2c4e244fb 100644
--- a/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py
+++ b/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py
@@ -7,7 +7,7 @@ from typing import Any, Final, Literal
import litellm
from litellm.constants import OPENAI_FILE_SEARCH_COST_PER_1K_CALLS
-from litellm.litellm_core_utils.llm_cost_calc.utils import _get_web_search_requests
+from litellm.litellm_core_utils.llm_cost_calc.utils import get_web_search_requests
from litellm.types.llms.openai import (
FileSearchTool,
ResponsesAPIResponse,
@@ -368,7 +368,7 @@ class StandardBuiltInToolCostTracking:
get_anthropic_web_search_requests_from_response,
)
- if usage is not None and (_get_web_search_requests(getattr(usage, "server_tool_use", None)) is not None):
+ if usage is not None and (get_web_search_requests(getattr(usage, "server_tool_use", None)) is not None):
return usage
web_search_requests: Final = get_anthropic_web_search_requests_from_response(response_object)
if web_search_requests is None:
@@ -416,7 +416,7 @@ class StandardBuiltInToolCostTracking:
# Anthropic Claude (direct API and Vertex AI) uses server_tool_use.web_search_requests.
# Without this check, Claude ModelResponse always falls through to return False
# and _handle_web_search_cost() is never called.
- if hasattr(usage, "server_tool_use") and _get_web_search_requests(usage.server_tool_use) is not None:
+ if hasattr(usage, "server_tool_use") and get_web_search_requests(usage.server_tool_use) is not None:
return True
# xAI reports usage.server_side_tool_usage_details.web_search_calls; a searched
# answer with no url_citation annotations has no other chat-path signal
@@ -431,7 +431,7 @@ class StandardBuiltInToolCostTracking:
elif usage is not None:
if (
hasattr(usage, "server_tool_use")
- and _get_web_search_requests(usage.server_tool_use) is not None
+ and get_web_search_requests(usage.server_tool_use) is not None
or (
hasattr(usage, "prompt_tokens_details")
and usage.prompt_tokens_details is not None
diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py
index 0a52e1d283e..8d19b44213b 100644
--- a/litellm/litellm_core_utils/llm_cost_calc/utils.py
+++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py
@@ -72,7 +72,7 @@ def _get_token_detail_value(details: object, key: str) -> int | None:
return value if isinstance(value, int) else None
-def _get_web_search_requests(server_tool_use: Any) -> int | None:
+def get_web_search_requests(server_tool_use: Any) -> int | None:
"""
Tolerantly read ``web_search_requests`` from a ``server_tool_use`` value
that may be ``None``, a ``dict``, a ``ServerToolUse`` pydantic instance,
diff --git a/litellm/llms/anthropic/cost_calculation.py b/litellm/llms/anthropic/cost_calculation.py
index f2f9d1c730d..ec6c480efcc 100644
--- a/litellm/llms/anthropic/cost_calculation.py
+++ b/litellm/llms/anthropic/cost_calculation.py
@@ -8,9 +8,9 @@ from typing import TYPE_CHECKING, Final, Optional
from pydantic import BaseModel, ValidationError
from litellm.litellm_core_utils.llm_cost_calc.utils import (
- _get_web_search_requests,
generic_cost_per_token,
get_provider_specific_geo_multiplier,
+ get_web_search_requests,
)
if TYPE_CHECKING:
@@ -104,7 +104,7 @@ def get_cost_for_anthropic_web_search(
if usage is None:
return 0.0
- web_search_requests: Final = _get_web_search_requests(getattr(usage, "server_tool_use", None))
+ web_search_requests: Final = get_web_search_requests(getattr(usage, "server_tool_use", None))
if web_search_requests is None:
return 0.0
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
index 109017bda27..d7b527824ea 100644
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
@@ -99,6 +99,7 @@ from litellm.types.llms.anthropic import (
ContextManagementResponse,
MessageBlockDelta,
MessageDelta,
+ ServerToolUsage,
StreamingContentBlockDeltaType,
UsageDelta,
UsageIteration,
@@ -1354,10 +1355,24 @@ class LiteLLMAnthropicMessagesAdapter:
return explicit_value
return cls._first_positive_prompt_tokens_detail_value(usage, ("cache_creation_tokens", "cache_write_tokens"))
+ @classmethod
+ def _get_web_search_request_count(cls, usage: Usage) -> int:
+ from litellm.litellm_core_utils.llm_cost_calc.utils import (
+ get_web_search_requests,
+ )
+
+ from_server_tool_use: Final = cls._positive_int(
+ get_web_search_requests(getattr(usage, "server_tool_use", None))
+ )
+ if from_server_tool_use > 0:
+ return from_server_tool_use
+ return cls._first_positive_prompt_tokens_detail_value(usage, ("web_search_requests",))
+
@classmethod
def _translate_openai_usage_to_anthropic_usage_delta(cls, usage: Usage) -> UsageDelta:
cache_read_input_tokens: Final = cls._get_cache_read_input_tokens(usage)
cache_creation_input_tokens: Final = cls._get_cache_creation_input_tokens(usage)
+ web_search_requests: Final = cls._get_web_search_request_count(usage)
input_tokens: Final = max(
(usage.prompt_tokens or 0) - cache_read_input_tokens - cache_creation_input_tokens,
0,
@@ -1371,6 +1386,11 @@ class LiteLLMAnthropicMessagesAdapter:
usage_delta["cache_creation_input_tokens"] = cache_creation_input_tokens
if cache_read_input_tokens > 0:
usage_delta["cache_read_input_tokens"] = cache_read_input_tokens
+ if web_search_requests > 0:
+ return UsageDelta(
+ **usage_delta,
+ server_tool_use=ServerToolUsage(web_search_requests=web_search_requests),
+ )
return usage_delta
@classmethod
diff --git a/litellm/llms/gemini/cost_calculator.py b/litellm/llms/gemini/cost_calculator.py
index 94326f0e657..fb7d340ecb3 100644
--- a/litellm/llms/gemini/cost_calculator.py
+++ b/litellm/llms/gemini/cost_calculator.py
@@ -38,29 +38,40 @@ def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> floa
Reads the per-request cost from ``search_context_cost_per_query`` in
``model_info`` when available, falling back to $0.035 for models not
yet updated in the pricing JSON.
+
+ The request count comes from ``prompt_tokens_details.web_search_requests``
+ (the native Gemini field), falling back to ``server_tool_use.web_search_requests``
+ for usage reconstructed from an Anthropic-format response (the /v1/messages
+ adapter surface).
"""
+ from litellm.litellm_core_utils.llm_cost_calc.utils import get_web_search_requests
from litellm.types.utils import PromptTokensDetailsWrapper
_DEFAULT_COST: Final = 35e-3
search_costs: Final = model_info.get("search_context_cost_per_query") or {}
_cost: Final = search_costs.get("search_context_size_medium", _DEFAULT_COST)
- number_of_web_search_requests = 0
- if (
- usage is not None
- and usage.prompt_tokens_details is not None
- and isinstance(usage.prompt_tokens_details, PromptTokensDetailsWrapper)
- and hasattr(usage.prompt_tokens_details, "web_search_requests")
- and usage.prompt_tokens_details.web_search_requests is not None
- ):
- number_of_web_search_requests = usage.prompt_tokens_details.web_search_requests
+ requests_from_prompt_details: Final = (
+ usage.prompt_tokens_details.web_search_requests
+ if (
+ usage is not None
+ and usage.prompt_tokens_details is not None
+ and isinstance(usage.prompt_tokens_details, PromptTokensDetailsWrapper)
+ and hasattr(usage.prompt_tokens_details, "web_search_requests")
+ and usage.prompt_tokens_details.web_search_requests is not None
+ )
+ else None
+ )
+ requests_from_server_tool_use: Final = get_web_search_requests(getattr(usage, "server_tool_use", None))
+ number_of_web_search_requests: Final = requests_from_prompt_details or requests_from_server_tool_use or 0
# per_prompt billing: clamp to 1 (flat fee per grounded API call)
billing_mode: Final = model_info.get("web_search_billing_unit") or "per_prompt"
- if number_of_web_search_requests > 0 and billing_mode == "per_prompt":
- number_of_web_search_requests = 1
+ billable_requests: Final = (
+ 1 if (number_of_web_search_requests > 0 and billing_mode == "per_prompt") else number_of_web_search_requests
+ )
- return _cost * number_of_web_search_requests
+ return _cost * billable_requests
GOOGLE_MAPS_GROUNDING_DEFAULT_COST_PER_QUERY: Final = 14e-3
diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py
index f127366cc21..901802a6640 100644
--- a/litellm/types/llms/anthropic.py
+++ b/litellm/types/llms/anthropic.py
@@ -502,11 +502,16 @@ class MessageDelta(TypedDict, total=False):
stop_reason: str | None
+class ServerToolUsage(TypedDict, total=False):
+ web_search_requests: ReadOnly[int]
+
+
class UsageDelta(TypedDict, total=False):
input_tokens: int
output_tokens: int
cache_creation_input_tokens: int
cache_read_input_tokens: int
+ server_tool_use: ReadOnly[ServerToolUsage]
class AppliedEdit(TypedDict, total=False):
diff --git a/litellm/types/llms/anthropic_messages/anthropic_response.py b/litellm/types/llms/anthropic_messages/anthropic_response.py
index 679948c5235..42ca3fd6d4b 100644
--- a/litellm/types/llms/anthropic_messages/anthropic_response.py
+++ b/litellm/types/llms/anthropic_messages/anthropic_response.py
@@ -1,11 +1,12 @@
from typing import Any, Literal, TypeAlias
-from typing_extensions import NotRequired, TypedDict
+from typing_extensions import NotRequired, ReadOnly, TypedDict
from litellm.types.llms.anthropic import (
AnthropicResponseContentBlockText,
AnthropicResponseContentBlockToolUse,
ContextManagementResponse,
+ ServerToolUsage,
)
@@ -71,6 +72,11 @@ class AnthropicUsage(TypedDict, total=False):
cache_creation_input_tokens: int
cache_read_input_tokens: int
+ """
+ Server-side tool usage (e.g. web search request counts)
+ """
+ server_tool_use: NotRequired[ReadOnly[ServerToolUsage]]
+
class AnthropicMessagesResponse(TypedDict, total=False):
"""
diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking_dict_safety.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking_dict_safety.py
index 61b94139bb8..3a0a3574539 100644
--- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking_dict_safety.py
+++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking_dict_safety.py
@@ -8,10 +8,9 @@ See https://github.com/BerriAI/litellm/issues/26153.
import pytest
-
from litellm.litellm_core_utils.llm_cost_calc.tool_call_cost_tracking import (
StandardBuiltInToolCostTracking,
- _get_web_search_requests,
+ get_web_search_requests,
)
from litellm.types.utils import ModelResponse, ServerToolUse, Usage
@@ -28,25 +27,25 @@ class _UsageWithDictServerToolUse:
def test_get_web_search_requests_handles_none():
- assert _get_web_search_requests(None) is None
+ assert get_web_search_requests(None) is None
def test_get_web_search_requests_handles_dict():
- assert _get_web_search_requests({"web_search_requests": 5}) == 5
+ assert get_web_search_requests({"web_search_requests": 5}) == 5
def test_get_web_search_requests_handles_dict_missing_key():
- assert _get_web_search_requests({}) is None
+ assert get_web_search_requests({}) is None
def test_get_web_search_requests_handles_pydantic():
stu = ServerToolUse(web_search_requests=7)
- assert _get_web_search_requests(stu) == 7
+ assert get_web_search_requests(stu) == 7
def test_get_web_search_requests_handles_pydantic_with_none_value():
stu = ServerToolUse()
- assert _get_web_search_requests(stu) is None
+ assert get_web_search_requests(stu) is None
def test_response_object_includes_web_search_call_with_dict_server_tool_use():
diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
index ee09baf28b6..95fbd06547b 100644
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
@@ -3997,3 +3997,98 @@ def test_translate_anthropic_messages_to_openai_carries_midturn_system_prompt_ca
assert result == [
{"role": "system", "content": [{"type": "text", "text": "fix", "prompt_cache_breakpoint": explicit}]}
]
+
+
+def _openai_response_with_usage(usage: Usage) -> ModelResponse:
+ return ModelResponse(
+ id="resp_web_search",
+ model="gemini-3-flash-preview",
+ choices=[
+ Choices(
+ finish_reason="stop",
+ index=0,
+ message=Message(role="assistant", content="searched"),
+ )
+ ],
+ usage=usage,
+ )
+
+
+def test_translate_openai_response_to_anthropic_maps_gemini_web_search_usage():
+ from litellm.types.utils import PromptTokensDetailsWrapper
+
+ usage = Usage(
+ prompt_tokens=385,
+ completion_tokens=566,
+ total_tokens=951,
+ prompt_tokens_details=PromptTokensDetailsWrapper(text_tokens=385, web_search_requests=2),
+ )
+
+ anthropic_response = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(usage)
+ )
+
+ assert anthropic_response["usage"]["server_tool_use"] == {"web_search_requests": 2}
+
+
+def test_translate_openai_response_to_anthropic_maps_server_tool_use_web_search_usage():
+ from litellm.types.utils import ServerToolUse
+
+ usage = Usage(
+ prompt_tokens=100,
+ completion_tokens=40,
+ total_tokens=140,
+ server_tool_use=ServerToolUse(web_search_requests=3),
+ )
+
+ anthropic_response = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(usage)
+ )
+
+ assert anthropic_response["usage"]["server_tool_use"] == {"web_search_requests": 3}
+
+
+def test_translate_openai_response_to_anthropic_omits_server_tool_use_without_web_search():
+ usage = Usage(prompt_tokens=100, completion_tokens=40, total_tokens=140)
+
+ anthropic_response = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(usage)
+ )
+
+ assert "server_tool_use" not in anthropic_response["usage"]
+
+
+def test_completion_cost_on_translated_anthropic_response_includes_web_search():
+ from litellm.types.utils import PromptTokensDetailsWrapper
+
+ adapter = LiteLLMAnthropicMessagesAdapter()
+ with_search = adapter.translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(
+ Usage(
+ prompt_tokens=385,
+ completion_tokens=566,
+ total_tokens=951,
+ prompt_tokens_details=PromptTokensDetailsWrapper(text_tokens=385, web_search_requests=2),
+ )
+ )
+ )
+ without_search = adapter.translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(Usage(prompt_tokens=385, completion_tokens=566, total_tokens=951))
+ )
+
+ cost_with_search = litellm.completion_cost(
+ completion_response=with_search,
+ model="gemini/gemini-3-flash-preview",
+ call_type="anthropic_messages",
+ )
+ cost_without_search = litellm.completion_cost(
+ completion_response=without_search,
+ model="gemini/gemini-3-flash-preview",
+ call_type="anthropic_messages",
+ )
+
+ per_query_cost = litellm.model_cost["gemini/gemini-3-flash-preview"]["search_context_cost_per_query"][
+ "search_context_size_medium"
+ ]
+ assert per_query_cost > 0
+ assert cost_with_search - cost_without_search == pytest.approx(2 * per_query_cost)
diff --git a/tests/test_litellm/llms/anthropic/test_cost_calculation_dict_safety.py b/tests/test_litellm/llms/anthropic/test_cost_calculation_dict_safety.py
index 5c88ae17679..27115ffe241 100644
--- a/tests/test_litellm/llms/anthropic/test_cost_calculation_dict_safety.py
+++ b/tests/test_litellm/llms/anthropic/test_cost_calculation_dict_safety.py
@@ -8,10 +8,9 @@ See https://github.com/BerriAI/litellm/issues/26153.
import pytest
-
from litellm.llms.anthropic.cost_calculation import (
- _get_web_search_requests,
get_cost_for_anthropic_web_search,
+ get_web_search_requests,
)
from litellm.types.utils import ModelInfo, ServerToolUse
@@ -33,19 +32,19 @@ def _make_model_info(cost_per_query: float = 0.01) -> ModelInfo:
def test_get_web_search_requests_handles_none():
- assert _get_web_search_requests(None) is None
+ assert get_web_search_requests(None) is None
def test_get_web_search_requests_handles_dict():
- assert _get_web_search_requests({"web_search_requests": 4}) == 4
+ assert get_web_search_requests({"web_search_requests": 4}) == 4
def test_get_web_search_requests_handles_dict_missing_key():
- assert _get_web_search_requests({}) is None
+ assert get_web_search_requests({}) is None
def test_get_web_search_requests_handles_pydantic():
- assert _get_web_search_requests(ServerToolUse(web_search_requests=2)) == 2
+ assert get_web_search_requests(ServerToolUse(web_search_requests=2)) == 2
def test_get_cost_for_anthropic_web_search_with_dict_server_tool_use():
diff --git a/tests/test_litellm/llms/gemini/test_cost_calculator.py b/tests/test_litellm/llms/gemini/test_cost_calculator.py
index f1d62800337..6d547b0dc55 100644
--- a/tests/test_litellm/llms/gemini/test_cost_calculator.py
+++ b/tests/test_litellm/llms/gemini/test_cost_calculator.py
@@ -84,6 +84,65 @@ def test_no_usage_details():
assert cost == 0.0
+def _make_server_tool_use_usage(web_search_requests: int) -> Usage:
+ from litellm.types.utils import ServerToolUse
+
+ return Usage(
+ prompt_tokens=100,
+ completion_tokens=50,
+ total_tokens=150,
+ server_tool_use=ServerToolUse(web_search_requests=web_search_requests),
+ )
+
+
+def test_server_tool_use_fallback_per_query_billing():
+ """Usage reconstructed from an Anthropic-format response carries the count in
+ server_tool_use, not prompt_tokens_details; per_query billing prices each request."""
+ model_info = {
+ "key": "gemini/gemini-3-flash-preview",
+ "web_search_billing_unit": "per_query",
+ "search_context_cost_per_query": {
+ "search_context_size_medium": 0.014,
+ },
+ }
+ cost = cost_per_web_search_request(usage=_make_server_tool_use_usage(3), model_info=model_info)
+ assert cost == pytest.approx(0.014 * 3)
+
+
+def test_server_tool_use_fallback_per_prompt_clamps_to_one():
+ """per_prompt billing clamps the server_tool_use count to one grounded prompt."""
+ model_info = {
+ "key": "gemini/gemini-2.5-flash",
+ "search_context_cost_per_query": {
+ "search_context_size_medium": 0.035,
+ },
+ }
+ cost = cost_per_web_search_request(usage=_make_server_tool_use_usage(4), model_info=model_info)
+ assert cost == pytest.approx(0.035 * 1)
+
+
+def test_prompt_tokens_details_take_precedence_over_server_tool_use():
+ """The native Gemini field wins when both counts are present."""
+ from litellm.types.utils import ServerToolUse
+
+ model_info = {
+ "key": "gemini/gemini-3-flash-preview",
+ "web_search_billing_unit": "per_query",
+ "search_context_cost_per_query": {
+ "search_context_size_medium": 0.014,
+ },
+ }
+ usage = Usage(
+ prompt_tokens=100,
+ completion_tokens=50,
+ total_tokens=150,
+ prompt_tokens_details=PromptTokensDetailsWrapper(web_search_requests=2),
+ server_tool_use=ServerToolUse(web_search_requests=5),
+ )
+ cost = cost_per_web_search_request(usage=usage, model_info=model_info)
+ assert cost == pytest.approx(0.014 * 2)
+
+
def _make_maps_usage(google_maps_grounding_requests: int) -> Usage:
return Usage(
prompt_tokens=100,
diff --git a/type-discipline-budget.json b/type-discipline-budget.json
index 6fd7828906c..399e02043a0 100644
--- a/type-discipline-budget.json
+++ b/type-discipline-budget.json
@@ -27,7 +27,7 @@
"limit": 0
},
"LIT010": {
- "limit": 16619
+ "limit": 16616
},
"LIT011": {
"limit": 5583