Search...
Ctrl KAsk AI
Search...
Navigation
Integrations & Platforms
Persistent Mastra Agents
Welcome Mem0 Platform Open Source Cookbooks Integrations API Reference Release Notes
Getting Started
Essentials
- Build a Companion with Mem0
- Partition Memories by Entity
- Control Memory Ingestion
- Set Memory Expiration
- Tag and Organize Memories
- Export Stored Memories
- Choose Vector vs Graph Memory
Companion Playbooks
- Interactive Memory Demo
- Build a Node.js Companion
- Personalized AI Tutor
- Smart Travel Assistant
- Research Assistant for YouTube
- Voice-First AI Companion
- Self-Hosted AI Companion
Ops & Automations
- Memory-Powered Support Agent
- Automated Email Intelligence
- Content Creation Workflow
- Multi-Session Research Agent
- Collaborative Task Assistant
Integrations & Platforms
- Memory-Powered Agent SDK
- Memory as OpenAI Tool
- Persistent Mastra Agents
- Healthcare Coach with ADK
- Bedrock with Persistent Memory
- Graph Memory on Neptune
- Search with Personal Context
Frameworks & Multimodal
- ReAct Agents with Memory
- Multi-Agent Collaboration
- Visual Memory Retrieval
- Persistent Eliza Characters
- Browser Extension Memory
- Gemini 3 with Mem0 MCP
- MiroFish Swarm Memory
On this page
In this example you’ll learn how to use Mem0 to add long-term memory capabilities to Mastra’s agent via tool-use. This memory integration can work alongside Mastra’s agent memory features.You can find the complete example code in the Mastra repository.
Overview
This guide will show you how to integrate Mem0 with Mastra to add long-term memory capabilities to your agents. We’ll create tools that allow agents to save and retrieve memories using Mem0’s API.
Installation
Install the Integration PackageTo install the Mem0 integration, run:
npm install @mastra/mem0Add the Integration to Your ProjectCreate a new file for your integrations and import the integration:
integrations/index.ts
import { Mem0Integration } from "@mastra/mem0";
export const mem0 = new Mem0Integration({
config: {
apiKey: process.env.MEM0_API_KEY!,
userId: "alice",
},
});Use the Integration in Tools or WorkflowsYou can now use the integration when defining tools for your agents or in workflows.
tools/index.ts
import { createTool } from "@mastra/core";
import { z } from "zod";
import { mem0 } from "../integrations";
export const mem0RememberTool = createTool({
id: "Mem0-remember",
description:
"Remember your agent memories that you've previously saved using the Mem0-memorize tool.",
inputSchema: z.object({
question: z
.string()
.describe("Question used to look up the answer in saved memories."),
}),
outputSchema: z.object({
answer: z.string().describe("Remembered answer"),
}),
execute: async ({ context }) => {
console.log(`Searching memory "${context.question}"`);
const memory = await mem0.searchMemory(context.question);
console.log(`\nFound memory "${memory}"\n`);
return {
answer: memory,
};
},
});
export const mem0MemorizeTool = createTool({
id: "Mem0-memorize",
description:
"Save information to mem0 so you can remember it later using the Mem0-remember tool.",
inputSchema: z.object({
statement: z.string().describe("A statement to save into memory"),
}),
execute: async ({ context }) => {
console.log(`\nCreating memory "${context.statement}"\n`);
// to reduce latency memories can be saved async without blocking tool execution
void mem0.createMemory(context.statement).then(() => {
console.log(`\nMemory "${context.statement}" saved.\n`);
});
return { success: true };
},
});Create a New Agent
agents/index.ts
import { openai } from '@ai-sdk/openai';
import { Agent } from '@mastra/core/agent';
import { mem0MemorizeTool, mem0RememberTool } from '../tools';
export const mem0Agent = new Agent({
name: 'Mem0 Agent',
instructions: `
You are a helpful assistant that has the ability to memorize and remember facts using Mem0.
`,
model: openai('gpt-4.1-nano'),
tools: { mem0RememberTool, mem0MemorizeTool },
});Run the Agent
index.ts
import { Mastra } from '@mastra/core/mastra';
import { createLogger } from '@mastra/core/logger';
import { mem0Agent } from './agents';
export const mastra = new Mastra({
agents: { mem0Agent },
logger: createLogger({
name: 'Mastra',
level: 'error',
}),
});In the example above:
- We import the
@mastra/mem0integration - We define two tools that use the Mem0 API client to create new memories and recall previously saved memories
- The tool accepts
questionas an input and returns the memory as a string
Agents SDK Tool with Mem0 \ \ Explore tool-calling patterns with the OpenAI Agents SDK.
Was this page helpful?
YesNo
Memory as OpenAI Tool\ \ Previous Healthcare Coach with ADK\ \ Next
Ctrl+I
Assistant
Responses are generated using AI and may contain mistakes.
Suggestions
How do I retrieve saved memories?What tools can agents use for memory?How do I add Mem0 to Mastra?
