Short-Term Conversation Memory › Solution 1: Sliding Window Memory

Michael BrenndoerferJuly 1, 20252 min read
AI Agent HandbookAll chapters
Part 6 of 15Memory and Retrieval - How Agents Remember
Chapter 15Short-Term Conversation Memory › Solution 1: Sliding Window Memory
Reading section 9 of 15 · Solution 1: Sliding Window Memory · 2 minSection 9 of 15 · 2 min

Book contents

Section 9 of 15 · 2 min read

Short-Term Conversation Memory › Solution 1: Sliding Window MemoryOne section · Sections in this chapter9 / 15 ↓

The simplest solution: only keep the most recent N messages. This is called a "sliding window" because you keep a fixed-size window that slides forward as the conversation progresses.

In[13]:
Code
class ConversationalAgentWithWindow:
    """Agent with sliding window memory."""
    
    def __init__(self, max_messages: int = 20):
        self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
        self.conversation_history = []
        self.max_messages = max_messages
    
    def chat(self, user_message: str) -> str:
        """Chat with sliding window memory."""
        # Add user message
        self.conversation_history.append({
            "role": "user",
            "content": user_message
        })
        
        # Keep only recent messages
        if len(self.conversation_history) > self.max_messages:
            # Keep the most recent max_messages
            self.conversation_history = self.conversation_history[-self.max_messages:]
        
        # Get response with limited history
        response = self.client.messages.create(
            model="claude-sonnet-4-5",
            max_tokens=1024,
            messages=self.conversation_history
        )
        
        assistant_message = response.content[0].text
        
        # Add response to history
        self.conversation_history.append({
            "role": "assistant",
            "content": assistant_message
        })
        
        # Trim again if needed
        if len(self.conversation_history) > self.max_messages:
            self.conversation_history = self.conversation_history[-self.max_messages:]
        
        return assistant_message

With a sliding window of 20 messages, the agent remembers the last 10 exchanges (10 user messages + 10 assistant responses). Older messages are discarded.

This works well for:

  • Casual conversations where old context isn't needed
  • Cost-sensitive applications
  • Very long conversations that would exceed context limits

The tradeoff: the agent forgets older parts of the conversation. If you reference something from 15 exchanges ago, the agent won't remember it.