Fix ConversationBufferMemory's unbounded growth problem with a real windowed variant that deliberately forgets old exchanges.
1The Unbounded Buffer Problem
A plain ConversationBufferMemory grows with every single message, with no upper bound. In a long-running conversation — a customer support session spanning dozens of exchanges — that buffer eventually exceeds the model's context window entirely, causing either a hard failure or silent truncation you don't control.
2Windowing Is a Real Trade-Off, Not a Free Fix
Windowed memory solves the size problem by genuinely discarding old messages — this lesson's quiz question isn't rhetorical. Ask about something from outside the window, and the model has no way to know, because the fact simply isn't in the prompt anymore. This is the same fundamental trade-off as the rolling window truncation technique from earlier context-management concepts, applied specifically to LangChain's memory abstraction.
3Step-by-Step Breakdown
ConversationBufferMemory keeps everything, forever — fine for a short chat, a real problem for a long-running one, since eventually the buffer exceeds the model's context window. LangChain's ConversationBufferWindowMemory fixes this by keeping only the last k exchanges.
Build Windowed Memory Yourself. With k=2, only the last 2 user/AI exchanges (4 messages total) should survive. Finish get_buffer_string(): slice self.messages down to the last (self.k * 2) messages before formatting — the earliest exchange should be dropped entirely.
With this k=2 windowed memory, if you now asked 'What's my name?', what would happen?
- →The model would not know — the 'My name is Sam' exchange has fallen outside the k=2 window and is no longer part of the injected context, so the model has genuinely lost access to that fact.
- →The model would still answer correctly, because memory never truly forgets anything.
Module 3 complete: you've built full-history and windowed memory, and proven memory works on a real model. Module 4 gives your chatbot access to external documents — the actual RAG mechanics, built LangChain-style this time.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Surface When Older Context Has Been Dropped
If a chat UI uses windowed memory, consider surfacing to the user when older messages are no longer part of the active context (e.g. a subtle divider), as real text content, so users understand why the assistant might not recall something from much earlier.
<div role="separator">Earlier messages are no longer in context</div>SEO Implications
- 1
Target 'LangChain ConversationBufferWindowMemory example' as a distinct search
Developers hitting the unbounded-buffer problem in production search for this exact class name once they've outgrown simple ConversationBufferMemory.
Best Practices
Choose a Memory Window Size Deliberately, Not Arbitrarily
The right k value depends on how much recent context your use case genuinely needs versus how much context budget you can afford — set it deliberately based on real conversation patterns, not as an arbitrary default.
Frequent Bugs
Slicing messages by count (last k*2 messages) when a more accurate cutoff would be by token count, silently allowing very long individual messages to still overflow the context window.
For conversations with highly variable message lengths, consider a token-count-based window instead of a fixed message-count window — this lesson's message-count approach is simpler but less precise about actual context budget.
Real-World Examples
Long-Running Support Session
A customer support chat spanning 50 exchanges uses windowed memory to stay within context limits, correctly recalling the last few messages about the current issue while genuinely no longer recalling small talk from 40 messages ago — an intentional, reasonable trade-off.
memory = ConversationBufferWindowMemory(k=5)