AutoGen integration
Add long-term agent memory to AutoGen agents
The zep-autogen package integrates Zep with Microsoft AutoGen agents, backing them with long-term memory and a temporal knowledge graph. It provides memory classes that plug into AutoGen’s native Memory interface for automatic context injection, plus function tools the agent can call to search and add data on demand. Choose between user-specific conversation memory or structured knowledge graph memory.
Core benefits
- Native
Memoryinterface:ZepUserMemoryandZepGraphMemoryimplement AutoGen’sMemoryinterface, so they drop straight into an agent’smemorylist - Automatic context injection: Relevant memory is retrieved and prepended to the model context before each turn via
update_context() - User and knowledge graphs: Persist a user’s conversation history or maintain a shared knowledge graph with custom entity models
- On-demand function tools: Pre-built tools let the agent explicitly search and add graph data when it chooses
- Graceful degradation: A Zep failure is logged but does not crash the agent run
How it works
The integration exposes two complementary retrieval paths:
- Memory classes (
ZepUserMemory,ZepGraphMemory) attach to an agent’smemorylist. AutoGen callsupdate_context()before each turn, and the class retrieves memory from Zep and injects it as a system message — transparent, automatic context on every interaction. - Function tools (
create_search_graph_tool,create_add_graph_data_tool) attach to an agent’stoolslist. The model decides when to call them, giving explicit, observable search and add operations that work with AutoGen’s tool reflection.
Both approaches can be combined on the same agent: memory for consistent background context, tools for targeted lookups.
Installation
Requires Python 3.11+, zep-cloud>=3.23.0, autogen-agentchat>=0.7.0, and a Zep Cloud API key. Get your API key from app.getzep.com.
Set up your environment variables:
Memory types
- User memory: Stores conversation history in user threads with automatic context injection
- Knowledge graph memory: Maintains structured knowledge with custom entity models
User memory
ZepUserMemory persists messages to a user’s thread and injects the context block into the agent before each turn. Set up the imports, create the user and thread, initialize the memory, attach it to an agent, then store messages as the conversation proceeds.
Create the user and thread
A user and thread must exist before memory can store messages against them.
Initialize the memory
ZepUserMemory binds the client, user, and thread into a memory object that AutoGen can attach to an agent.
Automatic context injection: ZepUserMemory injects relevant memory via the update_context() method before each turn. It injects the context block, and when one is available also appends up to 10 recent thread messages.
Allow time for indexing — Zep extracts knowledge asynchronously, so facts from a turn are not instantly searchable. Allow time for indexing before querying for newly added content.
Knowledge graph memory
ZepGraphMemory maintains a standalone knowledge graph with custom entity models. Define an ontology, create the graph, initialize the memory with search filters, add data, then attach the memory to an agent.
Define entity models
Custom entity models shape how Zep extracts structured knowledge from the data you add.
Set the ontology and create the graph
Register the entity models as the graph’s ontology, then create the graph that will hold the extracted knowledge.
Initialize the graph memory
Configure search filters and context limits to control what ZepGraphMemory injects on each turn.
Graph memory context injection: ZepGraphMemory automatically retrieves the last 2 episodes from the graph and uses their content to query for relevant facts (up to facts_limit) and entities (up to entity_limit). This context is injected as a system message during agent interactions.
Tools integration
Zep tools let agents search and add data directly to memory storage with manual control and structured responses.
Important: Tools must be bound to either graph_id OR user_id, not both. This determines whether they operate on knowledge graphs or user graphs.
Tool function parameters
Search tool parameters:
query: str (required) - Search query textlimit: int (optional, default 10) - Maximum results to returnscope: str (optional, default “edges”) - Search scope: “edges”, “nodes”, “episodes”
Add tool parameters:
data: str (required) - Content to storedata_type: str (optional, default “text”) - Data type: “text”, “json”, “message”
User graph tools
Knowledge graph tools
Query memory
Both memory types support direct querying with different scope parameters.
User memory queries
Graph memory queries
Search result structure
Edge results (facts)
Node results (entities)
Episode results (messages)
Memory vs tools comparison
Memory objects (ZepUserMemory / ZepGraphMemory):
- Automatic context injection via
update_context() - Attached to the agent’s
memorylist - Transparent operation — happens automatically
- Better for consistent memory across interactions
Function tools (search/add tools):
- Manual control — the agent decides when to use them
- More explicit and observable operations
- Better for specific search/add operations
- Works with AutoGen’s tool reflection features
- Provides structured return values
Note: Both approaches can be combined — use memory for automatic context and tools for explicit operations.
Best practices
- Pick the right memory type — use
ZepUserMemoryfor per-user conversation history andZepGraphMemoryfor a shared knowledge graph - Bind tools to exactly one scope — a search or add tool targets either a
graph_idor auser_id, never both - Combine memory and tools — attach a memory class for automatic context and add function tools for targeted lookups
- Allow time for indexing — Zep extracts knowledge asynchronously, so facts from a turn are not instantly searchable
Next steps
- Explore customizing graph structure for advanced knowledge organization
- Learn about searching the graph and how to tune search
- See code examples for additional patterns