Fixing Stream Freezes with Anthropic’s Programmatic Tool Calling in Mastra
The agent would call a tool using Anthropic… and then the entire stream would just freeze. No error, no crash — just silence. The conversation would hang forever.
This was a painful bug for anyone using Anthropic models with tools in Mastra.
Context
Mastra is a powerful framework for building AI agents and workflows. Anthropic recently introduced Programmatic Tool Calling (PTC), which works differently from normal tool calling. When tools are executed via code, Anthropic sends tool results through a special streaming format (tool-input-start → delta → tool-input-end). Mastra wasn’t handling this properly, causing streams to get stuck.
The Investigation
I reproduced the issue and traced it through the streaming pipeline. The problem was that Mastra expected a regular tool-call event, but with PTC, Anthropic only sent partial streaming events and a separate tool result. Nothing was synthesizing the missing tool-call chunk, so the stream never completed.
The Solution
I implemented full support for Anthropic’s Programmatic Tool Calling:
Track per-tool streaming metadata during execution
Synthesize proper tool-call chunks from streamed deltas
Merge provider-executed tool results back into the correct tool call
Handle stream continuation and state serialization properly
This fixed the freezing issue and made Anthropic tool calling much more reliable.
PR: feat: support Anthropic programmatic tool calling (PTC) #12400
Lessons Learned
Different LLM providers handle tool calling in surprisingly different ways. When working with streaming, always look at what the provider actually emits in edge cases (especially when tools are executed externally). A little extra metadata tracking can prevent major stream deadlocks.
What about you?
I enjoy debugging complex agent loops and making AI frameworks more reliable. If you’re building with Mastra, LangGraph, or any agent framework and running into streaming/tool issues feel free to reach out.




