Compaction fails when trying to modify thinking blocks in latest assistant message

Resolved 💬 3 comments Opened Feb 8, 2026 by azisso Closed Feb 8, 2026

Compaction fails when trying to modify thinking blocks in latest assistant message

Summary

When Claude Code attempts to compact conversation history to manage context limits, it fails with an API error when the latest assistant message contains thinking blocks. The Anthropic API correctly rejects the modification attempt, but this leaves the session in an unrecoverable state.

Environment

  • Claude Code version: 2.1.31
  • Session ID: f01ef1d2-3cef-4eda-9f76-c8e8cb0d2bd2
  • Date: 2026-02-08
  • Platform: macOS (Darwin 25.2.0)

Error Message

Error: Error during compaction: Error: API Error: 400 {
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "messages.9.content.35: `thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified. These blocks must remain as they were in the original response."
  },
  "request_id": "req_011CXur736CC1FYXiBmpeg76"
}

Steps to Reproduce

  1. Start a long conversation session with extended_thinking enabled (interleaved or auto thinking mode)
  2. Continue the conversation until automatic compaction is triggered OR manually run /compact
  3. If the latest assistant message contains thinking blocks, the compaction will fail

What Happened

The error occurred multiple times during the session:

  • First attempt: req_011CXuqCmswsMnXospmcTwzv at 2026-02-08T02:42:45.864Z
  • Second attempt: req_011CXuqxNk8Nke12Wxshcmm1 at 2026-02-08T02:52:37.633Z
  • Third attempt: req_011CXur5CSx4JmiNcNNv7Q5x at 2026-02-08T02:54:10.551Z
  • Final /compact command: req_011CXur736CC1FYXiBmpeg76 at 2026-02-08T02:54:34.899Z

Each attempt resulted in the same error about modifying thinking blocks in message 9, content block 35.

Debug Log Excerpt

2026-02-08T02:54:34.895Z [DEBUG] Forked agent [compact] received message: type=assistant
2026-02-08T02:54:34.897Z [DEBUG] Forked agent [compact] finished: 1 messages, types=[assistant], totalUsage: input=0 output=0 cacheRead=0 cacheCreate=0
2026-02-08T02:54:34.899Z [ERROR] Error: Error: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.9.content.35: `thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified. These blocks must remain as they were in the original response."},"request_id":"req_011CXur736CC1FYXiBmpeg76"}

Expected Behavior

The compaction process should:

  1. Detect that the latest assistant message contains thinking blocks
  2. Either preserve those blocks unmodified during compaction
  3. Or skip compaction of the latest message entirely
  4. Successfully compress older messages without corrupting the session

Actual Behavior

  • The compaction agent attempts to modify thinking blocks in the latest assistant message
  • The API correctly rejects this with a 400 error
  • The session becomes unrecoverable and cannot continue
  • User must start a new conversation, losing all context

Impact

  • Severity: High - causes complete session failure
  • Workaround: None - session is unrecoverable
  • Frequency: Occurs reliably when compaction is needed and latest message has thinking blocks
  • Data loss: All conversation context is lost

Root Cause Analysis

The compaction logic appears to process all messages uniformly, including the latest assistant message. However, the Anthropic API enforces immutability of thinking/redacted_thinking blocks in the latest assistant message (which is correct behavior to prevent tampering with the model's reasoning process).

Suggested Fix

The compaction agent should:

  1. Check if the message being compacted is the latest assistant message
  2. If it contains thinking/redacted_thinking blocks, preserve them exactly as-is
  3. Only compact older messages that are not subject to this constraint
  4. Add validation to prevent modification attempts before sending to the API

Related Information

  • Session debug log: ~/.claude/debug/f01ef1d2-3cef-4eda-9f76-c8e8cb0d2bd2.txt
  • The conversation was working normally until compaction was needed
  • The issue is reproducible with extended thinking enabled (thinking_mode: interleaved/auto)

Additional Notes

  • The API error is correct behavior - thinking blocks should not be modified
  • The bug is in Claude Code's compaction logic, not the API
  • This affects any long conversation with extended thinking enabled
  • The error message correctly identifies the problematic content block (messages.9.content.35)

View original on GitHub ↗

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