[BUG] Conversation Compaction Corrupts Tool Use/Result Pairs
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Description
After conversation compaction occurs, subsequent API calls fail with a 400 error indicating orphaned tool_result blocks without corresponding tool_use blocks in the previous message.
Error Message
API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.2.content.3: unexpected `tool_use_id` found in `tool_result` blocks: toolu_01Mhh3CD3BeVEU2mUPenc1X3. Each `tool_result` block must have a corresponding `tool_use` block in the previous message."},"request_id":"req_011CTeiXF7oECCgLbvci5oey"}
Steps to Reproduce
- Start a Claude Code conversation
- Perform many tool uses (Read, Write, Bash, etc.) over an extended conversation
- Wait for automatic conversation compaction to trigger (likely based on token threshold)
- After compaction, attempt to continue the conversation
- Observe API 400 error about mismatched tool_use_id
Expected Behavior
Conversation compaction should maintain the integrity of tool_use/tool_result pairs by either:
- Removing both the
tool_useAND correspondingtool_resultblocks together, OR - Keeping both blocks intact
Actual Behavior
Compaction appears to remove tool_use blocks while leaving orphaned tool_result blocks, which violates the Claude API requirement that every tool_result must have a corresponding tool_use in the previous message.
Impact
- Forces users to start a new conversation, losing context and flow
- Conversation history becomes corrupted and unusable
- Disrupts multi-step workflows and long-running tasks
Workaround
Start a new conversation when this error occurs. There is no way to recover the corrupted conversation.
Environment
- Claude Code Version: [Current version]
- Platform: Linux
- OS Version: Linux 6.8.12-11-pve
- Date Observed: 2025-09-30
Additional Context
This issue appears to be related to the conversation compaction algorithm that reduces token usage. The compaction logic needs to ensure atomic removal/retention of tool_use/tool_result pairs to maintain API contract compliance.
Suggested Fix
The compaction algorithm should:
- Identify tool_use/tool_result pairs by matching tool_use_id
- Remove pairs atomically (both blocks together or neither)
- Validate message structure after compaction to ensure no orphaned tool_result blocks remain
What Should Happen?
Conversation compaction should maintain the integrity of tool_use/tool_result pairs. When compacting a conversation to reduce token usage, the algorithm must either:
- Remove both the tool_use block AND its corresponding tool_result block atomically (as a matched pair), OR
- Keep both blocks intact
After compaction, the conversation should remain valid and usable, with all tool_result blocks having their corresponding tool_use blocks in the previous message. The conversation should continue without API errors.
Error Messages/Logs
Steps to Reproduce
- Start a new Claude Code conversation
- Perform multiple tasks that require many tool uses (Read, Write, Bash, Edit, etc.) - approximately 50+ tool calls
- Continue the conversation until it reaches the token threshold that triggers automatic conversation compaction
- After compaction occurs (you may see a brief pause or indication), attempt to continue the conversation with another request
- Observe the API 400 error: "unexpected
tool_use_idfound intool_resultblocks: [id]. Eachtool_resultblock must have a correspondingtool_useblock in the previous message"
Note: The exact token threshold for compaction is unknown, but this typically occurs in longer conversations with extensive tool usage.
Claude Model
None
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
Claude Code version 2.0.1
Platform
Anthropic API
Operating System
Ubuntu/Debian Linux
Terminal/Shell
Non-interactive/CI environment
Additional Information
_No response_
This issue has 14 comments on GitHub. Read the full discussion on GitHub ↗