Skip to content
derpx06Notes on systems, models & learning
6. Memory & State Management · lesson 48 of 68 · 1 min · January 10, 2026

ConversationBuffer & Window Memory

Short-term recall. The simplest memory is a 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.

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."