Add persistent memory to Mastra AI agents with Supermemory processors
Integrate Supermemory with Mastra to give your AI agents persistent memory. Use the withSupermemory wrapper for zero-config setup or processors for fine-grained control.
Wrap your agent config with withSupermemory to add memory capabilities:
Copy
Ask AI
import { Agent } from "@mastra/core/agent"import { withSupermemory } from "@supermemory/tools/mastra"import { openai } from "@ai-sdk/openai"// Create agent with memory-enhanced configconst agent = new Agent(withSupermemory( { id: "my-assistant", name: "My Assistant", model: openai("gpt-4o"), instructions: "You are a helpful assistant.", }, "user-123", // containerTag - scopes memories to this user { mode: "full", addMemory: "always", threadId: "conv-456", }))const response = await agent.generate("What do you know about me?")
Memory saving is disabled by default. The wrapper only retrieves existing memories. To automatically save conversations:
Profile Mode (Default) - Retrieves the user’s complete profile without query-based filtering:
Copy
Ask AI
const agent = new Agent(withSupermemory(config, "user-123", { mode: "profile" }))
Query Mode - Searches memories based on the user’s message:
Copy
Ask AI
const agent = new Agent(withSupermemory(config, "user-123", { mode: "query" }))
Full Mode - Combines profile AND query-based search for maximum context:
Copy
Ask AI
const agent = new Agent(withSupermemory(config, "user-123", { mode: "full" }))### Mode Comparison| Mode | Description | Use Case ||------|-------------|----------|| `profile` | Static + dynamic user facts | General personalization || `query` | Semantic search on user message | Specific Q&A || `full` | Both profile and search | Chatbots, assistants |---## Saving ConversationsEnable automatic conversation saving with `addMemory: "always"`. A `threadId` is required to group messages:```typescriptconst agent = new Agent(withSupermemory( { id: "my-assistant", model: openai("gpt-4o"), instructions: "..." }, "user-123", { addMemory: "always", threadId: "conv-456", }))// All messages in this conversation are savedawait agent.generate("I prefer TypeScript over JavaScript")await agent.generate("My favorite framework is Next.js")
Without a threadId, the output processor will log a warning and skip saving. Always provide a threadId when using addMemory: "always".