mastra/Core Concepts
Streaming
Stream responses from agents and workflows for real-time interactions
streamingreal-timeeventstool-streaming
Streaming
Stream responses from agents and workflows for real-time interactions.
Topics
- Overview - Introduction to streaming
- Events - Streaming event types
- Tool Streaming - Stream tool execution
- Workflow Streaming - Stream workflow progress
Streaming Overview
Streaming enables real-time responses from agents and workflows.
Why Streaming?
- Faster perceived response time
- Real-time feedback during long operations
- Progressive disclosure of results
- Better user experience
Basic Streaming
const agent = mastra.getAgent('myAgent');
const stream = await agent.stream('Tell me a story');
for await (const chunk of stream) {
process.stdout.write(chunk.text);
}
Stream Types
- Text streaming - Stream text tokens as they're generated
- Tool streaming - Stream tool execution progress
- Workflow streaming - Stream step completion and results
Tool Streaming
Stream tool execution progress and results.
Enabling Tool Streaming
const agent = mastra.getAgent('myAgent', {
streaming: {
tools: true,
},
});
Tool Call Events
const stream = await agent.stream('Search for AI news');
stream.on('tool_call', (event) => {
console.log('Calling tool:', event.tool);
console.log('Input:', event.input);
});
stream.on('tool_start', (event) => {
console.log('Tool execution started');
});
stream.on('tool_end', (event) => {
console.log('Tool execution completed');
console.log('Result:', event.result);
});
Tool Delta Streaming
For long-running tools, receive progressive updates:
stream.on('tool_delta', (event) => {
console.log('Progress:', event.progress);
console.log('Partial result:', event.partial);
});