Working with Memory


Memories are the central data structure in Zep's Memory Store. They contain a list of Messages and a Summary.

Persisting a Memory to a Session

A Memory may include a single message or a series of messages. Each Message has a role, role_type and content field, with role being the identifiers for your human and AI/agent users, and content being the text of the message.

Additionally, you can store custom metadata with each Message.

Sessions don’t need to be explicitly created

Sessions are created automatically when adding Memories. If the SessionID is already exists, then the Memory is upserted into the Session.

Manually creating a session can be useful if you want to associate it with a user or add metadata.

1from zep_cloud.client import AsyncZep
2from zep_cloud.types import Message
4import uuid
6API_KEY = "<your_api_key>"
7client = AsyncZep(
8 api_key=API_KEY,
10session_id = uuid.uuid4().hex # A new session identifier
12history = [
13 {"role": "Jane", "role_type": "user", "content": "Who was Octavia Butler?"},
14 {"role": "HistoryBot", "role_type": "assistant", "content": "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American science fiction author."},
15 {"role": "Jane", "role_type": "user", "content": "Which books of hers were made into movies?", "metadata": {"foo": "bar"}},
18messages = [
19 Message(
20 role=m["role"],
21 role_type=m["role_type"],
22 content=m["content"],
23 metadata=m.get("metadata"),
24 ) for m in history
26result = await client.memory.add(session_id, messages=messages)

Getting a Session’s Memory

Read more about the difference between Perpetual and Message Window Buffer Memory.

Perpetual Memory

The example below uses Zep’s async API and a context manager.

1client = AsyncZep(
2 api_key=API_KEY,
4memory = await client.memory.get(session_id, memory_type="perpetual")
6for message in memory.messages:
7 print(message)
Sample Output:
2 {
3 "uuid": "7291333f-2e01-4b06-9fe0-3efc59b3399c",
4 "created_at": "2023-05-16T21:59:11.057919Z",
5 "role": "HistoryBot",
6 "role_type": "assistant",
7 "content": "Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.",
8 "token_count": 56
9 },
10 {
11 "uuid": "61f862c5-945b-49b1-b87c-f9338518b7cb",
12 "created_at": "2023-05-16T21:59:11.057919Z",
13 "role": "Jane",
14 "role_type": "user",
15 "content": "Write a short synopsis of Butler's book, Parable of the Sower. What is it about?",
16 "token_count": 23
17 }

Message Window Buffer Memory

1client = AsyncZep(
2 api_key=API_KEY,
4memory = await client.memory.get(session_id, memory_type="message_window")