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(cost): bill bedrock_mantle web search at $12 per 1k queries using Bedrock's reported count
mateo-berri51d821ae45ae
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.pyindex 5504756ceb8..bf99035a6b1 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@@ -5,6 +5,8 @@ Helper utilities for tracking the cost of built-in tools.from collections.abc import Mappingfrom typing import Final, Literal+from pydantic import ValidationError+import litellmfrom litellm.constants import OPENAI_FILE_SEARCH_COST_PER_1K_CALLSfrom litellm.litellm_core_utils.llm_cost_calc.utils import (@@ -13,6 +15,7 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import (from litellm.types.llms.openai import (FileSearchTool,ResponsesAPIResponse,+ ResponsesToolUsage,WebSearchOptions,)from litellm.types.utils import (@@ -32,6 +35,17 @@ def _output_item_type(output_item: object) -> str | None:return item_type if isinstance(item_type, str) else None+def _reported_web_search_requests(response_object: ResponsesAPIResponse) -> int | None:+ tool_usage: Final = getattr(response_object, "tool_usage", None)+ if tool_usage is None:+ return None+ try:+ web_search: Final = ResponsesToolUsage.model_validate(tool_usage).web_search+ except ValidationError:+ return None+ return None if web_search is None else web_search.num_requests++def _usage_reports_server_side_web_search_calls(usage: Usage) -> bool:details: Final = getattr(usage, "server_side_tool_usage_details", None)if not isinstance(details, Mapping):@@ -182,15 +196,19 @@ class StandardBuiltInToolCostTracking:Providers that report a request count in usage (gemini, anthropic, xai, vertex) are handled byget_cost_for_web_search_request and never reach here. This path prices per call, so it must count- the web_search_call items. Chat-completions responses only expose url_citation annotations with no- count, so they floor to a single billable search.+ the web_search_call items, unless the response reports the billable count itself+ (Bedrock's tool_usage.web_search.num_requests, which excludes open_page fetches). Chat-completions+ responses only expose url_citation annotations with no count, so they floor to a single billable search."""- if isinstance(response_object, ResponsesAPIResponse):- count = sum(- 1 for output_item in response_object.output if _output_item_type(output_item) == "web_search_call"- )- return max(count, 1)- return 1+ if not isinstance(response_object, ResponsesAPIResponse):+ return 1+ reported: Final = _reported_web_search_requests(response_object)+ if reported is not None:+ return reported+ count: Final = sum(+ 1 for output_item in response_object.output if _output_item_type(output_item) == "web_search_call"+ )+ return max(count, 1)@staticmethoddef _handle_file_search_cost(diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.jsonindex 9b358a1cedd..ffac34ea37e 100644--- a/litellm/model_prices_and_context_window_backup.json+++ b/litellm/model_prices_and_context_window_backup.json@@ -52911,6 +52911,11 @@"cache_read_input_token_cost_above_272k_tokens": 1.1e-06,"output_cost_per_token": 3.3e-05,"output_cost_per_token_above_272k_tokens": 4.95e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -52945,6 +52950,11 @@"cache_read_input_token_cost_above_272k_tokens": 4.4e-07,"output_cost_per_token": 1.32e-05,"output_cost_per_token_above_272k_tokens": 1.98e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -53007,6 +53017,11 @@"cache_read_input_token_cost_above_272k_tokens": 4.4e-08,"output_cost_per_token": 1.32e-06,"output_cost_per_token_above_272k_tokens": 1.98e-06,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -53195,6 +53210,11 @@"cache_read_input_token_cost_above_272k_tokens": 1.1e-06,"output_cost_per_token": 3.3e-05,"output_cost_per_token_above_272k_tokens": 4.95e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -53226,6 +53246,11 @@"cache_read_input_token_cost_above_272k_tokens": 5.5e-07,"output_cost_per_token": 1.65e-05,"output_cost_per_token_above_272k_tokens": 2.475e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.pyindex 32d88da0085..b33ff954c35 100644--- a/litellm/types/llms/openai.py+++ b/litellm/types/llms/openai.py@@ -66,6 +66,7 @@ from pydantic import (ConfigDict,Discriminator,Field,+ NonNegativeInt,PrivateAttr,SerializerFunctionWrapHandler,field_serializer,@@ -1321,6 +1322,18 @@ class ResponseAPIUsage(BaseLiteLLMOpenAIResponseObject):model_config = {"extra": "allow"}+class WebSearchToolUsage(BaseModel):+ model_config = ConfigDict(frozen=True)++ num_requests: NonNegativeInt+++class ResponsesToolUsage(BaseModel):+ model_config = ConfigDict(frozen=True)++ web_search: WebSearchToolUsage | None = None++ResponsesAPIStatus = Literal["completed", "failed", "in_progress", "cancelled", "queued", "incomplete"]"""The status of the response generation.diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.jsonindex 9b358a1cedd..ffac34ea37e 100644--- a/model_prices_and_context_window.json+++ b/model_prices_and_context_window.json@@ -52911,6 +52911,11 @@"cache_read_input_token_cost_above_272k_tokens": 1.1e-06,"output_cost_per_token": 3.3e-05,"output_cost_per_token_above_272k_tokens": 4.95e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -52945,6 +52950,11 @@"cache_read_input_token_cost_above_272k_tokens": 4.4e-07,"output_cost_per_token": 1.32e-05,"output_cost_per_token_above_272k_tokens": 1.98e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -53007,6 +53017,11 @@"cache_read_input_token_cost_above_272k_tokens": 4.4e-08,"output_cost_per_token": 1.32e-06,"output_cost_per_token_above_272k_tokens": 1.98e-06,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -53195,6 +53210,11 @@"cache_read_input_token_cost_above_272k_tokens": 1.1e-06,"output_cost_per_token": 3.3e-05,"output_cost_per_token_above_272k_tokens": 4.95e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,@@ -53226,6 +53246,11 @@"cache_read_input_token_cost_above_272k_tokens": 5.5e-07,"output_cost_per_token": 1.65e-05,"output_cost_per_token_above_272k_tokens": 2.475e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.012,+ "search_context_size_low": 0.012,+ "search_context_size_medium": 0.012+ },"litellm_provider": "bedrock_mantle","max_input_tokens": 1050000,"max_output_tokens": 128000,diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.pyindex fd795ffcc96..0d75301a57a 100644--- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py+++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py@@ -928,3 +928,121 @@ def test_web_search_gate_reads_server_side_tool_usage_details_without_citations(standard_built_in_tools_params=None,)assert cost == 3 * _DEFAULT_WEB_SEARCH_COST_PER_CALL+++_BEDROCK_MANTLE_WEB_SEARCH_MODELS = (+ "bedrock_mantle/openai.gpt-5.6-sol",+ "bedrock_mantle/openai.gpt-5.6-terra",+ "bedrock_mantle/openai.gpt-5.6-luna",+ "bedrock_mantle/openai.gpt-5.5",+ "bedrock_mantle/openai.gpt-5.4",+)++_BEDROCK_MANTLE_WEB_SEARCH_RATE = 0.012+++def _bedrock_mantle_responses_with_web_search(model, actions, tool_usage=None):+ from litellm.types.llms.openai import ResponsesAPIResponse++ payload = {+ "id": "resp_1",+ "created_at": 1756900000,+ "model": model.split("/", 1)[-1],+ "object": "response",+ "status": "completed",+ "output": [+ {"type": "web_search_call", "id": f"ws_{i}", "status": "completed", "action": action}+ for i, action in enumerate(actions)+ ],+ }+ return ResponsesAPIResponse.model_validate(+ payload if tool_usage is None else {**payload, "tool_usage": tool_usage}+ )+++def _bedrock_mantle_web_search_cost(model, response):+ from litellm.types.utils import Usage++ return StandardBuiltInToolCostTracking.get_cost_for_built_in_tools(+ model=model,+ response_object=response,+ usage=Usage(prompt_tokens=10, completion_tokens=5, total_tokens=15),+ custom_llm_provider="bedrock_mantle",+ standard_built_in_tools_params=None,+ )++[email protected]("model", _BEDROCK_MANTLE_WEB_SEARCH_MODELS)+def test_bedrock_mantle_web_search_billed_per_query(local_model_cost_map, model):+ """+ Regression for LIT-6870: the bedrock_mantle GPT ids forward the web_search tool but carried no+ search_context_cost_per_query, so every Bedrock web search (billed at $12 per 1,000 queries)+ was costed at $0. Two reported queries must bill 2 x $0.012, whichever model prefix shape the+ cost path resolves the deployment under.+ """+ pricing = litellm.get_model_info(model)["search_context_cost_per_query"]+ assert pricing == {+ "search_context_size_low": _BEDROCK_MANTLE_WEB_SEARCH_RATE,+ "search_context_size_medium": _BEDROCK_MANTLE_WEB_SEARCH_RATE,+ "search_context_size_high": _BEDROCK_MANTLE_WEB_SEARCH_RATE,+ }++ response = _bedrock_mantle_responses_with_web_search(+ model,+ actions=[{"type": "search", "query": "litellm"}, {"type": "search", "query": "bedrock web search"}],+ tool_usage={"web_search": {"num_requests": 2}},+ )+ for cost_model in (model, model.split("/", 1)[1]):+ cost = _bedrock_mantle_web_search_cost(cost_model, response)+ assert cost == pytest.approx(2 * _BEDROCK_MANTLE_WEB_SEARCH_RATE), (+ f"{cost_model} must bill 2 x ${_BEDROCK_MANTLE_WEB_SEARCH_RATE} for 2 web searches, got ${cost}"+ )++[email protected]("num_requests", [1, 0])+def test_web_search_call_count_prefers_provider_reported_num_requests(local_model_cost_map, num_requests):+ """+ Regression for LIT-6870: Bedrock bills one query per search and reports the billable count as+ tool_usage.web_search.num_requests, while its open_page fetches share the web_search_call item+ type. A search plus an open_page must bill the reported count, never the two items.+ """+ model = "bedrock_mantle/openai.gpt-5.6-sol"+ response = _bedrock_mantle_responses_with_web_search(+ model,+ actions=[+ {"type": "search", "query": "litellm"},+ {"type": "open_page", "url": "https://docs.litellm.ai/"},+ ],+ tool_usage={"web_search": {"num_requests": num_requests}},+ )++ cost = _bedrock_mantle_web_search_cost(model, response)++ assert cost == pytest.approx(num_requests * _BEDROCK_MANTLE_WEB_SEARCH_RATE), (+ f"{num_requests} reported web search requests must bill {num_requests} x "+ f"${_BEDROCK_MANTLE_WEB_SEARCH_RATE}, got ${cost}"+ )+++ "tool_usage",+ [None, {}, {"web_search": None}, {"web_search": {"num_requests": "many"}}, {"web_search": {"num_requests": -1}}],+)+def test_web_search_call_count_falls_back_to_items_without_reported_count(local_model_cost_map, tool_usage):+ """+ Without a usable reported count (no tool_usage, no web_search block, or a malformed one) the+ per-call path must keep counting web_search_call items instead of raising or billing zero.+ """+ model = "bedrock_mantle/openai.gpt-5.6-sol"+ response = _bedrock_mantle_responses_with_web_search(+ model,+ actions=[{"type": "search", "query": "litellm"}, {"type": "search", "query": "bedrock web search"}],+ tool_usage=tool_usage,+ )++ cost = _bedrock_mantle_web_search_cost(model, response)++ assert cost == pytest.approx(2 * _BEDROCK_MANTLE_WEB_SEARCH_RATE), (+ f"2 web_search_call items with tool_usage={tool_usage!r} must bill 2 x "+ f"${_BEDROCK_MANTLE_WEB_SEARCH_RATE}, got ${cost}"+ )