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).
This commit is contained in:
Alexsander Hamir 2026-01-31 12:32:47 -08:00
parent b8380f237d
commit 535f721849

View file

@ -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