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-tracking): count dict-shaped web_search_call output items
mateo-berriff547be3e3f6
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 6439458b0de..887f167c262 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@@ -24,6 +24,11 @@ from litellm.types.utils import ()+def _output_item_type(output_item: object) -> str | None:+ item_type: Final = output_item.get("type") if isinstance(output_item, dict) else getattr(output_item, "type", None)+ return item_type if isinstance(item_type, str) else None++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):@@ -144,7 +149,7 @@ class StandardBuiltInToolCostTracking:"""if isinstance(response_object, ResponsesAPIResponse):count = sum(- 1 for output_item in response_object.output if getattr(output_item, "type", None) == "web_search_call"+ 1 for output_item in response_object.output if _output_item_type(output_item) == "web_search_call")return max(count, 1)return 1@@ -463,14 +468,7 @@ class StandardBuiltInToolCostTracking:Returns:True if the ResponsesAPIResponse includes one of the specified output types, False otherwise."""- output: Final = response_object.output- for output_item in output:- _output_type: str | None = (- output_item.get("type") if isinstance(output_item, dict) else getattr(output_item, "type", None)- )- if _output_type == output_type:- return True- return False+ return any(_output_item_type(output_item) == output_type for output_item in response_object.output)@staticmethoddef _safe_get_model_info(model: str, custom_llm_provider: str | None = None) -> ModelInfo | None: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 0f363718716..1adee045e17 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@@ -687,6 +687,49 @@ def test_openai_responses_web_search_multiplied_by_call_count(local_model_cost_m)+def test_web_search_call_count_reads_dict_output_items(local_model_cost_map):+ """+ Regression: output items that fail OpenAI SDK validation (e.g. xAI web_search_call+ items without an "action" field) stay plain dicts in the output union. The per-call+ counter must read their "type" key like the detection gate does, instead of flooring+ a multi-search response to a single billable search.+ """+ from litellm.types.llms.openai import ResponsesAPIResponse+ from litellm.types.utils import Usage++ model = "gpt-4o-search-preview"+ per_call = litellm.get_model_info(model)["search_context_cost_per_query"][+ "search_context_size_medium"+ ]++ response = ResponsesAPIResponse.model_validate(+ {+ "id": "resp_1",+ "created_at": 1754900000,+ "model": model,+ "object": "response",+ "status": "completed",+ "output": [+ {"type": "web_search_call", "id": f"ws_{i}", "status": "completed"}+ for i in range(3)+ ],+ }+ )+ assert all(isinstance(item, dict) for item in response.output)++ cost = 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="openai",+ standard_built_in_tools_params=None,+ )++ assert cost == pytest.approx(3 * per_call), (+ f"3 dict-shaped web searches must bill 3 x ${per_call}, got ${cost}"+ )++# Note: File search integration test removed due to complex annotation detection logic# The unit tests in test_azure_assistant_cost_tracking.py provide comprehensive coverage