ConversationBufferWindowMemory

1. What is ConversationBufferWindowMemory?

ConversationBufferWindowMemory stores only the last K interactions of a conversation.

Instead of remembering everything, it remembers a sliding window of recent messages.


2. Why does it exist?

ConversationBufferMemory grows forever → high token cost.

ConversationBufferWindowMemory solves this by:

  • Limiting memory size

  • Keeping only recent context

  • Preventing token explosion

In short:

Remember what matters most: the recent conversation.


3. Real-world analogy

Talking to a human who:

  • Clearly remembers the last few minutes

  • Forgets older details unless repeated

That’s exactly how window memory behaves.


4. Minimal working example (Gemini)


5. What actually gets remembered?

With k=2, memory keeps:

"My name is John" is forgotten because it fell outside the window.

So the model may fail to answer correctly.


6. Key parameter: k

k value
Meaning

k=1

Only last exchange

k=2

Last 2 user–AI turns

k=5

Last 5 turns

k=0

No memory


7. Inspecting the memory buffer

Shows only the most recent messages, not the full history.


8. Comparison with other memory types

Memory Type
Stores
Token Cost

Buffer

Full chat

High

Window

Last K turns

Medium

Summary

Summarized history

Low


9. Common mistakes

❌ Setting k too small and losing important facts ❌ Assuming it “understands importance” (it doesn’t) ❌ Using it for long-term facts (names, preferences)


10. When to use it

Use ConversationBufferWindowMemory when:

  • You need short-term context

  • Conversations are medium length

  • Cost control matters

  • You don’t need long-term recall


11. One-line mental model

ConversationBufferWindowMemory = last K messages only

Last updated