Keep the last N turns of conversation. The simplest and most reliable memory pattern. Works great for short sessions and simple agents.
Turn 1: User: "Hi" → Agent: "Hello!"
Turn 2: User: "What's the weather?" → Agent: "Let me check..."
Turn 3: User: "Thanks" → Agent: "You're welcome!"
Window size = 2:
After turn 3, only turns 2-3 are kept.
Turn 1 is discarded.
class SlidingWindow:
def __init__(self, max_turns=10):
self.max_turns = max_turns
self.history = []
def add(self, turn):
self.history.append(turn)
if len(self.history) > self.max_turns:
self.history.pop(0)
def get_context(self):
return "\n".join(self.history)| Scenario | Window Size |
|---|---|
| Chatbot | 5-10 turns |
| Code assistant | 3-5 turns |
| Long analysis | 15-20 turns |
| Agent with tools | 10-15 turns |
Pros: Simple, fast, no external dependencies, predictable token usage.
Cons: Forgets everything outside the window, context gaps confuse the agent.
## Recent Conversation
{last 10 turns}
Use the above conversation history for context.
If information you need is not in the recent history,
ask the user to provide it again.