Repository navigation
Description
Summary
Anthropic's newer web_search_20260209 tool filters search results with code execution before they enter the context. It cuts input tokens a lot, but cannot be used with @librechat/agents today: after a turn that used it, the next request that continues the same assistant message (for example after a client/MCP tool call) fails with HTTP 400, because the code_execution_tool_result block is dropped and the caller field is stripped.
What we tried and why
Same 6-search question sent straight to the Anthropic API, claude-opus-5-5:
| Tool version | Input tokens |
|---|---|
web_search_20250305 |
224,120 |
web_search_20260209 |
36,987 - 40,701 |
That is roughly 5.5x fewer input tokens, so we tried to switch LibreChat to the new version. LibreChat hardcodes { type: 'web_search_20250305', name: 'web_search' } (see Environment), so we patched the type to web_search_20260209 locally. The search itself works. The first request after a server search turn plus a client (MCP) tool call fails:
400 messages.1: `code_execution` tool use with id `srvtoolu_...` was found without a corresponding `code_execution_tool_result` block
Root cause
With web_search_20260209 the model calls code_execution (server tool, srvtoolu_ id) and the nested web_search calls carry caller: { type: 'code_execution_20260120', tool_id: '<srvtoolu_ id of the code_execution call>' }. The assistant message then contains server_tool_use (code_execution), server_tool_use (web_search, with caller), web_search_tool_result (with caller) and code_execution_tool_result. All of them have to be replayed unchanged.
Checked on main at b3387e6 (v4.2.5). All three problems were also reproduced with a throwaway unit test calling the functions below directly.
-
The stream parser drops
code_execution_tool_result._makeMessageChunkFromAnthropicEventonly turnstool_use,document,server_tool_useandweb_search_tool_resultblock starts into chunks; any other block type reaches the finalreturn null, so the block never enters the aggregated message. This is the path used by_streamResponseChunks. The newerconvertAnthropicStreammaps unknown types tonon_standard, so it keeps them.agents/src/llm/anthropic/utils/message_outputs.ts
Lines 116 to 124 in b3387e6
} else if ( data.type === 'content_block_start' && [ 'tool_use', 'document', 'server_tool_use', 'web_search_tool_result', ].includes(data.content_block.type) ) { return null; agents/src/llm/anthropic/utils/stream_events.ts
Lines 313 to 318 in b3387e6
case 'web_search_tool_result': return { ...block, index }; case 'compaction': return { ...block, index }; default: return { type: 'non_standard' as const, value: { ...block }, index };
Probe: a
content_block_startwithtype: 'code_execution_tool_result'returnsnull. A nestedserver_tool_useblock keeps itscallerat this stage. -
_formatContentrebuilds everysrvtoolu_block as{ type: 'server_tool_use', id, name, input }, which dropscaller.agents/src/llm/anthropic/utils/message_inputs.ts
Lines 679 to 701 in b3387e6
* Normalize server_tool_use blocks into a clean shape the API accepts. * These blocks may arrive with the correct type (server_tool_use), as a * standardized server_tool_call, or mislabeled after state serialization. * Regardless of current type, if the id starts with 'srvtoolu_' we rebuild * a clean block with only the properties the API expects. */ if ( 'id' in contentPart && typeof (contentPart as Record<string, unknown>).id === 'string' && ((contentPart as Record<string, unknown>).id as string).startsWith( Constants.ANTHROPIC_SERVER_TOOL_PREFIX ) && 'name' in contentPart ) { const rawPart = contentPart as Record<string, unknown>; const corrected: AnthropicServerToolUseBlockParam = { type: 'server_tool_use', id: rawPart.id as string, name: (rawPart.name ?? 'web_search') as 'web_search', input: coerceAnthropicToolUseInput(rawPart.input ?? rawPart.args), }; return corrected; }
web_search_tool_resultis rebuilt the same way with onlytype,tool_use_idandcontent, so itscalleris dropped too:agents/src/llm/anthropic/utils/message_inputs.ts
Lines 703 to 737 in b3387e6
/** * Normalize web_search_tool_result blocks into a clean shape. * Same rationale as above — the block may carry extra properties from * streaming (input, index, etc.) that the API rejects. Rebuild cleanly. */ if ( 'tool_use_id' in contentPart && typeof (contentPart as Record<string, unknown>).tool_use_id === 'string' && ( (contentPart as Record<string, unknown>).tool_use_id as string ).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) && 'content' in contentPart ) { const rawPart = contentPart as Record<string, unknown>; const content = rawPart.content; const isValidContent = Array.isArray(content) || (content != null && typeof content === 'object' && 'type' in content && (content as Record<string, unknown>).type === 'web_search_tool_result_error'); if (isValidContent) { const corrected: AnthropicWebSearchToolResultBlockParam = { type: 'web_search_tool_result', tool_use_id: rawPart.tool_use_id as string, content: content as AnthropicWebSearchToolResultBlockParam['content'], }; return corrected; } return null; }
-
Any
srvtoolu_result whosecontentis neither an array nor aweb_search_tool_result_erroris replaced bynull, which removescode_execution_tool_result(itscontentis an object of typecode_execution_result). The type is also missing from thetoolTypeslist at the top of the function.agents/src/llm/anthropic/utils/message_inputs.ts
Lines 648 to 655 in b3387e6
const toolTypes = [ 'tool_use', 'tool_result', 'input_json_delta', 'server_tool_use', 'web_search_tool_result', 'web_search_result', ]; agents/src/llm/anthropic/utils/message_inputs.ts
Lines 719 to 736 in b3387e6
const isValidContent = Array.isArray(content) || (content != null && typeof content === 'object' && 'type' in content && (content as Record<string, unknown>).type === 'web_search_tool_result_error'); if (isValidContent) { const corrected: AnthropicWebSearchToolResultBlockParam = { type: 'web_search_tool_result', tool_use_id: rawPart.tool_use_id as string, content: content as AnthropicWebSearchToolResultBlockParam['content'], }; return corrected; } return null;
Probe on the replay side: an assistant message with a nested server_tool_use (with caller) followed by a code_execution_tool_result is formatted as [server_tool_use (no caller), text]; the code_execution_tool_result is gone, which is exactly what the 400 reports. A code_execution server_tool_use followed by a nested web_search and its web_search_tool_result is formatted without any caller.
Even when the stream keeps the block (path 1 fixed), step 3 would drop it again, and a type that is not recognised at all would reach the Unsupported message content format throw at the end of _formatContent. So both sides need to change.
UI side effect
code_execution and the nested web_search calls show up in the LibreChat UI as failed tool calls with empty args and output parts. This is related to the server-tool run-step handling (see #575) and, for streams with several searches, to the eager-execution defect fixed separately in #689 (server srvtoolu_ calls dispatched as on_tool_execute, "Tool web_search not found").
Suggested fix
In @librechat/agents:
- Stream: keep
code_execution_tool_result(and the other Anthropic server tool result types:web_fetch_tool_result,bash_code_execution_tool_result,text_editor_code_execution_tool_result,tool_search_tool_result) when aggregating chunks in_makeMessageChunkFromAnthropicEvent, asconvertAnthropicStreamalready does. - Replay: preserve
calleron rebuiltserver_tool_useandweb_search_tool_resultblocks when present. - Replay: pass through server tool result blocks whose
tool_use_idstarts withsrvtoolu_for every server result type, not onlyweb_search_tool_result; rebuild them without stream-only properties (index) but keepcontentandcaller. - Add replay tests for a
code_execution+ nestedweb_searchmessage.
In LibreChat: make the web search tool version configurable (for example an endpoint-level option such as webSearchToolVersion, defaulting to web_search_20250305) so deployments can opt in to web_search_20260209 once replay is fixed. The version is currently hardcoded.
Environment
- LibreChat v0.8.8
@librechat/agents4.0.1- Anthropic provider,
claude-opus-5-5, built-in web search enabled - Confirmed the relevant code is unchanged on
LibreChat-AI/agentsmain at b3387e6 (v4.2.5), andLibreChat-AI/LibreChatmain at 9529c99 (v0.8.9-rc1, depends on@librechat/agents^4.2.4) still hardcodesweb_search_20250305.
Activity
roman-redl commentedon Nov 22, 2026
To make dynamic filtering (
web_search_20260209and later) work end to end:code_execution_tool_resultandcaller(_formatContent, stream parser)modifyContentTest details and numbers: #720 (comment)
⛓️ fix: Preserve Anthropic Server Tool State Across Client Tool Calls (…