Skip to content

🔍 feat: Make the Anthropic Web Search Tool Version Configurable - #18707

Open
roman-redl wants to merge 1 commit into
LibreChat-AI:devfrom
roman-redl:feat/anthropic-web-search-version
Open

roman-redl wants to merge 1 commit into
LibreChat-AI:devfrom
roman-redl:feat/anthropic-web-search-version

Conversation

@roman-redl

Copy link
Copy Markdown

Summary

The Anthropic built-in web search tool is always sent as web_search_20250305, so deployments cannot opt into the newer versions, which filter search results with code execution before they enter the context (web_search_20260209; web_search_20260318 adds the optional response_inclusion parameter). Measured directly against the Anthropic API on the same 6-search question with claude-opus-5-5, the older tool used 224,120 input tokens and the newer one 36,987 to 40,701.

This adds an optional endpoints.anthropic.webSearchToolVersion setting in librechat.yaml. It accepts web_search_20250305 (the default), web_search_20260209 or web_search_20260318, is validated as an enum in the endpoint schema, and becomes the type of the web search tool built in getLLMConfig. With the setting absent, the request is unchanged.

The newer versions also need changes in @librechat/agents: LibreChat-AI/agents#720 (for LibreChat-AI/agents#690), plus two more found on a live test and described in my comment there (the allowed Anthropic block types in modifyContent, and sending the code execution container id). Without them, a turn that mixes a search with a client tool call fails with HTTP 400. That is why the default stays web_search_20250305 and why the example config notes the requirement.

Closes #18222

Depends on LibreChat-AI/agents#720 and the follow-ups described there (only for opting into the newer versions; the default needs nothing new).

How it works

endpoints:
  anthropic:
    webSearchToolVersion: web_search_20260318
librechat.yaml endpoints.anthropic.webSearchToolVersion
  anthropicEndpointSchema          # z.enum, validated at load
  initializeAnthropic              # copies it into AnthropicConfigOptions
    getLLMConfig                   # tools.push({ type: webSearchToolVersion ?? 'web_search_20250305', name: 'web_search' })

Agents with an Anthropic provider reach getLLMConfig through initializeAnthropic, so they pick the setting up without further changes. Provider-tool detection in packages/api/src/agents/initialize.ts matches on the web_search prefix and tool name, so it also covers the new types.

Type of change

  • Feature

Testing

Tested environments/configuration:

  • Unit tests for this change (below).
  • Live, on a v0.8.9 deployment with the same type switch applied at build time and the @librechat/agents changes above ported to the 4.2.7 dist: an agent on claude-opus-5-5 with MCP tools and researcher subagents on claude-sonnet-5-5, web_search_20260209 and web_search_20260318. Turns with code_execution + web_search and parallel MCP tool calls complete (6 of 6 runs, including turns of 6 requests), and follow-up turns with more searches work. Old vs new tool version on the same deployment:
Scenario web_search_20250305 web_search_20260318
Search-only question, 2 turns (6 + 8 searches with both versions) $0.41 + $0.40 $0.21 + $0.15
One search question that also runs a terminal command $0.27 $0.11 – $0.13
4-turn research session (subagents, follow-ups, file write, context question) 27 searches, $2.22 70 searches, $1.90, 0 errors

In the long session each search costs about 3.7x less; the agent spends part of the saving on more searches.

  • Config: endpoints.anthropic.webSearchToolVersion (covered through initializeAnthropic with a mocked app config).

Automated tests:

  • Added packages/data-provider/src/config.anthropic.spec.ts: setting is optional, the three versions are accepted, other values are rejected.
  • Extended packages/api/src/endpoints/anthropic/llm.spec.ts: the configured version is used (both newer versions), and no tool is added when web_search is off.
  • Extended packages/api/src/endpoints/anthropic/initialize.spec.ts: the default stays web_search_20250305; the endpoint setting reaches the tool.
  • cd packages/api && npx jest src/endpoints/anthropic (161 passed)
  • cd packages/data-provider && npx jest src/config (all passed)
  • packages/api/src/endpoints/openai/config.anthropic.spec.ts has 12 failing tests in my environment with and without this change (checked on the base commit); I did not investigate them.
  • npx tsc --noEmit in packages/api and npm run typecheck in packages/data-provider

Screenshots / recordings

No user-facing change.

Risk / compatibility

Additive and optional: no migration, and an unset value keeps web_search_20250305. Not covered: custom endpoints that use defaultParamsEndpoint: anthropic build the tool through the same function but have no endpoints.anthropic setting to read, so they keep the default. The Vertex AI path still sends the web-search-2025-03-05 beta header; whether Vertex accepts the newer versions was not verified.

Checklist

  • I reviewed my own changes
  • Relevant tests have been added or updated
  • Existing relevant tests pass
  • The change does not introduce new warnings or errors
  • User-facing or complex behavior is documented where necessary
  • Required dependency changes have been merged/published (fix(anthropic): preserve server tool results and caller for web_search_20260209 agents#720 and the follow-ups, needed only for the newer versions)
  • Required documentation PR: N/A for this PR; librechat.example.yaml is updated, a librechat.ai docs entry for the new key may follow

Add `endpoints.anthropic.webSearchToolVersion` to librechat.yaml, accepting
`web_search_20250305` (default), `web_search_20260209` or `web_search_20260318`,
and use it as the `type` of the built-in web search tool instead of the
hardcoded value.
roman-redl added a commit to roman-redl/parley that referenced this pull request Nov 22, 2026
…rch_20260318)

LibreChat 0.8.9 hardcodes web_search_20250305. The newer version filters the search results
with code execution before they enter the context. On the test stack a search-only question
cost $0.21 + $0.15 instead of $0.41 + $0.40 for two turns with the same searches, and a
4-turn research session with subagents $1.90 instead of $2.22 (70 searches instead of 27).
patch-websearch.sh swaps the type (LibreChat issue #18222, PR LibreChat-AI/LibreChat#18707).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant