Quickstart

Get up and running with Zep in minutes

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

This quickstart guide will help you get up and running with Zep quickly. We’ll cover:

  • Initializing the client
  • Creating a user and session
  • Adding messages and retrieving context with the memory API
  • Adding business data to a user or group graph
  • Searching for edges or nodes in the graph

Initialize the Client

First, install the Zep SDK for your preferred language. Then initialize the client with your API key.

The Python SDK Supports Async Use

The Python SDK supports both synchronous and asynchronous usage. For async operations, import AsyncZep instead of Zep and remember to await client calls in your async code.

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)

Create a User and Session

Before adding messages, you need to create a user and a session. A session is a chat thread - a container for messages between a user and an assistant. A user can have multiple sessions (different conversation threads).

While messages are stored in sessions, the knowledge extracted from these messages is stored at the user level. This means that facts and entities learned in one session are available across all of the user’s sessions. When you use memory.get(), Zep returns the most relevant memory from the user’s entire knowledge graph, not just from the current session.

Creating a User

1# Create a new user
2user_id = "user123"
3new_user = client.user.add(
4 user_id=user_id,
5 email="[email protected]",
6 first_name="Jane",
7 last_name="Smith",
8)

Creating a Session

1import uuid
2
3# Generate a unique session ID
4session_id = uuid.uuid4().hex
5
6# Create a new session for the user
7client.memory.add_session(
8 session_id=session_id,
9 user_id=user_id,
10)

Adding Messages and Retrieving Context

Adding Messages with memory.add

Add chat messages to a session using the memory.add method. These messages will be stored in the session history and used to build the user’s knowledge graph.

1# Define messages to add
2from zep_cloud.types import Message
3
4messages = [
5 Message(
6 role="Jane",
7 content="Hi, my name is Jane Smith and I work at Acme Corp.",
8 role_type="user",
9 ),
10 Message(
11 role="AI Assistant",
12 content="Hello Jane! Nice to meet you. How can I help you with Acme Corp today?",
13 role_type="assistant",
14 )
15]
16
17# Add messages to the session
18client.memory.add(session_id, messages=messages)

Retrieving Context with memory.get

Use the memory.get method to retrieve relevant context for a session. This includes a context string with facts and entities and recent messages that can be used in your prompt.

1# Get memory for the session
2memory = client.memory.get(session_id=session_id)
3
4# Access the context string (for use in prompts)
5context_string = memory.context
6print(context_string)
7
8# Access recent messages
9recent_messages = memory.messages
10for msg in recent_messages:
11 print(f"{msg.role}: {msg.content}")

Adding Business Data to a Graph

You can add business data directly to a user’s graph or to a group graph using the graph.add method. This data can be in the form of messages, text, or JSON.

1# Add text data to a user's graph
2new_episode = client.graph.add(
3 user_id=user_id,
4 type="text",
5 data="Jane Smith is a senior software engineer who has been with Acme Corp for 5 years."
6)
7print("New episode created:", new_episode)
8# Add JSON data to a user's graph
9import json
10json_data = {
11 "employee": {
12 "name": "Jane Smith",
13 "position": "Senior Software Engineer",
14 "department": "Engineering",
15 "projects": ["Project Alpha", "Project Beta"]
16 }
17}
18client.graph.add(
19 user_id=user_id,
20 type="json",
21 data=json.dumps(json_data)
22)
23
24# Add data to a group graph (shared across users)
25group_id = "engineering_team"
26client.graph.add(
27 group_id=group_id,
28 type="text",
29 data="The engineering team is working on Project Alpha and Project Beta."
30)

Searching the Graph

Use the graph.search method to search for edges or nodes in the graph. This is useful for finding specific information about a user or group.

1# Search for edges in a user's graph
2edge_results = client.graph.search(
3 user_id=user_id,
4 query="What projects is Jane working on?",
5 scope="edges", # Default is "edges"
6 limit=5
7)
8
9# Search for nodes in a user's graph
10node_results = client.graph.search(
11 user_id=user_id,
12 query="Jane Smith",
13 scope="nodes",
14 limit=5
15)
16
17# Search in a group graph
18group_results = client.graph.search(
19 group_id=group_id,
20 query="Project Alpha",
21 scope="edges",
22 limit=5
23)

Using Zep as an Agentic Tool

Zep’s memory retrieval methods can be used as agentic tools, enabling your agent to query Zep for relevant information. The example below shows how to create a LangChain LangGraph tool to search for facts in a user’s graph.

Python
1from zep_cloud.client import AsyncZep
2
3from langchain_core.tools import tool
4from langchain_openai import ChatOpenAI
5from langgraph.graph import StateGraph, MessagesState
6from langgraph.prebuilt import ToolNode
7
8zep = AsyncZep(api_key=os.environ.get('ZEP_API_KEY'))
9
10@tool
11async def search_facts(state: MessagesState, query: str, limit: int = 5):
12 """Search for facts in all conversations had with a user.
13
14 Args:
15 state (MessagesState): The Agent's state.
16 query (str): The search query.
17 limit (int): The number of results to return. Defaults to 5.
18 Returns:
19 list: A list of facts that match the search query.
20 """
21 search_results = await client.graph.search(
22 user_id=state['user_name'],
23 query=query,
24 limit=limit,
25 )
26
27 return [edge.fact for edge in search_results.edges]
28
29tools = [search_facts]
30tool_node = ToolNode(tools)
31llm = ChatOpenAI(model='gpt-4o-mini', temperature=0).bind_tools(tools)

Next Steps

Now that you’ve learned the basics of using Zep, you can: