Overview
TheConversation class manages conversation history for agents, allowing for addition, deletion, and retrieval of messages. It supports saving and loading in JSON/YAML formats, automatic token counting, and dynamic context window management.
Installation
Parameters
str
default:"auto-generated"
Unique identifier for the conversation.
str
default:"conversation-test"
Name of the conversation.
Optional[str]
default:"None"
The system prompt for the conversation.
bool
default:"False"
Enable ISO timestamps on each message.
bool
default:"False"
Enable automatic saving of conversation history.
str
default:"None"
File path for saving the conversation history.
str
default:"None"
File path to load conversation history from on initialization.
int
default:"8192"
Maximum number of tokens allowed in the conversation history. Used by token-based truncation and dynamic context windowing.
str
default:"None"
Rules injected into the conversation to govern participant behavior.
str
default:"None"
Custom prompt prepended alongside
rules.str
default:"User"
The user identifier used as the role for user messages.
bool
default:"False"
When
True, persisted history is written as YAML.bool
default:"False"
When
True, persisted history is written as JSON.bool
default:"False"
Enable per-message token counting.
bool
default:"False"
Attach a unique ID to every message.
str
default:"gpt-5.4"
Model name used by the tokenizer for token counting and truncation.
Optional[str]
default:"None"
Directory used to persist and load named conversations.
str
default:"json"
Export format used by
export(): "json" or "yaml".bool
default:"True"
Enable dynamic context window management (grow/shrink the kept history based on token usage).
bool
default:"False"
Enable token-count caching for repeated history reads.
bool
default:"False"
Include per-message metadata in formatted output.
Optional[str]
default:"None"
Path to the
MEMORY.md file used for persistent-memory reads/writes.Methods
add()
Add a message to the conversation history.role(str): The role of the speaker (e.g., ‘User’, ‘System’, ‘Agent’)content(Union[str, dict, list]): The content of the messagemetadata(Optional[dict]): Optional metadata for the messagecategory(Optional[str]): Optional category for the message (e.g., ‘input’, ‘output’)
return_history_as_string()
Return the conversation history as a formatted string.export()
Export the conversation to a file based on the export method.force(bool): If True, saves regardless of autosave setting
load()
Load conversation history from a file (auto-detects format).filename(str): Path to the file to load from
search()
Search for messages containing a keyword.keyword(str): The keyword to search for
truncate_memory_with_tokenizer()
Truncate conversation history based on token count using tokenizer.export_and_count_categories()
Export all messages with category ‘input’ and ‘output’ and count their tokens.Usage Examples
Basic Usage
Export and Load
Token Management
Search and Query
Features
- Automatic Saving: Enable autosave to automatically persist conversation history
- Token Management: Track token counts and automatically truncate based on context length
- Multiple Export Formats: Save as JSON or YAML
- Dynamic Context Window: Automatically manage conversation length to fit context limits
- Message Search: Search through conversation history by keyword
- Categorization: Tag messages with categories for organized tracking
- Time Tracking: Optionally track timestamps for all messages