Memory Management
MCPAgent provides flexible memory management options to control how conversation history is handled. You can choose between three different memory modes depending on your use case.Memory Modes
1. Self-Managed Memory (memory_enabled=True
)
When memory_enabled=True
, the agent automatically manages conversation history internally. This is the simplest option for most use cases.
2. No Memory (memory_enabled=False
)
When memory_enabled=False
, the agent has no internal memory and treats each interaction independently.
3. External Memory Management
You can provide conversation history externally for full control over memory management. This allows you to implement custom memory strategies like limited history, persistence, or filtering.Memory Strategies
Unlimited History
Keep all conversation history for maximum context:Limited History
Limit conversation history to manage memory usage and context length:Sliding Window
Implement a sliding window approach to maintain recent context while discarding older messages:Best Practices
- For Simple Use Cases: Use
memory_enabled=True
for automatic memory management - For Stateless Operations: Use
memory_enabled=False
when each interaction should be independent - For Custom Control: Use external memory management when you need specific memory strategies
- Memory Limits: Consider implementing memory limits to prevent context overflow with large conversations
- Persistence: For long-running applications, consider persisting conversation history to external storage
Examples
See the following examples for practical implementations:chat_example.py
- Unlimited memory with external managementlimited_memory_chat.py
- Limited memory with sliding window
Configuration Options
Parameter | Type | Default | Description |
---|---|---|---|
memory_enabled | bool | True | Enable/disable internal memory management |
external_history | List[BaseMessage] | None | External conversation history to use |
external_history
parameter takes precedence over internal memory when provided, allowing you to override the agent’s internal memory with your own implementation.