Add long‑term memory to a Mastra agent (input/output processors, per‑user recall)
Description and use
Integrating MemorySync into a Mastra agent enables durable per‑user, per‑thread memory by injecting recalled facts before model calls and extracting new facts after responses via input and output processors.
Problem
Integrate durable per‑user memory into a Mastra agent so that facts are extracted and recalled across sessions without storing assistant replies.
Outcome
The agent now recalls durable facts per user and thread, extracts new facts after each response, and continues to function with existing Mastra memory. Recall is injected once per generate/stream call, and failures are logged via onError.
Workflow
- Install memorysync‑mastra via npm
- Create inputProcessor and outputProcessor with createMemorySyncProcessors, passing userId and sessionId
- Instantiate Agent with inputProcessors and outputProcessors
- Resolve user identity per call using createMemorySyncProcessors with resolveIdentity or static ids or RequestContext
- Generate agent calls; MemorySync injects recall and extracts facts
- Optional: use withMemorySync wrapper to auto‑merge processors
- Handle streaming by passing stream option to agent.generate
Evidence
Mastra agents already have memory. Give an agent a Memory from @mastra/memory and pass memory: { resource, thread } on each call
If that's working for you, keep it. This post is for when you want durable facts about each user extracted and recalled by a hosted service, wired in through Mastra's own processor pipeline.
Create inputProcessor and outputProcessor with createMemorySyncProcessors, passing userId and sessionId
Prefer less wiring? withMemorySync(config, { userId }) wraps your agent config and merges both processors in, keeping any processors you already have
- Record date: 2026-10-24T17:26:12.048Z