Managing Context Windows

Master how AI agents manage limited context windows to maintain coherent, efficient conversations

Sliding Window Strategies

Sliding windows manage context by maintaining a moving window of recent messages. As new messages arrive, old ones are dropped or archivedβ€”keeping context fresh and within token limits.

Interactive: Sliding Window Visualizer

Msg 1: HelloOutside window
Msg 2: Hi thereOutside window
Msg 3: How are you?Outside window
Msg 4: Great!Outside window
Msg 5: Need help
Msg 6: Sure thing
Msg 7: Thanks!
Msg 8: Current
Strategy Details:
Fixed Window: Keeps last N messages (here: 4). Oldest is dropped when new arrives. Simple, predictable token usage.

πŸͺŸ Window Patterns

πŸ“

Fixed-Size Window

Keep last N messages (e.g., 10 turns). When 11th arrives, drop the 1st. Token usage stays constant.

Use case: Customer support, chatbots
⏱️

Time-Based Window

Keep messages from last T minutes (e.g., 30 mins). Older messages are auto-archived regardless of count.

Use case: Real-time monitoring, live support
🎯

Session-Based Window

Reset context at session boundaries. Each new session starts fresh. Useful when tasks are independent.

Use case: Multi-task agents, workflow steps
πŸ”„

Hierarchical Window

Keep recent messages in detail, older messages as summaries. Multi-level context: detailed + compressed.

Use case: Long conversations, research assistants

πŸ’‘ Implementation Tips

β€’
Combine with Compression: Use sliding window for recent messages, but compress (not drop) older ones. Best of both worlds.
β€’
Preserve System Prompts: System instructions should stay at the top, outside the window. Never drop them.
β€’
Track User Intent: If user references "earlier discussion," fetch from archived messages. Window is working memory, not total memory.
β€’
Dynamic Window Sizing: Adjust window size based on task. Simple Q&A: small window. Complex reasoning: larger window.
← Prev