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

Resolved 💬 3 comments Opened May 15, 2026 by Aaditya-git 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 ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗