-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
Description
When using streamText with Anthropic thinking models (e.g., claude-sonnet-4-20250514), reasoning content is lost during multi-step tool call workflows.
Two related issues:
-
response.messagesreasoning parts are missingproviderMetadatawith signatures - The signature is only available in the separatereasoningarray, making it impossible to properly reconstruct messages for subsequent turns. -
In
onFinish, reasoning is lost from consolidatedresponse.messages- When using AI SDK's built-in agent loop (maxSteps),onStepFinishshows reasoning on all turns, butonFinishloses 75% of reasoning blocks.
Reproduction
Gist: https://gist.github.com/p3droml/503c5d4888182a75222377109bfdd291
To run:
git clone https://gist.github.com/p3droml/503c5d4888182a75222377109bfdd291.git mre
cd mre && npm install
ANTHROPIC_API_KEY=your-key npx tsx anthropic-thinking-bug.ts
ProviderMetadata Issue
When extracting reasoning from response.messages, the signature is not included:
// response.messages reasoning part:
{ type: "reasoning", text: "..." } // NO providerMetadata!
// separate reasoning array:
{ type: "reasoning", text: "...", providerMetadata: { anthropic: { signature: "Esg..." } } }Root Cause Analysis
The Pattern Explained
- Turn 1: User message → Model generates thinking + tool call → reasoning present ✅
- Turn 2-4: Tool results sent back → Model processes results without generating NEW thinking → reasoning missing ❌
The model doesn't always generate new thinking blocks when processing tool results. This is expected model behavior. However:
response.messagesreasoning parts don't includeproviderMetadatawith signatures - making it impossible to properly forward thinking blocksonFinishloses reasoning that was present inonStepFinish- consolidation drops 75% of reasoning
Anthropic's Requirements
From Anthropic's Extended Thinking Documentation:
"When thinking is enabled, a final assistant message must start with a thinking block (preceding the lastmost set of tool_use and tool_result blocks). We recommend you include thinking blocks from previous turns."
"You must include the complete, unmodified thinking or redacted_thinking block back to the API."
Related Issues
- openai/openai-agents-js#770 - "Anthropic with reasoning and tool calling fails to send thinking content back up on subsequent turn"
- vercel/ai#4992 -
appendResponseMessagesignores reasoning (fixed in PR fix (ai/core): add reasoning support to appendResponseMessages #5041) - vercel/ai#8823 - Post-tool reasoning missing from steps
- vercel/ai#5087 - Reasoning promises undefined with streamText
Environment
ai: 6.0.16
@ai-sdk/anthropic: 3.0.8
Model: claude-sonnet-4-20250514
Node.js: 22.x
OS: macOS
Expected Behavior
- Reasoning should be preserved and available in
onFinishfor ALL responses - The
response.messagesarray should include reasoning blocks withproviderMetadata(signatures) - The separate
reasoningparameter should be consistent withresponse.messages - Multi-step tool call workflows should maintain reasoning context throughout
Actual Behavior
response.messagesreasoning parts are missingproviderMetadatawith signatures- In
onFinish, 75% of reasoning is lost (even thoughonStepFinishshows reasoning on all turns) - Both
response.messagesand the separatereasoningarray are empty for turns 2-4
Additional Context
The issue was tested both before and after upgrading from:
[email protected]→[email protected]@ai-sdk/[email protected]→@ai-sdk/[email protected]
The bug persists across versions.
AI SDK Version
ai: 6.0.16
@ai-sdk/anthropic: 3.0.8
Model: claude-sonnet-4-20250514
Node.js: 22.x
OS: macOS
Code of Conduct
- I agree to follow this project's Code of Conduct