Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 7 · 6. Memory & State Management
Lesson 48 of 68

ConversationBuffer & Window Memory

Short-term recall. The simplest memory is a list.

Derpx0610 Jan · 1 min read
LangChain Mastery
Why Memory Breaks SystemsConversationBuffer & Window MemoryConversationSummaryMemoryEntity MemorySession IsolationPersistence Layers (Redis / DB)
Contents
1. ConversationBuffer (The List)2. ConversationBufferWindow (The Slide)3. The Trade-off: Recency Bias4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Why Memory Breaks SystemsConversationBuffer & Window MemoryConversationSummaryMemoryEntity MemorySession IsolationPersistence Layers (Redis / DB)
Contents
1. ConversationBuffer (The List)2. ConversationBufferWindow (The Slide)3. The Trade-off: Recency Bias4. Summary

1. ConversationBuffer (The List)

The simplest form of memory is "Keep Everything." This is ConversationBufferMemory.

buffer_memory.py
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
memory.save_context({"input": "Hi"}, {"output": "Hello"})
memory.load_memory_variables({})
# Result: "Human: Hi
AI: Hello"

Pros: Perfect recall. The AI knows exactly what you said. Cons: It crashes your app after ~20 turns because you hit the token limit.

2. ConversationBufferWindow (The Slide)

To stop the crash, we use a Sliding Window. We only keep the last KKK interactions (e.g., k=5k=5k=5). When message #6 arrives, message #1 is deleted forever.

window_memory.py
from langchain.memory import ConversationBufferWindowMemory

memory = ConversationBufferWindowMemory(k=2)
# Stores strictly the last 2 exchanges.

3. The Trade-off: Recency Bias

Window memory assumes "Recent = Relevant." This is usually true for chit-chat. "What is my name?" (User said it 2 mins ago -> window has it).

It is disastrous for long tasks. "Here is the rule for the game." (User said it 10 mins ago -> window deleted it). The AI now plays the game wrong because the "Rule" fell off the edge of the world.

4. Summary

Window memory requires no intelligence. It is cheap and fast. Use it for short, transactional sessions. Do not use it for long-running companions.

Key Intuition: "If it scrolls off the screen, it doesn't exist."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonWhy Memory Breaks SystemsNext lesson →ConversationSummaryMemory