SlidingWindowMemory Example
SlidingWindowMemory automatically manages conversation history by enforcing both token budgets and turn limits. It keeps recent context while preventing context window overflow.What SlidingWindowMemory Does
SlidingWindowMemory:- Tracks conversation “turns” (user-agent message pairs)
- Enforces a maximum token budget to prevent context overflow
- Limits the number of turns kept in memory
- Automatically removes oldest messages when limits are exceeded
- Keeps recent context intact while sliding away old messages
- Ideal for long-running conversations with token constraints
Complete Working Example
Key Configuration
maxTokens
Sets the maximum token budget for the conversation history:maxTurns
Limits the number of conversation turns (user-agent pairs) to keep:- Each turn consists of one user message and one agent response
- Oldest turns are removed when the limit is exceeded
- Helps manage memory usage in addition to token limits
How It Works
- Dual Constraints: Enforces both
maxTokensandmaxTurnslimits - Automatic Pruning: Removes oldest turns when either limit is exceeded
- Token Estimation: Estimates token usage to stay within budget
- Recent Context: Always preserves the most recent conversation turns
- Stats Tracking: Provides
stats()method to inspect current memory usage
Memory Stats
You can inspect memory usage at any time:When to Use SlidingWindowMemory
- Long-running conversations that might exceed token limits
- Applications with strict token budget constraints
- When you need to balance context retention with cost management
- Chatbots that need to remember recent context but not entire history
- Scenarios where oldest context is less relevant than recent exchanges
Comparison with BufferMemory
| Feature | BufferMemory | SlidingWindowMemory |
|---|---|---|
| Limit type | Message count | Tokens + Turns |
| Token awareness | No | Yes |
| Use case | Simple apps | Long conversations |
| Overflow protection | Basic | Advanced |
| Stats tracking | No | Yes |