Skip to content

Bug: Anthropic rejects history with dangling tool_use blocks after compaction #33621

@clawbottim

Description

@clawbottim

Description

When sending conversation history to Anthropic models (e.g. Claude Opus), the API rejects requests with:

tool_use ids found without tool_result blocks immediately after

This happens when compaction or history truncation leaves orphaned tool_use content blocks in assistant messages without corresponding tool_result blocks in the following user message.

Root Cause

validateAnthropicTurns() (in pi-embedded-helpers-*.js) currently merges consecutive user turns but does not strip dangling tool_use blocks from assistant messages when corresponding tool_result blocks are missing from the next user turn.

The existing repairToolUseResultPairing operates at the message level (pairing assistant + toolResult messages), but doesn't handle mismatches at the content-block level within messages.

Steps to Reproduce

  1. Have a conversation with multiple tool calls in a session
  2. After compaction trims history, some tool_use blocks may lose their paired tool_result blocks
  3. Send the compacted history to an Anthropic model
  4. Anthropic rejects with the error above

Expected Behavior

validateAnthropicTurns() should strip tool_use content blocks from assistant messages when the immediately following user message does not contain a matching tool_result block (by tool_use_id). Non-tool content in the assistant message should be preserved; if all content would be removed, a minimal fallback text block should be inserted.

Workaround

We applied a local hotfix to the dist files adding a stripDanglingAnthropicToolUses() step inside validateAnthropicTurns(). This resolves the issue but is overwritten on update.

Environment

  • OpenClaw 2026.3.2
  • Model: anthropic/claude-opus-4-6
  • Channel: Telegram (embedded agent path)

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