Skip to content

fix(anthropic): preserve server tool results and caller for web_search_20260209 - #720

Open
tang-ontheway wants to merge 1 commit into
LibreChat-AI:mainfrom
tang-ontheway:fix/anthropic-web-search-20260209-caller-and-code-exec
Open

tang-ontheway wants to merge 1 commit into
LibreChat-AI:mainfrom
tang-ontheway:fix/anthropic-web-search-20260209-caller-and-code-exec

Conversation

@tang-ontheway

Copy link
Copy Markdown
Contributor

Fixes #690

Root Cause

When using Anthropic's web_search_20260209 dynamic filtering tool, assistant turns contain server_tool_use (code_execution), nested server_tool_use (web_search) with a caller field, web_search_tool_result with caller, and code_execution_tool_result (whose content is a code_execution_result object). Previously:

  1. _makeMessageChunkFromAnthropicEvent in src/llm/anthropic/utils/message_outputs.ts only accepted tool_use, document, server_tool_use, and web_search_tool_result on content_block_start, dropping code_execution_tool_result (and other server tool result types).
  2. _formatContent in src/llm/anthropic/utils/message_inputs.ts rebuilt server_tool_use and web_search_tool_result blocks without preserving caller.
  3. _formatContent rejected any srvtoolu_ tool result whose content was neither an array nor web_search_tool_result_error, dropping code_execution_tool_result blocks and causing Anthropic HTTP 400 errors on follow-up turns.

Changes

  • Added code_execution_tool_result, web_fetch_tool_result, bash_code_execution_tool_result, text_editor_code_execution_tool_result, and tool_search_tool_result to _makeMessageChunkFromAnthropicEvent in src/llm/anthropic/utils/message_outputs.ts.
  • Preserved caller (when present) on rebuilt server_tool_use and server tool result blocks in src/llm/anthropic/utils/message_inputs.ts, and passed through non-web_search_tool_result Anthropic server tool result blocks cleanly (stripping stream-only properties like index and input).
  • Added unit tests in src/llm/anthropic/utils/server-tool-inputs.test.ts covering both streaming chunk creation and multi-turn replay for code_execution + nested web_search with caller + code_execution_tool_result.

Verification

  • npx jest src/llm/anthropic/utils/server-tool-inputs.test.ts src/llm/anthropic/utils/streaming-tool-input.test.ts src/llm/anthropic/utils/tool-id-normalization.test.ts: 3 suites, 42 passed.

@roman-redl

roman-redl commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested this on LibreChat v0.8.9 (@librechat/agents 4.2.7, the four changes of this PR ported to the dist at build time) with web_search_20260209 / web_search_20260318, claude-opus-5-5, researcher subagents on claude-sonnet-5-5 and MCP tools. The changes here are needed, and two more were needed before turns that mix the search with client tool calls worked reliably:

  1. modifyContent drops code_execution_tool_result before the replay — 🧱 fix: Keep Anthropic Server Tool Result Blocks in the Final Message #731. After the stream, the graph passes the final Anthropic message through modifyDeltaProperties → modifyContent, which turns every block whose type is not in allowedTypesByProvider[Providers.ANTHROPIC] into a text block. code_execution_tool_result is not in the list, so the results never reach _formatContent. On a logged run, the response had two code_execution_tool_result blocks and the next request had none; Anthropic resends a missing result once, and the request after that fails with the 400 from Anthropic web_search_20260209 (dynamic filtering): code_execution_tool_result is dropped and caller is stripped, so the next request fails with HTTP 400 #690. It shows up only on turns of four or more requests, so short tests pass.
  2. The code execution container id — 🫙 fix: Repeat the Code Execution Container on Follow-up Anthropic Requests #732. When the model emits code_execution and a client tool call in the same response, the request with the tool result fails with 400 invalid_request_error: container_id is required when there are pending tool uses generated by code execution with tools. The id is already in additional_kwargs.container of the AI message, and invocationParams already forwards callOptions.container; it just has to be set from the latest AI message of the current turn.

With all three, 6 of 6 runs passed (including turns of 6 requests), plus a 4-turn session with 4 researcher subagents, 70 searches and 20 code execution calls without errors. 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

The new version ran more searches in the long session (and one more subagent in turn 2): with filtered results each search adds far less to the context, about 3.7x less cost per search, so the agents searched more within the same session and still spent less.

The LibreChat side (configurable tool version) is LibreChat-AI/LibreChat#18707.

roman-redl added a commit to roman-redl/parley that referenced this pull request Nov 22, 2026
…layed

With Anthropic web search dynamic filtering (web_search_20260209 and later) the model calls
code_execution and the nested web_search calls carry caller. @librechat/agents 4.2.7 dropped
code_execution_tool_result (stream parser, replay and modifyContent) and stripped caller, so a
later request of the turn failed with 400. patch-agents-servertools.sh ports
LibreChat-AI/agents#720 to the compiled code and adds the allowed block types fix
(LibreChat-AI/agents#731).
danny-avila pushed a commit that referenced this pull request Nov 23, 2026
…#736)

Retain native results and caller linkage through both stream paths and replay. Scope automatic container reuse to the current user turn.

Design credits: tang-ontheway (#720) and roman-redl (#731, #732; reported #690).

Co-authored-by: Lia <lia@librechat.ai>

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic web_search_20260209 (dynamic filtering): code_execution_tool_result is dropped and caller is stripped, so the next request fails with HTTP 400

2 participants