← Back to Pull requests

fix: make Messages web search max_uses limit searches, not calls #391

Closed
unknown closed this pull request without merging on Sep 30, 2026 08:44 UTC
View on GitHub
unknown unknown commented on Sep 29, 2026 19:44 UTC
Author

Summary

Anthropic defines max_uses on the web search tool as a hard cap on searches: "The max_uses parameter limits the number of searches performed." The Messages loops charge that budget once per tool call. That was exact when #174 added it, because a call carried one query. #214 then let the shared web_search function tool batch up to five queries per call, and that tool is what a native declaration is normalized to for the upstream model. One call can therefore run five searches, and max_uses: N allows up to 5N.

With max_uses: 2 and a model that asks for eight searches over three tool rounds:

| Round | Call | Queries | main | This PR |
| --- | --- | --- | --- | --- |
| 1 | t1 | 3 | runs 3 searches | refused |
| 1 | t2 | 1 | runs 1 search | runs 1 search |
| 1 | t3 | 2 | refused | refused |
| 2 | t4 | 1 | refused | runs 1 search |
| 3 | t5 | 1 | refused | refused |
| | Searches performed | | 4 | 2 |

The budget now counts searches:

  • Each gateway call is admitted in model order, before any call starts, only if the remaining budget covers every query it would run. The count comes from the web search handler's own argument parser, so it is the number of searches the handler would perform.

  • A call the budget cannot cover is refused whole with the existing max_uses tool_result and leaves the budget for a later call that fits. A call's result is therefore either the output of the call the model made or the max_uses error, never the output of a shortened call.

  • A call whose arguments cannot be parsed performs no search and is no longer charged. An admitted call is charged for the searches it requests whatever their outcome, as a call was before.

  • Requests without max_uses, aliased client tools, mixed client and gateway rounds, and the normalized tool schema are unchanged, so batching stays available.
  • Both loops now dispatch through one function, executor/messages_tools.rs, so JSON and SSE cannot apply the rule differently. The streaming path had no max_uses test; it has one now. messages_stream.rs loses three production lines and stays under its file-size cap. The README and ARCHITECTURE.md state the budget unit.

    Related: #313 and Enterprise Readiness #316 (bounded, predictable built-in tool execution for Messages).

    Test Plan

    Red on unmodified main (d162633) and green with this change. The red run used a separate worktree at main with only the test files applied and the workspace crates rebuilt from that worktree.

  • native_web_search_max_uses_counts_every_batched_query and messages_stream_max_uses_counts_every_batched_query run the scenario in the table through the JSON and SSE loops and assert the queries the search backend received and every fed-back tool_result. On main four searches reach the backend.

  • native_web_search_malformed_calls_do_not_use_the_budget and its streaming twin put an empty queries list and a non-object input ahead of a valid search under max_uses: 1. On main the malformed call uses up the budget and the valid search never runs.

  • messages_web_search_budget_test.rs drives the table scenario through the HTTP gateway with stream: false and stream: true, against an inference and a search backend on a real listener. On main both fail with four searches.

  • Controls that pass on main and here: a batch the budget covers runs in full, a request without max_uses is not limited, and one search per call is limited as before on both transports.

  • Unit tests cover requested_searches against the handler's parser and the admission rule of the budget.

  • Against the real agentic-server binary with a stdlib inference and search backend, over JSON and SSE: the upstream model is offered the batched queries parameter, the binary built from main runs four searches under max_uses: 2, and the one built from this commit runs exactly two and returns 200 with the final answer and no gateway call.

  • pre-commit run --all-files (including rust-file-sizes with RUST_FILE_SIZE_BASE=origin/main), cargo fmt -- --check, cargo clippy --all-targets -- -D warnings, and cargo test --no-fail-fast across the workspace pass on the exact commit from a clean worktree.
  • Diff: 530 lines (+397 / -133) across 6 files Reviewer: franciscojavierarceo Review duration: 779m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 530 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.375
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #391"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.9% (MEDIUM) · Higher = larger blast radius
    47.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.9% · Safe to merge
    18.9%