Skip to content

V2: errored-message replay can 400 with Anthropic thinking + tool use #38620

Description

@rekram1-node

Problem

In the V2 session core, replaying history that contains an errored assistant message can produce a request Anthropic rejects with a 400 when extended thinking is enabled.

reuseProviderMetadata requires message.error === undefined (packages/core/src/session/runner/to-llm-message.ts:114), so an aborted/failed step drops all reasoning state on replay — both signed thinking (anthropic.signature) and redacted thinking (anthropic.redactedData) — while still replaying that same message's tool_use parts.

Anthropic requires a tool_use block to be preceded by its thinking/redacted_thinking block when thinking is enabled. Replaying tool_use without the reasoning block yields a 400 from the API.

Impact

Multi-turn thinking + tool-use conversations with Anthropic break after an interrupt or failure mid-step: the follow-up request replays the errored message's tool calls stripped of their reasoning blocks and the provider rejects it.

Scope

  • Pre-existing behavior; unrelated to and not introduced by feat(ai): round-trip Anthropic redacted thinking blocks #38614 (which added redacted thinking round-trip support in packages/ai). Signed thinking is affected identically.
  • The errored-message guard is otherwise a deliberate design decision, so the fix should be deliberate too.

Possible directions

  • Relax the message.error === undefined guard in to-llm-message.ts so reasoning provider state is reused for errored messages when provider+model match, or
  • Drop the errored message's tool-call parts alongside the reasoning so the replayed message stays self-consistent (no orphaned tool_use).

Either way, the invariant to preserve is: never replay a tool_use part to Anthropic without its preceding thinking block while thinking is enabled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcoreAnything pertaining to core functionality of the application (opencode server stuff)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions