Repository navigation
Description
Describe the bug
When an agent using the Anthropic API backend makes multiple parallel tool calls in a single reasoning step (e.g., 6 × list_files), the API returns a 400 error because the message format produced by ReActAgent.CallExecution#runToolBatch does not satisfy Anthropic's tool_use/tool_result alternation requirement.
Anthropic's API requires each tool_use block in an assistant message to be immediately followed by a user message containing the corresponding tool_result block:
assistant(tool_use_1) → user(tool_result_1) → assistant(tool_use_2) → user(tool_result_2) ...
However, agentscope's batch tool execution path packs all N tool_use blocks into a single assistant message and puts all N tool_result blocks into a single subsequent user message:
assistant(tool_use_1, tool_use_2, ..., tool_use_N) → user(tool_result_1, tool_result_2, ..., tool_result_N)
This causes the Anthropic API to reject the request with a 400 error, as orphaned tool_use IDs are detected.
The issue also affects the compaction middleware — when compaction retries the request, the same corrupted message format is replayed, leading to "Retries exhausted: 2/2".
To Reproduce
- Configure an agent to use any model that goes through the Anthropic API backend (e.g.,
deepseek-v4-provia Anthropic-compatible endpoint, or any Claude model) - Start a chat session and prompt the agent in a way that causes it to make 2+ parallel tool calls in a single reasoning step. Example prompt:
List the contents of the root directory and all its subdirectories.
- The agent calls
list_files("/")successfully, then reasons about the result and decides to explore N subdirectories in parallel by issuing Nlist_filescalls simultaneously - The backend throws
RetryExhaustedException: Retries exhausted: 2/2and the chat fails
Code reference:
io.agentscope.core.ReActAgent$CallExecution#runToolBatch(ReActAgent.java ~line 2647) — executes tools in parallel and assembles resultsio.agentscope.extensions.model.anthropic.AnthropicChatModel#doStream(AnthropicChatModel.java ~line 186) — sends the assembled messages to the Anthropic API
Expected behavior
When multiple tool calls are executed in batch, the messages should be split into alternating assistant(tool_use) / user(tool_result) pairs before being sent to the Anthropic API, matching the format required by the API specification.
Alternatively, the toolkit/batch-execution layer could serialize parallel tool calls into sequential tool_use → tool_result pairs when the backend is Anthropic-compatible.
Error messages
io.agentscope.core.model.ModelException: Failed to stream Anthropic API: 400:
{error={message=messages.2:`tool_use` ids were found without `tool_result` blocks immediately after:
call_01_YbmOajOck8NJNRorbJsy4185. Each `tool_use` block must have a corresponding
`tool_result` block in the next message., type=invalid_request_error, param=null, code=invalid_request_error}}
Caused by: com.anthropic.errors.BadRequestException: 400:
{error={message=messages.2:`tool_use` ids were found without `tool_result` blocks immediately after: ...}}
When the error occurs with 5 parallel tool calls, all 5 IDs are listed:
messages.6:`tool_use` ids were found without `tool_result` blocks immediately after:
call_01_nRdVlzpcJUrgzFQKPC4W1374, call_02_47doT8Mk3QtUenP4rx285750,
call_03_efCIiRalJ59fBLBCemHw0807, call_04_NTlKmMGgKhLlI2y1laNO8740,
call_05_gVhawZJABQEq5dJmyMr90958.
Followed by compaction failure:
i.a.h.a.middleware.CompactionMiddleware: Compaction failed, continuing without compaction:
Retries exhausted: 2/2
And finally:
i.a.builder.web.api.ChatController: Chat stream error: userId=admin, agentId=xxx,
error=Retries exhausted: 2/2
Environment
- AgentScope-Java Version: 2.0.0-RC5
- Java Version: OpenJDK 21
- OS: Windows 10 IoT Enterprise LTSC 2021
- Model: deepseek-v4-pro (via Anthropic-compatible API endpoint)
Additional context
- Single tool calls work perfectly — the issue only triggers when N ≥ 2 tools are called in parallel
- The same 400 error occurs regardless of which specific tools are called (reproduced with
list_files,read_file,search_memory) - Non-Anthropic backends (e.g., DashScope native) are not affected
- This is a platform-level blocker: users see "chat failed" with no actionable feedback when their agent happens to make parallel tool calls
Activity
fix(anthropic): split parallel tool calls into alternating messages (#…
fix(anthropic): split parallel tool calls into alternating messages (a…