Missing Tool Result Block After Tool Use This error suggests that after a tool use block in the conversation, you did not include the corresponding tool result block, which is required by the API. To resolve this: 1. Ensure that any `tool_use` block is i
Bug Description
stuck on Error: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.156: Did not find 1 tool_result block(s) at the beginning of this message. Messages following tool_use blocks must begin with a matching number of
tool_result blocks."}}
Environment Info
- Platform: macos
- Terminal: Apple_Terminal
- Version: 0.2.14
- Feedback ID: 4441d4b9-707d-4983-b467-74dd96d8ddc5
I ran across this error when using the Anthropic API (not Claude Code) and I assumed it was my fault initially. However I've looked more into it and I'm seeing something that looks like it could be a mistake on Anthropic's side. And I'm wondering if Claude Code may be subject to the same situation occasionally.
The key message sequence seems to be:
- An assistant message with two content blocks in an atypical order (tool_use first, text second)
- Then a user message with the corresponding tool_result
Note the assistant's own text content is BETWEEN the "tool_use" and the "tool_result" in the overall sequence. This seems to be the atypical order, as Claude most often will respond with (text first, tool_use second).
Here's an excerpt from the messages that cause the problem for me:
[
... truncated ...
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01Ya9WAYPEZjkyjxcw8J2SxB",
"content": "Error: Failed to read directory /home. open /home: operation not permitted"
}
]
},
{
"role": "assistant",
"content": [
{
"id": "toolu_01FGfUwSu2N7d8f1tAFh4snU",
"type": "tool_use",
"name": "ListDirectory",
"input": {
"path": "."
}
},
{
"type": "text",
"text": "I apologize for the confusion. It seems we don't have permission to access the /home directory directly. Let's try listing the current directory instead:"
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01FGfUwSu2N7d8f1tAFh4snU",
"content": "Directory listing for ..."
}
]
}
]
The error I get is this:
`tool_use` ids were found without `tool_result` blocks immediately after: toolu_01FGfUwSu2N7d8f1tAFh4snU.
Each `tool_use` block must have a corresponding `tool_result` block in the next message.
Note that the tool_result toolu_01FGfUwSu2N7d8f1tAFh4snU IS directly after the assistant message.
Curious if this resonates with anyone else that stumbles across this!
Actually, I ran into this just this evening as well - my solution was to order content blocks in messages like this:
- Tool Result Block
- Text Block
- Tool Request Block
This makes the API happy and guarantees responses are always right after requests, if you take all the blocks out and process them in literal order.
I think this is a bug in the Anthropic API - it used to work fine with the blocks out of order until just recently.
@myzie
Thanks @johnewart for chiming in!
Running into the same issue as well. This becomes pretty complicated when it comes to building agents where you may not necessarily have a lot of control with the ordering of messages.
I thought i was going crazy but seems like the Anthropic API calls occasionally will return the messages out of order with tool use and result
Happening to me right now out of nowhere also
@TweedBeetle same here, right now
version 0.2.100
Hello. Same for me.
hi same for me
From an affected user in Claude Code CLI: Is there a way I can recover my conversation when this happens? There's a lot of context I don't want to lose!
Edit: it's extremely hairy but I managed to rescue this session by finding the session JSON under ~/.claude/projects/_path_to_my_project/[LATEST-SESSION-STORE-HERE].json and editing out messages that caused the error (in my case the last 6 or so lines) before continuing the session with claude -c
Happening to me now as well in Claude Code. Anyone have a solution? Im stuck.
I am experiencing this the past 2 weeks trying to use Neovim Avante plugin with Anthropic API:
Error: {
message = "messages.10: tool_use ids were found without tool_result blocks immediately after: toolu_019dFspJ71N2tnHV31Bwkgq5. Each tool_use block must have a corresponding tool_result block in the next message.",
type = "invalid_request_error"
}
Claude claims this is due to Avante. But looking at the discussions here, the problem seems to be related to Anthropic. I would appreciate any help.
I'll chime in that I too have just experienced this error in Neovim's avante plugin (claude-3-7-sonnet-latest), basically the same as @Erik-2040:
Error: {
message = "messages.1: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_0166ConBrjB26ouQs8PfFyRv. Each `tool_use` block must have a corresponding `tool_result` block in the next message.",
type = "invalid_request_error"
}
This issue was opened over two months ago with many reported sufferers and π s. Anthropic, can we at least hear from you?
I'll chime in that I too have just experienced this error in Neovim's
avanteplugin (claude-3-7-sonnet-latest), basically the same as @Erik-2040:Error: { message = "messages.1: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_0166ConBrjB26ouQs8PfFyRv. Each `tool_use` block must have a corresponding `tool_result` block in the next message.", type = "invalid_request_error" }This issue was opened over two months ago with many reported sufferers and π s. Anthropic, can we at least hear from you?
Here is how I solved the problem. In avante.lua, I added:
disable_tools = true
Like this: 6 provider = 'claude', 7 claude = { 8 endpoint = 'https://api.anthropic.com', 9 model = 'claude-3-7-sonnet-20250219', 10 timeout = 30000, 11 temperature = 0, 12 --max_tokens = 20480, 13 max_tokens = 4096, 14 disable_tools = true, 15 -- REMOVED: strip_whitespace = true, 16 },
@Erik-2040 disable_tools = true kind of throws the baby out with the bathwater though, doesn't it? Sure you no longer get the error, but you also don't get tool calls, significantly limiting Claude's agentic intelligence. No?
@Erik-2040
disable_tools = truekind of throws the baby out with the bathwater though, doesn't it? Sure you no longer get the error, but you also don't get tool calls, significantly limiting Claude's agentic intelligence. No?
You're right. Today, I dealt with the issue again and the following solved the issue: mode = 'agentic',
... opts = { provider = 'claude', claude = { endpoint = 'https://api.anthropic.com', model = 'claude-3-5-sonnet-20241022', timeout = 30000, temperature = 0, max_tokens = 1024, -- TRY THIS: More conservative tool handling disable_tools = false,
) }, -- IMPORTANT: Try 'agentic' mode - it handles tools better mode = 'agentic', -- Better tool message handling than 'legacy'
...
I just ran into this error as well while using claude-3-7-sonnet-20250219. With help from this thread I was able to see where the error was in the logs. I ran into the error during an interactive claude session (installed via https://www.npmjs.com/package/@anthropic-ai/claude-code). It just kind of broke in the middle of processing a command. The output said "interrupted by user" and then I got the error on every following prompt.
I ended up fixing it, and keeping the context, by using the "undo" feature ( pressing escape twice ), and going to the command before the one that broke it. After that it worked just fine again. Feels like it broke during processing but moving back to before the problem made claude .. forget(?)? Whats strange is that I still see the affected blocks in the log.
I'm unsure if my situation is or will be similar to anyone else's, but that's what happened with me.
Ran into this on claude code 1.10, did a /bug. maybe it will help you debug
> http://localhost:5173/users the button is too dark to read
βΏ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.23: `tool_use` ids were found without
`tool_result` blocks immediately after: toolu_017HUZ46a25U7fc75Qt2JheW. Each `tool_use` block must have a corresponding `tool_result`
block in the next message."}}
> http://localhost:5173/users the button is too dark to read
βΏ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.23: `tool_use` ids were found without
`tool_result` blocks immediately after: toolu_017HUZ46a25U7fc75Qt2JheW. Each `tool_use` block must have a corresponding `tool_result`
block in the next message."}}
β Submit Bug Report β
β β
β Thank you for your report! β
β Feedback ID: e41c0453-fd44-423d-b702-c6c7f1b08bce β
β β
β Press Enter to also create a GitHub issue, or any other key to close. β
β β
also getting this and no way to recover in the claude code session
API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.92:tool_use ids were found withouttool_resultblocks immediately after: toolu_01S1bUuuq1FAWEg8FEJsXFaG. Each tool_useblock must have a correspondingtool_result block in the next message."}}
I ended up fixing it, and keeping the context, by using the "undo" feature ( pressing escape twice ), and going to the command before the one that broke it. After that it worked just fine again. Feels like it broke during processing but moving back
Thank you, this (double escape and choose an earlier history checkpoint) worked for me to recover
This happens to me all the time if I press [ESC] while it is trying to do something...
Also happened to me twice now (once was after pasting logs) and wasn't able to recover the session. Double ESC works -- thanks everyone!
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl - Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>
Again, remember to backup the session file first!
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl- Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>Again, remember to backup the session file first!
@dhirajsb how did you find the session id?
@abhagsain
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl- Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>Again, remember to backup the session file first!
@dhirajsb how did you find the session id?
use --output-format json option.
like claude -c -p --output-format json "hello"| jq
refs: https://docs.anthropic.com/en/docs/claude-code/sdk#json-output
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl- Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>Again, remember to backup the session file first!
@dhirajsb how did you find the session id?
use --output-format json option. like
claude -c -p --output-format json "hello"| jqrefs: https://docs.anthropic.com/en/docs/claude-code/sdk#json-output
I don't know what's wrong but whenever I try to resume my session the claude memory spikes up and after 1-2 minute the system restarts itself, so probably I lost the context? :(
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl- Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>Again, remember to backup the session file first!
@dhirajsb how did you find the session id?
use --output-format json option. like
claude -c -p --output-format json "hello"| jqrefs: https://docs.anthropic.com/en/docs/claude-code/sdk#json-outputI don't know what's wrong but whenever I try to resume my session the claude memory spikes up and after 1-2 minute the system restarts itself, so probably I lost the context? :(
This tool issue is happening when Claude is doing something and you hit ESC. You'll see the message "Interrupted by the user" in the console. After that, you'll get the tool call issue. My memory spike issue was happening because to edit this JSON lines file I was opening it in WindSurf because the content was not readable. I set the language mode of the editor to JSON so I can read the content, and the editor formatted the file. When you format the file to a JSON type, it will ad new lines, spacing etc, in the file
Iβm running into the same issue.
Environment details:
-
Running Claude Code inside Docker (custom image based on
node:18-slim) -
Installed
@anthropic-ai/claude-codeglobally via npm -
Using a non-root user for execution
-
Entrypoint runs a shell script that calls:
claude --dangerously-skip-permissions --model opus -p "$(cat "$prompt_file")"; -
Project workspace is mounted to
/workspaceand i got this: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.97:tool_useids were found withouttool_resultblocks immediately after: toolu_018TaGiocJstYpjX5biVRdDu. Eachtool_useblock must have a correspondingtool_resultblock in the next message."}}
Let me know if any extra logs or configs would helpβhappy to provide more info.
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl- Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>Again, remember to backup the session file first!
@dhirajsb how did you find the session id?
Luckily, I just had one session at the time, but if you do get an error with a tool call id, you can grep for it in your sessions and find the session id from the jsonl file name.
Welp, I ran into this too, but I was able to recover the session with the following:
- Copy the tool call id it complains about, which doesn't have a return block
- First backup and then edit the session file
~/.claude/projects/<project-path-name>/<session-uuid>.jsonl- Remove the entries from the problematic tool id in step 1 above
- Recover the session with the uuid from step 3 with
claude --resume <session-uuid>Again, remember to backup the session file first!
It works, thank you!