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(xai): read web_search per-call rate from model_info

Yang Yang7aaa9358aa67
diff --git a/litellm/llms/xai/cost_calculator.py b/litellm/llms/xai/cost_calculator.py
index 981cdb7ac88..ebeac61fb43 100644
--- a/litellm/llms/xai/cost_calculator.py
+++ b/litellm/llms/xai/cost_calculator.py
@@ -13,8 +13,8 @@ from litellm.types.utils import PromptTokensDetailsWrapper, Usage
if TYPE_CHECKING:
from litellm.types.utils import ModelInfo
-# https://docs.x.ai/developers/pricing#tools-pricing
-_WEB_SEARCH_COST_PER_CALL = 5.0 / 1000.0
+# https://docs.x.ai/developers/pricing#tools-pricing — default when unset in model map
+_DEFAULT_WEB_SEARCH_COST_PER_CALL = 5.0 / 1000.0
def apply_server_side_tool_usage_details_to_usage(
@@ -77,14 +77,40 @@ def cost_per_token(model: str, usage: Usage) -> tuple[float, float]:
return prompt_cost, completion_cost
+def _web_search_cost_per_call_from_model_info(model_info: "ModelInfo") -> float:
+ """
+ Per-invocation web_search price from model_info when configured.
+
+ Prefer ``search_context_cost_per_query`` (same shape as Gemini/Anthropic web
+ search pricing in the model cost map). Fall back to current xAI list pricing.
+ """
+ search_costs = model_info.get("search_context_cost_per_query") or {}
+ if isinstance(search_costs, Mapping):
+ for key in (
+ "search_context_size_medium",
+ "search_context_size_low",
+ "search_context_size_high",
+ ):
+ value = search_costs.get(key)
+ if value is None:
+ continue
+ try:
+ cost = float(value)
+ except (TypeError, ValueError):
+ continue
+ if cost > 0:
+ return cost
+ return _DEFAULT_WEB_SEARCH_COST_PER_CALL
+
+
def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> float:
"""
Calculate the cost of web search requests for X.AI models.
- Uses usage.server_side_tool_usage_details.web_search_calls at $5 / 1k calls
- (xAI tools pricing), not legacy num_sources_used / web_search_requests.
+ Counts invocations from usage.server_side_tool_usage_details.web_search_calls.
+ Per-call rate comes from model_info.search_context_cost_per_query when set,
+ otherwise the default xAI tools rate ($5 / 1k calls).
"""
- _ = model_info
details = getattr(usage, "server_side_tool_usage_details", None)
if not isinstance(details, Mapping):
return 0.0
@@ -94,4 +120,4 @@ def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> floa
return 0.0
if web_search_calls <= 0:
return 0.0
- return _WEB_SEARCH_COST_PER_CALL * web_search_calls
+ return _web_search_cost_per_call_from_model_info(model_info) * web_search_calls
diff --git a/tests/test_litellm/llms/xai/test_xai_cost_calculator.py b/tests/test_litellm/llms/xai/test_xai_cost_calculator.py
index 6435cf6218b..585a23725e6 100644
--- a/tests/test_litellm/llms/xai/test_xai_cost_calculator.py
+++ b/tests/test_litellm/llms/xai/test_xai_cost_calculator.py
@@ -362,7 +362,7 @@ class TestXAICostCalculator:
assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
def test_web_search_cost_via_server_side_tool_usage_details(self):
- """usage.server_side_tool_usage_details.web_search_calls at $5/1k."""
+ """usage.server_side_tool_usage_details.web_search_calls at default $5/1k."""
usage = Usage(prompt_tokens=100, completion_tokens=50, total_tokens=150)
setattr(
usage,
@@ -380,6 +380,19 @@ class TestXAICostCalculator:
web_search_cost = cost_per_web_search_request(usage=usage, model_info={})
assert math.isclose(web_search_cost, 3 * (5.0 / 1000.0), rel_tol=1e-10)
+ def test_web_search_cost_uses_model_info_search_context_pricing(self):
+ usage = Usage(prompt_tokens=10, completion_tokens=5, total_tokens=15)
+ setattr(usage, "server_side_tool_usage_details", {"web_search_calls": 2})
+ model_info = {
+ "search_context_cost_per_query": {
+ "search_context_size_medium": 0.01,
+ }
+ }
+ web_search_cost = cost_per_web_search_request(
+ usage=usage, model_info=model_info
+ )
+ assert math.isclose(web_search_cost, 0.02, rel_tol=1e-10)
+
def test_web_search_cost_zero_without_details(self):
usage = Usage(prompt_tokens=100, completion_tokens=50, total_tokens=150)
assert cost_per_web_search_request(usage=usage, model_info={}) == 0.0