Learn/LangChain Mastery/Part 7 · 6. Memory & State Management
Lesson 48 of 68
SyllabusLangChain Mastery · Part 7

ConversationBuffer & Window Memory

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

Course outline & contents
LangChain Mastery
Why Memory Breaks SystemsConversationBuffer & Window MemoryConversationSummaryMemoryEntity MemorySession IsolationPersistence Layers (Redis / DB)

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.

To stop the crash, we use a Sliding Window. We only keep the last KK interactions (e.g., k=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.

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.

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