Skip to main content

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.
Returns: List[ChatMessage] - List of messages in the current conversation

add_message()

Add a message to the conversation history.
Parameters:
  • 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.
Parameters:
  • conversation_id (Optional[str]): Custom ID for the conversation. If not provided, a timestamp-based ID is generated.
Returns: str - The conversation ID Example:

load_conversation()

Load a previously saved conversation.
Parameters:
  • conversation_id (str): The ID of the conversation to load
Example:

clear_conversation()

Clear the current conversation from ephemeral memory.
Example:

Memory Statistics

Get detailed information about memory usage:

Advanced History Management

Custom Message Filtering

Conversation Context Building

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

  1. Regular Cleanup: Clear ephemeral messages periodically for long-running applications
  2. Meaningful IDs: Use descriptive conversation IDs for easier management
  3. Context Limits: Be mindful of token limits when using long conversation histories
  4. Backup Important Conversations: Save critical conversations to persistent storage

Next Steps