Skip to content

Orphan tool_result causes 400 error after API overload/error during tool call #42607

Closed

Description

@saulocuevas-cmd

Bug Description

When the Anthropic API returns an error (e.g., overloaded_error) during a tool call execution, Clawdbot inserts a synthetic tool_result to "repair" the transcript. However, this breaks the conversation structure because:

  1. The stopReason on the assistant message is error, not tool_use
  2. The API does not consider the tool_use as valid when the response errored
  3. The subsequent synthetic tool_result becomes orphaned
  4. All future requests fail with: unexpected tool_use_id found in tool_result blocks

Steps to Reproduce

  1. Have a session running with tool calls
  2. API returns overloaded_error (or similar) mid-tool-call
  3. Clawdbot inserts synthetic tool_result (line shows: [clawdbot] missing tool result in session history; inserted synthetic error result for transcript repair)
  4. Next message to the session fails with 400 error

Error Message

400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.34.content.1: unexpected `tool_use_id` found in `tool_result` blocks: toolu_01CvTeaPH28nkKQyXrfUFiqh. Each `tool_result` block must have a corresponding `tool_use` block in the previous message."}}

Transcript Evidence

Line 53 (assistant message with error):

{
  "role": "assistant",
  "content": [
    {"type": "toolCall", "id": "toolu_01CvTeaPH28nkKQyXrfUFiqh", "name": "exec", ...}
  ],
  "stopReason": "error",
  "errorMessage": "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"Overloaded\"}}"
}

Line 54 (orphan tool_result):

{
  "role": "toolResult",
  "toolCallId": "toolu_01CvTeaPH28nkKQyXrfUFiqh",
  "content": [{"type":"text","text":"[clawdbot] missing tool result in session history; inserted synthetic error result for transcript repair."}],
  "isError": true
}

Expected Behavior

When a tool call fails due to API error (stopReason: "error"), the transcript repair should either:

  1. Remove the incomplete tool_use from the assistant message, OR
  2. Not insert a synthetic tool_result since the tool_use is not valid, OR
  3. Mark/filter errored tool calls when building the messages array for the next API request

Workaround

Currently the only fix is to manually delete the session file and sessions.json entry to force a fresh session.

Environment

  • Clawdbot version: 2026.1.24-3
  • Model: claude-opus-4-5
  • OS: macOS (Darwin 24.5.0 arm64)

Frequency

This happens frequently when the API is under load or returns transient errors during tool execution.

Activity

  1. steipete commented on Apr 24, 2026

    @steipete
    Contributor

    Closing this as implemented after Codex review.

    Current main already handles this case: errored or aborted assistant tool-call turns no longer synthesize orphan tool results, and the latest shipped release v2026.4.22 already contains the same guard plus regression coverage.

    What I checked:

    So I’m closing this as already implemented rather than keeping a duplicate issue open.

    Review notes: reviewed against 564f820efa2b; fix evidence: release v2026.4.22, commit 00bd2cf7a376.

  2. locked as resolved and limited conversation to collaborators on Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions