Give Your Agent Domain Knowledge
This guide shows you how to build a searchable Context Graph from text, JSON, or messages. The data can include email, chat messages, transcripts, document chunks, and inventory records.
Zep updates the graph as new data arrives. The Graph RAG comparison explains how this differs from static retrieval-augmented generation (RAG).
Looking for a more in-depth understanding? Check out our Key Concepts page.
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 graph
Before adding data, you need to create a standalone graph. This gives you an independent knowledge graph that isn’t tied to individual users—useful for shared knowledge bases, domain-specific graphs, or specialized use cases.
Before adding streaming data, consider seeding the graph with the core facts about its subject (for example, a company’s name and industry) so later data attaches to a well-formed subject. See Seed the graph.
Add streaming data to Zep
Zep builds Context Graphs from data that changes over time. You can add text, JSON, or message data. Common examples include:
- Customer support conversations (emails, chat logs, Slack messages)
- Meeting transcripts and notes
- Chunked documents and knowledge base articles
- Inventory data and business records (JSON format)
- Any ongoing communication or evolving business data
Zep tracks relationships and facts that change over time. You can also add static documents.
One-time data uploads: If you have existing data to backfill (such as a set of documents or historical data), zep-ingest is the recommended path. It loads your sources, prepares them, submits them in order, and monitors processing. You can also loop through your data calling graph.add for each item, or drive the Batch API yourself for large imports.
Zep supports three data types when adding data to a graph:
Message data
Use message data for communications with designated speakers, such as email or chat logs. Read Adding business data for details.
Text data
Use text data for text without speaker attribution, such as internal documents or wiki articles. Read Adding business data for details.
When you split a source into chunks, pass a document_id on every add. This value lets extraction resolve references against earlier chunks. document_id is available in the pre-release v4 SDKs. The current v3 SDKs do not include this field.
JSON data
Use JSON data for structured business data, REST API responses, or JSON records. Read Adding business data for details.
Retrieve Zep context block
After adding data to your knowledge graph and before generating the AI response, you need to construct a custom context block from graph search results. Unlike user-specific context retrieval, knowledge graphs require you to manually search the graph and build the context block.
Why context block construction?
Knowledge graphs don’t have the concept of threads or conversation history, so you need to explicitly search for relevant information and format it into a context block. This gives you full control over what information is included and how it’s structured.
To build a custom context block, you’ll:
- Search the graph for relevant edges (facts) and nodes (entities) using your query
- Format the search results into a structured context block
- Include this context block in your agent’s prompt
The Advanced Context Block construction guide shows how to build a custom block from graph search results.
Constructed context block example
Here’s a simplified example of searching a knowledge graph and building a context block:
For production use, the Advanced Context Block construction guide includes:
- Helper functions for formatting edges and nodes
- Breadth-first search integration for recent context
- Custom entity and edge type filtering
- Temporal validity information handling
- User summary integration
Add context block to agent context window
The Context Block can contain text that came from end users, documents, tools, or other external sources. A privileged message gives that text higher instruction priority than ordinary input. Keep the Context Block out of system messages, developer messages, and other privileged instruction channels.
Follow your model provider’s documented method for separating instructions from data:
- For the OpenAI Responses API, send preloaded context through ordinary
inputor ausermessage. Usefunction_call_outputonly for the result of an actual function call. - For the Anthropic Messages API, design retrieval as a tool call when context can contain third-party data. Return the context in a
tool_resultblock linked to the originaltool_use_id. - For other providers, use the documented untrusted-data channel. If the provider does not define one, use an ordinary user-level message with explicit data framing.
OpenAI with preloaded context
Place the Context Block after the conversation history and before the latest user request. Everything before the block stays unchanged between turns, so this order preserves the cacheable prefix that prompt caching needs. Replace the previous turn’s block instead of appending a second one.
If the model requests memory through a function, return the Context Block as function_call_output linked to the original call_id.
OpenAI Chat Completions with tool-retrieved context
Anthropic with tool-retrieved context
Do not create a tool message for preloaded context unless the provider documents that pattern. A tool-result type must remain linked to the model’s actual tool request.
Read Memory security best practices for provider-specific mappings, write controls, action authorization, and recovery guidance.
Next steps
Now that you’ve learned how to give your agent knowledge through graph capabilities, you can explore additional features:
- Customize graph structure to your domain - Define custom entity and edge types to structure domain-specific information.
- Advanced Context Block construction - Build custom blocks with helper functions, graph traversal, and type filters.
- Searching the graph - Configure parameters for Context Graph queries.