Bug: /clear corrupts message history — orphaned tool_result causes 400 API error on next turn
Summary
Using /clear mid-conversation after a Skill tool invocation corrupts the message history sent to the API. On the next turn, Claude Code sends a tool_result block as messages[0] with no preceding tool_use, causing a 400 error.
Error
API Error: 400 messages.0.content.0: unexpected `tool_use_id` found in `tool_result` blocks: toolu_016BFYNFBYQ1QSvqkqprjGpq. Each `tool_result` block must have a corresponding `tool_use` block in the previous message.
Steps to Reproduce
- In a conversation, invoke the
Skilltool (e.g. load thebrainstormingskill) - Skill loads successfully (
tool_use+tool_resultpair created) - Run
/clear - Continue the conversation — Claude attempts the next API call
Expected Behavior
/clear should either:
- Remove both the
tool_useblock (assistant turn) and its matchingtool_resultblock (user turn), or - Refuse to clear when there are pending tool pairs in the history, or
- Reset to a clean state with no orphaned blocks
Actual Behavior
The assistant turn containing the tool_use is cleared, but the user turn containing the tool_result survives as messages[0]. The API rejects the request because messages[0] contains a tool_result with no preceding tool_use.
Root Cause
/clear strips the assistant message with the tool_use block but leaves the user message with the corresponding tool_result. This creates an invalid message array where messages[0].content[0] is a tool_result — which the API rejects with a 400.
Environment
- Claude Code version: v2.1.142
- Model: claude-sonnet-4-6 (Claude Pro)
- OS: macOS (Darwin 24.6.0)
- Skill tool was used immediately before
/clear
Workaround
Start a fresh conversation instead of using /clear after tool invocations.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗