Agent Domain Knowledge

Provide your agent with searchable knowledge graphs built from your data

This guide shows you how to give your agent searchable knowledge graphs built from any text data using Zep’s Graph capabilities. Zep allows you to build knowledge graphs from unstructured text, JSON, or messages—including emails, Slack messages, transcripts, chunked documents, and inventory data. Unlike traditional RAG systems, Zep is designed for evolving, streaming data that continuously updates your agent’s knowledge.

Looking for a more in-depth understanding? Check out our Key Concepts page.

Install the Zep SDK

Set up your Python project, ideally with a virtual environment, and then:

$pip install zep-cloud

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.

1import os
2from zep_cloud.client import Zep
3
4API_KEY = os.environ.get('ZEP_API_KEY')
5
6client = Zep(
7 api_key=API_KEY,
8)

Add streaming data to Zep

Zep excels at building knowledge graphs from streaming data that evolves over time. You can add any unstructured text, JSON, or message data to build your knowledge graph. 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 is optimized for evolving data, not static RAG. While you can add static documents, Zep’s knowledge graph construction shines when tracking relationships and facts that change over time.

One-time data uploads: If you have existing data to backfill (such as a set of documents or historical data), you can add it using a one-time data migration. Simply loop through your data and call graph.add for each item, or use our batch processing method for faster concurrent processing.

Zep supports three data types when adding data to a graph:

Message data

Use message data when you have communications with designated speakers, such as emails or chat logs. See our Adding Data to the Graph guide for details.

1from zep_cloud.client import Zep
2
3client = Zep(api_key=API_KEY)
4
5# Add message data to a graph
6message = "Sarah (customer): I need help configuring my API keys for production"
7
8new_episode = client.graph.add(
9 graph_id="customer-support",
10 type="message",
11 data=message
12)

Text data

Use text data for raw text without speaker attribution, like internal documents or wiki articles. See our Adding Data to the Graph guide for details.

1from zep_cloud.client import Zep
2
3client = Zep(api_key=API_KEY)
4
5# Add text data to a graph
6text_data = "Production API keys must be configured with rate limiting enabled."
7
8new_episode = client.graph.add(
9 graph_id="company-knowledge",
10 type="text",
11 data=text_data
12)

JSON data

Use JSON data for structured business data, REST API responses, or any JSON-formatted records. See our Adding Data to the Graph guide for details.

1from zep_cloud.client import Zep
2import json
3
4client = Zep(api_key=API_KEY)
5
6# Add JSON data to a graph
7json_data = {
8 "product": {
9 "id": "prod_123",
10 "name": "Enterprise Plan",
11 "features": ["Priority Support", "Custom Integration", "99.9% SLA"],
12 "price": 299
13 }
14}
15
16new_episode = client.graph.add(
17 graph_id="product-catalog",
18 type="json",
19 data=json.dumps(json_data)
20)

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 memory 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:

  1. Search the graph for relevant edges (facts) and nodes (entities) using your query
  2. Format the search results into a structured context block
  3. Include this context block in your agent’s prompt

See our Advanced Context Block Construction guide for complete examples, helper functions, and best practices for building custom context blocks from graph search results.

Constructed context block example

Here’s a simplified example of searching a knowledge graph and building a context block:

1from zep_cloud.client import Zep
2
3client = Zep(api_key=API_KEY)
4
5# Search for relevant edges (facts) and nodes (entities)
6query = "What are the API key configuration requirements?"
7
8edge_results = client.graph.search(
9 graph_id="company-knowledge",
10 query=query,
11 scope="edges",
12 limit=10
13)
14
15node_results = client.graph.search(
16 graph_id="company-knowledge",
17 query=query,
18 scope="nodes",
19 limit=5
20)
21
22# Build context block from results
23facts = "\n".join([f" - {edge.fact}" for edge in edge_results.edges])
24entities = "\n".join([f" - {node.name}: {node.summary}" for node in node_results.nodes])
25
26context_block = f"""# These are relevant facts from the knowledge base
27<FACTS>
28{facts}
29</FACTS>
30
31# These are relevant entities from the knowledge base
32<ENTITIES>
33{entities}
34</ENTITIES>
35"""
36
37print(context_block)

For production use cases, refer to the Advanced Context Block Construction guide, which 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

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.

MessageTypeContent
SystemYour system prompt

{Zep context block}
AssistantAn assistant message stored in Zep
UserA user message stored in Zep
UserThe latest user message

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.

MessageTypeContent
SystemYour system prompt (static, cacheable)
AssistantAn assistant message stored in Zep
UserA user message stored in Zep
UserThe latest user message
Tool{Zep context block}

Next steps

Now that you’ve learned how to give your agent knowledge through graph capabilities, you can explore additional features: