Repository navigation
Closed
Description
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:
- The
stopReasonon the assistant message iserror, nottool_use - The API does not consider the
tool_useas valid when the response errored - The subsequent synthetic
tool_resultbecomes orphaned - All future requests fail with:
unexpected tool_use_id found in tool_result blocks
Steps to Reproduce
- Have a session running with tool calls
- API returns
overloaded_error(or similar) mid-tool-call - Clawdbot inserts synthetic tool_result (line shows:
[clawdbot] missing tool result in session history; inserted synthetic error result for transcript repair) - 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:
- Remove the incomplete tool_use from the assistant message, OR
- Not insert a synthetic tool_result since the tool_use is not valid, OR
- 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.
Metadata
Metadata
Assignees
Labels
No labels
Type
Fields
Priority
None yet
Activity
steipete commentedon Apr 24, 2026
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:
installSessionToolResultGuardexplicitly avoids extracting tool calls when an assistant message hasstopReasonerrororaborted, with an inline note that synthetic results here causeunexpected tool_use_id found in tool_result blocks400s. (src/agents/session-tool-result-guard.ts:217, 564f820efa2b)repairToolUseResultPairingpreserves errored/aborted assistant turns without inserting missing synthetic tool results, while still allowing matching real tool results to remain. (src/agents/session-transcript-repair.ts:527, 564f820efa2b)stopReason: "error"and a tool call does not create a synthetictoolResulton flush. (src/agents/session-tool-result-guard.test.ts:521, 564f820efa2b)stopReason: "error"do not get synthetic tool results added, specifically citing the same Anthropic 400 error text from the report. (src/agents/session-transcript-repair.test.ts:147, 564f820efa2b)src/agents/pi-embedded-runner/run/attempt.test.ts:2085, 564f820efa2b)v2026.4.22(00bd2cf7a376f1fba26291c6c4766f1f15cbdfa5) includes the samestopReason !== "error" && stopReason !== "aborted"guard insrc/agents/session-tool-result-guard.tsand the same errored-turn handling insrc/agents/session-transcript-repair.ts. (00bd2cf7a376)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.