Adding Memory
You can add both messages and business data to User Graphs.
Adding Messages
Add your chat history to Zep using the thread.add_messages
method. thread.add_messages
is thread-specific and expects data in chat message format, including a name
(e.g., user’s real name), role
(AI, human, tool), and message content
. Zep stores the chat history and builds a user-level knowledge graph from the messages.
For best results, add chat history to Zep on every chat turn. That is, add both the AI and human messages in a single operation and in the order that the messages were created.
The example below adds messages to Zep’s memory for the user in the given thread:
You can find additional arguments to thread.add_messages
in the SDK reference. Notably, for latency sensitive applications, you can set return_context
to true which will make thread.add_messages
return a context block in the way that thread.get_user_context
does (discussed below).
Setting message timestamps
When creating messages via the API, you should provide the created_at
timestamp in RFC3339 format. The created_at
timestamp represents the time when the message was originally sent by the user. Setting the created_at
timestamp is important to ensure the user’s knowledge graph has accurate temporal understanding of user history (since this time is used in our fact invalidation process).
Message Limits
When adding messages to a thread, there are limits on both the number of messages and message size:
- Messages per call: You can add at most 30 messages in a single
thread.add_messages
call - Message size limits:
- Free plans: Each message can be at most 2,500 characters
- Paid plans (including metered): Each message can be at most 14,000 characters
If you need to add more than 30 messages or have messages exceeding the character limits, you’ll need to split them across multiple API calls or truncate the content accordingly. Our additional recommendations include:
- Have users attach documents rather than paste them into the message, and then process documents separately with
graph.add
- Reduce the max message size for your users to match our max message size
- Optional: allow users to paste in documents with an auto detection algorithm that turns it into an attachment as opposed to part of the message
Check when messages are finished processing
You can use the message UUIDs from the response to poll the messages and check when they are finished processing:
An example of this can be found in the check data ingestion status cookbook.
Ignore Assistant Messages
You can also pass in a list of roles to ignore when adding messages to a User Graph using the ignore_roles
argument. For example, you may not want assistant messages to be added to the user graph; providing the assistant messages in the thread.add_messages
call while setting ignore_roles
to include “assistant” will make it so that only the user messages are ingested into the graph, but the assistant messages are still used to contextualize the user messages. This is important in case the user message itself does not have enough context, such as the message “Yes.” Additionally, the assistant messages will still be added to the thread’s message history.
Adding Business Data
You can also add JSON or unstructured text as memory to a User Graph using our Graph API.
Customizing Memory Creation
Zep offers two ways to customize how memory is created. You can read more about these features at their guide pages:
- Custom entity and edge types: Feature allowing use of Pydantic-like classes to customize creation/retrieval of entities and relations in the knowledge graph.
- Fact ratings: Feature for rating and filtering facts by relevance to your use case.