Skip to content

fix web search tool cost - #25028

Closed
mubashir1osmani wants to merge 1 commit into
BerriAI:mainfrom
mubashir1osmani:fix_web_search_pricing
Closed

fix web search tool cost#25028
mubashir1osmani wants to merge 1 commit into
BerriAI:mainfrom
mubashir1osmani:fix_web_search_pricing

Conversation

@mubashir1osmani

Copy link
Copy Markdown
Contributor

Relevant issues

Capture d’écran, le 2026-04-02 à 17 33 30

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test

Changes

@vercel

vercel Bot commented Apr 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Apr 2, 2026 9:59pm

Request Review

@codspeed-hq

codspeed-hq Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing mubashir1osmani:fix_web_search_pricing (7280721) with main (9c5fda4)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a gap in Anthropic web search cost tracking: when a real ModelResponse was returned from an Anthropic completion that used the web search tool, the cost was silently dropped to zero because the ModelResponse branch of response_object_includes_web_search_call only checked for url_citation message annotations and usage.prompt_tokens_details.web_search_requests — it never checked the Anthropic-specific usage.server_tool_use.web_search_requests field.

Key changes:

  • tool_call_cost_tracking.py: Adds a server_tool_use.web_search_requests check inside the isinstance(response_object, ModelResponse) branch, mirroring the identical check that already existed in the elif usage is not None: fallback branch (used when no ModelResponse is present).
  • test_tool_call_cost_tracking.py: Adds a focused regression test (test_get_cost_for_anthropic_web_search_with_model_response) that constructs a ModelResponse + Usage object with server_tool_use.web_search_requests=2, asserts the detection returns True, and asserts the resulting cost is non-zero. Only local/mock objects are used — no real network calls.

Confidence Score: 5/5

  • Safe to merge — a small, targeted bug fix with a clear regression test and no side effects.
  • The change is minimal (6 lines of production code, a comment update, and one new test). It mirrors an identical check that already existed in the fallback branch, uses a model that is present in the cost map, and the new test exercises the exact previously-broken path without making real network calls. No custom rules are violated.
  • No files require special attention.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py Adds usage.server_tool_use.web_search_requests detection to the ModelResponse branch of response_object_includes_web_search_call, fixing a gap where Anthropic web search costs were missed when a real ModelResponse object was passed.
tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py Adds a regression test (test_get_cost_for_anthropic_web_search_with_model_response) that constructs a real ModelResponse with usage.server_tool_use.web_search_requests=2 and verifies both detection and non-zero cost; uses only local objects, no network calls.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[response_object_includes_web_search_call] --> B{isinstance ModelResponse?}
    B -->|Yes| C{has url_citation\nannotations?}
    C -->|Yes| D[return True]
    C -->|No| E{usage is not None?}
    E -->|Yes| F{usage.prompt_tokens_details\n.web_search_requests?}
    F -->|Yes| G[return True]
    F -->|No| H{usage.server_tool_use\n.web_search_requests?\n🆕 NEW CHECK}
    H -->|Yes| I[return True]
    H -->|No| J[return False]
    E -->|No| J
    B -->|No, ResponsesAPIResponse| K{has web_search_call\nin output?}
    K -->|Yes| D
    K -->|No| J
    B -->|No, other| L{usage.server_tool_use\n.web_search_requests?\nexisting check}
    L -->|Yes| D
    L -->|No| M{usage.prompt_tokens_details\n.web_search_requests?}
    M -->|Yes| D
    M -->|No| J

    style H fill:#90EE90,stroke:#006400
    style I fill:#90EE90,stroke:#006400
Loading

Reviews (1): Last reviewed commit: "fix web search tool cost" | Re-trigger Greptile

@codecov

codecov Bot commented Apr 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mubashir1osmani

Copy link
Copy Markdown
Contributor Author

fixed in #25052

@mubashir1osmani
mubashir1osmani deleted the fix_web_search_pricing branch April 4, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant