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(cost): bill azure_ai claude web searches per server_tool_use request (#46114)

devin-ai-integration[bot]5338edb7e321
diff --git a/litellm/llms/__init__.py b/litellm/llms/__init__.py
index 88a44f38c57..816311e401e 100644
--- a/litellm/llms/__init__.py
+++ b/litellm/llms/__init__.py
@@ -62,6 +62,13 @@ def get_cost_for_web_search_request(custom_llm_provider: str, usage: "Usage", mo
)
return cost_per_web_search_request_vertex_ai(usage=usage, model_info=model_info)
+ elif custom_llm_provider == "azure_ai":
+ azure_ai_model_key: Final[str] = model_info.get("key", "") if model_info else ""
+ if "claude" not in azure_ai_model_key.lower():
+ return None
+ from .anthropic.cost_calculation import get_cost_for_anthropic_web_search
+
+ return get_cost_for_anthropic_web_search(model_info=model_info, usage=usage)
elif custom_llm_provider == "perplexity":
# Perplexity handles search costs internally in its own cost calculator
# Return 0.0 to indicate costs are already accounted for
diff --git a/tests/integration/cost_calculation/cases/misc.py b/tests/integration/cost_calculation/cases/misc.py
index 61dae9a3b53..3e76181c1ec 100644
--- a/tests/integration/cost_calculation/cases/misc.py
+++ b/tests/integration/cost_calculation/cases/misc.py
@@ -740,6 +740,258 @@ AZURE_AI_GPT_5_4_MINI_LATEST_STREAM: Final = CostTrackingTestCase(
)
+# azure_ai/claude-haiku-4-5 (Azure AI Foundry Anthropic, web search billed per server_tool_use.web_search_requests)
+AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_WEB_SEARCH: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-messages_web_search_two_requests",
+ covers="quota_management.spend_tracking.cost_matrix.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ endpoint="/v1/messages",
+ request={
+ "model": "$MODEL",
+ "max_tokens": 412,
+ "messages": [{"role": "user", "content": "azure-ai-claude-haiku-4-5-messages_web_search_two_requests"}],
+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}],
+ },
+ response=JsonResponse(
+ content_type="application/json",
+ body={
+ "id": "msg_$UNIQUE_ID",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "scripted answer a1b2c3d4e5f6"}],
+ "stop_reason": "end_turn",
+ "usage": {"input_tokens": 1840, "output_tokens": 412, "server_tool_use": {"web_search_requests": 2}},
+ },
+ ),
+ expected=ExactExpected(
+ spend=0.0239,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.02,
+ rollups=True,
+ ),
+)
+
+AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_WEB_SEARCH_STREAM: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-messages_web_search_two_requests_stream",
+ covers="quota_management.spend_tracking.scripted_wire.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ endpoint="/v1/messages",
+ request={
+ "model": "$MODEL",
+ "max_tokens": 412,
+ "messages": [{"role": "user", "content": "azure-ai-claude-haiku-4-5-messages_web_search_two_requests_stream"}],
+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}],
+ "stream": True,
+ },
+ response=SseResponse(
+ content_type="text/event-stream",
+ frames=(
+ 'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_$REQUEST_ID","type":"message","role":"assistant","model":"claude-haiku-4-5","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":1840}}}',
+ 'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}',
+ 'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"scripted response"}}',
+ 'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}',
+ 'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":412,"server_tool_use":{"web_search_requests":2}}}',
+ 'event: message_stop\ndata: {"type":"message_stop"}',
+ ),
+ ),
+ expected=ExactExpected(
+ spend=0.0239,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.02,
+ ),
+)
+
+AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_WEB_SEARCH_ONE_REQUEST: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-messages_web_search_one_request",
+ covers="quota_management.spend_tracking.cost_matrix.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ endpoint="/v1/messages",
+ request={
+ "model": "$MODEL",
+ "max_tokens": 412,
+ "messages": [{"role": "user", "content": "azure-ai-claude-haiku-4-5-messages_web_search_one_request"}],
+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}],
+ },
+ response=JsonResponse(
+ content_type="application/json",
+ body={
+ "id": "msg_$REQUEST_ID",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "scripted answer 0f1e2d3c4b5a"}],
+ "stop_reason": "end_turn",
+ "usage": {"input_tokens": 1840, "output_tokens": 412, "server_tool_use": {"web_search_requests": 1}},
+ },
+ ),
+ expected=ExactExpected(
+ spend=0.0139,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.01,
+ ),
+)
+
+AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_NO_SEARCH: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-messages_no_server_tool_use",
+ covers="quota_management.spend_tracking.cost_matrix.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ endpoint="/v1/messages",
+ request={
+ "model": "$MODEL",
+ "max_tokens": 412,
+ "messages": [{"role": "user", "content": "azure-ai-claude-haiku-4-5-messages_no_server_tool_use"}],
+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}],
+ },
+ response=JsonResponse(
+ content_type="application/json",
+ body={
+ "id": "msg_$REQUEST_ID",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "scripted answer 9a8b7c6d5e4f"}],
+ "stop_reason": "end_turn",
+ "usage": {"input_tokens": 1840, "output_tokens": 412},
+ },
+ ),
+ expected=ExactExpected(
+ spend=0.0039,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.0,
+ ),
+)
+
+AZURE_AI_CLAUDE_HAIKU_4_5_CHAT_WEB_SEARCH: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-chat_web_search_three_requests",
+ covers="quota_management.spend_tracking.cost_matrix.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ request={
+ "model": "$MODEL",
+ "messages": [{"role": "user", "content": "azure-ai-claude-haiku-4-5-chat_web_search_three_requests"}],
+ "stream": False,
+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}],
+ "allowed_openai_params": ["web_search_options"],
+ },
+ response=JsonResponse(
+ content_type="application/json",
+ body={
+ "id": "msg_$REQUEST_ID",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "scripted answer 5e4f3a2b1c0d"}],
+ "stop_reason": "end_turn",
+ "usage": {"input_tokens": 1840, "output_tokens": 412, "server_tool_use": {"web_search_requests": 3}},
+ },
+ ),
+ expected=ExactExpected(
+ spend=0.0339,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.03,
+ ),
+)
+
+AZURE_AI_CLAUDE_HAIKU_4_5_CHAT_WEB_SEARCH_STREAM: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-chat_web_search_two_requests_stream",
+ covers="quota_management.spend_tracking.scripted_wire.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ request={
+ "model": "$MODEL",
+ "messages": [{"role": "user", "content": "azure-ai-claude-haiku-4-5-chat_web_search_two_requests_stream"}],
+ "stream": True,
+ "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}],
+ "allowed_openai_params": ["web_search_options"],
+ },
+ response=SseResponse(
+ content_type="text/event-stream",
+ frames=sse_frames(
+ {
+ "type": "message_start",
+ "message": {
+ "id": "msg_$REQUEST_ID",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [],
+ "stop_reason": None,
+ "usage": {"input_tokens": 1840},
+ },
+ },
+ {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
+ {
+ "type": "content_block_delta",
+ "index": 0,
+ "delta": {"type": "text_delta", "text": "scripted answer 2c3d4e5f6a7b"},
+ },
+ {"type": "content_block_stop", "index": 0},
+ {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {"output_tokens": 412, "server_tool_use": {"web_search_requests": 2}},
+ },
+ {"type": "message_stop"},
+ ),
+ ),
+ expected=ExactExpected(
+ spend=0.0239,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.02,
+ ),
+)
+
+
+AZURE_AI_CLAUDE_HAIKU_4_5_RESPONSES_WEB_SEARCH: Final = CostTrackingTestCase(
+ name="azure-ai-claude-haiku-4-5-responses_web_search_two_requests",
+ covers="quota_management.spend_tracking.cost_matrix.logs_cost",
+ model="azure_ai/claude-haiku-4-5",
+ endpoint="/v1/responses",
+ request={
+ "model": "$MODEL",
+ "input": "azure-ai-claude-haiku-4-5-responses_web_search_two_requests",
+ "tools": [{"type": "web_search"}],
+ },
+ response=JsonResponse(
+ content_type="application/json",
+ body={
+ "id": "msg_$REQUEST_ID",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "scripted answer 7d6e5f4a3b2c"}],
+ "stop_reason": "end_turn",
+ "usage": {"input_tokens": 1840, "output_tokens": 412, "server_tool_use": {"web_search_requests": 2}},
+ },
+ ),
+ expected=ExactExpected(
+ spend=0.0239,
+ input_cost=0.00184,
+ output_cost=0.00206,
+ prompt_tokens=1840,
+ completion_tokens=412,
+ tool_usage_cost=0.02,
+ ),
+)
+
+
# groq/qwen/qwen3.8-27b
GROQ_QWEN_3_8_JSON: Final = CostTrackingTestCase(
name="groq-qwen-3.8-json",
@@ -896,6 +1148,13 @@ CASES: Final[tuple[CostTrackingTestCase, ...]] = (
BEDROCK_INVOKE_HAIKU_STREAM,
AZURE_AI_GPT_5_4_MINI_LATEST,
AZURE_AI_GPT_5_4_MINI_LATEST_STREAM,
+ AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_WEB_SEARCH,
+ AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_WEB_SEARCH_STREAM,
+ AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_WEB_SEARCH_ONE_REQUEST,
+ AZURE_AI_CLAUDE_HAIKU_4_5_MESSAGES_NO_SEARCH,
+ AZURE_AI_CLAUDE_HAIKU_4_5_CHAT_WEB_SEARCH,
+ AZURE_AI_CLAUDE_HAIKU_4_5_CHAT_WEB_SEARCH_STREAM,
+ AZURE_AI_CLAUDE_HAIKU_4_5_RESPONSES_WEB_SEARCH,
GROQ_QWEN_3_8_JSON,
GROQ_QWEN_3_8_STREAM_X_GROQ_RECOUNT,
COHERE_COMMAND_A_V2_TOKENS,
diff --git a/tests/integration/cost_calculation/cost_map.py b/tests/integration/cost_calculation/cost_map.py
index f8daa15a783..4cf217a6701 100644
--- a/tests/integration/cost_calculation/cost_map.py
+++ b/tests/integration/cost_calculation/cost_map.py
@@ -604,6 +604,18 @@ COST_MAP_ENTRIES: Final[dict[str, dict[str, JsonValue]]] = {
"input_cost_per_token": 7.5e-07,
"output_cost_per_token": 4.5e-06,
},
+ "azure_ai/claude-haiku-4-5": {
+ "litellm_provider": "azure_ai",
+ "mode": "chat",
+ "input_cost_per_token": 1e-06,
+ "output_cost_per_token": 5e-06,
+ "search_context_cost_per_query": {
+ "search_context_size_low": 0.01,
+ "search_context_size_medium": 0.01,
+ "search_context_size_high": 0.01,
+ },
+ "web_search_billing_unit": "per_query",
+ },
"groq/qwen/qwen3.8-27b": {
"litellm_provider": "groq",
"mode": "chat",
diff --git a/tests/unit/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py b/tests/unit/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py
index 5a01b28e8ac..759f686f32e 100644
--- a/tests/unit/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py
+++ b/tests/unit/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py
@@ -591,3 +591,68 @@ _BEDROCK_MANTLE_WEB_SEARCH_MODELS = (
)
_BEDROCK_MANTLE_WEB_SEARCH_RATE = 0.012
+
+
+ ("model_key", "web_search_requests", "medium_cost_per_query", "expected_cost"),
+ [
+ ("azure_ai/claude-haiku-4-5", 2, 0.01, 0.02),
+ ("azure_ai/claude-sonnet-4-5", 3, 0.01, 0.03),
+ ("azure_ai/claude-haiku-4-5", 1, 0.01, 0.01),
+ ("azure_ai/claude-haiku-4-5", 2, 0.0, 0.0),
+ ],
+)
+def test_azure_ai_claude_web_search_billed_per_server_tool_use_request(
+ model_key: str, web_search_requests: int, medium_cost_per_query: float, expected_cost: float
+) -> None:
+ from litellm.llms import get_cost_for_web_search_request
+ from litellm.types.utils import ModelInfo, SearchContextCostPerQuery, ServerToolUse, Usage
+
+ model_info = ModelInfo(
+ key=model_key,
+ litellm_provider="azure_ai",
+ mode="chat",
+ max_tokens=None,
+ max_input_tokens=None,
+ max_output_tokens=None,
+ input_cost_per_token=1e-6,
+ output_cost_per_token=5e-6,
+ search_context_cost_per_query=SearchContextCostPerQuery(search_context_size_medium=medium_cost_per_query),
+ )
+ usage = Usage(
+ prompt_tokens=1840,
+ completion_tokens=412,
+ total_tokens=2252,
+ server_tool_use=ServerToolUse(web_search_requests=web_search_requests),
+ )
+
+ cost = get_cost_for_web_search_request(custom_llm_provider="azure_ai", usage=usage, model_info=model_info)
+
+ assert cost == pytest.approx(expected_cost), (
+ f"azure_ai claude must bill {web_search_requests} searches at {medium_cost_per_query} each, got {cost}"
+ )
+
+
+def test_azure_ai_non_claude_web_search_keeps_generic_fallback() -> None:
+ from litellm.llms import get_cost_for_web_search_request
+ from litellm.types.utils import ModelInfo, SearchContextCostPerQuery, ServerToolUse, Usage
+
+ model_info = ModelInfo(
+ key="azure_ai/gpt-5.4-mini-2026-03-17",
+ litellm_provider="azure_ai",
+ mode="chat",
+ max_tokens=None,
+ max_input_tokens=None,
+ max_output_tokens=None,
+ input_cost_per_token=1e-6,
+ output_cost_per_token=5e-6,
+ search_context_cost_per_query=SearchContextCostPerQuery(search_context_size_medium=0.01),
+ )
+ usage = Usage(
+ prompt_tokens=1840,
+ completion_tokens=412,
+ total_tokens=2252,
+ server_tool_use=ServerToolUse(web_search_requests=2),
+ )
+
+ assert get_cost_for_web_search_request(custom_llm_provider="azure_ai", usage=usage, model_info=model_info) is None