AG2 integration
AG2 agents using Zep maintain context across conversations and access a temporal knowledge graph built from prior turns. The zep-ag2 package injects relevant context into an agent’s system message and exposes Zep search and data tools that AG2 calls during a conversation.
Core benefits
- Persistent memory: Conversations and extracted knowledge persist across sessions
- System message injection: Relevant context is added to an agent’s system message before it responds
- Knowledge graph access: Search and write to Zep’s temporal knowledge graph from AG2 agents
- Tool-based access: Register Zep search and add operations as AG2 tools the agent invokes on demand
How it works
The integration provides two ways to give an AG2 agent memory:
- System message injection —
ZepMemoryManager(conversation memory) andZepGraphMemoryManager(knowledge graph) fetch a relevant context block from Zep and enrich an agent’s system message before the model runs. - Tools — factory functions return AG2-compatible tools the model can call mid-conversation to search memory or write new data. Tools execute synchronously (AG2’s execution model) while bridging to the async Zep SDK internally, so you pass an
AsyncZepclient.
Both approaches can be combined: inject context automatically and let the agent search or store explicitly when needed.
Installation
Requires Python 3.11+, ag2>=0.9.0, and a Zep Cloud API key. Get your API key from app.getzep.com.
Set up your environment variables:
System message injection
Use ZepMemoryManager to enrich an agent’s system message with relevant conversation context before it responds:
ZepMemoryManager also exposes get_memory_context() to retrieve the formatted context string directly, add_messages() to persist conversation turns, and get_session_facts() to read the thread’s context block.
Tool integration
Register Zep operations as AG2 tools so the agent can search memory or write new data during a conversation. register_all_tools wires up the full set in one call, or use the individual factories for finer control:
Available tool factories:
create_search_memory_tool(client, user_id, session_id=None)— searches the user’s graphcreate_add_memory_tool(client, user_id, session_id=None)— routes to the thread when asession_idis set, otherwise writes to the user’s graphcreate_search_graph_tool(client, user_id=None, graph_id=None)— search the knowledge graphcreate_add_graph_data_tool(client, user_id=None, graph_id=None)— add data to the knowledge graphregister_all_tools(agent, executor, client, user_id, ...)— register all tools at once
Graph tools are bound to either a user_id (the user’s personal graph) or a graph_id (a shared standalone graph), not both.
Knowledge graph memory
Use ZepGraphMemoryManager to work with a shared knowledge graph that multiple agents can read and write:
Query memory
You can read memory directly, outside of agent tool calls:
Search result structure
The tool factories return human-readable strings formatted for the model. ZepGraphMemoryManager.search() returns a list of structured result dicts for programmatic use; the fields depend on the scope:
Memory vs tools
The integration supports two complementary patterns that work together on the same agent:
Use injection for consistent baseline context and tools for explicit, on-demand lookups and writes.
Configuration options
ZepMemoryManager
ZepMemoryManager(client, user_id, session_id=None)— initialize with a Zep client and user identityenrich_system_message(agent, query=None, limit=5)— inject memory context into an agentget_memory_context(query=None, limit=5)— return the formatted context stringadd_messages(messages)— store messages in the Zep threadget_session_facts()— read the thread’s context block
ZepGraphMemoryManager
ZepGraphMemoryManager(client, graph_id)— initialize with a graph IDsearch(query, limit=5, scope="edges")— search the graph (scope:edges,nodes,episodes)add_data(data, data_type="text")— add data to the graph (data_type:text,json,message)enrich_system_message(agent, query=None, limit=5)— inject graph context into an agent
Best practices
- Pass an
AsyncZepclient — tools bridge to it on a shared background event loop, so reuse a single instance - Bind tools to one target — a
user_idfor personal memory or agraph_idfor shared knowledge, never both - Combine injection and tools — inject context for consistent grounding, add tools for explicit lookups and writes
- Allow time for indexing — Zep extracts knowledge asynchronously, so data added during a turn is 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