Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 1.34 KB

File metadata and controls

58 lines (42 loc) · 1.34 KB

Sliding Window Memory

Keep the last N turns of conversation. The simplest and most reliable memory pattern. Works great for short sessions and simple agents.

How It Works

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.

Implementation

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)

When to Use

Scenario Window Size
Chatbot 5-10 turns
Code assistant 3-5 turns
Long analysis 15-20 turns
Agent with tools 10-15 turns

Pros & Cons

Pros: Simple, fast, no external dependencies, predictable token usage.

Cons: Forgets everything outside the window, context gaps confuse the agent.

Prompt Integration

## 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.