Memory
Learn how to give your agents long-term, reflective memory using Mem0.
Overview
AgentKit allows you to equip your agents with long-term memory, enabling them to recall past interactions, learn user preferences, and maintain context across conversations. By integrating with Mem0, you can build sophisticated agents that offer personalized and context-aware experiences.
A key advantage of combining Mem0 with AgentKit is the power of Inngest for handling memory operations. When an agent needs to create, update, or delete a memory, it can send an event to Inngest for durable background processing. This means:
Faster Responses
Your agent can respond to the user immediately, without waiting for database writes to complete.
Durable Background Processing
The memory operation runs reliably in the background as a separate, durable Inngest function. If it fails, Inngest automatically retries it.
Memory Tools
To empower your agent with memory, you need to provide it with tools. How you design these tools can significantly impact your agent’s behavior, performance, and reliability. AgentKit supports multiple patterns for memory tools, allowing you to choose the best fit for your use case.
The core idea is to abstract memory operations (create, read, update, delete) into tools that an agent can call. These tools can then use Inngest to perform the actual database writes asynchronously, ensuring the agent remains responsive.
Let’s explore two common patterns for designing and integrating these tools into agents.
Pattern 1: Granular, Single-Purpose Tools
This pattern involves creating a distinct tool for each memory operation:
create_memories
: Adds new information.recall_memories
: Retrieves existing information.update_memories
: Corrects or changes existing information.delete_memories
: Removes information.
This gives the agent fine-grained control, but requires it to make more decisions and more tool calls.
Here’s how you might define the recall_memories
and create_memories
tools:
This approach is used in the Autonomous Agent pattern described below, where a single, powerful LLM is prompted to reason about which of the specific tools to use at each turn.
Pattern 2: Consolidated Tools
This pattern simplifies the agent’s job by consolidating write operations into a single tool.
recall_memories
: Same as above, for reading.manage_memories
: A single tool that handles creating, updating, and deleting memories in one atomic action.
This reduces the number of tools the agent needs to know about and can make its behavior more predictable. It’s particularly effective in structured, multi-agent workflows.
The manage_memories
tool can accept lists of creations, updates, and deletions, and then send corresponding events to Inngest.
This consolidated manage_memories
tool is a perfect fit for a multi-agent network, where a dedicated “Memory Updater” agent has the single, clear responsibility of calling this tool at the end of a conversation - only running once with a tool that can emit many events / memory operations.
Deterministic vs Non-Deterministic Memory
There are two primary patterns for integrating memory into your agents:
- Autonmous Agent w/ Tools (Non-Deterministic): A single, powerful agent is given memory-related tools and decides for itself when and how to use them based on its system prompt and the conversation. This approach offers maximum flexibility and autonomy.
- Multi-Agent or Lifecycle-based (Deterministic): The process is broken down into a structured sequence of specialized agents (e.g., one for retrieval, one for responding, one for updating memory), orchestrated by a code-based router. This approach provides predictability and control.
Let’s explore both!
Pattern 1: Autonomous Agent with Memory Tools
In this setup, a single agent is responsible for all tasks. Its system prompt instructs it to follow a recall-reflect-respond process. The agent uses its own reasoning (powered by the LLM) to decide which memory tool to use, making the flow non-deterministic.
Example Agent
Here is an agent designed to manage its own memory. Note the detailed system prompt guiding its behavior.
Execution Flow
The agent’s internal monologue drives the process, deciding which tools to call in sequence.
Pros:
-
Flexibility & Autonomy: The agent can handle unforeseen scenarios by reasoning about which tools to use.
-
Simpler Setup: Requires only one agent and a comprehensive prompt.
Cons:
-
Unpredictability: The agent’s behavior can be inconsistent. It might get stuck in loops, call tools in the wrong order, or fail to answer the user’s question directly.
-
Complex Prompting: The system prompt must be carefully engineered to cover all cases, which can be brittle and hard to maintain.
Pattern 2: Multi-Agent Network for Memory
To address the unpredictability of a single autonomous agent, you can use a deterministic, multi-agent network. The workflow is broken down into a sequence of specialized agents orchestrated by a code-based router.
Example Agents & Router
The process is divided into three distinct steps, each handled by a dedicated agent:
- Memory Retrieval Agent: Its sole job is to use the
recall_memories
tool. - Personal Assistant Agent: Has no tools. Its only job is to synthesize the final answer for the user based on the retrieved memories and history.
- Memory Updater Agent: Reviews the entire conversation and uses a
manage_memories
tool to perform all necessary creations, updates, and deletions in one go.
Execution Flow
The router guarantees a predictable, step-by-step execution path.
Pros:
-
Predictability & Control: The workflow is explicit and reliable. Each agent has a single, well-defined responsibility.
-
Maintainability: It’s easier to debug and modify a specific part of the process without affecting the others.
Cons:
-
More Boilerplate: Requires defining multiple agents and a router, which can be more verbose for simple use cases.
-
Less Flexible: The rigid structure may not adapt as well to unexpected conversational turns compared to an autonomous agent which can determine on its own - when memories should be retrieved.
Advanced Patterns
State-Based Memory Retrieval / Routing
Instead of callCount
, you can use the network state to create more flexible and explicit routing logic. This is powerful when different agents have different memory needs.
Lifecycle Integration
For a more seamless approach, you can integrate memory operations directly into an agent’s or network’s lifecycle hooks, avoiding the need for explicit memory tools.
onStart
: Fetch memories before an agent runs and inject them into the prompt.onFinish
: Analyze the conversation after an agent has run and schedule memory updates.
Complete Example
Check out the Mem0 Memory Example for a complete implementation featuring:
- Both single-agent and multi-agent patterns.
- Asynchronous memory operations with Inngest.
- A local Qdrant vector store setup with Docker.