Overview
TheConversationManager class maintains the conversation history for voice agents as an array of ModelMessage objects (AI SDK format). It implements automatic history trimming based on configurable limits to prevent context overflow and manage token costs.
Key responsibilities:
- Store conversation history as
ModelMessage[]array - Add new messages and maintain turn order
- Automatically trim history to stay within limits
- Preserve conversation coherence (removes pairs of messages)
- Emit events when history is modified
src/core/ConversationManager.ts:13
Constructor
Optional configuration object
History management configuration:
maxMessages: Maximum number of messages to retain (default: 20)maxTotalChars: Maximum total characters across all messages (default: 50000)
Properties
length
Returns the current number of messages in the conversation history.
Methods
addMessage()
Add a message to the conversation history and automatically trim if needed.A message object compatible with AI SDK’s
ModelMessage type (contains role and content)- Appends message to the end of history
- Automatically calls
trimHistory()after adding - Does not validate message format
getHistory()
Get a copy of the current conversation history.getHistoryRef()
Get a direct reference to the internal history array.ModelMessage[] array.
Use case:
setHistory()
Replace the entire conversation history with a new array.New conversation history array
- Creates a shallow copy of the provided array
- Does NOT automatically trim the history
- Does NOT emit any events
clearHistory()
Remove all messages from the conversation history.- Sets history to empty array
- Emits
history_clearedevent
Events
TheConversationManager extends EventEmitter and emits the following events:
history_cleared
Emitted when the conversation history is cleared.history_trimmed
Emitted when messages are automatically removed to stay within limits.Number of messages removed from history
The limit that triggered trimming:
max_messages: Exceeded the message count limitmax_total_chars: Exceeded the total character limit
History Trimming Logic
Message Count Trimming
When history exceedsmaxMessages:
- Calculates excess messages:
excess = length - maxMessages - Rounds up to even number to preserve user/assistant turn pairs
- Removes oldest messages from the beginning of the array
- Emits
history_trimmedevent with reasonmax_messages
Character Count Trimming
When total characters exceedmaxTotalChars:
- Calculates total characters across all messages (including JSON-stringified complex content)
- Removes oldest messages one at a time until under limit
- Always keeps at least 2 messages (one user/assistant pair)
- Emits
history_trimmedevent with reasonmax_total_chars
Preserving Conversation Coherence
The manager attempts to preserve complete conversation turns by removing messages in pairs. This ensures the history always starts with a user message followed by an assistant response.
Usage in Agent Architecture
Configuration Guidelines
maxMessages
Recommended values:
- Short conversations: 10-20 messages (5-10 turns)
- Standard conversations: 20-40 messages (10-20 turns)
- Extended conversations: 40-100 messages (20-50 turns)
- Unlimited: Set to 0 to disable (relies on maxTotalChars only)
maxTotalChars
Recommended values based on model context:
- GPT-3.5-turbo (4K): 10,000-15,000 chars
- GPT-3.5-turbo (16K): 40,000-50,000 chars
- GPT-4 (8K): 20,000-30,000 chars
- GPT-4 (32K): 80,000-100,000 chars
- GPT-4-turbo (128K): 300,000-400,000 chars
- Unlimited: Set to 0 to disable
Performance Considerations
Memory Usage
- Message storage: Each message contains role + content string
- Character counting: Recalculated on every trim (O(n) where n = message count)
- Typical memory: ~100 messages = ~1-2 MB depending on content length
Optimization Tips
- Set appropriate limits - Don’t keep more history than needed for conversation quality
- Use getHistoryRef() for read-only iteration to avoid copy overhead
- Batch operations - Use
setHistory()when loading from storage instead of repeatedaddMessage()calls - Monitor trimming events - Track how often history is trimmed to tune limits
Thread Safety
Related
- VoiceAgent - Uses ConversationManager to maintain chat history
- StreamProcessor - Works with conversation history for LLM context
- AI SDK Types -
ModelMessagetype definition