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.
3 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
<img width="1600" height="274" alt="Image" src="https://github.com/user-attachments/assets/c8d9a17d-b39e-4dd6-ae73-3cfa80452054" />
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.