withSupermemory wrapper for zero-config setup or processors for fine-grained control.
Migrating to v2 from 1.4.x? Check the migration guide.
@supermemory/tools on npm
Check out the NPM page for more details
Installation
Quick Start
Wrap your agent config withwithSupermemory to add memory capabilities:
Memory saving is enabled by default. Conversations are automatically saved to Supermemory. To disable saving:
How It Works
The Mastra integration uses Mastra’s native Processor interface:- Input Processor - Fetches relevant memories from Supermemory and injects them into the system prompt before the LLM call
- Output Processor - Optionally saves the conversation to Supermemory after generation completes
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
containerTag | string | Required | User/container tag for scoping memories |
customId | string | Required | Groups messages into a single document for contextual memory |
apiKey | string | SUPERMEMORY_API_KEY env | Your Supermemory API key |
baseUrl | string | https://api.supermemory.ai | Custom API endpoint |
mode | "profile" | "query" | "full" | "profile" | Memory search mode |
addMemory | "always" | "never" | "always" | Auto-save conversations |
verbose | boolean | false | Enable debug logging |
promptTemplate | function | - | Custom memory formatting |
Memory Search Modes
Profile Mode (Default) - Retrieves the user’s complete profile without query-based filtering:Custom Prompt Templates
Customize how memories are formatted and injected. The template receivesuserMemories, generalSearchMemories, and searchResults (raw array for filtering by metadata):
Direct Processor Usage
For advanced use cases, use processors directly instead of the wrapper:Input Processor Only
Inject memories without saving conversations:Output Processor Only
Save conversations without memory injection:Both Processors
Use the factory function for shared configuration:Using RequestContext for Dynamic Thread IDs
For server setups where one agent instance handles multiple concurrent conversations, use Mastra’sRequestContext to provide per-request thread IDs. RequestContext takes precedence over the construction-time customId:
Server-side usage: Always use
RequestContext to pass unique conversation IDs per request. Using a fixed customId for all requests will merge conversations from different users.Verbose Logging
Enable detailed logging for debugging:Working with Existing Processors
The wrapper correctly merges with existing processors in the config:API Reference
withSupermemory
Enhances a Mastra agent config with memory capabilities.
config- The Mastra agent configuration objectoptions- Configuration options (includes requiredcontainerTagandcustomId)
createSupermemoryProcessor
Creates an input processor for memory injection.
createSupermemoryOutputProcessor
Creates an output processor for conversation saving.
createSupermemoryProcessors
Creates both processors with shared configuration.
SupermemoryMastraOptions
Environment Variables
Error Handling
Processors gracefully handle errors without breaking the agent:- API errors - Logged and skipped; agent continues without memories
- Missing API key - Throws immediately with helpful error message
Next Steps
Vercel AI SDK
Use with Vercel AI SDK for streamlined development
User Profiles
Learn about user profile management