Agent Memory
Get started with the example in the video using:
This guide shows you how to give your agent persistent memory of user interactions through Zep’s Agent Memory capabilities. Agent Memory allows you to systematically assemble personalized context—user preferences, traits, and business data—for reliable agent applications.
Looking for a more in-depth understanding? Check out our Key Concepts page.
Migrating from Mem0? Check out our Mem0 Migration guide.
Install the Zep SDK
Python
TypeScript
Go
Set up your Python project, ideally with a virtual environment, and then:
Initialize the Zep client
After creating a Zep account, obtaining an API key, and setting the API key as an environment variable, initialize the client once at application startup and reuse it throughout your application.
Initialize Zep client
.env
Create a Zep user for each of your users
Whenever users are created in your application, you need to trigger the creation of a Zep user. Make sure to include at least their first name, and ideally also their last name and email to ensure correct identification of the user in future messages. We recommend setting the Zep user ID equal to your internal user ID.
Backfilling existing users: For existing users, you will need to run a one-time migration to create a user for each of the existing users (simply loop through and call user.add for each).
Provide at least the first name and ideally the last name when calling user.add to ensure Zep correctly associates the user with references in your data. If needed, add this information later using the update user method.
Create a Zep thread for each of your threads
Whenever a user starts a new conversation with your agent, you need to trigger the creation of a Zep thread. Learn more about adding messages.
Backfilling prior conversations: For prior conversations, you will need to run a one-time migration to create Zep threads for those conversations and add the prior messages to the respective Zep threads. You can loop through messages and call thread.add_messages for each, or use our batch processing method for faster concurrent processing.
Add incoming user messages to Zep
When a new user message comes in, add the user message to Zep, providing the user’s name in the message if possible.
It is important to provide the name of the user in the name field if possible, to help with graph construction.
Retrieve Zep context block
After adding the user message to the thread and before generating the AI response, retrieve the Zep context block, which will contain the most relevant information to the user’s message from the user’s knowledge graph.
Use the default context block
Zep’s default Context Block is an optimized, automatically assembled string that combines semantic search, full text search, and breadth first search to return context that is highly relevant to the user’s current conversation slice, utilizing the past two messages.
The Context Block provides low latency (P95 < 200ms) while preserving detailed information from the user’s graph.
The Context Block includes a user summary and relevant facts:
Use a custom context block
Using custom context templates, you can easily design your own custom context block type and retrieve that from the thread.get_user_context() method instead.
Create your custom context template
Create your custom context template for your Zep project and save the template ID. See the Context Templates guide for more information on template syntax and variables.
Retrieve custom context block using thread.get_user_context()
Retrieve your custom context block using the thread.get_user_context() method, passing in your template ID.
Add context block to agent context window
Once you’ve retrieved the Context Block, you can include this string in your agent’s context window.
Option 1: Add context block to system prompt
You can append the context block directly to your system prompt. Note that this means the system prompt dynamically updates on every chat turn.
Option 2: Append context block as “context message”
Dynamically updating the system prompt on every chat turn has the downside of preventing prompt caching with LLM providers. In order to reap the benefits of prompt caching while still adding a new Zep context block in every chat, you can append the context block as a “context message” (technically a tool message) just after the user message in the chat history. On each new chat turn, remove the prior context message and replace it with the new one. This allows everything before the context message to be cached.
Add assistant response to Zep
After generating the assistant response, add it to Zep to continue building the user’s knowledge graph.
Next steps
Now that you’ve learned how to give your agent memory, you can explore additional features:
- Customize graph structure to your domain - Define custom entity and edge types to structure domain-specific information.
- Add user interactions and metadata - Any ongoing user interactions or one-time user profile information can be added to the user’s knowledge graph.
- Custom context templates - Design custom context block formats tailored to your application’s needs.
- User summary instructions - Customize how Zep generates summaries of user data in their knowledge graph.