Skip to main content

Command Palette

Search for a command to run...

Fixing Stream Freezes with Anthropic’s Programmatic Tool Calling in Mastra

Updated
2 min read
N
Neha Prasad Software Engineer · Open Source Contributor · AI Agent Builder I build production-grade AI agents and contribute to the frameworks that power them. With 150+ pull requests merged across the AI ecosystem - including fixing Anthropic's core agent streaming freeze in Mastra, multi-turn tooling in LlamaIndex, and guardrail data-loss bugs in LiteLLM — I don’t just use AI tools. I shape them. Right now, I'm building DigiNav AI: India's first autonomous regulatory compliance agent that handles incorporation, GST, audits, and more - so founders can build the future instead of fighting paperwork. Built the first working version in a single day. Now turning it into a full product, in public. I ship fast, think in defensible moats, and believe technology should disappear behind a simple, human experience. If you need an AI agent built, a compliance health scan for your startup, or an engineer who treats open source as a calling card - let's talk.

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.

27 views