claude-code - 💡(How to fix) Fix Bug: /clear corrupts message history — orphaned tool_result causes 400 API error on next turn

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

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 Message

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.

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.

Fix Action

Workaround

Start a fresh conversation instead of using /clear after tool invocations.

Code Example

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.
RAW_BUFFERClick to expand / collapse

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.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING