@mastra/memory 1.1.0 → 1.2.0-alpha.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.
Files changed (95) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +30 -17
  3. package/dist/{chunk-6TXUWFIU.js → chunk-5YW6JV6Y.js} +1958 -321
  4. package/dist/chunk-5YW6JV6Y.js.map +1 -0
  5. package/dist/{chunk-FQJWVCDF.cjs → chunk-7SCXX4S7.cjs} +1957 -320
  6. package/dist/chunk-7SCXX4S7.cjs.map +1 -0
  7. package/dist/chunk-EQ4M72KU.js +439 -0
  8. package/dist/chunk-EQ4M72KU.js.map +1 -0
  9. package/dist/{chunk-O3CS4UGX.cjs → chunk-IDRQZVB4.cjs} +4 -4
  10. package/dist/{chunk-O3CS4UGX.cjs.map → chunk-IDRQZVB4.cjs.map} +1 -1
  11. package/dist/{chunk-YF4R74L2.js → chunk-RC6RZVYE.js} +4 -4
  12. package/dist/{chunk-YF4R74L2.js.map → chunk-RC6RZVYE.js.map} +1 -1
  13. package/dist/chunk-ZD3BKU5O.cjs +441 -0
  14. package/dist/chunk-ZD3BKU5O.cjs.map +1 -0
  15. package/dist/docs/SKILL.md +51 -50
  16. package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +22 -22
  17. package/dist/docs/{agents/03-agent-approval.md → references/docs-agents-agent-approval.md} +19 -19
  18. package/dist/docs/references/docs-agents-agent-memory.md +212 -0
  19. package/dist/docs/{agents/04-network-approval.md → references/docs-agents-network-approval.md} +13 -12
  20. package/dist/docs/{agents/02-networks.md → references/docs-agents-networks.md} +10 -12
  21. package/dist/docs/{memory/06-memory-processors.md → references/docs-memory-memory-processors.md} +6 -8
  22. package/dist/docs/{memory/03-message-history.md → references/docs-memory-message-history.md} +31 -20
  23. package/dist/docs/references/docs-memory-observational-memory.md +169 -0
  24. package/dist/docs/{memory/01-overview.md → references/docs-memory-overview.md} +8 -8
  25. package/dist/docs/{memory/05-semantic-recall.md → references/docs-memory-semantic-recall.md} +33 -17
  26. package/dist/docs/{memory/02-storage.md → references/docs-memory-storage.md} +29 -39
  27. package/dist/docs/{memory/04-working-memory.md → references/docs-memory-working-memory.md} +16 -27
  28. package/dist/docs/references/reference-core-getMemory.md +50 -0
  29. package/dist/docs/references/reference-core-listMemory.md +56 -0
  30. package/dist/docs/references/reference-memory-clone-utilities.md +199 -0
  31. package/dist/docs/references/reference-memory-cloneThread.md +130 -0
  32. package/dist/docs/references/reference-memory-createThread.md +68 -0
  33. package/dist/docs/references/reference-memory-getThreadById.md +24 -0
  34. package/dist/docs/references/reference-memory-listThreads.md +145 -0
  35. package/dist/docs/references/reference-memory-memory-class.md +147 -0
  36. package/dist/docs/references/reference-memory-observational-memory.md +219 -0
  37. package/dist/docs/{processors/01-reference.md → references/reference-processors-token-limiter-processor.md} +25 -12
  38. package/dist/docs/references/reference-storage-dynamodb.md +282 -0
  39. package/dist/docs/references/reference-storage-libsql.md +135 -0
  40. package/dist/docs/references/reference-storage-mongodb.md +262 -0
  41. package/dist/docs/references/reference-storage-postgresql.md +529 -0
  42. package/dist/docs/references/reference-storage-upstash.md +160 -0
  43. package/dist/docs/references/reference-vectors-libsql.md +305 -0
  44. package/dist/docs/references/reference-vectors-mongodb.md +295 -0
  45. package/dist/docs/references/reference-vectors-pg.md +408 -0
  46. package/dist/docs/references/reference-vectors-upstash.md +294 -0
  47. package/dist/index.cjs +919 -507
  48. package/dist/index.cjs.map +1 -1
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +914 -502
  51. package/dist/index.js.map +1 -1
  52. package/dist/{observational-memory-3Q42SITP.cjs → observational-memory-G3HACXHE.cjs} +14 -14
  53. package/dist/{observational-memory-3Q42SITP.cjs.map → observational-memory-G3HACXHE.cjs.map} +1 -1
  54. package/dist/{observational-memory-VXLHOSDZ.js → observational-memory-LI6QFTRE.js} +3 -3
  55. package/dist/{observational-memory-VXLHOSDZ.js.map → observational-memory-LI6QFTRE.js.map} +1 -1
  56. package/dist/processors/index.cjs +12 -12
  57. package/dist/processors/index.js +1 -1
  58. package/dist/processors/observational-memory/index.d.ts +1 -1
  59. package/dist/processors/observational-memory/index.d.ts.map +1 -1
  60. package/dist/processors/observational-memory/observational-memory.d.ts +283 -1
  61. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  62. package/dist/processors/observational-memory/observer-agent.d.ts +3 -1
  63. package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -1
  64. package/dist/processors/observational-memory/reflector-agent.d.ts +10 -3
  65. package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -1
  66. package/dist/processors/observational-memory/types.d.ts +243 -19
  67. package/dist/processors/observational-memory/types.d.ts.map +1 -1
  68. package/dist/{token-6GSAFR2W-WGTMOPEU.js → token-APYSY3BW-2DN6RAUY.js} +11 -11
  69. package/dist/token-APYSY3BW-2DN6RAUY.js.map +1 -0
  70. package/dist/{token-6GSAFR2W-2B4WM6AQ.cjs → token-APYSY3BW-ZQ7TMBY7.cjs} +14 -14
  71. package/dist/token-APYSY3BW-ZQ7TMBY7.cjs.map +1 -0
  72. package/dist/token-util-RMHT2CPJ-6TGPE335.cjs +10 -0
  73. package/dist/token-util-RMHT2CPJ-6TGPE335.cjs.map +1 -0
  74. package/dist/token-util-RMHT2CPJ-RJEA3FAN.js +8 -0
  75. package/dist/token-util-RMHT2CPJ-RJEA3FAN.js.map +1 -0
  76. package/dist/tools/working-memory.d.ts.map +1 -1
  77. package/package.json +5 -6
  78. package/dist/chunk-6TXUWFIU.js.map +0 -1
  79. package/dist/chunk-FQJWVCDF.cjs.map +0 -1
  80. package/dist/chunk-WM6IIUQW.js +0 -250
  81. package/dist/chunk-WM6IIUQW.js.map +0 -1
  82. package/dist/chunk-ZSBBXHNM.cjs +0 -252
  83. package/dist/chunk-ZSBBXHNM.cjs.map +0 -1
  84. package/dist/docs/README.md +0 -36
  85. package/dist/docs/agents/01-agent-memory.md +0 -166
  86. package/dist/docs/core/01-reference.md +0 -114
  87. package/dist/docs/memory/07-reference.md +0 -687
  88. package/dist/docs/storage/01-reference.md +0 -1218
  89. package/dist/docs/vectors/01-reference.md +0 -942
  90. package/dist/token-6GSAFR2W-2B4WM6AQ.cjs.map +0 -1
  91. package/dist/token-6GSAFR2W-WGTMOPEU.js.map +0 -1
  92. package/dist/token-util-NEHG7TUY-TV2H7N56.js +0 -8
  93. package/dist/token-util-NEHG7TUY-TV2H7N56.js.map +0 -1
  94. package/dist/token-util-NEHG7TUY-WJZIPNNX.cjs +0 -10
  95. package/dist/token-util-NEHG7TUY-WJZIPNNX.cjs.map +0 -1
@@ -0,0 +1,145 @@
1
+ # Memory.listThreads()
2
+
3
+ The `listThreads()` method retrieves threads with pagination support and optional filtering by `resourceId`, `metadata`, or both.
4
+
5
+ ## Usage Examples
6
+
7
+ ### List all threads with pagination
8
+
9
+ ```typescript
10
+ const result = await memory.listThreads({
11
+ page: 0,
12
+ perPage: 10,
13
+ });
14
+ ```
15
+
16
+ ### Fetch all threads without pagination
17
+
18
+ Use `perPage: false` to retrieve all matching threads at once.
19
+
20
+ > **Warning:** Generally speaking it's recommended to use pagination, especially for large datasets. Use this option cautiously.
21
+
22
+ ```typescript
23
+ const result = await memory.listThreads({
24
+ filter: { resourceId: "user-123" },
25
+ perPage: false,
26
+ });
27
+ ```
28
+
29
+ ### Filter by resourceId
30
+
31
+ ```typescript
32
+ const result = await memory.listThreads({
33
+ filter: { resourceId: "user-123" },
34
+ page: 0,
35
+ perPage: 10,
36
+ });
37
+ ```
38
+
39
+ ### Filter by metadata
40
+
41
+ ```typescript
42
+ const result = await memory.listThreads({
43
+ filter: { metadata: { category: "support", priority: "high" } },
44
+ page: 0,
45
+ perPage: 10,
46
+ });
47
+ ```
48
+
49
+ ### Combined filter (resourceId & metadata)
50
+
51
+ ```typescript
52
+ const result = await memory.listThreads({
53
+ filter: {
54
+ resourceId: "user-123",
55
+ metadata: { status: "active" },
56
+ },
57
+ page: 0,
58
+ perPage: 10,
59
+ });
60
+ ```
61
+
62
+ ## Parameters
63
+
64
+ **filter?:** (`{ resourceId?: string; metadata?: Record<string, unknown> }`): Optional filter object. resourceId filters threads by resource ID. metadata filters threads by metadata key-value pairs (AND logic - all must match)
65
+
66
+ **page?:** (`number`): Page number (0-indexed) to retrieve
67
+
68
+ **perPage?:** (`number | false`): Maximum number of threads to return per page, or false to fetch all
69
+
70
+ **orderBy?:** (`{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }`): Sort configuration with field and direction (defaults to { field: 'createdAt', direction: 'DESC' })
71
+
72
+ ## Returns
73
+
74
+ **result:** (`Promise<StorageListThreadsOutput>`): A promise that resolves to paginated thread results with metadata
75
+
76
+ The return object contains:
77
+
78
+ - `threads`: Array of thread objects
79
+ - `total`: Total number of threads matching the filter
80
+ - `page`: Current page number (same as the input `page` parameter)
81
+ - `perPage`: Items per page (same as the input `perPage` parameter)
82
+ - `hasMore`: Boolean indicating if more results are available
83
+
84
+ ## Extended usage example
85
+
86
+ ```typescript
87
+ import { mastra } from "./mastra";
88
+
89
+ const agent = mastra.getAgent("agent");
90
+ const memory = await agent.getMemory();
91
+
92
+ let currentPage = 0;
93
+ const perPage = 25;
94
+ let hasMorePages = true;
95
+
96
+ // Fetch all active threads for a user, sorted by creation date
97
+ while (hasMorePages) {
98
+ const result = await memory?.listThreads({
99
+ filter: {
100
+ resourceId: "user-123",
101
+ metadata: { status: "active" },
102
+ },
103
+ page: currentPage,
104
+ perPage: perPage,
105
+ orderBy: { field: "createdAt", direction: "ASC" },
106
+ });
107
+
108
+ if (!result) {
109
+ console.log("No threads");
110
+ break;
111
+ }
112
+
113
+ result.threads.forEach((thread) => {
114
+ console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`);
115
+ });
116
+
117
+ hasMorePages = result.hasMore;
118
+ currentPage++; // Move to next page
119
+ }
120
+ ```
121
+
122
+ ## Metadata Filtering
123
+
124
+ The metadata filter uses AND logic - all specified key-value pairs must match for a thread to be included in the results:
125
+
126
+ ```typescript
127
+ // This will only return threads where BOTH conditions are true:
128
+ // - category === 'support'
129
+ // - priority === 'high'
130
+ await memory.listThreads({
131
+ filter: {
132
+ metadata: {
133
+ category: "support",
134
+ priority: "high",
135
+ },
136
+ },
137
+ });
138
+ ```
139
+
140
+ ## Related
141
+
142
+ - [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
143
+ - [Getting Started with Memory](https://mastra.ai/docs/memory/overview)
144
+ - [createThread](https://mastra.ai/reference/memory/createThread)
145
+ - [getThreadById](https://mastra.ai/reference/memory/getThreadById)
@@ -0,0 +1,147 @@
1
+ # Memory Class
2
+
3
+ The `Memory` class provides a robust system for managing conversation history and thread-based message storage in Mastra. It enables persistent storage of conversations, semantic search capabilities, and efficient message retrieval. You must configure a storage provider for conversation history, and if you enable semantic recall you will also need to provide a vector store and embedder.
4
+
5
+ ## Usage example
6
+
7
+ ```typescript
8
+ import { Memory } from "@mastra/memory";
9
+ import { Agent } from "@mastra/core/agent";
10
+
11
+ export const agent = new Agent({
12
+ name: "test-agent",
13
+ instructions: "You are an agent with memory.",
14
+ model: "openai/gpt-5.1",
15
+ memory: new Memory({
16
+ options: {
17
+ workingMemory: {
18
+ enabled: true,
19
+ },
20
+ },
21
+ }),
22
+ });
23
+ ```
24
+
25
+ > To enable `workingMemory` on an agent, you’ll need a storage provider configured on your main Mastra instance. See [Mastra class](https://mastra.ai/reference/core/mastra-class) for more information.
26
+
27
+ ## Constructor parameters
28
+
29
+ **storage?:** (`MastraCompositeStore`): Storage implementation for persisting memory data. Defaults to \`new DefaultStorage({ config: { url: "file:memory.db" } })\` if not provided.
30
+
31
+ **vector?:** (`MastraVector | false`): Vector store for semantic search capabilities. Set to \`false\` to disable vector operations.
32
+
33
+ **embedder?:** (`EmbeddingModel<string> | EmbeddingModelV2<string>`): Embedder instance for vector embeddings. Required when semantic recall is enabled.
34
+
35
+ **options?:** (`MemoryConfig`): Memory configuration options.
36
+
37
+ ### Options parameters
38
+
39
+ **lastMessages?:** (`number | false`): Number of most recent messages to retrieve. Set to false to disable. (Default: `10`)
40
+
41
+ **readOnly?:** (`boolean`): When true, prevents memory from saving new messages and provides working memory as read-only context (without the updateWorkingMemory tool). Useful for read-only operations like previews, internal routing agents, or sub agents that should reference but not modify memory. (Default: `false`)
42
+
43
+ **semanticRecall?:** (`boolean | { topK: number; messageRange: number | { before: number; after: number }; scope?: 'thread' | 'resource' }`): Enable semantic search in message history. Can be a boolean or an object with configuration options. When enabled, requires both vector store and embedder to be configured. Default topK is 4, default messageRange is {before: 1, after: 1}. (Default: `false`)
44
+
45
+ **workingMemory?:** (`WorkingMemory`): Configuration for working memory feature. Can be \`{ enabled: boolean; template?: string; schema?: ZodObject\<any> | JSONSchema7; scope?: 'thread' | 'resource' }\` or \`{ enabled: boolean }\` to disable. (Default: `{ enabled: false, template: '# User Information\n- **First Name**:\n- **Last Name**:\n...' }`)
46
+
47
+ **observationalMemory?:** (`boolean | ObservationalMemoryOptions`): Enable Observational Memory for long-context agentic memory. Set to \`true\` for defaults, or pass a config object to customize token budgets, models, and scope. See \[Observational Memory reference]\(/reference/memory/observational-memory) for configuration details. (Default: `false`)
48
+
49
+ **generateTitle?:** (`boolean | { model: DynamicArgument<MastraLanguageModel>; instructions?: DynamicArgument<string> }`): Controls automatic thread title generation from the user's first message. Can be a boolean or an object with custom model and instructions. (Default: `false`)
50
+
51
+ ## Returns
52
+
53
+ **memory:** (`Memory`): A new Memory instance with the specified configuration.
54
+
55
+ ## Extended usage example
56
+
57
+ ```typescript
58
+ import { Memory } from "@mastra/memory";
59
+ import { Agent } from "@mastra/core/agent";
60
+ import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
61
+
62
+ export const agent = new Agent({
63
+ name: "test-agent",
64
+ instructions: "You are an agent with memory.",
65
+ model: "openai/gpt-5.1",
66
+ memory: new Memory({
67
+ storage: new LibSQLStore({
68
+ id: 'test-agent-storage',
69
+ url: "file:./working-memory.db",
70
+ }),
71
+ vector: new LibSQLVector({
72
+ id: 'test-agent-vector',
73
+ url: "file:./vector-memory.db",
74
+ }),
75
+ options: {
76
+ lastMessages: 10,
77
+ semanticRecall: {
78
+ topK: 3,
79
+ messageRange: 2,
80
+ scope: "resource",
81
+ },
82
+ workingMemory: {
83
+ enabled: true,
84
+ },
85
+ generateTitle: true,
86
+ },
87
+ }),
88
+ });
89
+ ```
90
+
91
+ ## PostgreSQL with index configuration
92
+
93
+ ```typescript
94
+ import { Memory } from "@mastra/memory";
95
+ import { Agent } from "@mastra/core/agent";
96
+ import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
97
+ import { PgStore, PgVector } from "@mastra/pg";
98
+
99
+ export const agent = new Agent({
100
+ name: "pg-agent",
101
+ instructions: "You are an agent with optimized PostgreSQL memory.",
102
+ model: "openai/gpt-5.1",
103
+ memory: new Memory({
104
+ storage: new PgStore({
105
+ id: 'pg-agent-storage',
106
+ connectionString: process.env.DATABASE_URL,
107
+ }),
108
+ vector: new PgVector({
109
+ id: 'pg-agent-vector',
110
+ connectionString: process.env.DATABASE_URL,
111
+ }),
112
+ embedder: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
113
+ options: {
114
+ lastMessages: 20,
115
+ semanticRecall: {
116
+ topK: 5,
117
+ messageRange: 3,
118
+ scope: "resource",
119
+ indexConfig: {
120
+ type: "hnsw", // Use HNSW for better performance
121
+ metric: "dotproduct", // Optimal for OpenAI embeddings
122
+ m: 16, // Number of bi-directional links
123
+ efConstruction: 64, // Construction-time candidate list size
124
+ },
125
+ },
126
+ workingMemory: {
127
+ enabled: true,
128
+ },
129
+ },
130
+ }),
131
+ });
132
+ ```
133
+
134
+ ### Related
135
+
136
+ - [Getting Started with Memory](https://mastra.ai/docs/memory/overview)
137
+ - [Semantic Recall](https://mastra.ai/docs/memory/semantic-recall)
138
+ - [Working Memory](https://mastra.ai/docs/memory/working-memory)
139
+ - [Observational Memory](https://mastra.ai/docs/memory/observational-memory)
140
+ - [Memory Processors](https://mastra.ai/docs/memory/memory-processors)
141
+ - [createThread](https://mastra.ai/reference/memory/createThread)
142
+ - [recall](https://mastra.ai/reference/memory/recall)
143
+ - [getThreadById](https://mastra.ai/reference/memory/getThreadById)
144
+ - [listThreads](https://mastra.ai/reference/memory/listThreads)
145
+ - [deleteMessages](https://mastra.ai/reference/memory/deleteMessages)
146
+ - [cloneThread](https://mastra.ai/reference/memory/cloneThread)
147
+ - [Clone Utility Methods](https://mastra.ai/reference/memory/clone-utilities)
@@ -0,0 +1,219 @@
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** that watches conversations and creates observations, and a **Reflector** that restructures observations by combining related items, reflecting on overarching patterns, and condensing where possible — maintain an observation log that replaces raw message history as it grows.
6
+
7
+ ## Usage
8
+
9
+ ```typescript
10
+ import { Memory } from "@mastra/memory";
11
+ import { Agent } from "@mastra/core/agent";
12
+
13
+ export const agent = new Agent({
14
+ name: "my-agent",
15
+ instructions: "You are a helpful assistant.",
16
+ model: "openai/gpt-5-mini",
17
+ memory: new Memory({
18
+ options: {
19
+ observationalMemory: true,
20
+ },
21
+ }),
22
+ });
23
+ ```
24
+
25
+ ## Configuration
26
+
27
+ The `observationalMemory` option accepts `true`, `false`, or a configuration object.
28
+
29
+ Setting `observationalMemory: true` enables it with all defaults. Setting `observationalMemory: false` or omitting it disables it.
30
+
31
+ **enabled?:** (`boolean`): Enable or disable Observational Memory. When omitted from a config object, defaults to \`true\`. Only \`enabled: false\` explicitly disables it. (Default: `true`)
32
+
33
+ **model?:** (`string | LanguageModel | DynamicModel | ModelWithRetries[]`): Model for both the Observer and Reflector agents. Sets the model for both at once. Cannot be used together with \`observation.model\` or \`reflection.model\` — an error will be thrown if both are set. (Default: `'google/gemini-2.5-flash'`)
34
+
35
+ **scope?:** (`'resource' | 'thread'`): Memory scope for observations. \`'thread'\` keeps observations per-thread. \`'resource'\` shares observations across all threads for a resource, enabling cross-conversation memory. (Default: `'thread'`)
36
+
37
+ **shareTokenBudget?:** (`boolean`): Share the token budget between messages and observations. When enabled, the total budget is \`observation.messageTokens + reflection.observationTokens\`. Messages can use more space when observations are small, and vice versa. This maximizes context usage through flexible allocation. (Default: `false`)
38
+
39
+ **observation?:** (`ObservationalMemoryObservationConfig`): Configuration for the observation step. Controls when the Observer agent runs and how it behaves.
40
+
41
+ **reflection?:** (`ObservationalMemoryReflectionConfig`): Configuration for the reflection step. Controls when the Reflector agent runs and how it behaves.
42
+
43
+ ### Observation config
44
+
45
+ **model?:** (`string | LanguageModel | DynamicModel | ModelWithRetries[]`): Model for the Observer agent. Cannot be set if a top-level \`model\` is also provided. (Default: `'google/gemini-2.5-flash'`)
46
+
47
+ **messageTokens?:** (`number`): Token count of unobserved messages that triggers observation. When unobserved message tokens exceed this threshold, the Observer agent is called. (Default: `30000`)
48
+
49
+ **maxTokensPerBatch?:** (`number`): Maximum tokens per batch when observing multiple threads in resource scope. Threads are chunked into batches of this size and processed in parallel. Lower values mean more parallelism but more API calls. (Default: `10000`)
50
+
51
+ **modelSettings?:** (`ObservationalMemoryModelSettings`): Model settings for the Observer agent. (Default: `{ temperature: 0.3, maxOutputTokens: 100_000 }`)
52
+
53
+ ### Reflection config
54
+
55
+ **model?:** (`string | LanguageModel | DynamicModel | ModelWithRetries[]`): Model for the Reflector agent. Cannot be set if a top-level \`model\` is also provided. (Default: `'google/gemini-2.5-flash'`)
56
+
57
+ **observationTokens?:** (`number`): Token count of observations that triggers reflection. When observation tokens exceed this threshold, the Reflector agent is called to condense them. (Default: `40000`)
58
+
59
+ **modelSettings?:** (`ObservationalMemoryModelSettings`): Model settings for the Reflector agent. (Default: `{ temperature: 0, maxOutputTokens: 100_000 }`)
60
+
61
+ ### Model settings
62
+
63
+ **temperature?:** (`number`): Temperature for generation. Lower values produce more consistent output. (Default: `0.3`)
64
+
65
+ **maxOutputTokens?:** (`number`): Maximum output tokens. Set high to prevent truncation of observations. (Default: `100000`)
66
+
67
+ ## Examples
68
+
69
+ ### Resource scope with custom thresholds
70
+
71
+ ```typescript
72
+ import { Memory } from "@mastra/memory";
73
+ import { Agent } from "@mastra/core/agent";
74
+
75
+ export const agent = new Agent({
76
+ name: "my-agent",
77
+ instructions: "You are a helpful assistant.",
78
+ model: "openai/gpt-5-mini",
79
+ memory: new Memory({
80
+ options: {
81
+ observationalMemory: {
82
+ scope: "resource",
83
+ observation: {
84
+ messageTokens: 20_000,
85
+ },
86
+ reflection: {
87
+ observationTokens: 60_000,
88
+ },
89
+ },
90
+ },
91
+ }),
92
+ });
93
+ ```
94
+
95
+ ### Shared token budget
96
+
97
+ ```typescript
98
+ import { Memory } from "@mastra/memory";
99
+ import { Agent } from "@mastra/core/agent";
100
+
101
+ export const agent = new Agent({
102
+ name: "my-agent",
103
+ instructions: "You are a helpful assistant.",
104
+ model: "openai/gpt-5-mini",
105
+ memory: new Memory({
106
+ options: {
107
+ observationalMemory: {
108
+ shareTokenBudget: true,
109
+ observation: {
110
+ messageTokens: 20_000,
111
+ },
112
+ reflection: {
113
+ observationTokens: 80_000,
114
+ },
115
+ },
116
+ },
117
+ }),
118
+ });
119
+ ```
120
+
121
+ When `shareTokenBudget` is enabled, the total budget is `observation.messageTokens + reflection.observationTokens` (100k in this example). If observations only use 30k tokens, messages can expand to use up to 70k. If messages are short, observations have more room before triggering reflection.
122
+
123
+ ### Custom model
124
+
125
+ ```typescript
126
+ import { Memory } from "@mastra/memory";
127
+ import { Agent } from "@mastra/core/agent";
128
+
129
+ export const agent = new Agent({
130
+ name: "my-agent",
131
+ instructions: "You are a helpful assistant.",
132
+ model: "openai/gpt-5-mini",
133
+ memory: new Memory({
134
+ options: {
135
+ observationalMemory: {
136
+ model: "openai/gpt-4o-mini",
137
+ },
138
+ },
139
+ }),
140
+ });
141
+ ```
142
+
143
+ ### Different models per agent
144
+
145
+ ```typescript
146
+ import { Memory } from "@mastra/memory";
147
+ import { Agent } from "@mastra/core/agent";
148
+
149
+ export const agent = new Agent({
150
+ name: "my-agent",
151
+ instructions: "You are a helpful assistant.",
152
+ model: "openai/gpt-5-mini",
153
+ memory: new Memory({
154
+ options: {
155
+ observationalMemory: {
156
+ observation: {
157
+ model: "google/gemini-2.5-flash",
158
+ },
159
+ reflection: {
160
+ model: "openai/gpt-4o-mini",
161
+ },
162
+ },
163
+ },
164
+ }),
165
+ });
166
+ ```
167
+
168
+ ## Standalone usage
169
+
170
+ Most users should use the `Memory` class above. Using `ObservationalMemory` directly is mainly useful for benchmarking, experimentation, or when you need to control processor ordering with other processors (like [guardrails](https://mastra.ai/docs/agents/guardrails)).
171
+
172
+ ```typescript
173
+ import { ObservationalMemory } from "@mastra/memory/processors";
174
+ import { Agent } from "@mastra/core/agent";
175
+ import { LibSQLStore } from "@mastra/libsql";
176
+
177
+ const storage = new LibSQLStore({
178
+ id: "my-storage",
179
+ url: "file:./memory.db",
180
+ });
181
+
182
+ const om = new ObservationalMemory({
183
+ storage: storage.stores.memory,
184
+ model: "google/gemini-2.5-flash",
185
+ scope: "resource",
186
+ observation: {
187
+ messageTokens: 20_000,
188
+ },
189
+ reflection: {
190
+ observationTokens: 60_000,
191
+ },
192
+ });
193
+
194
+ export const agent = new Agent({
195
+ name: "my-agent",
196
+ instructions: "You are a helpful assistant.",
197
+ model: "openai/gpt-5-mini",
198
+ inputProcessors: [om],
199
+ outputProcessors: [om],
200
+ });
201
+ ```
202
+
203
+ ### Standalone config
204
+
205
+ The standalone `ObservationalMemory` class accepts all the same options as the `observationalMemory` config object above, plus the following:
206
+
207
+ **storage:** (`MemoryStorage`): Storage adapter for persisting observations. Must be a MemoryStorage instance (from \`MastraStorage.stores.memory\`).
208
+
209
+ **onDebugEvent?:** (`(event: ObservationDebugEvent) => void`): Debug callback for observation events. Called whenever observation-related events occur. Useful for debugging and understanding the observation flow.
210
+
211
+ **obscureThreadIds?:** (`boolean`): When enabled, thread IDs are hashed before being included in observation context. This prevents the LLM from recognizing patterns in thread identifiers. Automatically enabled when using resource scope through the Memory class. (Default: `false`)
212
+
213
+ ### Related
214
+
215
+ - [Observational Memory](https://mastra.ai/docs/memory/observational-memory)
216
+ - [Memory Overview](https://mastra.ai/docs/memory/overview)
217
+ - [Memory Class](https://mastra.ai/reference/memory/memory-class)
218
+ - [Memory Processors](https://mastra.ai/docs/memory/memory-processors)
219
+ - [Processors](https://mastra.ai/docs/agents/processors)
@@ -1,13 +1,4 @@
1
- # Processors API Reference
2
-
3
- > API reference for processors - 1 entries
4
-
5
-
6
- ---
7
-
8
- ## Reference: Token Limiter Processor
9
-
10
- > Documentation for the TokenLimiterProcessor in Mastra, which limits the number of tokens in messages.
1
+ # TokenLimiterProcessor
11
2
 
12
3
  The `TokenLimiterProcessor` limits the number of tokens in messages. It can be used as both an input and output processor:
13
4
 
@@ -28,10 +19,32 @@ const processor = new TokenLimiterProcessor({
28
19
 
29
20
  ## Constructor parameters
30
21
 
22
+ **options:** (`number | Options`): Either a simple number for token limit, or configuration options object
23
+
31
24
  ### Options
32
25
 
26
+ **limit:** (`number`): Maximum number of tokens to allow in the response
27
+
28
+ **encoding?:** (`TiktokenBPE`): Optional encoding to use. Defaults to o200k\_base which is used by gpt-5.1
29
+
30
+ **strategy?:** (`'truncate' | 'abort'`): Strategy when token limit is reached: 'truncate' stops emitting chunks, 'abort' calls abort() to stop the stream
31
+
32
+ **countMode?:** (`'cumulative' | 'part'`): Whether to count tokens from the beginning of the stream or just the current part: 'cumulative' counts all tokens from start, 'part' only counts tokens in current part
33
+
33
34
  ## Returns
34
35
 
36
+ **id:** (`string`): Processor identifier set to 'token-limiter'
37
+
38
+ **name?:** (`string`): Optional processor display name
39
+
40
+ **processInput:** (`(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never }) => Promise<MastraDBMessage[]>`): Filters input messages to fit within token limit, prioritizing recent messages while preserving system messages
41
+
42
+ **processOutputStream:** (`(args: { part: ChunkType; streamParts: ChunkType[]; state: Record<string, any>; abort: (reason?: string) => never }) => Promise<ChunkType | null>`): Processes streaming output parts to limit token count during streaming
43
+
44
+ **processOutputResult:** (`(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never }) => Promise<MastraDBMessage[]>`): Processes final output results to limit token count in non-streaming scenarios
45
+
46
+ **getMaxTokens:** (`() => number`): Get the maximum token limit
47
+
35
48
  ## Error behavior
36
49
 
37
50
  When used as an input processor, `TokenLimiterProcessor` throws a `TripWire` error in the following cases:
@@ -57,7 +70,7 @@ try {
57
70
 
58
71
  Use `inputProcessors` to limit historical messages sent to the model, which helps stay within context window limits:
59
72
 
60
- ```typescript title="src/mastra/agents/context-limited-agent.ts"
73
+ ```typescript
61
74
  import { Agent } from "@mastra/core/agent";
62
75
  import { Memory } from "@mastra/memory";
63
76
  import { TokenLimiterProcessor } from "@mastra/core/processors";
@@ -77,7 +90,7 @@ export const agent = new Agent({
77
90
 
78
91
  Use `outputProcessors` to limit the length of generated responses:
79
92
 
80
- ```typescript title="src/mastra/agents/response-limited-agent.ts"
93
+ ```typescript
81
94
  import { Agent } from "@mastra/core/agent";
82
95
  import { TokenLimiterProcessor } from "@mastra/core/processors";
83
96