Bug: /clear corrupts message history — orphaned tool_result causes 400 API error on next turn

Status Fixed / completed
Maintainer reply None cached
Activity 3 comments · opened May 15, 2026 · closed May 16, 2026

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

  1. In a conversation, invoke the Skill tool (e.g. load the brainstorming skill)
  2. Skill loads successfully (tool_use + tool_result pair created)
  3. Run /clear
  4. Continue the conversation — Claude attempts the next API call

Expected Behavior

/clear should either:

  • Remove both the tool_use block (assistant turn) and its matching tool_result block (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.

View original on GitHub ↗

3 Comments

github-actions[bot] · 5 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/40305
  2. https://github.com/anthropics/claude-code/issues/14173
  3. https://github.com/anthropics/claude-code/issues/8484

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

Aaditya-git · 5 months ago

<img width="1600" height="274" alt="Image" src="https://github.com/user-attachments/assets/c8d9a17d-b39e-4dd6-ae73-3cfa80452054" />

github-actions[bot] · 3 months ago

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.