repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | --- |
| #2 | title: Persistent Mastra Agents |
| #3 | description: "Extend Mastra agents with persistent memories powered by Mem0." |
| #4 | --- |
| #5 | |
| #6 | |
| #7 | In this example you'll learn how to use Mem0 to add long-term memory capabilities to [Mastra's agent](https://mastra.ai/) via tool-use. This memory integration can work alongside Mastra's [agent memory features](https://mastra.ai/docs/agents/01-agent-memory). |
| #8 | |
| #9 | You can find the complete example code in the [Mastra repository](https://github.com/mastra-ai/mastra/tree/main/examples/memory-with-mem0). |
| #10 | |
| #11 | ## Overview |
| #12 | |
| #13 | 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. |
| #14 | |
| #15 | ## Installation |
| #16 | |
| #17 | **Install the Integration Package** |
| #18 | |
| #19 | To install the Mem0 integration, run: |
| #20 | |
| #21 | ```bash |
| #22 | npm install @mastra/mem0 |
| #23 | ``` |
| #24 | |
| #25 | **Add the Integration to Your Project** |
| #26 | |
| #27 | Create a new file for your integrations and import the integration: |
| #28 | |
| #29 | ```typescript integrations/index.ts |
| #30 | import { Mem0Integration } from "@mastra/mem0"; |
| #31 | |
| #32 | export const mem0 = new Mem0Integration({ |
| #33 | config: { |
| #34 | apiKey: process.env.MEM0_API_KEY!, |
| #35 | userId: "alice", |
| #36 | }, |
| #37 | }); |
| #38 | ``` |
| #39 | |
| #40 | **Use the Integration in Tools or Workflows** |
| #41 | |
| #42 | You can now use the integration when defining tools for your agents or in workflows. |
| #43 | |
| #44 | ```typescript tools/index.ts |
| #45 | import { createTool } from "@mastra/core"; |
| #46 | import { z } from "zod"; |
| #47 | import { mem0 } from "../integrations"; |
| #48 | |
| #49 | export const mem0RememberTool = createTool({ |
| #50 | id: "Mem0-remember", |
| #51 | description: |
| #52 | "Remember your agent memories that you've previously saved using the Mem0-memorize tool.", |
| #53 | inputSchema: z.object({ |
| #54 | question: z |
| #55 | .string() |
| #56 | .describe("Question used to look up the answer in saved memories."), |
| #57 | }), |
| #58 | outputSchema: z.object({ |
| #59 | answer: z.string().describe("Remembered answer"), |
| #60 | }), |
| #61 | execute: async ({ context }) => { |
| #62 | console.log(`Searching memory "${context.question}"`); |
| #63 | const memory = await mem0.searchMemory(context.question); |
| #64 | console.log(`\nFound memory "${memory}"\n`); |
| #65 | |
| #66 | return { |
| #67 | answer: memory, |
| #68 | }; |
| #69 | }, |
| #70 | }); |
| #71 | |
| #72 | export const mem0MemorizeTool = createTool({ |
| #73 | id: "Mem0-memorize", |
| #74 | description: |
| #75 | "Save information to mem0 so you can remember it later using the Mem0-remember tool.", |
| #76 | inputSchema: z.object({ |
| #77 | statement: z.string().describe("A statement to save into memory"), |
| #78 | }), |
| #79 | execute: async ({ context }) => { |
| #80 | console.log(`\nCreating memory "${context.statement}"\n`); |
| #81 | // to reduce latency memories can be saved async without blocking tool execution |
| #82 | void mem0.createMemory(context.statement).then(() => { |
| #83 | console.log(`\nMemory "${context.statement}" saved.\n`); |
| #84 | }); |
| #85 | return { success: true }; |
| #86 | }, |
| #87 | }); |
| #88 | ``` |
| #89 | |
| #90 | **Create a New Agent** |
| #91 | |
| #92 | ```typescript agents/index.ts |
| #93 | import { openai } from '@ai-sdk/openai'; |
| #94 | import { Agent } from '@mastra/core/agent'; |
| #95 | import { mem0MemorizeTool, mem0RememberTool } from '../tools'; |
| #96 | |
| #97 | export const mem0Agent = new Agent({ |
| #98 | name: 'Mem0 Agent', |
| #99 | instructions: ` |
| #100 | You are a helpful assistant that has the ability to memorize and remember facts using Mem0. |
| #101 | `, |
| #102 | model: openai('gpt-4.1-nano'), |
| #103 | tools: { mem0RememberTool, mem0MemorizeTool }, |
| #104 | }); |
| #105 | ``` |
| #106 | |
| #107 | **Run the Agent** |
| #108 | |
| #109 | ```typescript index.ts |
| #110 | import { Mastra } from '@mastra/core/mastra'; |
| #111 | import { createLogger } from '@mastra/core/logger'; |
| #112 | |
| #113 | import { mem0Agent } from './agents'; |
| #114 | |
| #115 | export const mastra = new Mastra({ |
| #116 | agents: { mem0Agent }, |
| #117 | logger: createLogger({ |
| #118 | name: 'Mastra', |
| #119 | level: 'error', |
| #120 | }), |
| #121 | }); |
| #122 | ``` |
| #123 | |
| #124 | In the example above: |
| #125 | - We import the `@mastra/mem0` integration |
| #126 | - We define two tools that use the Mem0 API client to create new memories and recall previously saved memories |
| #127 | - The tool accepts `question` as an input and returns the memory as a string |
| #128 | |
| #129 | --- |
| #130 | |
| #131 | <CardGroup cols={2}> |
| #132 | <Card title="Partition Memories by Entity" icon="layers" href="/cookbooks/essentials/entity-partitioning-playbook"> |
| #133 | Separate user, agent, and app memories to keep multi-agent flows clean. |
| #134 | </Card> |
| #135 | <Card title="Agents SDK Tool with Mem0" icon="robot" href="/cookbooks/integrations/agents-sdk-tool"> |
| #136 | Explore tool-calling patterns with the OpenAI Agents SDK. |
| #137 | </Card> |
| #138 | </CardGroup> |
| #139 |