Skip to main content
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.
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 with withSupermemory 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:
  1. Input Processor - Fetches relevant memories from Supermemory and injects them into the system prompt before the LLM call
  2. Output Processor - Optionally saves the conversation to Supermemory after generation completes

Configuration Options

OptionTypeDefaultDescription
containerTagstringRequiredUser/container tag for scoping memories
customIdstringRequiredGroups messages into a single document for contextual memory
apiKeystringSUPERMEMORY_API_KEY envYour Supermemory API key
baseUrlstringhttps://api.supermemory.aiCustom API endpoint
mode"profile" | "query" | "full""profile"Memory search mode
addMemory"always" | "never""always"Auto-save conversations
verbosebooleanfalseEnable debug logging
promptTemplatefunction-Custom memory formatting

Memory Search Modes

Profile Mode (Default) - Retrieves the user’s complete profile without query-based filtering:
Query Mode - Searches memories based on the user’s message:
Full Mode - Combines profile AND query-based search for maximum context:
To disable automatic saving:

Custom Prompt Templates

Customize how memories are formatted and injected. The template receives userMemories, 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’s RequestContext 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.
Parameters:
  • config - The Mastra agent configuration object
  • options - Configuration options (includes required containerTag and customId)
Returns: Enhanced config with Supermemory processors injected

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