openclaw - 💡(How to fix) Fix Thinking blocks modified during compaction/session replay cause API rejection [1 participants]

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…
GitHub stats
openclaw/openclaw#43741Fetched 2026-04-08 00:17:37
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Timeline (top)
cross-referenced ×1

When extended thinking is enabled (thinking=adaptive or thinking=on), Anthropic API calls fail after session compaction because thinking blocks are modified during the compaction/replay process. The API requires thinking blocks to be preserved verbatim.

Error Message

Thinking blocks are modified during compaction/session replay (possibly truncated, reformatted, or partially included), causing the Anthropic API to reject the entire request with a validation error. The thinking block content must match exactly what was originally generated.

Root Cause

When extended thinking is enabled (thinking=adaptive or thinking=on), Anthropic API calls fail after session compaction because thinking blocks are modified during the compaction/replay process. The API requires thinking blocks to be preserved verbatim.

RAW_BUFFERClick to expand / collapse

Summary

When extended thinking is enabled (thinking=adaptive or thinking=on), Anthropic API calls fail after session compaction because thinking blocks are modified during the compaction/replay process. The API requires thinking blocks to be preserved verbatim.

Steps to Reproduce

  1. Configure an agent with extended thinking enabled (e.g., thinking=adaptive in runtime config)
  2. Have a conversation that generates thinking blocks in the message history
  3. Allow the session to reach compaction threshold (context window fills up)
  4. Compaction triggers and attempts to replay/reconstruct the session
  5. The Anthropic API rejects the request because thinking block content has been altered

Expected Behavior

Thinking blocks should either be:

  • Preserved verbatim during compaction (exact content, no modification)
  • Stripped entirely before replay (removed from history since they are not needed for context continuity)

Actual Behavior

Thinking blocks are modified during compaction/session replay (possibly truncated, reformatted, or partially included), causing the Anthropic API to reject the entire request with a validation error. The thinking block content must match exactly what was originally generated.

Environment

  • OpenClaw version: 2026.3.2 / 2026.3.8
  • Model: anthropic/claude-opus-4-6 with thinking=adaptive
  • Runtime: agent mode with session compaction enabled

Suggested Fix

During compaction, either:

  1. Strip thinking blocks from the message history entirely (simplest, recommended — thinking content is ephemeral and not needed for context continuity)
  2. Preserve thinking blocks byte-for-byte if they must be retained (more complex, requires careful handling)

Option 1 is preferred since thinking blocks are not referenced by subsequent messages and their removal does not affect conversation coherence.

Impact

This effectively makes extended thinking unusable for long-running agent sessions, as any session that triggers compaction will fail on the next API call.

extent analysis

Fix Plan

To resolve the issue, we will implement the suggested fix of stripping thinking blocks from the message history entirely during compaction. Here are the concrete steps:

  • Modify the compaction function to remove thinking blocks:
def compact_session(session):
    # ... existing code ...
    session['message_history'] = [msg for msg in session['message_history'] if not is_thinking_block(msg)]
    # ... existing code ...
  • Implement the is_thinking_block function to identify thinking blocks:
def is_thinking_block(msg):
    return msg.get('type') == 'thinking_block'
  • Update the session replay function to handle the modified message history:
def replay_session(session):
    # ... existing code ...
    for msg in session['message_history']:
        # ... existing code ...
    # ... existing code ...

Verification

To verify that the fix worked, follow these steps:

  • Configure an agent with extended thinking enabled
  • Have a conversation that generates thinking blocks in the message history
  • Allow the session to reach compaction threshold
  • Verify that the thinking blocks are removed from the message history after compaction
  • Make an Anthropic API call to ensure that the request is no longer rejected due to modified thinking blocks

Extra Tips

  • Make sure to test the fix thoroughly to ensure that conversation coherence is not affected by the removal of thinking blocks.
  • Consider adding logging to monitor the removal of thinking blocks during compaction.
  • Review the code changes to ensure that they do not introduce any new issues or regressions.

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