BufferMemory Example
BufferMemory is the simplest memory strategy that keeps conversation history in-process. It maintains a fixed number of recent messages, making it ideal for development and simple use cases.What BufferMemory Does
BufferMemory:- Stores conversation history in memory (RAM)
- Keeps a configurable maximum number of messages
- Automatically removes oldest messages when the limit is reached
- Maintains context across multiple agent runs using session IDs
- Perfect for development, testing, and simple applications
Complete Working Example
Key Configuration
maxMessages
Controls how many messages to keep in memory:How It Works
- Session Management: Use a unique
sessionIdto maintain separate conversation contexts - Automatic Pruning: When the message count exceeds
maxMessages, oldest messages are removed - Context Persistence: The agent remembers previous conversation turns within the same session
- Memory Inspection: You can inspect stored messages using
memory.entries(sessionId)
When to Use BufferMemory
- Development and testing
- Simple applications with low traffic
- Short conversations that don’t exceed token limits
- When you don’t need persistence across restarts
Production Considerations
For production applications, consider:- Using a database-backed memory provider for persistence
- Implementing Redis for distributed systems
- Using SlidingWindowMemory or SummarizingMemory for long conversations
- Monitoring memory usage in high-traffic scenarios