@mastra/memory 1.0.0-beta.10 → 1.0.0-beta.11
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 +87 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +42 -0
- package/dist/docs/SOURCE_MAP.json +31 -0
- package/dist/docs/agents/01-agent-memory.md +160 -0
- package/dist/docs/agents/02-networks.md +236 -0
- package/dist/docs/agents/03-agent-approval.md +317 -0
- package/dist/docs/core/01-reference.md +114 -0
- package/dist/docs/memory/01-overview.md +76 -0
- package/dist/docs/memory/02-storage.md +181 -0
- package/dist/docs/memory/03-working-memory.md +386 -0
- package/dist/docs/memory/04-semantic-recall.md +235 -0
- package/dist/docs/memory/05-memory-processors.md +319 -0
- package/dist/docs/memory/06-reference.md +617 -0
- package/dist/docs/processors/01-reference.md +81 -0
- package/dist/docs/storage/01-reference.md +972 -0
- package/dist/docs/vectors/01-reference.md +929 -0
- package/dist/index.cjs +247 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +134 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +247 -14
- package/dist/index.js.map +1 -1
- package/dist/tools/working-memory.d.ts +1 -1
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +11 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,92 @@
|
|
|
1
1
|
# @mastra/memory
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.11
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- dependencies updates: ([#10133](https://github.com/mastra-ai/mastra/pull/10133))
|
|
8
|
+
- Updated dependency [`js-tiktoken@^1.0.21` ↗︎](https://www.npmjs.com/package/js-tiktoken/v/1.0.21) (from `^1.0.20`, in `dependencies`)
|
|
9
|
+
|
|
10
|
+
- Add embedded documentation support for Mastra packages ([#11472](https://github.com/mastra-ai/mastra/pull/11472))
|
|
11
|
+
|
|
12
|
+
Mastra packages now include embedded documentation in the published npm package under `dist/docs/`. This enables coding agents and AI assistants to understand and use the framework by reading documentation directly from `node_modules`.
|
|
13
|
+
|
|
14
|
+
Each package includes:
|
|
15
|
+
- **SKILL.md** - Entry point explaining the package's purpose and capabilities
|
|
16
|
+
- **SOURCE_MAP.json** - Machine-readable index mapping exports to types and implementation files
|
|
17
|
+
- **Topic folders** - Conceptual documentation organized by feature area
|
|
18
|
+
|
|
19
|
+
Documentation is driven by the `packages` frontmatter field in MDX files, which maps docs to their corresponding packages. CI validation ensures all docs include this field.
|
|
20
|
+
|
|
21
|
+
- Fixed client-side tool invocations not being stored in memory. Previously, tool invocations with state 'call' were filtered out before persistence, which incorrectly removed client-side tools. Now only streaming intermediate states ('partial-call') are filtered. ([#11630](https://github.com/mastra-ai/mastra/pull/11630))
|
|
22
|
+
|
|
23
|
+
Fixed a crash when updating working memory with an empty or null update; existing data is now preserved.
|
|
24
|
+
|
|
25
|
+
- Adds thread cloning to create independent copies of conversations that can diverge. ([#11517](https://github.com/mastra-ai/mastra/pull/11517))
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
// Clone a thread
|
|
29
|
+
const { thread, clonedMessages } = await memory.cloneThread({
|
|
30
|
+
sourceThreadId: 'thread-123',
|
|
31
|
+
title: 'My Clone',
|
|
32
|
+
options: {
|
|
33
|
+
messageLimit: 10, // optional: only copy last N messages
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Check if a thread is a clone
|
|
38
|
+
if (memory.isClone(thread)) {
|
|
39
|
+
const source = await memory.getSourceThread(thread.id);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// List all clones of a thread
|
|
43
|
+
const clones = await memory.listClones('thread-123');
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Includes:
|
|
47
|
+
- Storage implementations for InMemory, PostgreSQL, LibSQL, Upstash
|
|
48
|
+
- API endpoint: `POST /api/memory/threads/:threadId/clone`
|
|
49
|
+
- Embeddings created for cloned messages (semantic recall)
|
|
50
|
+
- Clone button in playground UI Memory tab
|
|
51
|
+
|
|
52
|
+
- Unified `getWorkflowRunById` and `getWorkflowRunExecutionResult` into a single API that returns `WorkflowState` with both metadata and execution state. ([#11429](https://github.com/mastra-ai/mastra/pull/11429))
|
|
53
|
+
|
|
54
|
+
**What changed:**
|
|
55
|
+
- `getWorkflowRunById` now returns a unified `WorkflowState` object containing metadata (runId, workflowName, resourceId, createdAt, updatedAt) along with processed execution state (status, result, error, payload, steps)
|
|
56
|
+
- Added optional `fields` parameter to request only specific fields for better performance
|
|
57
|
+
- Added optional `withNestedWorkflows` parameter to control nested workflow step inclusion
|
|
58
|
+
- Removed `getWorkflowRunExecutionResult` - use `getWorkflowRunById` instead (breaking change)
|
|
59
|
+
- Removed `/execution-result` API endpoints from server (breaking change)
|
|
60
|
+
- Removed `runExecutionResult()` method from client SDK (breaking change)
|
|
61
|
+
- Removed `GetWorkflowRunExecutionResultResponse` type from client SDK (breaking change)
|
|
62
|
+
|
|
63
|
+
**Before:**
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// Had to call two different methods for different data
|
|
67
|
+
const run = await workflow.getWorkflowRunById(runId); // Returns raw WorkflowRun with snapshot
|
|
68
|
+
const result = await workflow.getWorkflowRunExecutionResult(runId); // Returns processed execution state
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**After:**
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Single method returns everything
|
|
75
|
+
const run = await workflow.getWorkflowRunById(runId);
|
|
76
|
+
// Returns: { runId, workflowName, resourceId, createdAt, updatedAt, status, result, error, payload, steps }
|
|
77
|
+
|
|
78
|
+
// Request only specific fields for better performance (avoids expensive step fetching)
|
|
79
|
+
const status = await workflow.getWorkflowRunById(runId, { fields: ['status'] });
|
|
80
|
+
|
|
81
|
+
// Skip nested workflow steps for faster response
|
|
82
|
+
const run = await workflow.getWorkflowRunById(runId, { withNestedWorkflows: false });
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Why:** The previous API required calling two separate methods to get complete workflow run information. This unification simplifies the API surface and gives users control over performance - fetching all steps (especially nested workflows) can be expensive, so the `fields` and `withNestedWorkflows` options let users request only what they need.
|
|
86
|
+
|
|
87
|
+
- Updated dependencies [[`d2d3e22`](https://github.com/mastra-ai/mastra/commit/d2d3e22a419ee243f8812a84e3453dd44365ecb0), [`bc72b52`](https://github.com/mastra-ai/mastra/commit/bc72b529ee4478fe89ecd85a8be47ce0127b82a0), [`05b8bee`](https://github.com/mastra-ai/mastra/commit/05b8bee9e50e6c2a4a2bf210eca25ee212ca24fa), [`c042bd0`](https://github.com/mastra-ai/mastra/commit/c042bd0b743e0e86199d0cb83344ca7690e34a9c), [`940a2b2`](https://github.com/mastra-ai/mastra/commit/940a2b27480626ed7e74f55806dcd2181c1dd0c2), [`e0941c3`](https://github.com/mastra-ai/mastra/commit/e0941c3d7fc75695d5d258e7008fd5d6e650800c), [`0c0580a`](https://github.com/mastra-ai/mastra/commit/0c0580a42f697cd2a7d5973f25bfe7da9055038a), [`28f5f89`](https://github.com/mastra-ai/mastra/commit/28f5f89705f2409921e3c45178796c0e0d0bbb64), [`e601b27`](https://github.com/mastra-ai/mastra/commit/e601b272c70f3a5ecca610373aa6223012704892), [`3d3366f`](https://github.com/mastra-ai/mastra/commit/3d3366f31683e7137d126a3a57174a222c5801fb), [`5a4953f`](https://github.com/mastra-ai/mastra/commit/5a4953f7d25bb15ca31ed16038092a39cb3f98b3), [`eb9e522`](https://github.com/mastra-ai/mastra/commit/eb9e522ce3070a405e5b949b7bf5609ca51d7fe2), [`20e6f19`](https://github.com/mastra-ai/mastra/commit/20e6f1971d51d3ff6dd7accad8aaaae826d540ed), [`4f0b3c6`](https://github.com/mastra-ai/mastra/commit/4f0b3c66f196c06448487f680ccbb614d281e2f7), [`74c4f22`](https://github.com/mastra-ai/mastra/commit/74c4f22ed4c71e72598eacc346ba95cdbc00294f), [`81b6a8f`](https://github.com/mastra-ai/mastra/commit/81b6a8ff79f49a7549d15d66624ac1a0b8f5f971), [`e4d366a`](https://github.com/mastra-ai/mastra/commit/e4d366aeb500371dd4210d6aa8361a4c21d87034), [`a4f010b`](https://github.com/mastra-ai/mastra/commit/a4f010b22e4355a5fdee70a1fe0f6e4a692cc29e), [`73b0bb3`](https://github.com/mastra-ai/mastra/commit/73b0bb394dba7c9482eb467a97ab283dbc0ef4db), [`5627a8c`](https://github.com/mastra-ai/mastra/commit/5627a8c6dc11fe3711b3fa7a6ffd6eb34100a306), [`3ff45d1`](https://github.com/mastra-ai/mastra/commit/3ff45d10e0c80c5335a957ab563da72feb623520), [`251df45`](https://github.com/mastra-ai/mastra/commit/251df4531407dfa46d805feb40ff3fb49769f455), [`f894d14`](https://github.com/mastra-ai/mastra/commit/f894d148946629af7b1f452d65a9cf864cec3765), [`c2b9547`](https://github.com/mastra-ai/mastra/commit/c2b9547bf435f56339f23625a743b2147ab1c7a6), [`580b592`](https://github.com/mastra-ai/mastra/commit/580b5927afc82fe460dfdf9a38a902511b6b7e7f), [`58e3931`](https://github.com/mastra-ai/mastra/commit/58e3931af9baa5921688566210f00fb0c10479fa), [`08bb631`](https://github.com/mastra-ai/mastra/commit/08bb631ae2b14684b2678e3549d0b399a6f0561e), [`4fba91b`](https://github.com/mastra-ai/mastra/commit/4fba91bec7c95911dc28e369437596b152b04cd0), [`12b0cc4`](https://github.com/mastra-ai/mastra/commit/12b0cc4077d886b1a552637dedb70a7ade93528c)]:
|
|
88
|
+
- @mastra/core@1.0.0-beta.20
|
|
89
|
+
|
|
3
90
|
## 1.0.0-beta.10
|
|
4
91
|
|
|
5
92
|
### Patch Changes
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @mastra/memory Documentation
|
|
2
|
+
|
|
3
|
+
> Embedded documentation for coding agents
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Read the skill overview
|
|
9
|
+
cat docs/SKILL.md
|
|
10
|
+
|
|
11
|
+
# Get the source map
|
|
12
|
+
cat docs/SOURCE_MAP.json
|
|
13
|
+
|
|
14
|
+
# Read topic documentation
|
|
15
|
+
cat docs/<topic>/01-overview.md
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Structure
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
docs/
|
|
22
|
+
├── SKILL.md # Entry point
|
|
23
|
+
├── README.md # This file
|
|
24
|
+
├── SOURCE_MAP.json # Export index
|
|
25
|
+
├── agents/ (3 files)
|
|
26
|
+
├── core/ (2 files)
|
|
27
|
+
├── memory/ (11 files)
|
|
28
|
+
├── processors/ (1 files)
|
|
29
|
+
├── storage/ (5 files)
|
|
30
|
+
├── vectors/ (4 files)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Version
|
|
34
|
+
|
|
35
|
+
Package: @mastra/memory
|
|
36
|
+
Version: 1.0.0-beta.11
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mastra-memory-docs
|
|
3
|
+
description: Documentation for @mastra/memory. Includes links to type definitions and readable implementation code in dist/.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @mastra/memory Documentation
|
|
7
|
+
|
|
8
|
+
> **Version**: 1.0.0-beta.11
|
|
9
|
+
> **Package**: @mastra/memory
|
|
10
|
+
|
|
11
|
+
## Quick Navigation
|
|
12
|
+
|
|
13
|
+
Use SOURCE_MAP.json to find any export:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cat docs/SOURCE_MAP.json
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Each export maps to:
|
|
20
|
+
- **types**: `.d.ts` file with JSDoc and API signatures
|
|
21
|
+
- **implementation**: `.js` chunk file with readable source
|
|
22
|
+
- **docs**: Conceptual documentation in `docs/`
|
|
23
|
+
|
|
24
|
+
## Top Exports
|
|
25
|
+
|
|
26
|
+
- extractWorkingMemoryContent: dist/index.d.ts
|
|
27
|
+
- extractWorkingMemoryTags: dist/index.d.ts
|
|
28
|
+
- removeWorkingMemoryTags: dist/index.d.ts
|
|
29
|
+
- MessageHistory: dist/index.d.ts
|
|
30
|
+
- SemanticRecall: dist/index.d.ts
|
|
31
|
+
- WorkingMemory: dist/index.d.ts
|
|
32
|
+
|
|
33
|
+
See SOURCE_MAP.json for the complete list.
|
|
34
|
+
|
|
35
|
+
## Available Topics
|
|
36
|
+
|
|
37
|
+
- [Agents](agents/) - 3 file(s)
|
|
38
|
+
- [Core](core/) - 2 file(s)
|
|
39
|
+
- [Memory](memory/) - 11 file(s)
|
|
40
|
+
- [Processors](processors/) - 1 file(s)
|
|
41
|
+
- [Storage](storage/) - 5 file(s)
|
|
42
|
+
- [Vectors](vectors/) - 4 file(s)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.0.0-beta.11",
|
|
3
|
+
"package": "@mastra/memory",
|
|
4
|
+
"exports": {
|
|
5
|
+
"extractWorkingMemoryContent": {
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"implementation": "dist/memory"
|
|
8
|
+
},
|
|
9
|
+
"extractWorkingMemoryTags": {
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"implementation": "dist/memory"
|
|
12
|
+
},
|
|
13
|
+
"removeWorkingMemoryTags": {
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"implementation": "dist/memory"
|
|
16
|
+
},
|
|
17
|
+
"MessageHistory": {
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"implementation": "dist/processors"
|
|
20
|
+
},
|
|
21
|
+
"SemanticRecall": {
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"implementation": "dist/processors"
|
|
24
|
+
},
|
|
25
|
+
"WorkingMemory": {
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"implementation": "dist/processors"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"modules": {}
|
|
31
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
> Learn how to add memory to agents to store message history and maintain context across interactions.
|
|
2
|
+
|
|
3
|
+
# Agent memory
|
|
4
|
+
|
|
5
|
+
Agents use memory to maintain context across interactions. LLMs are stateless and don't retain information between calls, so agents need memory to track message history and recall relevant information.
|
|
6
|
+
|
|
7
|
+
Mastra agents can be configured to store message history, with optional [working memory](../memory/working-memory) to maintain recent context or [semantic recall](../memory/semantic-recall) to retrieve past messages based on meaning.
|
|
8
|
+
|
|
9
|
+
## When to use memory
|
|
10
|
+
|
|
11
|
+
Use memory when your agent needs to maintain multi-turn conversations that reference prior exchanges, recall user preferences or facts from earlier in a session, or build context over time within a conversation thread. Skip memory for single-turn requests where each interaction is independent.
|
|
12
|
+
|
|
13
|
+
## Setting up memory
|
|
14
|
+
|
|
15
|
+
To enable memory in Mastra, install the `@mastra/memory` package along with a storage provider.
|
|
16
|
+
|
|
17
|
+
```bash npm2yarn
|
|
18
|
+
npm install @mastra/memory@beta @mastra/libsql@beta
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Storage providers
|
|
22
|
+
|
|
23
|
+
Memory requires a storage provider to persist message history, including user messages and agent responses. For more details on available providers and how storage works in Mastra, see the [Storage](https://mastra.ai/docs/v1/memory/storage) documentation.
|
|
24
|
+
|
|
25
|
+
## Configuring memory
|
|
26
|
+
|
|
27
|
+
### Step
|
|
28
|
+
|
|
29
|
+
Enable memory by creating a `Memory` instance and passing it to the agent’s `memory` option.
|
|
30
|
+
|
|
31
|
+
```typescript {7-11} title="src/mastra/agents/memory-agent.ts"
|
|
32
|
+
import { Agent } from "@mastra/core/agent";
|
|
33
|
+
import { Memory } from "@mastra/memory";
|
|
34
|
+
|
|
35
|
+
export const memoryAgent = new Agent({
|
|
36
|
+
id: 'memory-agent',
|
|
37
|
+
name: 'Memory Agent',
|
|
38
|
+
memory: new Memory({
|
|
39
|
+
options: {
|
|
40
|
+
lastMessages: 20,
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> **Note:**
|
|
47
|
+
|
|
48
|
+
Visit [Memory Class](https://mastra.ai/reference/v1/memory/memory-class) for a full list of configuration options.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
### Step
|
|
53
|
+
|
|
54
|
+
Add a storage provider to your main Mastra instance to enable memory across all configured agents.
|
|
55
|
+
|
|
56
|
+
```typescript {5-8} title="src/mastra/index.ts"
|
|
57
|
+
import { Mastra } from "@mastra/core";
|
|
58
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
59
|
+
|
|
60
|
+
export const mastra = new Mastra({
|
|
61
|
+
storage: new LibSQLStore({
|
|
62
|
+
id: 'mastra-storage',
|
|
63
|
+
url: ":memory:",
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> **Note:**
|
|
69
|
+
|
|
70
|
+
Visit [libSQL Storage](https://mastra.ai/reference/v1/storage/libsql) for a full list of configuration options.
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Alternatively, add storage directly to an agent’s memory to keep data separate or use different providers per agent.
|
|
75
|
+
|
|
76
|
+
```typescript {9-12} title="src/mastra/agents/memory-agent.ts"
|
|
77
|
+
import { Agent } from "@mastra/core/agent";
|
|
78
|
+
import { Memory } from "@mastra/memory";
|
|
79
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
80
|
+
|
|
81
|
+
export const memoryAgent = new Agent({
|
|
82
|
+
id: 'memory-agent',
|
|
83
|
+
name: 'Memory Agent',
|
|
84
|
+
memory: new Memory({
|
|
85
|
+
storage: new LibSQLStore({
|
|
86
|
+
id: 'mastra-storage',
|
|
87
|
+
url: ":memory:",
|
|
88
|
+
}),
|
|
89
|
+
}),
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Message history
|
|
94
|
+
|
|
95
|
+
Include a `memory` object with both `resource` and `thread` to track message history during agent calls.
|
|
96
|
+
|
|
97
|
+
- `resource`: A stable identifier for the user or entity.
|
|
98
|
+
- `thread`: An ID that isolates a specific conversation or session.
|
|
99
|
+
|
|
100
|
+
These fields tell the agent where to store and retrieve context, enabling persistent, thread-aware memory across a conversation.
|
|
101
|
+
|
|
102
|
+
```typescript {4-7}
|
|
103
|
+
const response = await memoryAgent.generate(
|
|
104
|
+
"Remember my favorite color is blue.",
|
|
105
|
+
{
|
|
106
|
+
memory: {
|
|
107
|
+
resource: "user-123",
|
|
108
|
+
thread: "conversation-123",
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
To recall information stored in memory, call the agent with the same `resource` and `thread` values used in the original conversation.
|
|
115
|
+
|
|
116
|
+
```typescript {2-5}
|
|
117
|
+
const response = await memoryAgent.generate("What's my favorite color?", {
|
|
118
|
+
memory: {
|
|
119
|
+
resource: "user-123",
|
|
120
|
+
thread: "conversation-123",
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
To learn more about memory see the [Memory](../memory/overview) documentation.
|
|
126
|
+
|
|
127
|
+
## Using `RequestContext`
|
|
128
|
+
|
|
129
|
+
Use [RequestContext](https://mastra.ai/docs/v1/server/request-context) to access request-specific values. This lets you conditionally select different memory or storage configurations based on the context of the request.
|
|
130
|
+
|
|
131
|
+
```typescript title="src/mastra/agents/memory-agent.ts"
|
|
132
|
+
export type UserTier = {
|
|
133
|
+
"user-tier": "enterprise" | "pro";
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const premiumMemory = new Memory();
|
|
137
|
+
|
|
138
|
+
const standardMemory = new Memory();
|
|
139
|
+
|
|
140
|
+
export const memoryAgent = new Agent({
|
|
141
|
+
id: 'memory-agent',
|
|
142
|
+
name: 'Memory Agent',
|
|
143
|
+
memory: ({ requestContext }) => {
|
|
144
|
+
const userTier = requestContext.get("user-tier") as UserTier["user-tier"];
|
|
145
|
+
|
|
146
|
+
return userTier === "enterprise" ? premiumMemory : standardMemory;
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
> **Note:**
|
|
152
|
+
|
|
153
|
+
Visit [Request Context](https://mastra.ai/docs/v1/server/request-context) for more information.
|
|
154
|
+
|
|
155
|
+
## Related
|
|
156
|
+
|
|
157
|
+
- [Working Memory](../memory/working-memory)
|
|
158
|
+
- [Semantic Recall](../memory/semantic-recall)
|
|
159
|
+
- [Storage](../memory/storage)
|
|
160
|
+
- [Request Context](https://mastra.ai/docs/v1/server/request-context)
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
> Learn how to coordinate multiple agents, workflows, and tools using agent networks for complex, non-deterministic task execution.
|
|
2
|
+
|
|
3
|
+
# Agent Networks
|
|
4
|
+
|
|
5
|
+
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 (sub-agents, workflows, or tools) to call, in what order, and with what data.
|
|
6
|
+
|
|
7
|
+
## When to use networks
|
|
8
|
+
|
|
9
|
+
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.
|
|
10
|
+
|
|
11
|
+
## Core principles
|
|
12
|
+
|
|
13
|
+
Mastra agent networks operate using these principles:
|
|
14
|
+
|
|
15
|
+
- Memory is required when using `.network()` and is used to store task history and determine when a task is complete.
|
|
16
|
+
- 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.
|
|
17
|
+
- 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.
|
|
18
|
+
|
|
19
|
+
## Creating an agent network
|
|
20
|
+
|
|
21
|
+
An agent network is built around a top-level routing agent that delegates tasks to agents, 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.
|
|
22
|
+
|
|
23
|
+
```typescript {22-23,26,29} title="src/mastra/agents/routing-agent.ts"
|
|
24
|
+
import { Agent } from "@mastra/core/agent";
|
|
25
|
+
import { Memory } from "@mastra/memory";
|
|
26
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
27
|
+
|
|
28
|
+
import { researchAgent } from "./research-agent";
|
|
29
|
+
import { writingAgent } from "./writing-agent";
|
|
30
|
+
|
|
31
|
+
import { cityWorkflow } from "../workflows/city-workflow";
|
|
32
|
+
import { weatherTool } from "../tools/weather-tool";
|
|
33
|
+
|
|
34
|
+
export const routingAgent = new Agent({
|
|
35
|
+
id: "routing-agent",
|
|
36
|
+
name: "Routing Agent",
|
|
37
|
+
instructions: `
|
|
38
|
+
You are a network of writers and researchers.
|
|
39
|
+
The user will ask you to research a topic.
|
|
40
|
+
Always respond with a complete report—no bullet points.
|
|
41
|
+
Write in full paragraphs, like a blog post.
|
|
42
|
+
Do not answer with incomplete or uncertain information.`,
|
|
43
|
+
model: "openai/gpt-5.1",
|
|
44
|
+
agents: {
|
|
45
|
+
researchAgent,
|
|
46
|
+
writingAgent,
|
|
47
|
+
},
|
|
48
|
+
workflows: {
|
|
49
|
+
cityWorkflow,
|
|
50
|
+
},
|
|
51
|
+
tools: {
|
|
52
|
+
weatherTool,
|
|
53
|
+
},
|
|
54
|
+
memory: new Memory({
|
|
55
|
+
storage: new LibSQLStore({
|
|
56
|
+
id: 'mastra-storage',
|
|
57
|
+
url: "file:../mastra.db",
|
|
58
|
+
}),
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Writing descriptions for network primitives
|
|
64
|
+
|
|
65
|
+
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.
|
|
66
|
+
|
|
67
|
+
#### Agent descriptions
|
|
68
|
+
|
|
69
|
+
Each agent in a network should include a clear `description` that explains what the agent does.
|
|
70
|
+
|
|
71
|
+
```typescript title="src/mastra/agents/research-agent.ts"
|
|
72
|
+
export const researchAgent = new Agent({
|
|
73
|
+
id: "research-agent",
|
|
74
|
+
name: "Research Agent",
|
|
75
|
+
description: `This agent gathers concise research insights in bullet-point form.
|
|
76
|
+
It's designed to extract key facts without generating full
|
|
77
|
+
responses or narrative content.`,
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```typescript title="src/mastra/agents/writing-agent.ts"
|
|
82
|
+
export const writingAgent = new Agent({
|
|
83
|
+
id: "writing-agent",
|
|
84
|
+
name: "Writing Agent",
|
|
85
|
+
description: `This agent turns researched material into well-structured
|
|
86
|
+
written content. It produces full-paragraph reports with no bullet points,
|
|
87
|
+
suitable for use in articles, summaries, or blog posts.`,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### Workflow descriptions
|
|
92
|
+
|
|
93
|
+
Workflows in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
94
|
+
|
|
95
|
+
```typescript title="src/mastra/workflows/city-workflow.ts"
|
|
96
|
+
export const cityWorkflow = createWorkflow({
|
|
97
|
+
id: "city-workflow",
|
|
98
|
+
description: `This workflow handles city-specific research tasks.
|
|
99
|
+
It first gathers factual information about the city, then synthesizes
|
|
100
|
+
that research into a full written report. Use it when the user input
|
|
101
|
+
includes a city to be researched.`,
|
|
102
|
+
inputSchema: z.object({
|
|
103
|
+
city: z.string(),
|
|
104
|
+
}),
|
|
105
|
+
outputSchema: z.object({
|
|
106
|
+
text: z.string(),
|
|
107
|
+
}),
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### Tool descriptions
|
|
112
|
+
|
|
113
|
+
Tools in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
114
|
+
|
|
115
|
+
```typescript title="src/mastra/tools/weather-tool.ts"
|
|
116
|
+
export const weatherTool = createTool({
|
|
117
|
+
id: "weather-tool",
|
|
118
|
+
description: ` Retrieves current weather information using the wttr.in API.
|
|
119
|
+
Accepts a city or location name as input and returns a short weather summary.
|
|
120
|
+
Use this tool whenever up-to-date weather data is requested.
|
|
121
|
+
`,
|
|
122
|
+
inputSchema: z.object({
|
|
123
|
+
location: z.string(),
|
|
124
|
+
}),
|
|
125
|
+
outputSchema: z.object({
|
|
126
|
+
weather: z.string(),
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Calling agent networks
|
|
132
|
+
|
|
133
|
+
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.
|
|
134
|
+
|
|
135
|
+
### Agent example
|
|
136
|
+
|
|
137
|
+
In this example, the network interprets the message and would route the request to both the `researchAgent` and `writingAgent` to generate a complete response.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const result = await routingAgent.network(
|
|
141
|
+
"Tell me three cool ways to use Mastra",
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
for await (const chunk of result) {
|
|
145
|
+
console.log(chunk.type);
|
|
146
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
147
|
+
console.log(chunk.payload.result);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### Agent output
|
|
153
|
+
|
|
154
|
+
The following `chunk.type` events are emitted during this request:
|
|
155
|
+
|
|
156
|
+
```text
|
|
157
|
+
routing-agent-start
|
|
158
|
+
routing-agent-end
|
|
159
|
+
agent-execution-start
|
|
160
|
+
agent-execution-event-start
|
|
161
|
+
agent-execution-event-step-start
|
|
162
|
+
agent-execution-event-text-start
|
|
163
|
+
agent-execution-event-text-delta
|
|
164
|
+
agent-execution-event-text-end
|
|
165
|
+
agent-execution-event-step-finish
|
|
166
|
+
agent-execution-event-finish
|
|
167
|
+
agent-execution-end
|
|
168
|
+
network-execution-event-step-finish
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Workflow example
|
|
172
|
+
|
|
173
|
+
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.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const result = await routingAgent.network(
|
|
177
|
+
"Tell me some historical facts about London",
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
for await (const chunk of result) {
|
|
181
|
+
console.log(chunk.type);
|
|
182
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
183
|
+
console.log(chunk.payload.result);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### Workflow output
|
|
189
|
+
|
|
190
|
+
The following `chunk.type` events are emitted during this request:
|
|
191
|
+
|
|
192
|
+
```text
|
|
193
|
+
routing-agent-end
|
|
194
|
+
workflow-execution-start
|
|
195
|
+
workflow-execution-event-workflow-start
|
|
196
|
+
workflow-execution-event-workflow-step-start
|
|
197
|
+
workflow-execution-event-workflow-step-result
|
|
198
|
+
workflow-execution-event-workflow-finish
|
|
199
|
+
workflow-execution-end
|
|
200
|
+
routing-agent-start
|
|
201
|
+
network-execution-event-step-finish
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Tool example
|
|
205
|
+
|
|
206
|
+
In this example, the routing agent skips the `researchAgent`, `writingAgent`, and `cityWorkflow`, and calls the `weatherTool` directly to complete the task.
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
const result = await routingAgent.network("What's the weather in London?");
|
|
210
|
+
|
|
211
|
+
for await (const chunk of result) {
|
|
212
|
+
console.log(chunk.type);
|
|
213
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
214
|
+
console.log(chunk.payload.result);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### Tool output
|
|
220
|
+
|
|
221
|
+
The following `chunk.type` events are emitted during this request:
|
|
222
|
+
|
|
223
|
+
```text
|
|
224
|
+
routing-agent-start
|
|
225
|
+
routing-agent-end
|
|
226
|
+
tool-execution-start
|
|
227
|
+
tool-execution-end
|
|
228
|
+
network-execution-event-step-finish
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Related
|
|
232
|
+
|
|
233
|
+
- [Agent Memory](./agent-memory)
|
|
234
|
+
- [Workflows Overview](../workflows/overview)
|
|
235
|
+
- [Request Context](https://mastra.ai/docs/v1/server/request-context)
|
|
236
|
+
- [Supervisor example](https://github.com/mastra-ai/mastra/tree/main/examples/supervisor-agent)
|