@mastra/memory 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/{chunk-6PKWQ3GH.js → chunk-HNPAIFCZ.js} +59 -16
  3. package/dist/chunk-HNPAIFCZ.js.map +1 -0
  4. package/dist/{chunk-6XVTMLW4.cjs → chunk-PVFLHAZX.cjs} +59 -16
  5. package/dist/chunk-PVFLHAZX.cjs.map +1 -0
  6. package/dist/docs/SKILL.md +55 -0
  7. package/dist/docs/assets/SOURCE_MAP.json +103 -0
  8. package/dist/docs/references/docs-agents-agent-approval.md +558 -0
  9. package/dist/docs/references/docs-agents-agent-memory.md +209 -0
  10. package/dist/docs/references/docs-agents-network-approval.md +275 -0
  11. package/dist/docs/references/docs-agents-networks.md +299 -0
  12. package/dist/docs/references/docs-agents-supervisor-agents.md +304 -0
  13. package/dist/docs/references/docs-memory-memory-processors.md +314 -0
  14. package/dist/docs/references/docs-memory-message-history.md +260 -0
  15. package/dist/docs/references/docs-memory-observational-memory.md +248 -0
  16. package/dist/docs/references/docs-memory-overview.md +45 -0
  17. package/dist/docs/references/docs-memory-semantic-recall.md +272 -0
  18. package/dist/docs/references/docs-memory-storage.md +261 -0
  19. package/dist/docs/references/docs-memory-working-memory.md +400 -0
  20. package/dist/docs/references/reference-core-getMemory.md +50 -0
  21. package/dist/docs/references/reference-core-listMemory.md +56 -0
  22. package/dist/docs/references/reference-memory-clone-utilities.md +199 -0
  23. package/dist/docs/references/reference-memory-cloneThread.md +130 -0
  24. package/dist/docs/references/reference-memory-createThread.md +68 -0
  25. package/dist/docs/references/reference-memory-getThreadById.md +24 -0
  26. package/dist/docs/references/reference-memory-listThreads.md +145 -0
  27. package/dist/docs/references/reference-memory-memory-class.md +147 -0
  28. package/dist/docs/references/reference-memory-observational-memory.md +565 -0
  29. package/dist/docs/references/reference-processors-token-limiter-processor.md +115 -0
  30. package/dist/docs/references/reference-storage-dynamodb.md +282 -0
  31. package/dist/docs/references/reference-storage-libsql.md +135 -0
  32. package/dist/docs/references/reference-storage-mongodb.md +262 -0
  33. package/dist/docs/references/reference-storage-postgresql.md +526 -0
  34. package/dist/docs/references/reference-storage-upstash.md +160 -0
  35. package/dist/docs/references/reference-vectors-libsql.md +305 -0
  36. package/dist/docs/references/reference-vectors-mongodb.md +295 -0
  37. package/dist/docs/references/reference-vectors-pg.md +408 -0
  38. package/dist/docs/references/reference-vectors-upstash.md +294 -0
  39. package/dist/index.cjs +1 -1
  40. package/dist/index.js +1 -1
  41. package/dist/{observational-memory-AJWSMZVP.js → observational-memory-KAFD4QZK.js} +3 -3
  42. package/dist/{observational-memory-AJWSMZVP.js.map → observational-memory-KAFD4QZK.js.map} +1 -1
  43. package/dist/{observational-memory-Q5TO525O.cjs → observational-memory-Q47HN5YL.cjs} +17 -17
  44. package/dist/{observational-memory-Q5TO525O.cjs.map → observational-memory-Q47HN5YL.cjs.map} +1 -1
  45. package/dist/processors/index.cjs +15 -15
  46. package/dist/processors/index.js +1 -1
  47. package/dist/processors/observational-memory/observational-memory.d.ts +2 -2
  48. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  49. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
  50. package/package.json +8 -8
  51. package/dist/chunk-6PKWQ3GH.js.map +0 -1
  52. package/dist/chunk-6XVTMLW4.cjs.map +0 -1
@@ -0,0 +1,526 @@
1
+ # PostgreSQL Storage
2
+
3
+ The PostgreSQL storage implementation provides a production-ready storage solution using PostgreSQL databases.
4
+
5
+ ## Installation
6
+
7
+ **npm**:
8
+
9
+ ```bash
10
+ npm install @mastra/pg@latest
11
+ ```
12
+
13
+ **pnpm**:
14
+
15
+ ```bash
16
+ pnpm add @mastra/pg@latest
17
+ ```
18
+
19
+ **Yarn**:
20
+
21
+ ```bash
22
+ yarn add @mastra/pg@latest
23
+ ```
24
+
25
+ **Bun**:
26
+
27
+ ```bash
28
+ bun add @mastra/pg@latest
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```typescript
34
+ import { PostgresStore } from '@mastra/pg'
35
+
36
+ const storage = new PostgresStore({
37
+ id: 'pg-storage',
38
+ connectionString: process.env.DATABASE_URL,
39
+ })
40
+ ```
41
+
42
+ ## Parameters
43
+
44
+ **id:** (`string`): Unique identifier for this storage instance.
45
+
46
+ **connectionString?:** (`string`): PostgreSQL connection string (e.g., postgresql://user:pass\@host:5432/dbname). Required unless using \`pool\` or individual host-based parameters (\`host\`, \`port\`, \`database\`, \`user\`, \`password\`).
47
+
48
+ **host?:** (`string`): Database server hostname or IP address. Used with other host-based parameters as an alternative to connectionString.
49
+
50
+ **port?:** (`number`): Database server port number. Defaults to 5432 if not specified.
51
+
52
+ **database?:** (`string`): Name of the database to connect to.
53
+
54
+ **user?:** (`string`): Database user for authentication.
55
+
56
+ **password?:** (`string`): Password for the database user.
57
+
58
+ **pool?:** (`pg.Pool`): Pre-configured pg.Pool instance. Use this to reuse an existing connection pool. When provided, Mastra will not create its own pool and will not close it when \`store.close()\` is called.
59
+
60
+ **schemaName?:** (`string`): The name of the schema you want the storage to use. Defaults to 'public'.
61
+
62
+ **ssl?:** (`boolean | ConnectionOptions`): SSL configuration for the connection; set to true to use default SSL or provide a ConnectionOptions object for custom SSL settings.
63
+
64
+ **max?:** (`number`): Maximum number of connections in the pool. Defaults to 20.
65
+
66
+ **idleTimeoutMillis?:** (`number`): How long a connection can sit idle before being closed. Defaults to 30000 (30 seconds).
67
+
68
+ **disableInit?:** (`boolean`): When true, automatic table creation/migrations are disabled. Useful for CI/CD pipelines where migrations are run separately.
69
+
70
+ **skipDefaultIndexes?:** (`boolean`): When true, default indexes will not be created during initialization.
71
+
72
+ **indexes?:** (`CreateIndexOptions[]`): Custom indexes to create during initialization.
73
+
74
+ ## Constructor Examples
75
+
76
+ You can instantiate `PostgresStore` in the following ways:
77
+
78
+ ```ts
79
+ import { PostgresStore } from '@mastra/pg'
80
+ import { Pool } from 'pg'
81
+
82
+ // Using a connection string
83
+ const store1 = new PostgresStore({
84
+ id: 'pg-storage-1',
85
+ connectionString: 'postgresql://user:password@localhost:5432/mydb',
86
+ })
87
+
88
+ // Using a connection string with pool options
89
+ const store2 = new PostgresStore({
90
+ id: 'pg-storage-2',
91
+ connectionString: 'postgresql://user:password@localhost:5432/mydb',
92
+ schemaName: 'custom_schema',
93
+ max: 30, // Max pool connections
94
+ idleTimeoutMillis: 60000, // Idle timeout
95
+ ssl: { rejectUnauthorized: false },
96
+ })
97
+
98
+ // Using individual connection parameters
99
+ const store3 = new PostgresStore({
100
+ id: 'pg-storage-3',
101
+ host: 'localhost',
102
+ port: 5432,
103
+ database: 'mydb',
104
+ user: 'user',
105
+ password: 'password',
106
+ })
107
+
108
+ // Using a pre-configured pg.Pool (recommended for pool reuse)
109
+ const existingPool = new Pool({
110
+ connectionString: 'postgresql://user:password@localhost:5432/mydb',
111
+ max: 20,
112
+ // ... your custom pool configuration
113
+ })
114
+
115
+ const store4 = new PostgresStore({
116
+ id: 'pg-storage-4',
117
+ pool: existingPool,
118
+ schemaName: 'custom_schema', // optional
119
+ })
120
+ ```
121
+
122
+ ## Additional Notes
123
+
124
+ ### Schema Management
125
+
126
+ The storage implementation handles schema creation and updates automatically. It creates the following tables:
127
+
128
+ - `mastra_workflow_snapshot`: Stores workflow state and execution data
129
+ - `mastra_evals`: Stores evaluation results and metadata
130
+ - `mastra_threads`: Stores conversation threads
131
+ - `mastra_messages`: Stores individual messages
132
+ - `mastra_traces`: Stores telemetry and tracing data
133
+ - `mastra_scorers`: Stores scoring and evaluation data
134
+ - `mastra_resources`: Stores resource working memory data
135
+
136
+ ### Observability
137
+
138
+ PostgreSQL supports observability and can handle low trace volumes. Throughput capacity depends on deployment factors such as hardware, schema design, indexing, and retention policies, and should be validated for your specific environment. For high-volume production environments, consider:
139
+
140
+ - Using the `insert-only` [tracing strategy](https://mastra.ai/docs/observability/tracing/exporters/default) to reduce database write operations
141
+ - Setting up table partitioning for efficient data retention
142
+ - Migrating observability to [ClickHouse via composite storage](https://mastra.ai/reference/storage/composite) if you need to scale further
143
+
144
+ ### Initialization
145
+
146
+ When you pass storage to the Mastra class, `init()` is called automatically before any storage operation:
147
+
148
+ ```typescript
149
+ import { Mastra } from '@mastra/core'
150
+ import { PostgresStore } from '@mastra/pg'
151
+
152
+ const storage = new PostgresStore({
153
+ id: 'pg-storage',
154
+ connectionString: process.env.DATABASE_URL,
155
+ })
156
+
157
+ const mastra = new Mastra({
158
+ storage, // init() is called automatically
159
+ })
160
+ ```
161
+
162
+ If you're using storage directly without Mastra, you must call `init()` explicitly to create the tables:
163
+
164
+ ```typescript
165
+ import { PostgresStore } from '@mastra/pg'
166
+
167
+ const storage = new PostgresStore({
168
+ id: 'pg-storage',
169
+ connectionString: process.env.DATABASE_URL,
170
+ })
171
+
172
+ // Required when using storage directly
173
+ await storage.init()
174
+
175
+ // Access domain-specific stores via getStore()
176
+ const memoryStore = await storage.getStore('memory')
177
+ const thread = await memoryStore?.getThreadById({ threadId: '...' })
178
+ ```
179
+
180
+ > **Warning:** If `init()` is not called, tables won't be created and storage operations will fail silently or throw errors.
181
+
182
+ ### Using an Existing Pool
183
+
184
+ If you already have a `pg.Pool` in your application (e.g., shared with an ORM or for Row Level Security), you can pass it directly to `PostgresStore`:
185
+
186
+ ```typescript
187
+ import { Pool } from 'pg'
188
+ import { PostgresStore } from '@mastra/pg'
189
+
190
+ // Your existing pool (shared across your application)
191
+ const pool = new Pool({
192
+ connectionString: process.env.DATABASE_URL,
193
+ max: 20,
194
+ })
195
+
196
+ const storage = new PostgresStore({
197
+ id: 'shared-storage',
198
+ pool: pool,
199
+ })
200
+ ```
201
+
202
+ **Pool lifecycle behavior:**
203
+
204
+ - When you **provide a pool**: Mastra uses your pool but does **not** close it when `store.close()` is called. You manage the pool lifecycle.
205
+ - When Mastra **creates a pool**: Mastra owns the pool and will close it when `store.close()` is called.
206
+
207
+ ### Direct Database and Pool Access
208
+
209
+ `PostgresStore` exposes the underlying database client and pool for advanced use cases:
210
+
211
+ ```typescript
212
+ store.db // DbClient - query interface with helpers (any, one, tx, etc.)
213
+ store.pool // pg.Pool - the underlying connection pool
214
+ ```
215
+
216
+ **Using `store.db` for queries:**
217
+
218
+ ```typescript
219
+ // Execute queries with helper methods
220
+ const users = await store.db.any('SELECT * FROM users WHERE active = $1', [true])
221
+ const user = await store.db.one('SELECT * FROM users WHERE id = $1', [userId])
222
+ const maybeUser = await store.db.oneOrNone('SELECT * FROM users WHERE email = $1', [email])
223
+
224
+ // Use transactions
225
+ const result = await store.db.tx(async t => {
226
+ await t.none('INSERT INTO logs (message) VALUES ($1)', ['Started'])
227
+ const data = await t.any('SELECT * FROM items')
228
+ return data
229
+ })
230
+ ```
231
+
232
+ **Using `store.pool` directly:**
233
+
234
+ ```typescript
235
+ // Get a client for manual connection management
236
+ const client = await store.pool.connect()
237
+ try {
238
+ await client.query('SET LOCAL app.user_id = $1', [userId])
239
+ const result = await client.query('SELECT * FROM protected_table')
240
+ return result.rows
241
+ } finally {
242
+ client.release()
243
+ }
244
+ ```
245
+
246
+ When using these fields:
247
+
248
+ - You are responsible for proper connection and transaction handling.
249
+ - Closing the store (`store.close()`) will destroy the pool only if Mastra created it.
250
+ - Direct access bypasses any additional logic or validation provided by PostgresStore methods.
251
+
252
+ This approach is intended for advanced scenarios where low-level access is required.
253
+
254
+ ### Using with Next.js
255
+
256
+ When using `PostgresStore` in Next.js applications, [Hot Module Replacement (HMR)](https://nextjs.org/docs/architecture/fast-refresh) during development can cause multiple storage instances to be created, resulting in this warning:
257
+
258
+ ```text
259
+ WARNING: Creating a duplicate database object for the same connection.
260
+ ```
261
+
262
+ To prevent this, store the `PostgresStore` instance on the global object so it persists across HMR reloads:
263
+
264
+ ```typescript
265
+ import { PostgresStore } from '@mastra/pg'
266
+ import { Memory } from '@mastra/memory'
267
+
268
+ // Extend the global type to include our instances
269
+ declare global {
270
+ var pgStore: PostgresStore | undefined
271
+ var memory: Memory | undefined
272
+ }
273
+
274
+ // Get or create the PostgresStore instance
275
+ function getPgStore(): PostgresStore {
276
+ if (!global.pgStore) {
277
+ if (!process.env.DATABASE_URL) {
278
+ throw new Error('DATABASE_URL is not defined in environment variables')
279
+ }
280
+ global.pgStore = new PostgresStore({
281
+ id: 'pg-storage',
282
+ connectionString: process.env.DATABASE_URL,
283
+ ssl: process.env.DATABASE_SSL === 'true' ? { rejectUnauthorized: false } : false,
284
+ })
285
+ }
286
+ return global.pgStore
287
+ }
288
+
289
+ // Get or create the Memory instance
290
+ function getMemory(): Memory {
291
+ if (!global.memory) {
292
+ global.memory = new Memory({
293
+ storage: getPgStore(),
294
+ })
295
+ }
296
+ return global.memory
297
+ }
298
+
299
+ export const storage = getPgStore()
300
+ export const memory = getMemory()
301
+ ```
302
+
303
+ Then use the exported instances in your Mastra configuration:
304
+
305
+ ```typescript
306
+ import { Mastra } from '@mastra/core/mastra'
307
+ import { storage } from './storage'
308
+
309
+ export const mastra = new Mastra({
310
+ storage,
311
+ // ...other config
312
+ })
313
+ ```
314
+
315
+ This pattern ensures only one `PostgresStore` instance is created regardless of how many times the module is reloaded during development. The same pattern can be applied to other storage providers like `LibSQLStore`.
316
+
317
+ > **Tip:** This singleton pattern is only necessary during local development with HMR. In production builds, modules are only loaded once.
318
+
319
+ ## Usage Example
320
+
321
+ ### Adding memory to an agent
322
+
323
+ To add PostgreSQL memory to an agent use the `Memory` class and create a new `storage` key using `PostgresStore`. The `connectionString` can either be a remote location, or a local database connection.
324
+
325
+ ```typescript
326
+ import { Memory } from '@mastra/memory'
327
+ import { Agent } from '@mastra/core/agent'
328
+ import { PostgresStore } from '@mastra/pg'
329
+
330
+ export const pgAgent = new Agent({
331
+ id: 'pg-agent',
332
+ name: 'PG Agent',
333
+ instructions:
334
+ 'You are an AI agent with the ability to automatically recall memories from previous interactions.',
335
+ model: 'openai/gpt-5.1',
336
+ memory: new Memory({
337
+ storage: new PostgresStore({
338
+ id: 'pg-agent-storage',
339
+ connectionString: process.env.DATABASE_URL!,
340
+ }),
341
+ options: {
342
+ generateTitle: true, // Explicitly enable automatic title generation
343
+ },
344
+ }),
345
+ })
346
+ ```
347
+
348
+ ### Using the agent
349
+
350
+ Use `memoryOptions` to scope recall for this request. Set `lastMessages: 5` to limit recency-based recall, and use `semanticRecall` to fetch the `topK: 3` most relevant messages, including `messageRange: 2` neighboring messages for context around each match.
351
+
352
+ ```typescript
353
+ import 'dotenv/config'
354
+
355
+ import { mastra } from './mastra'
356
+
357
+ const threadId = '123'
358
+ const resourceId = 'user-456'
359
+
360
+ const agent = mastra.getAgent('pg-agent')
361
+
362
+ const message = await agent.stream('My name is Mastra', {
363
+ memory: {
364
+ thread: threadId,
365
+ resource: resourceId,
366
+ },
367
+ })
368
+
369
+ await message.textStream.pipeTo(new WritableStream())
370
+
371
+ const stream = await agent.stream("What's my name?", {
372
+ memory: {
373
+ thread: threadId,
374
+ resource: resourceId,
375
+ },
376
+ memoryOptions: {
377
+ lastMessages: 5,
378
+ semanticRecall: {
379
+ topK: 3,
380
+ messageRange: 2,
381
+ },
382
+ },
383
+ })
384
+
385
+ for await (const chunk of stream.textStream) {
386
+ process.stdout.write(chunk)
387
+ }
388
+ ```
389
+
390
+ ## Index Management
391
+
392
+ PostgreSQL storage provides index management to optimize query performance.
393
+
394
+ ### Default Indexes
395
+
396
+ PostgreSQL storage creates composite indexes during initialization for common query patterns:
397
+
398
+ - `mastra_threads_resourceid_createdat_idx`: (resourceId, createdAt DESC)
399
+ - `mastra_messages_thread_id_createdat_idx`: (thread\_id, createdAt DESC)
400
+ - `mastra_ai_spans_traceid_startedat_idx`: (traceId, startedAt DESC)
401
+ - `mastra_ai_spans_parentspanid_startedat_idx`: (parentSpanId, startedAt DESC)
402
+ - `mastra_ai_spans_name_startedat_idx`: (name, startedAt DESC)
403
+ - `mastra_ai_spans_scope_startedat_idx`: (scope, startedAt DESC)
404
+ - `mastra_scores_trace_id_span_id_created_at_idx`: (traceId, spanId, createdAt DESC)
405
+
406
+ These indexes improve performance for filtered queries with sorting, including `dateRange` filters on message queries.
407
+
408
+ ### Configuring Indexes
409
+
410
+ You can control index creation via constructor options:
411
+
412
+ ```typescript
413
+ import { PostgresStore } from '@mastra/pg'
414
+
415
+ // Skip default indexes (manage indexes separately)
416
+ const store = new PostgresStore({
417
+ id: 'pg-storage',
418
+ connectionString: process.env.DATABASE_URL,
419
+ skipDefaultIndexes: true,
420
+ })
421
+
422
+ // Add custom indexes during initialization
423
+ const storeWithCustomIndexes = new PostgresStore({
424
+ id: 'pg-storage',
425
+ connectionString: process.env.DATABASE_URL,
426
+ indexes: [
427
+ {
428
+ name: 'idx_threads_metadata_type',
429
+ table: 'mastra_threads',
430
+ columns: ["metadata->>'type'"],
431
+ },
432
+ {
433
+ name: 'idx_messages_status',
434
+ table: 'mastra_messages',
435
+ columns: ["metadata->>'status'"],
436
+ },
437
+ ],
438
+ })
439
+ ```
440
+
441
+ For advanced index types, you can specify additional options:
442
+
443
+ - `unique: true` for unique constraints
444
+ - `where: 'condition'` for partial indexes
445
+ - `method: 'brin'` for time-series data
446
+ - `storage: { fillfactor: 90 }` for update-heavy tables
447
+ - `concurrent: true` for non-blocking creation (default)
448
+
449
+ ### Index Options
450
+
451
+ **name:** (`string`): Unique name for the index
452
+
453
+ **table:** (`string`): Table name (e.g., 'mastra\_threads')
454
+
455
+ **columns:** (`string[]`): Array of column names with optional sort order (e.g., \['id', 'createdAt DESC'])
456
+
457
+ **unique?:** (`boolean`): Creates a unique constraint index
458
+
459
+ **concurrent?:** (`boolean`): Creates index without locking table (default: true)
460
+
461
+ **where?:** (`string`): Partial index condition (PostgreSQL specific)
462
+
463
+ **method?:** (`'btree' | 'hash' | 'gin' | 'gist' | 'spgist' | 'brin'`): Index method (default: 'btree')
464
+
465
+ **opclass?:** (`string`): Operator class for GIN/GIST indexes
466
+
467
+ **storage?:** (`Record<string, any>`): Storage parameters (e.g., { fillfactor: 90 })
468
+
469
+ **tablespace?:** (`string`): Tablespace name for index placement
470
+
471
+ ### Schema-Specific Indexes
472
+
473
+ When using custom schemas, index names are prefixed with the schema name:
474
+
475
+ ```typescript
476
+ const storage = new PostgresStore({
477
+ id: 'pg-storage',
478
+ connectionString: process.env.DATABASE_URL,
479
+ schemaName: 'custom_schema',
480
+ indexes: [
481
+ {
482
+ name: 'idx_threads_status',
483
+ table: 'mastra_threads',
484
+ columns: ['status'],
485
+ },
486
+ ],
487
+ })
488
+
489
+ // Creates index as: custom_schema_idx_threads_status
490
+ ```
491
+
492
+ ### Managing Indexes via SQL
493
+
494
+ For advanced index management (listing, dropping, analyzing), use direct SQL queries via the `db` accessor:
495
+
496
+ ```typescript
497
+ // List indexes for a table
498
+ const indexes = await storage.db.any(`
499
+ SELECT indexname, indexdef
500
+ FROM pg_indexes
501
+ WHERE tablename = 'mastra_messages'
502
+ `)
503
+
504
+ // Drop an index
505
+ await storage.db.none('DROP INDEX IF EXISTS idx_my_custom_index')
506
+
507
+ // Analyze index usage
508
+ const stats = await storage.db.one(`
509
+ SELECT idx_scan, idx_tup_read
510
+ FROM pg_stat_user_indexes
511
+ WHERE indexrelname = 'mastra_messages_thread_id_createdat_idx'
512
+ `)
513
+ ```
514
+
515
+ ### Index Types and Use Cases
516
+
517
+ PostgreSQL offers different index types optimized for specific scenarios:
518
+
519
+ | Index Type | Best For | Storage | Speed |
520
+ | ------------------- | --------------------------------------- | ---------- | -------------------------- |
521
+ | **btree** (default) | Range queries, sorting, general purpose | Moderate | Fast |
522
+ | **hash** | Equality comparisons only | Small | Very fast for `=` |
523
+ | **gin** | JSONB, arrays, full-text search | Large | Fast for contains |
524
+ | **gist** | Geometric data, full-text search | Moderate | Fast for nearest-neighbor |
525
+ | **spgist** | Non-balanced data, text patterns | Small | Fast for specific patterns |
526
+ | **brin** | Large tables with natural ordering | Very small | Fast for ranges |
@@ -0,0 +1,160 @@
1
+ # Upstash Storage
2
+
3
+ The Upstash storage implementation provides a serverless-friendly storage solution using Upstash's Redis-compatible key-value store.
4
+
5
+ > **Pricing:** When using Mastra with Upstash, the pay-as-you-go model can result in unexpectedly high costs due to the high volume of Redis commands generated during agent conversations. We strongly recommend using a **fixed pricing plan** for predictable costs. See [Upstash pricing](https://upstash.com/pricing/redis) for details and [GitHub issue #5850](https://github.com/mastra-ai/mastra/issues/5850) for context.
6
+
7
+ > **Observability Not Supported:** Upstash storage **does not support the observability domain**. Traces from the `DefaultExporter` cannot be persisted to Upstash, and Mastra Studio's observability features won't work with Upstash as your only storage provider. To enable observability, use [composite storage](https://mastra.ai/reference/storage/composite) to route observability data to a supported provider like ClickHouse or PostgreSQL.
8
+
9
+ ## Installation
10
+
11
+ **npm**:
12
+
13
+ ```bash
14
+ npm install @mastra/upstash@latest
15
+ ```
16
+
17
+ **pnpm**:
18
+
19
+ ```bash
20
+ pnpm add @mastra/upstash@latest
21
+ ```
22
+
23
+ **Yarn**:
24
+
25
+ ```bash
26
+ yarn add @mastra/upstash@latest
27
+ ```
28
+
29
+ **Bun**:
30
+
31
+ ```bash
32
+ bun add @mastra/upstash@latest
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```typescript
38
+ import { UpstashStore } from '@mastra/upstash'
39
+
40
+ const storage = new UpstashStore({
41
+ id: 'upstash-storage',
42
+ url: process.env.UPSTASH_URL,
43
+ token: process.env.UPSTASH_TOKEN,
44
+ })
45
+ ```
46
+
47
+ ## Parameters
48
+
49
+ **url:** (`string`): Upstash Redis URL
50
+
51
+ **token:** (`string`): Upstash Redis authentication token
52
+
53
+ **prefix?:** (`string`): Key prefix for all stored items (Default: `mastra:`)
54
+
55
+ ## Additional Notes
56
+
57
+ ### Key Structure
58
+
59
+ The Upstash storage implementation uses a key-value structure:
60
+
61
+ - Thread keys: `{prefix}thread:{threadId}`
62
+ - Message keys: `{prefix}message:{messageId}`
63
+ - Metadata keys: `{prefix}metadata:{entityId}`
64
+
65
+ ### Serverless Benefits
66
+
67
+ Upstash storage is particularly well-suited for serverless deployments:
68
+
69
+ - No connection management needed
70
+ - Pay-per-request pricing
71
+ - Global replication options
72
+ - Edge-compatible
73
+
74
+ ### Data Persistence
75
+
76
+ Upstash provides:
77
+
78
+ - Automatic data persistence
79
+ - Point-in-time recovery
80
+ - Cross-region replication options
81
+
82
+ ### Performance Considerations
83
+
84
+ For optimal performance:
85
+
86
+ - Use appropriate key prefixes to organize data
87
+ - Monitor Redis memory usage
88
+ - Consider data expiration policies if needed
89
+
90
+ ## Usage Example
91
+
92
+ ### Adding memory to an agent
93
+
94
+ To add Upstash memory to an agent use the `Memory` class and create a new `storage` key using `UpstashStore` and a new `vector` key using `UpstashVector`. The configuration can point to either a remote service or a local setup.
95
+
96
+ ```typescript
97
+ import { Memory } from '@mastra/memory'
98
+ import { Agent } from '@mastra/core/agent'
99
+ import { UpstashStore } from '@mastra/upstash'
100
+
101
+ export const upstashAgent = new Agent({
102
+ id: 'upstash-agent',
103
+ name: 'Upstash Agent',
104
+ instructions:
105
+ 'You are an AI agent with the ability to automatically recall memories from previous interactions.',
106
+ model: 'openai/gpt-5.1',
107
+ memory: new Memory({
108
+ storage: new UpstashStore({
109
+ id: 'upstash-agent-storage',
110
+ url: process.env.UPSTASH_REDIS_REST_URL!,
111
+ token: process.env.UPSTASH_REDIS_REST_TOKEN!,
112
+ }),
113
+ options: {
114
+ generateTitle: true, // Explicitly enable automatic title generation
115
+ },
116
+ }),
117
+ })
118
+ ```
119
+
120
+ ### Using the agent
121
+
122
+ Use `memoryOptions` to scope recall for this request. Set `lastMessages: 5` to limit recency-based recall, and use `semanticRecall` to fetch the `topK: 3` most relevant messages, including `messageRange: 2` neighboring messages for context around each match.
123
+
124
+ ```typescript
125
+ import 'dotenv/config'
126
+
127
+ import { mastra } from './mastra'
128
+
129
+ const threadId = '123'
130
+ const resourceId = 'user-456'
131
+
132
+ const agent = mastra.getAgent('upstashAgent')
133
+
134
+ const message = await agent.stream('My name is Mastra', {
135
+ memory: {
136
+ thread: threadId,
137
+ resource: resourceId,
138
+ },
139
+ })
140
+
141
+ await message.textStream.pipeTo(new WritableStream())
142
+
143
+ const stream = await agent.stream("What's my name?", {
144
+ memory: {
145
+ thread: threadId,
146
+ resource: resourceId,
147
+ },
148
+ memoryOptions: {
149
+ lastMessages: 5,
150
+ semanticRecall: {
151
+ topK: 3,
152
+ messageRange: 2,
153
+ },
154
+ },
155
+ })
156
+
157
+ for await (const chunk of stream.textStream) {
158
+ process.stdout.write(chunk)
159
+ }
160
+ ```