Chat History
Manage and retrieve conversation history using the Memory system.Overview
Chat history in the 0G AI SDK is managed through the Memory system, which provides both ephemeral (temporary) and persistent storage for conversation messages. This allows you to maintain context across sessions and build sophisticated conversational AI applications.Memory Integration
The Chat class works seamlessly with the Memory class to store and retrieve conversation history:Methods
get_messages()
Retrieve current conversation messages from ephemeral memory.List[ChatMessage]
- List of messages in the current conversation
add_message()
Add a message to the conversation history.message
(ChatMessage): The message to add to history
clear_ephemeral_messages()
Clear the current conversation from ephemeral memory.Examples
Agent History Methods
save_conversation()
Save the current conversation to persistent storage.conversation_id
(Optional[str]): Custom ID for the conversation. If not provided, a timestamp-based ID is generated.
str
- The conversation ID
Example:
load_conversation()
Load a previously saved conversation.conversation_id
(str): The ID of the conversation to load
clear_conversation()
Clear the current conversation from ephemeral memory.Memory Statistics
Get detailed information about memory usage:Advanced History Management
Custom Message Filtering
Conversation Context Building
Message Search
Configuration
Max Ephemeral Messages
Control how many messages are kept in ephemeral memory:Memory Bucket Organization
Organize conversations using different memory buckets:Error Handling
Best Practices
- Regular Cleanup: Clear ephemeral messages periodically for long-running applications
- Meaningful IDs: Use descriptive conversation IDs for easier management
- Context Limits: Be mindful of token limits when using long conversation histories
- Backup Important Conversations: Save critical conversations to persistent storage