Skip to content

Compaction/pruning corrupts thinking blocks → Anthropic API rejection #30157

Description

@Nicolas0315

Bug Description

When using Anthropic models with extended thinking enabled, the following error occurs intermittently:

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

Root Cause Analysis

Anthropic's API requires that thinking / redacted_thinking blocks in the latest assistant message remain exactly as they were in the original response. Any modification triggers a rejection.

OpenClaw's compaction and context pruning pipelines do not currently have special protection for thinking blocks in the latest assistant message:

  1. compaction.ts: No thinking-aware logic. When compaction replaces old messages with a summary, the latest assistant message's thinking blocks may be lost or altered.

  2. context-pruning/pruner.ts: Only touches toolResult messages (not assistant messages), so unlikely the direct cause. However, it counts thinking block chars for window estimation.

  3. transcript-policy.ts: dropThinkingBlocks is only enabled for GitHub Copilot Claude (isCopilotClaude), not for direct Anthropic API usage — correct behavior. But there's no complementary "preserve latest assistant thinking blocks" guard for Anthropic.

Expected Behavior

The latest assistant message's thinking / redacted_thinking blocks should be preserved exactly as-is through compaction, pruning, and session restore operations when targeting Anthropic's API.

Suggested Fix

Add a guard in the compaction and/or session sanitization pipeline that:

  • Identifies the last assistant message in the transcript
  • Ensures its thinking / redacted_thinking blocks are never modified or dropped
  • Applies this guard specifically when the target provider is Anthropic

Environment

  • OpenClaw version: 2026.2.27 (dev channel)
  • Model: anthropic/claude-opus-4-6
  • Thinking: enabled (low)
  • Compaction mode: default
  • Context pruning: cache-ttl (20m)

Reproduction

  1. Use Anthropic model with thinking enabled
  2. Have a long session that triggers compaction or context pruning
  3. Send a follow-up message after compaction runs
  4. Error occurs when the compacted transcript is sent to Anthropic with modified thinking blocks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions