Zenaique

How does a sliding window memory keep a chat under budget, and what does it always lose?

Flashcard·Easy·4.0 · 0·~30s·Asked atAlibabaInfosysLocus
Attempt it
TL;DR

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.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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.

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.

What the pattern gets right
The single thing it cannot retain
Choosing N and composing with other layers
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

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.
Sign in to see more production examples.

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?
A

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.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Thinking the dropped turns get summarized somewhere. In a pure sliding window, no summary is created and the dropped content is genuinely lost.

Sign in to see all red flags and common mistakes.

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

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Pick the most effective intervention when an agent's context grows by 8KB every iteration
MCQ·Medium