Skip to main content
The Mastra integration demonstrates how to use Mastra’s agent system with Mem0 as the memory backend through custom tools. This enables agents to remember and recall information across conversations.

Overview

In this guide, we’ll create a Mastra agent that:
  1. Uses Mem0 to store information using a memory tool
  2. Retrieves relevant memories using a search tool
  3. Provides personalized responses based on past interactions
  4. Maintains context across conversations and sessions

Setup and Configuration

Install the required libraries:
Set up your environment variables:
Remember to get the Mem0 API key from Mem0 Platform.

Initialize Mem0 Integration

Import required modules and set up the Mem0 integration:

Create Memory Tools

Set up tools for memorizing and remembering information:

Create Mastra Agent

Initialize an agent with memory tools and clear instructions:

Key Features

  1. Tool-based Memory Control: The agent decides when to save and retrieve information using specific tools
  2. Semantic Search: Mem0 finds relevant memories based on semantic similarity, not just exact matches
  3. User-specific Memory Spaces: Each user_id maintains separate memory contexts
  4. Asynchronous Saving: Memories are saved in the background to reduce response latency
  5. Cross-conversation Persistence: Memories persist across different conversation threads
  6. Transparent Operations: Memory operations are visible through tool usage

Conclusion

By integrating Mastra with Mem0, you can build intelligent agents that learn and remember information across conversations. The tool-based approach provides transparency and control over memory operations, making it easy to create personalized and context-aware AI experiences.

Mastra Agent Cookbook

Build a complete Mastra agent with persistent memory

Vercel AI SDK Integration

Create web applications with Vercel AI SDK
Enjoying Mem0? Star us on GitHub — it takes two seconds and helps more developers discover open-source memory.