claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

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

Open Belkins opened this issue 9 months ago β€’ 47 comments

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

Belkins avatar Mar 13 '25 22:03 Belkins

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!

myzie avatar Mar 20 '25 01:03 myzie

Actually, I ran into this just this evening as well - my solution was to order content blocks in messages like this:

  1. Tool Result Block
  2. Text Block
  3. 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

johnewart avatar Mar 20 '25 04:03 johnewart

Thanks @johnewart for chiming in!

myzie avatar Mar 20 '25 13:03 myzie

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.

sharyar avatar Mar 30 '25 04:03 sharyar

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

Kitenite avatar Apr 09 '25 00:04 Kitenite

Happening to me right now out of nowhere also

TweedBeetle avatar May 03 '25 14:05 TweedBeetle

@TweedBeetle same here, right now version 0.2.100

notabene01 avatar May 03 '25 14:05 notabene01

Hello. Same for me.

IlyaMaksheev avatar May 07 '25 11:05 IlyaMaksheev

hi same for me

ravishrawal avatar May 14 '25 06:05 ravishrawal

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

henricook avatar May 17 '25 15:05 henricook

Happening to me now as well in Claude Code. Anyone have a solution? Im stuck.

kashramli avatar May 19 '25 17:05 kashramli

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.

Erik-2040 avatar May 20 '25 15:05 Erik-2040

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?

cohml avatar May 22 '25 19:05 cohml

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?

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 avatar May 22 '25 21:05 Erik-2040

@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?

cohml avatar May 22 '25 22:05 cohml

@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?

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'

...

Erik-2040 avatar May 23 '25 00:05 Erik-2040

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.

awilcots avatar May 30 '25 18:05 awilcots

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.                                                                          β”‚
β”‚                                                                                                                                                β”‚

erg avatar Jun 03 '25 21:06 erg

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."}}

jounih avatar Jun 05 '25 10:06 jounih

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

jounih avatar Jun 05 '25 10:06 jounih

This happens to me all the time if I press [ESC] while it is trying to do something...

robsonfelix avatar Jun 05 '25 21:06 robsonfelix

Also happened to me twice now (once was after pasting logs) and wasn't able to recover the session. Double ESC works -- thanks everyone!

VPhung24 avatar Jun 07 '25 00:06 VPhung24

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. Recover the session with the uuid from step 3 with claude --resume <session-uuid>

Again, remember to backup the session file first!

dhirajsb avatar Jun 07 '25 05:06 dhirajsb

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. 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 avatar Jun 07 '25 06:06 abhagsain

@abhagsain

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. 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

mkXultra avatar Jun 07 '25 06:06 mkXultra

@abhagsain

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. 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

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? :(

Image

Image

abhagsain avatar Jun 07 '25 08:06 abhagsain

@abhagsain

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. 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

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? :(

Image

Image

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

Image

abhagsain avatar Jun 07 '25 11:06 abhagsain

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-code globally 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 /workspace and i got this: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.97: tool_use ids were found without tool_result blocks immediately after: toolu_018TaGiocJstYpjX5biVRdDu. Each tool_use block must have a corresponding tool_result block in the next message."}}

Let me know if any extra logs or configs would helpβ€”happy to provide more info.

maze1377 avatar Jun 07 '25 11:06 maze1377

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. 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.

dhirajsb avatar Jun 07 '25 18:06 dhirajsb

Welp, I ran into this too, but I was able to recover the session with the following:

  1. Copy the tool call id it complains about, which doesn't have a return block
  2. First backup and then edit the session file ~/.claude/projects/<project-path-name>/<session-uuid>.jsonl
  3. Remove the entries from the problematic tool id in step 1 above
  4. 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!

mrThomasTeller avatar Jun 07 '25 19:06 mrThomasTeller