Skip to content

Compaction corrupts thinking/redacted_thinking blocks, breaking Anthropic API sessions #39887

@camila623

Description

@camila623

Bug Description

When a session uses extended thinking (thinking=adaptive or similar) with Anthropic models, and compaction runs on the conversation history, the compactor modifies thinking and/or redacted_thinking content blocks in assistant messages. The Anthropic API then rejects the next request with:

LLM request rejected: messages.N.content.N: thinking or redacted_thinking blocks
in the latest assistant message cannot be modified. These blocks must remain as
they were in the original response.

This permanently breaks the session since every subsequent API call fails with the same error.

Secondary Issue: Error Leakage to Chat

When the compaction-triggered API call fails:

  1. Error messages leak to the chat channel despite suppressToolErrors: true being set in the agent config
  2. Compaction failure sentinel messages (e.g. "Gateway: agents.defaults.compaction failed") also leak to the user's chat
  3. The session enters a loop of failed API calls with visible error messages

Steps to Reproduce

  1. Configure an agent with an Anthropic model that supports extended thinking (e.g. claude-opus-4-6)
  2. Enable thinking (e.g. thinking: adaptive)
  3. Set compaction mode to safeguard
  4. Have a long enough conversation to trigger compaction
  5. Compaction runs and modifies thinking blocks in the conversation history
  6. Next API call fails with the error above
  7. Session becomes unresponsive

Expected Behavior

  • Compaction should preserve thinking blocks exactly as-is, or strip them entirely before resubmitting to the API. The Anthropic API requires these blocks to be unmodified.
  • Compaction failures should NOT leak to the user's chat channel. The suppressToolErrors: true config should cover compaction-related errors as well.
  • Restart sentinel messages from compaction failures should be suppressed from the chat output.

Suggested Fix

When compacting conversation history that contains thinking or redacted_thinking blocks:

  • Option A (preferred): Leave all thinking and redacted_thinking blocks byte-identical to the original API response. Only compact/summarize text and tool_use/tool_result blocks.
  • Option B: Strip thinking and redacted_thinking blocks entirely from messages being sent back to the API (Anthropic allows omitting them from non-latest messages).
  • Separately: Ensure compaction errors are caught by the suppressToolErrors mechanism and do not propagate to the chat channel.

Environment

  • OpenClaw version: 2026.3.7
  • Model: Anthropic Claude Opus 4-6
  • Compaction mode: safeguard
  • Thinking config: adaptive

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions