@mastra/memory 1.5.0 → 1.5.1
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 +30 -0
- package/dist/{chunk-DF7NDDSM.js → chunk-6PKWQ3GH.js} +28 -11
- package/dist/chunk-6PKWQ3GH.js.map +1 -0
- package/dist/{chunk-LLTHE64H.cjs → chunk-6XVTMLW4.cjs} +28 -11
- package/dist/chunk-6XVTMLW4.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{observational-memory-ZNTAIUGT.js → observational-memory-AJWSMZVP.js} +3 -3
- package/dist/{observational-memory-ZNTAIUGT.js.map → observational-memory-AJWSMZVP.js.map} +1 -1
- package/dist/{observational-memory-4PCXEZIS.cjs → observational-memory-Q5TO525O.cjs} +17 -17
- package/dist/{observational-memory-4PCXEZIS.cjs.map → observational-memory-Q5TO525O.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.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/chunk-DF7NDDSM.js.map +0 -1
- package/dist/chunk-LLTHE64H.cjs.map +0 -1
- package/dist/docs/SKILL.md +0 -54
- package/dist/docs/assets/SOURCE_MAP.json +0 -103
- package/dist/docs/references/docs-agents-agent-approval.md +0 -377
- package/dist/docs/references/docs-agents-agent-memory.md +0 -212
- package/dist/docs/references/docs-agents-network-approval.md +0 -275
- package/dist/docs/references/docs-agents-networks.md +0 -290
- package/dist/docs/references/docs-memory-memory-processors.md +0 -316
- package/dist/docs/references/docs-memory-message-history.md +0 -260
- package/dist/docs/references/docs-memory-observational-memory.md +0 -246
- package/dist/docs/references/docs-memory-overview.md +0 -45
- package/dist/docs/references/docs-memory-semantic-recall.md +0 -272
- package/dist/docs/references/docs-memory-storage.md +0 -261
- package/dist/docs/references/docs-memory-working-memory.md +0 -400
- package/dist/docs/references/reference-core-getMemory.md +0 -50
- package/dist/docs/references/reference-core-listMemory.md +0 -56
- package/dist/docs/references/reference-memory-clone-utilities.md +0 -199
- package/dist/docs/references/reference-memory-cloneThread.md +0 -130
- package/dist/docs/references/reference-memory-createThread.md +0 -68
- package/dist/docs/references/reference-memory-getThreadById.md +0 -24
- package/dist/docs/references/reference-memory-listThreads.md +0 -145
- package/dist/docs/references/reference-memory-memory-class.md +0 -147
- package/dist/docs/references/reference-memory-observational-memory.md +0 -565
- package/dist/docs/references/reference-processors-token-limiter-processor.md +0 -113
- package/dist/docs/references/reference-storage-dynamodb.md +0 -282
- package/dist/docs/references/reference-storage-libsql.md +0 -135
- package/dist/docs/references/reference-storage-mongodb.md +0 -262
- package/dist/docs/references/reference-storage-postgresql.md +0 -529
- package/dist/docs/references/reference-storage-upstash.md +0 -160
- package/dist/docs/references/reference-vectors-libsql.md +0 -305
- package/dist/docs/references/reference-vectors-mongodb.md +0 -295
- package/dist/docs/references/reference-vectors-pg.md +0 -408
- package/dist/docs/references/reference-vectors-upstash.md +0 -294
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
# Message History
|
|
2
|
-
|
|
3
|
-
Message history is the most basic and important form of memory. It gives the LLM a view of recent messages in the context window, enabling your agent to reference earlier exchanges and respond coherently.
|
|
4
|
-
|
|
5
|
-
You can also retrieve message history to display past conversations in your UI.
|
|
6
|
-
|
|
7
|
-
> **Info:** Each message belongs to a thread (the conversation) and a resource (the user or entity it's associated with). See [Threads and resources](https://mastra.ai/docs/memory/storage) for more detail.
|
|
8
|
-
|
|
9
|
-
## Getting started
|
|
10
|
-
|
|
11
|
-
Install the Mastra memory module along with a [storage adapter](https://mastra.ai/docs/memory/storage) for your database. The examples below use `@mastra/libsql`, which stores data locally in a `mastra.db` file.
|
|
12
|
-
|
|
13
|
-
**npm**:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install @mastra/memory@latest @mastra/libsql@latest
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
**pnpm**:
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
pnpm add @mastra/memory@latest @mastra/libsql@latest
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
**Yarn**:
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
yarn add @mastra/memory@latest @mastra/libsql@latest
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
**Bun**:
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
bun add @mastra/memory@latest @mastra/libsql@latest
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
Message history requires a storage adapter to persist conversations. Configure storage on your Mastra instance if you haven't already:
|
|
38
|
-
|
|
39
|
-
```typescript
|
|
40
|
-
import { Mastra } from "@mastra/core";
|
|
41
|
-
import { LibSQLStore } from "@mastra/libsql";
|
|
42
|
-
|
|
43
|
-
export const mastra = new Mastra({
|
|
44
|
-
storage: new LibSQLStore({
|
|
45
|
-
id: 'mastra-storage',
|
|
46
|
-
url: "file:./mastra.db",
|
|
47
|
-
}),
|
|
48
|
-
});
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
Give your agent a `Memory`:
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
import { Memory } from "@mastra/memory";
|
|
55
|
-
import { Agent } from "@mastra/core/agent";
|
|
56
|
-
|
|
57
|
-
export const agent = new Agent({
|
|
58
|
-
id: "test-agent",
|
|
59
|
-
memory: new Memory({
|
|
60
|
-
options: {
|
|
61
|
-
lastMessages: 10,
|
|
62
|
-
},
|
|
63
|
-
}),
|
|
64
|
-
});
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
When you call the agent, messages are automatically saved to the database. You can specify a `threadId`, `resourceId`, and optional `metadata`:
|
|
68
|
-
|
|
69
|
-
**Generate**:
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
await agent.generate("Hello", {
|
|
73
|
-
memory: {
|
|
74
|
-
thread: {
|
|
75
|
-
id: "thread-123",
|
|
76
|
-
title: "Support conversation",
|
|
77
|
-
metadata: { category: "billing" },
|
|
78
|
-
},
|
|
79
|
-
resource: "user-456",
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
**Stream**:
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
await agent.stream("Hello", {
|
|
88
|
-
memory: {
|
|
89
|
-
thread: {
|
|
90
|
-
id: "thread-123",
|
|
91
|
-
title: "Support conversation",
|
|
92
|
-
metadata: { category: "billing" },
|
|
93
|
-
},
|
|
94
|
-
resource: "user-456",
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
> **Info:** Threads and messages are created automatically when you call `agent.generate()` or `agent.stream()`, but you can also create them manually with [`createThread()`](https://mastra.ai/reference/memory/createThread) and [`saveMessages()`](https://mastra.ai/reference/memory/memory-class).
|
|
100
|
-
|
|
101
|
-
There are two ways to use this history:
|
|
102
|
-
|
|
103
|
-
- **Automatic inclusion** - Mastra automatically fetches and includes recent messages in the context window. By default, it includes the last 10 messages, keeping agents grounded in the conversation. You can adjust this number with `lastMessages`, but in most cases you don't need to think about it.
|
|
104
|
-
- [**Manual querying**](#querying) - For more control, use the `recall()` function to query threads and messages directly. This lets you choose exactly which memories are included in the context window, or fetch messages to render conversation history in your UI.
|
|
105
|
-
|
|
106
|
-
## Accessing Memory
|
|
107
|
-
|
|
108
|
-
To access memory functions for querying, cloning, or deleting threads and messages, call `getMemory()` on an agent:
|
|
109
|
-
|
|
110
|
-
```typescript
|
|
111
|
-
const agent = mastra.getAgent("weatherAgent");
|
|
112
|
-
const memory = await agent.getMemory();
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
The `Memory` instance gives you access to functions for listing threads, recalling messages, cloning conversations, and more.
|
|
116
|
-
|
|
117
|
-
## Querying
|
|
118
|
-
|
|
119
|
-
Use these methods to fetch threads and messages for displaying conversation history in your UI or for custom memory retrieval logic.
|
|
120
|
-
|
|
121
|
-
> **Warning:** The memory system does not enforce access control. Before running any query, verify in your application logic that the current user is authorized to access the `resourceId` being queried.
|
|
122
|
-
|
|
123
|
-
### Threads
|
|
124
|
-
|
|
125
|
-
Use [`listThreads()`](https://mastra.ai/reference/memory/listThreads) to retrieve threads for a resource:
|
|
126
|
-
|
|
127
|
-
```typescript
|
|
128
|
-
const result = await memory.listThreads({
|
|
129
|
-
filter: { resourceId: "user-123" },
|
|
130
|
-
perPage: false,
|
|
131
|
-
});
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
Paginate through threads:
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
const result = await memory.listThreads({
|
|
138
|
-
filter: { resourceId: "user-123" },
|
|
139
|
-
page: 0,
|
|
140
|
-
perPage: 10,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
console.log(result.threads); // thread objects
|
|
144
|
-
console.log(result.hasMore); // more pages available?
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
You can also filter by metadata and control sort order:
|
|
148
|
-
|
|
149
|
-
```typescript
|
|
150
|
-
const result = await memory.listThreads({
|
|
151
|
-
filter: {
|
|
152
|
-
resourceId: "user-123",
|
|
153
|
-
metadata: { status: "active" },
|
|
154
|
-
},
|
|
155
|
-
orderBy: { field: "createdAt", direction: "DESC" },
|
|
156
|
-
});
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
To fetch a single thread by ID, use [`getThreadById()`](https://mastra.ai/reference/memory/getThreadById):
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
const thread = await memory.getThreadById({ threadId: "thread-123" });
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### Messages
|
|
166
|
-
|
|
167
|
-
Once you have a thread, use [`recall()`](https://mastra.ai/reference/memory/recall) to retrieve its messages. It supports pagination, date filtering, and [semantic search](https://mastra.ai/docs/memory/semantic-recall).
|
|
168
|
-
|
|
169
|
-
Basic recall returns all messages from a thread:
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
const { messages } = await memory.recall({
|
|
173
|
-
threadId: "thread-123",
|
|
174
|
-
perPage: false,
|
|
175
|
-
});
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
Paginate through messages:
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
const { messages } = await memory.recall({
|
|
182
|
-
threadId: "thread-123",
|
|
183
|
-
page: 0,
|
|
184
|
-
perPage: 50,
|
|
185
|
-
});
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
Filter by date range:
|
|
189
|
-
|
|
190
|
-
```typescript
|
|
191
|
-
const { messages } = await memory.recall({
|
|
192
|
-
threadId: "thread-123",
|
|
193
|
-
filter: {
|
|
194
|
-
dateRange: {
|
|
195
|
-
start: new Date("2025-01-01"),
|
|
196
|
-
end: new Date("2025-06-01"),
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
});
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
Fetch a single message by ID:
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
const { messages } = await memory.recall({
|
|
206
|
-
threadId: "thread-123",
|
|
207
|
-
include: [{ id: "msg-123" }],
|
|
208
|
-
});
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
Fetch multiple messages by ID with surrounding context:
|
|
212
|
-
|
|
213
|
-
```typescript
|
|
214
|
-
const { messages } = await memory.recall({
|
|
215
|
-
threadId: "thread-123",
|
|
216
|
-
include: [
|
|
217
|
-
{ id: "msg-123" },
|
|
218
|
-
{
|
|
219
|
-
id: "msg-456",
|
|
220
|
-
withPreviousMessages: 3,
|
|
221
|
-
withNextMessages: 1,
|
|
222
|
-
},
|
|
223
|
-
],
|
|
224
|
-
});
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
Search by meaning (see [Semantic recall](https://mastra.ai/docs/memory/semantic-recall) for setup):
|
|
228
|
-
|
|
229
|
-
```typescript
|
|
230
|
-
const { messages } = await memory.recall({
|
|
231
|
-
threadId: "thread-123",
|
|
232
|
-
vectorSearchString: "project deadline discussion",
|
|
233
|
-
threadConfig: {
|
|
234
|
-
semanticRecall: true,
|
|
235
|
-
},
|
|
236
|
-
});
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
### UI format
|
|
240
|
-
|
|
241
|
-
Message queries return `MastraDBMessage[]` format. To display messages in a frontend, you may need to convert them to a format your UI library expects. For example, [`toAISdkV5Messages`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-v5-messages) converts messages to AI SDK UI format.
|
|
242
|
-
|
|
243
|
-
## Thread cloning
|
|
244
|
-
|
|
245
|
-
Thread cloning creates a copy of an existing thread with its messages. This is useful for branching conversations, creating checkpoints before a potentially destructive operation, or testing variations of a conversation.
|
|
246
|
-
|
|
247
|
-
```typescript
|
|
248
|
-
const { thread, clonedMessages } = await memory.cloneThread({
|
|
249
|
-
sourceThreadId: "thread-123",
|
|
250
|
-
title: "Branched conversation",
|
|
251
|
-
});
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
You can filter which messages get cloned (by count or date range), specify custom thread IDs, and use utility methods to inspect clone relationships.
|
|
255
|
-
|
|
256
|
-
See [`cloneThread()`](https://mastra.ai/reference/memory/cloneThread) and [clone utilities](https://mastra.ai/reference/memory/clone-utilities) for the full API.
|
|
257
|
-
|
|
258
|
-
## Deleting messages
|
|
259
|
-
|
|
260
|
-
To remove messages from a thread, use [`deleteMessages()`](https://mastra.ai/reference/memory/deleteMessages). You can delete by message ID or clear all messages from a thread.
|
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
# Observational Memory
|
|
2
|
-
|
|
3
|
-
**Added in:** `@mastra/memory@1.1.0`
|
|
4
|
-
|
|
5
|
-
Observational Memory (OM) is Mastra's memory system for long-context agentic memory. Two background agents — an **Observer** and a **Reflector** — watch your agent's conversations and maintain a dense observation log that replaces raw message history as it grows.
|
|
6
|
-
|
|
7
|
-
## Quick Start
|
|
8
|
-
|
|
9
|
-
Enable `observationalMemory` in the memory options when creating your agent:
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
import { Memory } from "@mastra/memory";
|
|
13
|
-
import { Agent } from "@mastra/core/agent";
|
|
14
|
-
|
|
15
|
-
export const agent = new Agent({
|
|
16
|
-
name: "my-agent",
|
|
17
|
-
instructions: "You are a helpful assistant.",
|
|
18
|
-
model: "openai/gpt-5-mini",
|
|
19
|
-
memory: new Memory({
|
|
20
|
-
options: {
|
|
21
|
-
observationalMemory: true,
|
|
22
|
-
},
|
|
23
|
-
}),
|
|
24
|
-
});
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
That's it. The agent now has humanlike long-term memory that persists across conversations. Setting `observationalMemory: true` uses `google/gemini-2.5-flash` by default. To use a different model or customize thresholds, pass a config object instead:
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
const memory = new Memory({
|
|
31
|
-
options: {
|
|
32
|
-
observationalMemory: {
|
|
33
|
-
model: "deepseek/deepseek-reasoner",
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
See [configuration options](https://mastra.ai/reference/memory/observational-memory) for full API details.
|
|
40
|
-
|
|
41
|
-
> **Note:** OM currently only supports `@mastra/pg`, `@mastra/libsql`, and `@mastra/mongodb` storage adapters. It uses background agents for managing memory. When using `observationalMemory: true`, the default model is `google/gemini-2.5-flash`. When passing a config object, a `model` must be explicitly set.
|
|
42
|
-
|
|
43
|
-
## Benefits
|
|
44
|
-
|
|
45
|
-
- **Prompt caching**: OM's context is stable — observations append over time rather than being dynamically retrieved each turn. This keeps the prompt prefix cacheable, which reduces costs.
|
|
46
|
-
- **Compression**: Raw message history and tool results get compressed into a dense observation log. Smaller context means faster responses and longer coherent conversations.
|
|
47
|
-
- **Zero context rot**: The agent sees relevant information instead of noisy tool calls and irrelevant tokens, so the agent stays on task over long sessions.
|
|
48
|
-
|
|
49
|
-
## How It Works
|
|
50
|
-
|
|
51
|
-
You don't remember every word of every conversation you've ever had. You observe what happened subconsciously, then your brain reflects — reorganizing, combining, and condensing into long-term memory. OM works the same way.
|
|
52
|
-
|
|
53
|
-
Every time an agent responds, it sees a context window containing its system prompt, recent message history, and any injected context. The context window is finite — even models with large token limits perform worse when the window is full. This causes two problems:
|
|
54
|
-
|
|
55
|
-
- **Context rot**: the more raw message history an agent carries, the worse it performs.
|
|
56
|
-
- **Context waste**: most of that history contains tokens no longer needed to keep the agent on task.
|
|
57
|
-
|
|
58
|
-
OM solves both problems by compressing old context into dense observations.
|
|
59
|
-
|
|
60
|
-
### Observations
|
|
61
|
-
|
|
62
|
-
When message history tokens exceed a threshold (default: 30,000), the Observer creates observations — concise notes about what happened:
|
|
63
|
-
|
|
64
|
-
```text
|
|
65
|
-
Date: 2026-01-15
|
|
66
|
-
- 🔴 12:10 User is building a Next.js app with Supabase auth, due in 1 week (meaning January 22nd 2026)
|
|
67
|
-
- 🔴 12:10 App uses server components with client-side hydration
|
|
68
|
-
- 🟡 12:12 User asked about middleware configuration for protected routes
|
|
69
|
-
- 🔴 12:15 User stated the app name is "Acme Dashboard"
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
The compression is typically 5–40×. The Observer also tracks a **current task** and **suggested response** so the agent picks up where it left off.
|
|
73
|
-
|
|
74
|
-
Example: an agent using Playwright MCP might see 50,000+ tokens per page snapshot. With OM, the Observer watches the interaction and creates a few hundred tokens of observations about what was on the page and what actions were taken. The agent stays on task without carrying every raw snapshot.
|
|
75
|
-
|
|
76
|
-
### Reflections
|
|
77
|
-
|
|
78
|
-
When observations exceed their threshold (default: 40,000 tokens), the Reflector condenses them — combining related items and reflecting on patterns.
|
|
79
|
-
|
|
80
|
-
The result is a three-tier system:
|
|
81
|
-
|
|
82
|
-
1. **Recent messages**: Exact conversation history for the current task
|
|
83
|
-
2. **Observations**: A log of what the Observer has seen
|
|
84
|
-
3. **Reflections**: Condensed observations when memory becomes too long
|
|
85
|
-
|
|
86
|
-
## Models
|
|
87
|
-
|
|
88
|
-
The Observer and Reflector run in the background. Any model that works with Mastra's model routing (e.g. `openai/...`, `google/...`, `deepseek/...`) can be used.
|
|
89
|
-
|
|
90
|
-
When using `observationalMemory: true`, the default model is `google/gemini-2.5-flash`. When passing a config object, a `model` must be explicitly set.
|
|
91
|
-
|
|
92
|
-
We recommend `google/gemini-2.5-flash` — it works well for both observation and reflection, and its 1M token context window gives the Reflector headroom.
|
|
93
|
-
|
|
94
|
-
We've also tested `deepseek`, `qwen3`, and `glm-4.7` for the Observer. For the Reflector, make sure the model's context window can fit all observations. Note that Claude 4.5 models currently don't work well as observer or reflector.
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
const memory = new Memory({
|
|
98
|
-
options: {
|
|
99
|
-
observationalMemory: {
|
|
100
|
-
model: "deepseek/deepseek-reasoner",
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
See [model configuration](https://mastra.ai/reference/memory/observational-memory) for using different models per agent.
|
|
107
|
-
|
|
108
|
-
## Scopes
|
|
109
|
-
|
|
110
|
-
### Thread scope (default)
|
|
111
|
-
|
|
112
|
-
Each thread has its own observations. This scope is well tested and works well as a general purpose memory system, especially for long horizon agentic use-cases.
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
const memory = new Memory({
|
|
116
|
-
options: {
|
|
117
|
-
observationalMemory: {
|
|
118
|
-
model: "google/gemini-2.5-flash",
|
|
119
|
-
scope: "thread",
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
});
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
### Resource scope (experimental)
|
|
126
|
-
|
|
127
|
-
Observations are shared across all threads for a resource (typically a user). Enables cross-conversation memory.
|
|
128
|
-
|
|
129
|
-
```typescript
|
|
130
|
-
const memory = new Memory({
|
|
131
|
-
options: {
|
|
132
|
-
observationalMemory: {
|
|
133
|
-
model: "google/gemini-2.5-flash",
|
|
134
|
-
scope: "resource",
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
Resource scope works, however it's marked as experimental for now until we prove task adherence/continuity across multiple ongoing simultaneous threads. As of today, you may need to tweak your system prompt to prevent one thread from continuing the work that another had already started (but hadn't finished).
|
|
141
|
-
|
|
142
|
-
This is because in resource scope, each thread is a perspective on _all_ threads for the resource.
|
|
143
|
-
|
|
144
|
-
For your use-case this may not be a problem, so your mileage may vary.
|
|
145
|
-
|
|
146
|
-
> **Warning:** In resource scope, unobserved messages across _all_ threads are processed together. For users with many existing threads, this can be slow. Use thread scope for existing apps.
|
|
147
|
-
|
|
148
|
-
## Token Budgets
|
|
149
|
-
|
|
150
|
-
OM uses token thresholds to decide when to observe and reflect. See [token budget configuration](https://mastra.ai/reference/memory/observational-memory) for details.
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
const memory = new Memory({
|
|
154
|
-
options: {
|
|
155
|
-
observationalMemory: {
|
|
156
|
-
model: "google/gemini-2.5-flash",
|
|
157
|
-
observation: {
|
|
158
|
-
// when to run the Observer (default: 30,000)
|
|
159
|
-
messageTokens: 30_000,
|
|
160
|
-
},
|
|
161
|
-
reflection: {
|
|
162
|
-
// when to run the Reflector (default: 40,000)
|
|
163
|
-
observationTokens: 40_000,
|
|
164
|
-
},
|
|
165
|
-
// let message history borrow from observation budget
|
|
166
|
-
// requires bufferTokens: false (temporary limitation)
|
|
167
|
-
shareTokenBudget: false,
|
|
168
|
-
},
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## Async Buffering
|
|
174
|
-
|
|
175
|
-
Without async buffering, the Observer runs synchronously when the message threshold is reached — the agent pauses mid-conversation while the Observer LLM call completes. With async buffering (enabled by default), observations are pre-computed in the background as the conversation grows. When the threshold is hit, buffered observations activate instantly with no pause.
|
|
176
|
-
|
|
177
|
-
### How it works
|
|
178
|
-
|
|
179
|
-
As the agent converses, message tokens accumulate. At regular intervals (`bufferTokens`), a background Observer call runs without blocking the agent. Each call produces a "chunk" of observations that's stored in a buffer.
|
|
180
|
-
|
|
181
|
-
When message tokens reach the `messageTokens` threshold, buffered chunks activate: their observations move into the active observation log, and the corresponding raw messages are removed from the context window. The agent never pauses.
|
|
182
|
-
|
|
183
|
-
Buffered observations also include continuation hints — a suggested next response and the current task — so the main agent maintains conversational continuity after activation shrinks the context window.
|
|
184
|
-
|
|
185
|
-
If the agent produces messages faster than the Observer can process them, a `blockAfter` safety threshold forces a synchronous observation as a last resort.
|
|
186
|
-
|
|
187
|
-
Reflection works similarly — the Reflector runs in the background when observations reach a fraction of the reflection threshold.
|
|
188
|
-
|
|
189
|
-
### Settings
|
|
190
|
-
|
|
191
|
-
| Setting | Default | What it controls |
|
|
192
|
-
| ------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
193
|
-
| `observation.bufferTokens` | `0.2` | How often to buffer. `0.2` means every 20% of `messageTokens` — with the default 30k threshold, that's roughly every 6k tokens. Can also be an absolute token count (e.g. `5000`). |
|
|
194
|
-
| `observation.bufferActivation` | `0.8` | How aggressively to clear the message window on activation. `0.8` means remove enough messages to keep only 20% of `messageTokens` remaining. Lower values keep more message history. |
|
|
195
|
-
| `observation.blockAfter` | `1.2` | Safety threshold as a multiplier of `messageTokens`. At `1.2`, synchronous observation is forced at 36k tokens (1.2 × 30k). Only matters if buffering can't keep up. |
|
|
196
|
-
| `reflection.bufferActivation` | `0.5` | When to start background reflection. `0.5` means reflection begins when observations reach 50% of the `observationTokens` threshold. |
|
|
197
|
-
| `reflection.blockAfter` | `1.2` | Safety threshold for reflection, same logic as observation. |
|
|
198
|
-
|
|
199
|
-
### Disabling
|
|
200
|
-
|
|
201
|
-
To disable async buffering and use synchronous observation/reflection instead:
|
|
202
|
-
|
|
203
|
-
```typescript
|
|
204
|
-
const memory = new Memory({
|
|
205
|
-
options: {
|
|
206
|
-
observationalMemory: {
|
|
207
|
-
model: "google/gemini-2.5-flash",
|
|
208
|
-
observation: {
|
|
209
|
-
bufferTokens: false,
|
|
210
|
-
},
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
Setting `bufferTokens: false` disables both observation and reflection async buffering. See [async buffering configuration](https://mastra.ai/reference/memory/observational-memory) for the full API.
|
|
217
|
-
|
|
218
|
-
> **Note:** Async buffering is not supported with `scope: 'resource'`. It is automatically disabled in resource scope.
|
|
219
|
-
|
|
220
|
-
## Migrating existing threads
|
|
221
|
-
|
|
222
|
-
No manual migration needed. OM reads existing messages and observes them lazily when thresholds are exceeded.
|
|
223
|
-
|
|
224
|
-
- **Thread scope**: The first time a thread exceeds `observation.messageTokens`, the Observer processes the backlog.
|
|
225
|
-
- **Resource scope**: All unobserved messages across all threads for a resource are processed together. For users with many existing threads, this could take significant time.
|
|
226
|
-
|
|
227
|
-
## Viewing in Mastra Studio
|
|
228
|
-
|
|
229
|
-
Mastra Studio shows OM status in real time in the memory tab: token usage, which model is running, current observations, and reflection history.
|
|
230
|
-
|
|
231
|
-
## Comparing OM with other memory features
|
|
232
|
-
|
|
233
|
-
- **[Message history](https://mastra.ai/docs/memory/message-history)**: High-fidelity record of the current conversation
|
|
234
|
-
- **[Working memory](https://mastra.ai/docs/memory/working-memory)**: Small, structured state (JSON or markdown) for user preferences, names, goals
|
|
235
|
-
- **[Semantic Recall](https://mastra.ai/docs/memory/semantic-recall)**: RAG-based retrieval of relevant past messages
|
|
236
|
-
|
|
237
|
-
If you're using working memory to store conversation summaries or ongoing state that grows over time, OM is a better fit. Working memory is for small, structured data; OM is for long-running event logs. OM also manages message history automatically—the `messageTokens` setting controls how much raw history remains before observation runs.
|
|
238
|
-
|
|
239
|
-
In practical terms, OM replaces both working memory and message history, and has greater accuracy (and lower cost) than Semantic Recall.
|
|
240
|
-
|
|
241
|
-
## Related
|
|
242
|
-
|
|
243
|
-
- [Observational Memory Reference](https://mastra.ai/reference/memory/observational-memory)
|
|
244
|
-
- [Memory Overview](https://mastra.ai/docs/memory/overview)
|
|
245
|
-
- [Message History](https://mastra.ai/docs/memory/message-history)
|
|
246
|
-
- [Memory Processors](https://mastra.ai/docs/memory/memory-processors)
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# Memory
|
|
2
|
-
|
|
3
|
-
Memory enables your agent to remember user messages, agent replies, and tool results across interactions, giving it the context it needs to stay consistent, maintain conversation flow, and produce better answers over time.
|
|
4
|
-
|
|
5
|
-
Mastra supports four complementary memory types:
|
|
6
|
-
|
|
7
|
-
- [**Message history**](https://mastra.ai/docs/memory/message-history) - keeps recent messages from the current conversation so they can be rendered in the UI and used to maintain short-term continuity within the exchange.
|
|
8
|
-
- [**Working memory**](https://mastra.ai/docs/memory/working-memory) - stores persistent, structured user data such as names, preferences, and goals.
|
|
9
|
-
- [**Semantic recall**](https://mastra.ai/docs/memory/semantic-recall) - retrieves relevant messages from older conversations based on semantic meaning rather than exact keywords, mirroring how humans recall information by association. Requires a [vector database](https://mastra.ai/docs/memory/semantic-recall) and an [embedding model](https://mastra.ai/docs/memory/semantic-recall).
|
|
10
|
-
- [**Observational memory**](https://mastra.ai/docs/memory/observational-memory) - uses background Observer and Reflector agents to maintain a dense observation log that replaces raw message history as it grows, keeping the context window small while preserving long-term memory across conversations.
|
|
11
|
-
|
|
12
|
-
If the combined memory exceeds the model's context limit, [memory processors](https://mastra.ai/docs/memory/memory-processors) can filter, trim, or prioritize content so the most relevant information is preserved.
|
|
13
|
-
|
|
14
|
-
## Getting started
|
|
15
|
-
|
|
16
|
-
Choose a memory option to get started:
|
|
17
|
-
|
|
18
|
-
- [Message history](https://mastra.ai/docs/memory/message-history)
|
|
19
|
-
- [Working memory](https://mastra.ai/docs/memory/working-memory)
|
|
20
|
-
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall)
|
|
21
|
-
- [Observational memory](https://mastra.ai/docs/memory/observational-memory)
|
|
22
|
-
|
|
23
|
-
## Storage
|
|
24
|
-
|
|
25
|
-
Before enabling memory, you must first configure a storage adapter. Mastra supports several databases including PostgreSQL, MongoDB, libSQL, and [more](https://mastra.ai/docs/memory/storage).
|
|
26
|
-
|
|
27
|
-
Storage can be configured at the [instance level](https://mastra.ai/docs/memory/storage) (shared across all agents) or at the [agent level](https://mastra.ai/docs/memory/storage) (dedicated per agent).
|
|
28
|
-
|
|
29
|
-
For semantic recall, you can use a separate vector database like Pinecone alongside your primary storage.
|
|
30
|
-
|
|
31
|
-
See the [Storage](https://mastra.ai/docs/memory/storage) documentation for configuration options, supported providers, and examples.
|
|
32
|
-
|
|
33
|
-
## Debugging memory
|
|
34
|
-
|
|
35
|
-
When [tracing](https://mastra.ai/docs/observability/tracing/overview) is enabled, you can inspect exactly which messages the agent uses for context in each request. The trace output shows all memory included in the agent's context window - both recent message history and messages recalled via semantic recall.
|
|
36
|
-
|
|
37
|
-

|
|
38
|
-
|
|
39
|
-
This visibility helps you understand why an agent made specific decisions and verify that memory retrieval is working as expected.
|
|
40
|
-
|
|
41
|
-
## Next steps
|
|
42
|
-
|
|
43
|
-
- Learn more about [Storage](https://mastra.ai/docs/memory/storage) providers and configuration options
|
|
44
|
-
- Add [Message history](https://mastra.ai/docs/memory/message-history), [Working memory](https://mastra.ai/docs/memory/working-memory), [Semantic recall](https://mastra.ai/docs/memory/semantic-recall), or [Observational memory](https://mastra.ai/docs/memory/observational-memory)
|
|
45
|
-
- Visit [Memory configuration reference](https://mastra.ai/reference/memory/memory-class) for all available options
|