Skip to content

Anthropic adapter sends tool_result blocks without tool_use_id, so multi-turn tool use fails #124

Closed

Description

@stackbilt-admin

Summary

When it replays tool results, AnthropicProvider builds tool_result content blocks with an id field (src/providers/anthropic.ts, formatRequest, the message.toolResults mapping). The Claude Messages API requires tool_use_id there: the id of the tool_use block being answered. tool_use_id appears nowhere in src/.

From the code and Anthropic's docs, any multi-turn tool round trip through the Anthropic adapter should be rejected for every Claude model. This includes the factory tool loop (generateResponseWithTools), which builds toolResults messages. This has not been confirmed with a live API call; the existing tests mock fetch and never assert the field name.

Evidence

  • Code: type: 'tool_result', id: tr.id, content: tr.output, is_error: !!tr.error
  • Anthropic docs ("Handle tool calls", platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls, checked 2026-10-15): a tool_result block contains tool_use_id: The id of the tool use request this is a result for, plus optional content and is_error.

Related: Claude 5.x thinking blocks

Claude 5.x models think by default. Anthropic requires their thinking blocks to be passed back unmodified with tool results, and returns a 400 for edited or reordered ones. The adapter keeps only the joined reasoning text (LLMResponse.reasoning) and drops the blocks and their signatures. That is why #123 keeps the 5.x models explicit-only. Fixing both together is the prerequisite for making them routable again.

Proposed fix

  1. Send tool_use_id: tr.id in tool_result blocks, and add a request-shape test, including through generateResponseWithTools.
  2. Check the role and ordering rules: the tool_result blocks go first in a user message that immediately follows the assistant tool_use turn.
  3. Design how thinking blocks (with signatures) survive a tool loop, for example opaque provider data on the assistant message that the adapter replays verbatim. Then lift explicitOnly from the Claude 5.x catalog entries.

Found by adversarial review of #123. Refs #112.

🤖 Generated with Claude Code

Activity

  1. stackbilt-admin commented on Oct 15, 2026

    @stackbilt-admin
    MemberAuthor

    Another adapter defect in the same path, found in the third review of #123:

    When a tool fails, the factory tool loop sends the error in toolResults[].error with output: '' (src/factory.ts, around the toolResults.push calls in the tool loop). The Anthropic adapter builds the tool_result block from content: tr.output and only uses error to set is_error: true (src/providers/anthropic.ts, formatRequest). So Claude receives is_error: true with empty content, and the error text is lost.

    Anthropic's docs ("Handle tool calls") show the error message going in content together with "is_error": true. The fix: when tr.error is set, send it as content (or append it to tr.output), and cover it in the same request-shape test as tool_use_id.

  2. stackbilt-admin commented on Nov 6, 2026

    @stackbilt-admin
    MemberAuthor

    Status: the adapter fix is merged in #142 (tool_use_id, tool error text, and verbatim replay of tool-use turns through a new providerContent field) and ships in 1.25.0 (release PR #143). The Claude 5.x catalog entries are still explicitOnly; the three steps below are now tracked on #112.

    Three adapter points to settle before the Claude 5.x models become routable (the first turned out to be handled already). All three came up in the #142 review and were checked against Anthropic's docs on 2026-11-06:

    1. Forced tool use: already handled. (Corrected: an earlier version of this comment said it would fail.) Define tools → Forcing tool use: on Claude Opus 5.5, Claude Sonnet 5.5, Claude Fable 5.1 and Claude Mythos 5.1, tool_choice any and tool return a 400; auto and none still work. Since 1.23.0 the adapter already throws ConfigurationError before the call when toolChoice is 'required' or a named function on the catalog's Fable 5.1, Opus 5.5 and Sonnet 5.5 entries (NO_FORCED_TOOL_CHOICE_MODELS, tested in model-refresh.test.ts); Mythos 5.1 is not in the catalog. Worth adding if it is. On models that do accept forced tool use (for example Claude Opus 5, Claude Haiku 5.5), a forced call skips thinking.
    2. toolChoice: 'none' removes tools from the request. Thinking → Preserved thinking: on Claude 5.1 and later, a replayed thinking block is valid only while the top-level system, the tools and the earlier messages are unchanged. Dropping tools for a none turn invalidates the earlier turns' thinking. Anthropic supports tool_choice: {"type": "none"} with the tools kept, which keeps the prefix unchanged.
    3. A fallback partway through a tool loop rebuilds the turn without its thinking. (Added after the final fix: Anthropic tool loops send tool_use_id, error text and thinking blocks (#124) #142 review.) If a loop starts on another provider and falls back to Anthropic, or moves between providers mid-loop, the earlier assistant tool-use turn has no Anthropic providerContent, so the adapter rebuilds it from content and toolCalls with no thinking block. Thinking → Thinking with tool use: adaptive mode, which Claude 5.x uses by default and is what the adapter gets since it sends no thinking parameter, does not require an assistant turn to start with a thinking block, so this is not a 400 (manual mode does enforce it). But "the API might strip thinking blocks that would create an invalid turn structure, or disable thinking when the conversation history is incompatible with thinking being enabled", silently, for that request. So the cost is lost reasoning for the rest of the turn, not an error. Before routing the 5.x models, decide whether a mid-loop fallback onto them is acceptable, should be kept off them, or should be surfaced (for example, by checking the response for thinking blocks).

    Also worth deciding at that point: providerContent is set only on tool-use turns (the ones Anthropic requires back). Anthropic recommends passing thinking back on every turn, and Claude 5.x keeps prior turns' thinking in context, so final-answer thinking is currently not carried forward. This doesn't cause an error.

  3. stackbilt-admin commented on Nov 7, 2026

    @stackbilt-admin
    MemberAuthor

    Fixed in 1.25.0 via #142 (released by #143): Anthropic tool_result blocks send tool_use_id; a failed tool's error is sent as content with is_error: true; tool-use turns are replayed verbatim, including signed thinking and redacted_thinking, through the new optional providerContent field. Groq, Cerebras and NVIDIA also send tool error text now.

    The steps left before the Claude 5.x models become routable (keeping tools on toolChoice: 'none' turns, deciding how to handle a mid-loop fallback onto a thinking model, and lifting explicitOnly) are listed in the comment above and are now tracked on #112.

  4. added a commit that references this issue on Nov 7, 2026
    a926dd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions