Sliding window memory
The simplest memory strategy: keep only the last N messages. Old messages are dropped. This puts a hard cap on token usage but means the agent forgets older context.
class SlidingWindowAgent:
def __init__(self, max_messages=5):
self.memory = []
self.max_messages = max_messages
self.system_message = {
"role": "system",
"content": "You are a helpful assistant."
}
def respond(self, user_content: str):
self.memory.append(
{"role": "user", "content": user_content}
)
# Keep only the last N messages
if len(self.memory) > self.max_messages:
self.memory = self.memory[-self.max_messages:]
full_messages = [self.system_message] + self.memory
response = completion(
model=DEFAULT_MODEL, messages=full_messages
)
assistant_content = (
response.choices[0].message.content
)
self.memory.append(
{"role": "assistant", "content": assistant_content}
)
return assistant_contentThe sliding window keeps self.memory trimmed to max_messages. Old messages are discarded.
Sliding window: only recent messages survive
agent = SlidingWindowAgent(max_messages=4)
agent.respond("My name is Alex") # stored
agent.respond("I live in Tokyo") # stored
agent.respond("I work at Google") # stored
agent.respond("I like sushi") # stored
# Memory: [name, city, work, sushi] (all 4 fit)
agent.respond("What is my favorite food?")
# "You like sushi!" (recent, still in window)
agent.respond("What is my name?")
# May not remember! "My name is Alex" was dropped.After max_messages, older messages are dropped. The agent loses early context.
AI prompt: Try it with AI
Loading practice…
Fill in the blanks: Implement sliding window
Loading practice…
Quiz: Quiz
Loading practice…
Matching exercise: Sliding window: pros and cons
Loading practice…