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.
Basic example
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).
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.
Creating messages with metadata
Messages can have metadata attached to store additional information like sentiment scores, source identifiers, processing flags, or other custom data. Metadata is preserved when getting threads, individual messages, and when searching episodes.
Message metadata is currently supported only for thread messages. Messages added via the graph.add API do not support metadata. Zep does not support filtering or searching over message metadata.
You can attach metadata when creating messages by including a metadata field in your message objects:
Updating message metadata
You can update the metadata of an existing message using the message UUID. This is useful for adding or modifying metadata after a message has been created, such as updating sentiment analysis results or processing status.
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_messagescall - Message size limit: Each message can be at most 2,500 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.
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.