Sliding-window memory keeps only the last N turns verbatim and drops older ones. It is cheap and deterministic, but anything older than the window vanishes with no summary or substitute to replace it.
Imagine a fridge with a strict rule: only the five most recent items stay, anything older gets thrown out. No leftover gets summarized into a note on the door, no fridge inventory gets kept somewhere else. It is simple, the fridge never overflows, but the milk you bought last week is gone the moment the sixth item arrives. A sliding-window chat memory works the same way. The last few turns are kept word for word, anything older drops off the back, and nothing replaces them. The bot behaves as if those old turns never happened.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Sliding-window memory is the simplest answer to the question "how do I keep a chatbot's conversation history under a token budget?" Keep the last N turns, drop the rest. The mechanism is so straightforward that it feels barely worth a name, but understanding what it does and does not do is the foundation for understanding every more sophisticated memory pattern.
This deep dive walks through the mechanism, the wins, the single hard limit, and how the pattern fits into the broader memory composition that most production stacks use.
The mechanism: FIFO over turns or tokens
A sliding window is a FIFO queue. Each new turn (user or assistant) is appended at the front. When the total exceeds the budget, the oldest turn at the back is removed. The model sees exactly whatever is currently in the queue at the time of the call.
The budget can be expressed in two units. Turn count: keep the last N turns regardless of size. Token count: keep the last K tokens regardless of how many turns that is. Production stacks typically use token count because turn sizes are highly variable (a user who pastes a 5,000-token document followed by short questions will blow a turn-count budget while a token-count budget evicts naturally).
There is no other moving part. No summarizer runs. No extracted facts are stored. No analysis is done on what is being dropped. The eviction happens silently, deterministically, and cheaply.
Implementation is trivially small. In Python it is essentially a list slice; in any other language it is the same. The complexity all sits in deciding the value of N or K, which depends on the workload's typical reference distance.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- LangChain's ConversationBufferWindowMemory is the canonical sliding-window implementation.
- LlamaIndex's ChatSummaryMemoryBuffer falls back to a sliding window when summarization is disabled.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do you pick the window size N for a chat application?
Sample real conversations and measure the typical reference distance: how many turns back does a pronoun or definite reference usually point? Set N to comfortably exceed the 90th percentile of that distance. For most chat workloads, N lands between 5 and 20 turns; for code-heavy workloads with long pasted blocks, K tokens is a better unit than N turns.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Thinking the dropped turns get summarized somewhere. In a pure sliding window, no summary is created and the dropped content is genuinely lost.
60 second bullets to scan on the way to the call.
Define sliding-window memory in one sentence
Explain the FIFO mechanism and what triggers an eviction
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.