@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
148 lines (117 loc) • 5.32 kB
Markdown
# Streaming events
Streaming from agents or workflows provides real-time visibility into either the LLM’s output or the status of a workflow run. This feedback can be passed directly to the user, or used within applications to handle workflow status more effectively, creating a smoother and more responsive experience.
Events emitted from agents or workflows represent different stages of generation and execution, such as when a run starts, when text is produced, or when a tool is invoked.
## Event types
Below is a complete list of events emitted from `.stream()`. Depending on whether you’re streaming from an **agent** or a **workflow**, only a subset of these events will occur:
- **start**: Marks the beginning of an agent or workflow run.
- **step-start**: Indicates a workflow step has begun execution.
- **text-delta**: Incremental text chunks as they're generated by the LLM.
- **tool-call**: When the agent decides to use a tool, including the tool name and arguments.
- **tool-result**: The result returned from tool execution.
- **step-finish**: Confirms that a specific step has fully finalized, and may include metadata like the finish reason for that step.
- **finish**: When the agent or workflow completes, including usage statistics.
## Network event types
When using `agent.network()` for multi-agent collaboration, additional event types are emitted to track the orchestration flow:
- **routing-agent-start**: The routing agent begins analyzing the task to decide which primitive (agent/workflow/tool) to delegate to.
- **routing-agent-text-delta**: Incremental text as the routing agent processes the response from the selected primitive.
- **routing-agent-end**: The routing agent completes its selection, including the selected primitive and reason for selection.
- **agent-execution-start**: A delegated agent begins execution.
- **agent-execution-end**: A delegated agent completes execution.
- **agent-execution-event-\***: Events from the delegated agent's execution (e.g., `agent-execution-event-text-delta`).
- **workflow-execution-start**: A delegated workflow begins execution.
- **workflow-execution-end**: A delegated workflow completes execution.
- **workflow-execution-event-\***: Events from the delegated workflow's execution.
- **tool-execution-start**: A delegated tool begins execution.
- **tool-execution-end**: A delegated tool completes execution.
- **network-execution-event-step-finish**: A network iteration step completes.
- **network-execution-event-finish**: The entire network execution completes.
## Inspecting agent streams
Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
```typescript
const testAgent = mastra.getAgent('testAgent')
const stream = await testAgent.stream([{ role: 'user', content: 'Help me organize my day' }])
for await (const chunk of stream) {
console.log(chunk)
}
```
> **Info:** Visit [Agent.stream()](https://mastra.ai/reference/streaming/agents/stream) for more information.
### Example agent output
Below is an example of events that may be emitted. Each event always includes a `type` and can include additional fields like `from` and `payload`.
```typescript
{
type: 'start',
from: 'AGENT',
// ..
}
{
type: 'step-start',
from: 'AGENT',
payload: {
messageId: 'msg-cdUrkirvXw8A6oE4t5lzDuxi',
// ...
}
}
{
type: 'tool-call',
from: 'AGENT',
payload: {
toolCallId: 'call_jbhi3s1qvR6Aqt9axCfTBMsA',
toolName: 'testTool'
// ..
}
}
```
## Inspecting workflow streams
Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
```typescript
const testWorkflow = mastra.getWorkflow('testWorkflow')
const run = await testWorkflow.createRun()
const stream = await run.stream({
inputData: {
value: 'initial data',
},
})
for await (const chunk of stream) {
console.log(chunk)
}
```
### Example workflow output
Below is an example of events that may be emitted. Each event always includes a `type` and can include additional fields like `from` and `payload`.
```typescript
{
type: 'workflow-start',
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
from: 'WORKFLOW',
// ...
}
{
type: 'workflow-step-start',
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
from: 'WORKFLOW',
payload: {
stepName: 'step-1',
args: { value: 'initial data' },
stepCallId: '9e8c5217-490b-4fe7-8c31-6e2353a3fc98',
startedAt: 1755269732792,
status: 'running'
}
}
```
### Foreach progress events
When a workflow uses `.foreach()`, each iteration emits a `workflow-step-progress` event. You can use these to track real-time progress:
```typescript
for await (const chunk of stream) {
if (chunk.type === 'workflow-step-progress') {
console.log(
`${chunk.payload.id}: ${chunk.payload.completedCount}/${chunk.payload.totalCount} — ${chunk.payload.iterationStatus}`,
)
}
}
```
Each progress event includes:
- **`id`**: The step ID of the foreach step
- **`completedCount`**: Number of iterations completed so far
- **`totalCount`**: Total number of iterations
- **`currentIndex`**: Index of the iteration that completed
- **`iterationStatus`**: Status of the iteration (`success`, `failed`, or `suspended`)
- **`iterationOutput`**: Output of the iteration (when successful)