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]

test_openai_responses_api_web_search_cost_tracking

Ishaan Jaff81033dc8c946
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 b1cdc119d63..629d6c9e311 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,7 +5,11 @@ Helper utilities for tracking the cost of built-in tools.
from typing import Any, Dict, Optional
import litellm
-from litellm.types.llms.openai import WebSearchOptions
+from litellm.types.llms.openai import (
+ ResponseOutputItem,
+ ResponsesAPIResponse,
+ WebSearchOptions,
+)
from litellm.types.utils import (
ModelInfo,
ModelResponse,
@@ -52,13 +56,12 @@ class StandardBuiltInToolCostTracking:
)
if isinstance(response_object, ModelResponse):
- if StandardBuiltInToolCostTracking.response_includes_annotations(
+ if StandardBuiltInToolCostTracking.chat_completion_response_includes_annotations(
response_object
):
return StandardBuiltInToolCostTracking.get_default_cost_for_web_search(
model_info
)
-
return 0.0
@staticmethod
@@ -99,7 +102,9 @@ class StandardBuiltInToolCostTracking:
return search_context_pricing.get("search_context_size_medium", 0.0)
@staticmethod
- def response_includes_annotations(response_object: ModelResponse) -> bool:
+ def chat_completion_response_includes_annotations(
+ response_object: ModelResponse,
+ ) -> bool:
for _choice in response_object.choices:
message = getattr(_choice, "message", None)
if message is not None and hasattr(message, "annotations"):
@@ -115,6 +120,14 @@ class StandardBuiltInToolCostTracking:
# Look for web search tool in the tools array
for tool in tools:
if isinstance(tool, dict):
- if "search_context_size" in tool:
+ if StandardBuiltInToolCostTracking._web_search_tool_call(tool):
return WebSearchOptions(**tool)
return None
+
+ @staticmethod
+ def _web_search_tool_call(tool: Dict) -> bool:
+ if tool.get("type", None) == "web_search_preview":
+ return True
+ if "search_context_size" in tool:
+ return True
+ return False
diff --git a/tests/logging_callback_tests/test_built_in_tools_cost_tracking.py b/tests/logging_callback_tests/test_built_in_tools_cost_tracking.py
index 726d270d55c..c269cd8cb22 100644
--- a/tests/logging_callback_tests/test_built_in_tools_cost_tracking.py
+++ b/tests/logging_callback_tests/test_built_in_tools_cost_tracking.py
@@ -113,8 +113,20 @@ async def test_openai_web_search_logging_cost_tracking_explicit_search_context_s
@pytest.mark.asyncio
-async def test_openai_web_search_with_tool_call_logging_cost_tracking():
- """search_context_size=high passed in tool call, so cost tracked as `search_context_size_high`"""
+ "tools_config,expected_context_size",
+ [
+ (
+ [{"type": "web_search_preview", "search_context_size": "high"}],
+ "search_context_size_high",
+ ),
+ ([{"type": "web_search_preview"}], "search_context_size_medium"),
+ ],
+)
+async def test_openai_responses_api_web_search_cost_tracking(
+ tools_config, expected_context_size
+):
+ """Test web search cost tracking with different search context sizes"""
test_custom_logger = await _setup_web_search_test()
response = await litellm.aresponses(
@@ -122,7 +134,7 @@ async def test_openai_web_search_with_tool_call_logging_cost_tracking():
input=[
{"role": "user", "content": "What was a positive news story from today?"}
],
- tools=[{"type": "web_search_preview", "search_context_size": "high"}],
+ tools=tools_config,
)
- await _verify_web_search_cost(test_custom_logger, "search_context_size_high")
+ await _verify_web_search_cost(test_custom_logger, expected_context_size)