From 535f721849134c7bfbd8797aee88056ab3cd80e6 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Sat, 31 Jan 2026 12:32:47 -0800 Subject: [PATCH] fix(cost): always call get_cost_for_built_in_tools to avoid under-counting Revert the guard that skipped built-in tool cost when standard_built_in_tools_params was falsy. get_cost_for_built_in_tools can return non-zero from response/usage alone (e.g. web search) even when params is None/empty, so skipping the call caused under-counting. Keep discount/margin/logging_obj guards (crash fix + optimization). --- litellm/cost_calculator.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index b8d50939b5d..0240f796732 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -1325,18 +1325,17 @@ def completion_cost( # noqa: PLR0915 _final_cost = ( prompt_tokens_cost_usd_dollar + completion_tokens_cost_usd_dollar ) - if standard_built_in_tools_params: - cost_for_built_in_tools = ( - StandardBuiltInToolCostTracking.get_cost_for_built_in_tools( - model=model, - response_object=completion_response, - usage=cost_per_token_usage_object, - standard_built_in_tools_params=standard_built_in_tools_params, - custom_llm_provider=custom_llm_provider, - ) + # Always call: get_cost_for_built_in_tools can return non-zero from + # response/usage alone (e.g. web search) even when params is None/empty. + cost_for_built_in_tools = ( + StandardBuiltInToolCostTracking.get_cost_for_built_in_tools( + model=model, + response_object=completion_response, + usage=cost_per_token_usage_object, + standard_built_in_tools_params=standard_built_in_tools_params, + custom_llm_provider=custom_llm_provider, ) - else: - cost_for_built_in_tools = 0.0 + ) _final_cost += cost_for_built_in_tools # Apply discount from module-level config if configured