@mastra/memory 1.5.1 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/dist/{chunk-6PKWQ3GH.js → chunk-HNPAIFCZ.js} +59 -16
- package/dist/chunk-HNPAIFCZ.js.map +1 -0
- package/dist/{chunk-6XVTMLW4.cjs → chunk-PVFLHAZX.cjs} +59 -16
- package/dist/chunk-PVFLHAZX.cjs.map +1 -0
- package/dist/docs/SKILL.md +55 -0
- package/dist/docs/assets/SOURCE_MAP.json +103 -0
- package/dist/docs/references/docs-agents-agent-approval.md +558 -0
- package/dist/docs/references/docs-agents-agent-memory.md +209 -0
- package/dist/docs/references/docs-agents-network-approval.md +275 -0
- package/dist/docs/references/docs-agents-networks.md +299 -0
- package/dist/docs/references/docs-agents-supervisor-agents.md +304 -0
- package/dist/docs/references/docs-memory-memory-processors.md +314 -0
- package/dist/docs/references/docs-memory-message-history.md +260 -0
- package/dist/docs/references/docs-memory-observational-memory.md +248 -0
- package/dist/docs/references/docs-memory-overview.md +45 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +272 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/reference-core-getMemory.md +50 -0
- package/dist/docs/references/reference-core-listMemory.md +56 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +199 -0
- package/dist/docs/references/reference-memory-cloneThread.md +130 -0
- package/dist/docs/references/reference-memory-createThread.md +68 -0
- package/dist/docs/references/reference-memory-getThreadById.md +24 -0
- package/dist/docs/references/reference-memory-listThreads.md +145 -0
- package/dist/docs/references/reference-memory-memory-class.md +147 -0
- package/dist/docs/references/reference-memory-observational-memory.md +565 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +115 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-libsql.md +135 -0
- package/dist/docs/references/reference-storage-mongodb.md +262 -0
- package/dist/docs/references/reference-storage-postgresql.md +526 -0
- package/dist/docs/references/reference-storage-upstash.md +160 -0
- package/dist/docs/references/reference-vectors-libsql.md +305 -0
- package/dist/docs/references/reference-vectors-mongodb.md +295 -0
- package/dist/docs/references/reference-vectors-pg.md +408 -0
- package/dist/docs/references/reference-vectors-upstash.md +294 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{observational-memory-AJWSMZVP.js → observational-memory-KAFD4QZK.js} +3 -3
- package/dist/{observational-memory-AJWSMZVP.js.map → observational-memory-KAFD4QZK.js.map} +1 -1
- package/dist/{observational-memory-Q5TO525O.cjs → observational-memory-Q47HN5YL.cjs} +17 -17
- package/dist/{observational-memory-Q5TO525O.cjs.map → observational-memory-Q47HN5YL.cjs.map} +1 -1
- package/dist/processors/index.cjs +15 -15
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +2 -2
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/chunk-6PKWQ3GH.js.map +0 -1
- package/dist/chunk-6XVTMLW4.cjs.map +0 -1
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
# Agent Networks
|
|
2
|
+
|
|
3
|
+
> **Supervisor Pattern Recommended:** The [supervisor pattern](https://mastra.ai/docs/agents/supervisor-agents) using `agent.stream()` or `agent.generate()` is now the recommended approach for coordinating multiple agents. It provides the same multi-agent coordination capabilities as `.network()` with significant improvements:
|
|
4
|
+
>
|
|
5
|
+
> - **Better control**: Iteration hooks, delegation hooks, and task completion scoring give you fine-grained control over execution
|
|
6
|
+
> - **Simpler API**: Uses familiar `stream()` and `generate()` methods instead of a separate `.network()` API
|
|
7
|
+
> - **More flexible**: Stop execution early, modify delegations, filter context, and provide feedback to guide the agent
|
|
8
|
+
> - **Type-safe**: Full TypeScript support for all hooks and callbacks
|
|
9
|
+
> - **Easier debugging**: Monitor progress with `onIterationComplete`, track delegations with `onDelegationStart`/`onDelegationComplete`
|
|
10
|
+
>
|
|
11
|
+
> See the [migration guide](https://mastra.ai/guides/migrations/network-to-supervisor) to upgrade from `.network()`.
|
|
12
|
+
|
|
13
|
+
Agent networks in Mastra coordinate multiple agents, workflows, and tools to handle tasks that aren't clearly defined upfront but can be inferred from the user's message or context. A top-level **routing agent** (a Mastra agent with other agents, workflows, and tools configured) uses an LLM to interpret the request and decide which primitives (subagents, workflows, or tools) to call, in what order, and with what data.
|
|
14
|
+
|
|
15
|
+
## When to use networks
|
|
16
|
+
|
|
17
|
+
Use networks for complex tasks that require coordination across multiple primitives. Unlike workflows, which follow a predefined sequence, networks rely on LLM reasoning to interpret the request and decide what to run.
|
|
18
|
+
|
|
19
|
+
## Core principles
|
|
20
|
+
|
|
21
|
+
Mastra agent networks operate using these principles:
|
|
22
|
+
|
|
23
|
+
- Memory is required when using `.network()` and is used to store task history and determine when a task is complete.
|
|
24
|
+
- Primitives are selected based on their descriptions. Clear, specific descriptions improve routing. For workflows and tools, the input schema helps determine the right inputs at runtime.
|
|
25
|
+
- If multiple primitives have overlapping functionality, the agent favors the more specific one, using a combination of schema and descriptions to decide which to run.
|
|
26
|
+
|
|
27
|
+
## Creating an agent network
|
|
28
|
+
|
|
29
|
+
An agent network is built around a top-level routing agent that delegates tasks to subagents, workflows, and tools defined in its configuration. Memory is configured on the routing agent using the `memory` option, and `instructions` define the agent's routing behavior.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { Agent } from '@mastra/core/agent'
|
|
33
|
+
import { Memory } from '@mastra/memory'
|
|
34
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
35
|
+
|
|
36
|
+
import { researchAgent } from './research-agent'
|
|
37
|
+
import { writingAgent } from './writing-agent'
|
|
38
|
+
|
|
39
|
+
import { cityWorkflow } from '../workflows/city-workflow'
|
|
40
|
+
import { weatherTool } from '../tools/weather-tool'
|
|
41
|
+
|
|
42
|
+
export const routingAgent = new Agent({
|
|
43
|
+
id: 'routing-agent',
|
|
44
|
+
name: 'Routing Agent',
|
|
45
|
+
instructions: `
|
|
46
|
+
You are a network of writers and researchers.
|
|
47
|
+
The user will ask you to research a topic.
|
|
48
|
+
Always respond with a complete report—no bullet points.
|
|
49
|
+
Write in full paragraphs, like a blog post.
|
|
50
|
+
Do not answer with incomplete or uncertain information.`,
|
|
51
|
+
model: 'openai/gpt-5.1',
|
|
52
|
+
agents: {
|
|
53
|
+
researchAgent,
|
|
54
|
+
writingAgent,
|
|
55
|
+
},
|
|
56
|
+
workflows: {
|
|
57
|
+
cityWorkflow,
|
|
58
|
+
},
|
|
59
|
+
tools: {
|
|
60
|
+
weatherTool,
|
|
61
|
+
},
|
|
62
|
+
memory: new Memory({
|
|
63
|
+
storage: new LibSQLStore({
|
|
64
|
+
id: 'mastra-storage',
|
|
65
|
+
url: 'file:../mastra.db',
|
|
66
|
+
}),
|
|
67
|
+
}),
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Writing descriptions for network primitives
|
|
72
|
+
|
|
73
|
+
When configuring a Mastra agent network, each primitive (agent, workflow, or tool) needs a clear description to help the routing agent decide which to use. The routing agent uses each primitive's description and schema to determine what it does and how to use it. Clear descriptions and well-defined input and output schemas improve routing accuracy.
|
|
74
|
+
|
|
75
|
+
#### Agent descriptions
|
|
76
|
+
|
|
77
|
+
Each subagent in a network should include a clear `description` that explains what the agent does.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
export const researchAgent = new Agent({
|
|
81
|
+
id: 'research-agent',
|
|
82
|
+
name: 'Research Agent',
|
|
83
|
+
description: `This agent gathers concise research insights in bullet-point form.
|
|
84
|
+
It's designed to extract key facts without generating full
|
|
85
|
+
responses or narrative content.`,
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
export const writingAgent = new Agent({
|
|
91
|
+
id: 'writing-agent',
|
|
92
|
+
name: 'Writing Agent',
|
|
93
|
+
description: `This agent turns researched material into well-structured
|
|
94
|
+
written content. It produces full-paragraph reports with no bullet points,
|
|
95
|
+
suitable for use in articles, summaries, or blog posts.`,
|
|
96
|
+
})
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Workflow descriptions
|
|
100
|
+
|
|
101
|
+
Workflows in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
export const cityWorkflow = createWorkflow({
|
|
105
|
+
id: 'city-workflow',
|
|
106
|
+
description: `This workflow handles city-specific research tasks.
|
|
107
|
+
It first gathers factual information about the city, then synthesizes
|
|
108
|
+
that research into a full written report. Use it when the user input
|
|
109
|
+
includes a city to be researched.`,
|
|
110
|
+
inputSchema: z.object({
|
|
111
|
+
city: z.string(),
|
|
112
|
+
}),
|
|
113
|
+
outputSchema: z.object({
|
|
114
|
+
text: z.string(),
|
|
115
|
+
}),
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### Tool descriptions
|
|
120
|
+
|
|
121
|
+
Tools in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
export const weatherTool = createTool({
|
|
125
|
+
id: 'weather-tool',
|
|
126
|
+
description: ` Retrieves current weather information using the wttr.in API.
|
|
127
|
+
Accepts a city or location name as input and returns a short weather summary.
|
|
128
|
+
Use this tool whenever up-to-date weather data is requested.
|
|
129
|
+
`,
|
|
130
|
+
inputSchema: z.object({
|
|
131
|
+
location: z.string(),
|
|
132
|
+
}),
|
|
133
|
+
outputSchema: z.object({
|
|
134
|
+
weather: z.string(),
|
|
135
|
+
}),
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Calling agent networks
|
|
140
|
+
|
|
141
|
+
Call a Mastra agent network using `.network()` with a user message. The method returns a stream of events that you can iterate over to track execution progress and retrieve the final result.
|
|
142
|
+
|
|
143
|
+
### Agent example
|
|
144
|
+
|
|
145
|
+
In this example, the network interprets the message and would route the request to both the `researchAgent` and `writingAgent` to generate a complete response.
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
const result = await routingAgent.network('Tell me three cool ways to use Mastra')
|
|
149
|
+
|
|
150
|
+
for await (const chunk of result) {
|
|
151
|
+
console.log(chunk.type)
|
|
152
|
+
if (chunk.type === 'network-execution-event-step-finish') {
|
|
153
|
+
console.log(chunk.payload.result)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### Agent output
|
|
159
|
+
|
|
160
|
+
The following `chunk.type` events are emitted during this request:
|
|
161
|
+
|
|
162
|
+
```text
|
|
163
|
+
routing-agent-start
|
|
164
|
+
routing-agent-end
|
|
165
|
+
agent-execution-start
|
|
166
|
+
agent-execution-event-start
|
|
167
|
+
agent-execution-event-step-start
|
|
168
|
+
agent-execution-event-text-start
|
|
169
|
+
agent-execution-event-text-delta
|
|
170
|
+
agent-execution-event-text-end
|
|
171
|
+
agent-execution-event-step-finish
|
|
172
|
+
agent-execution-event-finish
|
|
173
|
+
agent-execution-end
|
|
174
|
+
network-execution-event-step-finish
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Workflow example
|
|
178
|
+
|
|
179
|
+
In this example, the routing agent recognizes the city name in the message and runs the `cityWorkflow`. The workflow defines steps that call the `researchAgent` to gather facts, then the `writingAgent` to generate the final text.
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const result = await routingAgent.network('Tell me some historical facts about London')
|
|
183
|
+
|
|
184
|
+
for await (const chunk of result) {
|
|
185
|
+
console.log(chunk.type)
|
|
186
|
+
if (chunk.type === 'network-execution-event-step-finish') {
|
|
187
|
+
console.log(chunk.payload.result)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Workflow output
|
|
193
|
+
|
|
194
|
+
The following `chunk.type` events are emitted during this request:
|
|
195
|
+
|
|
196
|
+
```text
|
|
197
|
+
routing-agent-end
|
|
198
|
+
workflow-execution-start
|
|
199
|
+
workflow-execution-event-workflow-start
|
|
200
|
+
workflow-execution-event-workflow-step-start
|
|
201
|
+
workflow-execution-event-workflow-step-result
|
|
202
|
+
workflow-execution-event-workflow-finish
|
|
203
|
+
workflow-execution-end
|
|
204
|
+
routing-agent-start
|
|
205
|
+
network-execution-event-step-finish
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Tool example
|
|
209
|
+
|
|
210
|
+
In this example, the routing agent skips the `researchAgent`, `writingAgent`, and `cityWorkflow`, and calls the `weatherTool` directly to complete the task.
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
const result = await routingAgent.network("What's the weather in London?")
|
|
214
|
+
|
|
215
|
+
for await (const chunk of result) {
|
|
216
|
+
console.log(chunk.type)
|
|
217
|
+
if (chunk.type === 'network-execution-event-step-finish') {
|
|
218
|
+
console.log(chunk.payload.result)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### Tool output
|
|
224
|
+
|
|
225
|
+
The following `chunk.type` events are emitted during this request:
|
|
226
|
+
|
|
227
|
+
```text
|
|
228
|
+
routing-agent-start
|
|
229
|
+
routing-agent-end
|
|
230
|
+
tool-execution-start
|
|
231
|
+
tool-execution-end
|
|
232
|
+
network-execution-event-step-finish
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Structured output
|
|
236
|
+
|
|
237
|
+
When you need typed, validated results from a network, use the `structuredOutput` option. After the network completes its task, it generates a structured response matching your schema.
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import { z } from 'zod'
|
|
241
|
+
|
|
242
|
+
const resultSchema = z.object({
|
|
243
|
+
summary: z.string().describe('A brief summary of the findings'),
|
|
244
|
+
recommendations: z.array(z.string()).describe('List of recommendations'),
|
|
245
|
+
confidence: z.number().min(0).max(1).describe('Confidence score'),
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
const stream = await routingAgent.network('Research AI trends', {
|
|
249
|
+
structuredOutput: {
|
|
250
|
+
schema: resultSchema,
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
// Consume the stream
|
|
255
|
+
for await (const chunk of stream) {
|
|
256
|
+
if (chunk.type === 'network-object') {
|
|
257
|
+
// Partial object during generation
|
|
258
|
+
console.log('Partial:', chunk.payload.object)
|
|
259
|
+
}
|
|
260
|
+
if (chunk.type === 'network-object-result') {
|
|
261
|
+
// Final structured object
|
|
262
|
+
console.log('Final:', chunk.payload.object)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Get the typed result
|
|
267
|
+
const result = await stream.object
|
|
268
|
+
console.log(result?.summary)
|
|
269
|
+
console.log(result?.recommendations)
|
|
270
|
+
console.log(result?.confidence)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Streaming partial objects
|
|
274
|
+
|
|
275
|
+
For real-time updates during structured output generation, use `objectStream`:
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
const stream = await routingAgent.network('Analyze market data', {
|
|
279
|
+
structuredOutput: { schema: resultSchema },
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
// Stream partial objects as they're generated
|
|
283
|
+
for await (const partial of stream.objectStream) {
|
|
284
|
+
console.log('Building result:', partial)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Get the final typed result
|
|
288
|
+
const final = await stream.object
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Related
|
|
292
|
+
|
|
293
|
+
- [Supervisor Agents](https://mastra.ai/docs/agents/supervisor-agents)
|
|
294
|
+
- [Migration: .network() to Supervisor Pattern](https://mastra.ai/guides/migrations/network-to-supervisor)
|
|
295
|
+
- [Guide: Research Coordinator](https://mastra.ai/guides/guide/research-coordinator)
|
|
296
|
+
- [Agent Memory](https://mastra.ai/docs/agents/agent-memory)
|
|
297
|
+
- [Agent Approval](https://mastra.ai/docs/agents/agent-approval)
|
|
298
|
+
- [Workflows Overview](https://mastra.ai/docs/workflows/overview)
|
|
299
|
+
- [Request Context](https://mastra.ai/docs/server/request-context)
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# Supervisor Agents
|
|
2
|
+
|
|
3
|
+
A supervisor agent coordinates multiple subagents using `agent.stream()` or `agent.generate()`. You configure subagents on the supervisor's `agents` property, and the supervisor uses its instructions and each subagent's `description` to decide when and how to delegate tasks.
|
|
4
|
+
|
|
5
|
+
## When to use supervisor agents
|
|
6
|
+
|
|
7
|
+
Use supervisor agents when a task requires multiple agents with different specializations to work together. The supervisor handles delegation decisions, context passing, and result synthesis.
|
|
8
|
+
|
|
9
|
+
Common use cases:
|
|
10
|
+
|
|
11
|
+
- Research and writing workflows where one agent gathers data and another produces content
|
|
12
|
+
- Multi-step tasks that need different expertise at each stage
|
|
13
|
+
- Tasks where you need fine-grained control over delegation behavior
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
Define subagents with clear descriptions, then create a supervisor agent that references them:
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Agent } from '@mastra/core/agent'
|
|
21
|
+
import { Memory } from '@mastra/memory'
|
|
22
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
23
|
+
|
|
24
|
+
const researchAgent = new Agent({
|
|
25
|
+
id: 'research-agent',
|
|
26
|
+
description: 'Gathers factual information and returns bullet-point summaries.',
|
|
27
|
+
model: 'openai/gpt-4o-mini',
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const writingAgent = new Agent({
|
|
31
|
+
id: 'writing-agent',
|
|
32
|
+
description: 'Transforms research into well-structured articles.',
|
|
33
|
+
model: 'openai/gpt-4o-mini',
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const supervisor = new Agent({
|
|
37
|
+
id: 'supervisor',
|
|
38
|
+
instructions: `You coordinate research and writing using specialized agents.
|
|
39
|
+
Delegate to research-agent for facts, then writing-agent for content.`,
|
|
40
|
+
model: 'openai/gpt-5.1',
|
|
41
|
+
agents: { researchAgent, writingAgent },
|
|
42
|
+
memory: new Memory({
|
|
43
|
+
storage: new LibSQLStore({ id: 'storage', url: 'file:mastra.db' }),
|
|
44
|
+
}),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const stream = await supervisor.stream('Research AI in education and write an article', {
|
|
48
|
+
maxSteps: 10,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
for await (const chunk of stream.textStream) {
|
|
52
|
+
process.stdout.write(chunk)
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Delegation hooks
|
|
57
|
+
|
|
58
|
+
Delegation hooks let you intercept, modify, or reject delegations as they happen. Configure them under the `delegation` option, either in the agent's `defaultOptions` or per-call.
|
|
59
|
+
|
|
60
|
+
### onDelegationStart
|
|
61
|
+
|
|
62
|
+
Called before the supervisor delegates to a subagent. Return an object to control the delegation:
|
|
63
|
+
|
|
64
|
+
- `proceed: true` — allow the delegation (default behavior)
|
|
65
|
+
- `proceed: false` — reject the delegation with a `rejectionReason`
|
|
66
|
+
- `modifiedPrompt` — rewrite the prompt sent to the subagent
|
|
67
|
+
- `modifiedMaxSteps` — limit the subagent's iteration count
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const stream = await supervisor.stream('Research AI trends', {
|
|
71
|
+
maxSteps: 10,
|
|
72
|
+
delegation: {
|
|
73
|
+
onDelegationStart: async context => {
|
|
74
|
+
console.log(`Delegating to: ${context.primitiveId}`)
|
|
75
|
+
|
|
76
|
+
// Modify the prompt for a specific agent
|
|
77
|
+
if (context.primitiveId === 'research-agent') {
|
|
78
|
+
return {
|
|
79
|
+
proceed: true,
|
|
80
|
+
modifiedPrompt: `${context.prompt}\n\nFocus on 2024-2025 data.`,
|
|
81
|
+
modifiedMaxSteps: 5,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Reject delegation after too many iterations
|
|
86
|
+
if (context.iteration > 8) {
|
|
87
|
+
return {
|
|
88
|
+
proceed: false,
|
|
89
|
+
rejectionReason: 'Max iterations reached. Synthesize current findings.',
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { proceed: true }
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The `context` object includes:
|
|
100
|
+
|
|
101
|
+
| Property | Description |
|
|
102
|
+
| ------------- | ----------------------------------------- |
|
|
103
|
+
| `primitiveId` | The ID of the subagent being delegated to |
|
|
104
|
+
| `prompt` | The prompt the supervisor is sending |
|
|
105
|
+
| `iteration` | Current iteration number |
|
|
106
|
+
|
|
107
|
+
### onDelegationComplete
|
|
108
|
+
|
|
109
|
+
Called after a delegation finishes. Use it to inspect results, provide feedback, or stop execution:
|
|
110
|
+
|
|
111
|
+
- `context.bail()` — stop the supervisor loop immediately
|
|
112
|
+
- Return `{ feedback: '...' }` — add feedback that gets saved to the supervisor's memory and is visible to subsequent iterations
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const stream = await supervisor.stream('Research AI trends', {
|
|
116
|
+
maxSteps: 10,
|
|
117
|
+
delegation: {
|
|
118
|
+
onDelegationComplete: async context => {
|
|
119
|
+
console.log(`Completed: ${context.primitiveId}`)
|
|
120
|
+
|
|
121
|
+
// Bail on errors
|
|
122
|
+
if (context.error) {
|
|
123
|
+
context.bail()
|
|
124
|
+
return {
|
|
125
|
+
feedback: `Delegation to ${context.primitiveId} failed: ${context.error}. Try a different approach.`,
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The `context` object includes:
|
|
134
|
+
|
|
135
|
+
| Property | Description |
|
|
136
|
+
| ------------- | ------------------------------------ |
|
|
137
|
+
| `primitiveId` | The ID of the subagent that ran |
|
|
138
|
+
| `result` | The subagent's response |
|
|
139
|
+
| `error` | Error if the delegation failed |
|
|
140
|
+
| `bail()` | Function to stop the supervisor loop |
|
|
141
|
+
|
|
142
|
+
## Message filtering
|
|
143
|
+
|
|
144
|
+
By default, subagents receive the full conversation context from the supervisor. Use `messageFilter` to control what messages are shared — for example, to remove sensitive data or limit context size.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const stream = await supervisor.stream('Research AI trends', {
|
|
148
|
+
maxSteps: 10,
|
|
149
|
+
delegation: {
|
|
150
|
+
messageFilter: ({ messages, primitiveId, prompt }) => {
|
|
151
|
+
// Remove messages containing sensitive data
|
|
152
|
+
return messages
|
|
153
|
+
.filter(msg => {
|
|
154
|
+
const content =
|
|
155
|
+
typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content)
|
|
156
|
+
return !content.includes('confidential')
|
|
157
|
+
})
|
|
158
|
+
.slice(-10) // Only pass the last 10 messages
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The callback receives `messages` (the full conversation history), `primitiveId` (the subagent ID), and `prompt` (the delegation prompt). Return the filtered array of messages.
|
|
165
|
+
|
|
166
|
+
## Iteration monitoring
|
|
167
|
+
|
|
168
|
+
`onIterationComplete` is called after each iteration of the supervisor loop. Use it to log progress, inject feedback, or stop execution early.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const stream = await supervisor.stream('Research AI trends', {
|
|
172
|
+
maxSteps: 10,
|
|
173
|
+
onIterationComplete: async context => {
|
|
174
|
+
console.log(`Iteration ${context.iteration}/${context.maxIterations}`)
|
|
175
|
+
console.log(`Finish reason: ${context.finishReason}`)
|
|
176
|
+
|
|
177
|
+
// Inject feedback to guide the agent
|
|
178
|
+
if (!context.text.includes('recommendations')) {
|
|
179
|
+
return {
|
|
180
|
+
continue: true,
|
|
181
|
+
feedback: 'Please include specific recommendations in your analysis.',
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Stop early when the response is sufficient
|
|
186
|
+
if (context.text.length > 1000 && context.finishReason === 'stop') {
|
|
187
|
+
return { continue: false }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { continue: true }
|
|
191
|
+
},
|
|
192
|
+
})
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Return `{ continue: true }` to keep iterating, or `{ continue: false }` to stop. Include optional `feedback` to add guidance that's visible to the next iteration.
|
|
196
|
+
|
|
197
|
+
## Memory isolation
|
|
198
|
+
|
|
199
|
+
The supervisor pattern implements memory isolation — subagents receive the full conversation context for better decision-making, but only their specific delegation prompt and response are saved to their memory.
|
|
200
|
+
|
|
201
|
+
How it works:
|
|
202
|
+
|
|
203
|
+
1. **Full context forwarded** — When the supervisor delegates, the subagent receives all messages from the supervisor's conversation
|
|
204
|
+
2. **Scoped memory saves** — Only the delegation prompt and the subagent's response are saved to the subagent's memory
|
|
205
|
+
3. **Fresh thread per invocation** — Each delegation uses a unique thread ID, ensuring clean separation
|
|
206
|
+
|
|
207
|
+
This ensures subagents have the context they need without cluttering their memory with the entire supervisor conversation.
|
|
208
|
+
|
|
209
|
+
## Tool approval propagation
|
|
210
|
+
|
|
211
|
+
Tool approvals propagate through the delegation chain. When a subagent uses a tool with `requireApproval: true` or calls `suspend()`, the approval request surfaces to the supervisor level.
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
const sensitiveDataTool = createTool({
|
|
215
|
+
id: 'get-user-data',
|
|
216
|
+
requireApproval: true,
|
|
217
|
+
execute: async input => {
|
|
218
|
+
return await database.getUserData(input.userId)
|
|
219
|
+
},
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
const dataAgent = new Agent({
|
|
223
|
+
id: 'data-agent',
|
|
224
|
+
tools: { sensitiveDataTool },
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
const supervisor = new Agent({
|
|
228
|
+
id: 'supervisor',
|
|
229
|
+
agents: { dataAgent },
|
|
230
|
+
memory: new Memory(),
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
const stream = await supervisor.stream('Get data for user 123')
|
|
234
|
+
|
|
235
|
+
for await (const chunk of stream.fullStream) {
|
|
236
|
+
if (chunk.type === 'tool-call-approval') {
|
|
237
|
+
console.log('Tool requires approval:', chunk.payload.toolName)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Task completion scoring
|
|
243
|
+
|
|
244
|
+
Task completion scorers validate whether the task is complete after each iteration. If validation fails, the supervisor continues iterating. Feedback from failed scorers is included in the conversation context so subagents can see what was missing.
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import { createScorer } from '@mastra/core/evals'
|
|
248
|
+
|
|
249
|
+
const taskCompleteScorer = createScorer({
|
|
250
|
+
id: 'task-complete',
|
|
251
|
+
name: 'Task Completeness',
|
|
252
|
+
}).generateScore(async context => {
|
|
253
|
+
const text = (context.run.output || '').toString()
|
|
254
|
+
const hasAnalysis = text.includes('analysis')
|
|
255
|
+
const hasRecommendations = text.includes('recommendation')
|
|
256
|
+
return hasAnalysis && hasRecommendations ? 1 : 0
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
const stream = await supervisor.stream('Research AI in education', {
|
|
260
|
+
maxSteps: 10,
|
|
261
|
+
isTaskComplete: {
|
|
262
|
+
scorers: [taskCompleteScorer],
|
|
263
|
+
strategy: 'all',
|
|
264
|
+
onComplete: async result => {
|
|
265
|
+
console.log('Task complete:', result.complete)
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
})
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Writing effective instructions
|
|
272
|
+
|
|
273
|
+
Clear instructions are essential for effective delegation. Your supervisor's `instructions` should specify available resources, when to use each one, how to coordinate them, and success criteria.
|
|
274
|
+
|
|
275
|
+
Each subagent should have a clear `description` that explains what it does, what format it returns, and when to use it. The supervisor uses these descriptions to make delegation decisions.
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
const supervisor = new Agent({
|
|
279
|
+
instructions: `You coordinate research and writing tasks.
|
|
280
|
+
|
|
281
|
+
Available resources:
|
|
282
|
+
- researchAgent: Gathers factual data and sources (returns bullet points)
|
|
283
|
+
- writingAgent: Transforms research into narrative content (returns full paragraphs)
|
|
284
|
+
|
|
285
|
+
Delegation strategy:
|
|
286
|
+
1. For research requests: Delegate to researchAgent first
|
|
287
|
+
2. For writing requests: Delegate to writingAgent
|
|
288
|
+
3. For complex requests: Delegate to researchAgent first, then writingAgent
|
|
289
|
+
|
|
290
|
+
Success criteria:
|
|
291
|
+
- All user questions are fully answered
|
|
292
|
+
- Response is well-formatted and complete`,
|
|
293
|
+
agents: { researchAgent, writingAgent },
|
|
294
|
+
})
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Related
|
|
298
|
+
|
|
299
|
+
- [Agent Networks](https://mastra.ai/docs/agents/networks)
|
|
300
|
+
- [Migration: .network() to Supervisor Pattern](https://mastra.ai/guides/migrations/network-to-supervisor)
|
|
301
|
+
- [Guide: Research Coordinator](https://mastra.ai/guides/guide/research-coordinator)
|
|
302
|
+
- [Agent.stream() Reference](https://mastra.ai/reference/streaming/agents/stream)
|
|
303
|
+
- [Agent.generate() Reference](https://mastra.ai/reference/agents/generate)
|
|
304
|
+
- [Agent Approval](https://mastra.ai/docs/agents/agent-approval)
|