Context Windows
A context window is the fixed run of tokens a model can read in one go. When the run is full, each new token pushes the oldest one out of sight.
Why Does This Exist?
A transformer never reads your whole document. It reads a fixed run of tokens, at most of them, in a single pass. That ceiling is set when the model is trained, and no clever prompting stretches it at request time. This page is about that frame: the context window.
You might wonder why the ceiling exists at all. Self-attention compares every token against every other token, so a pass over tokens does work that grows with , not . The KV cache held during generation grows with too. Without a fixed cap, one long request could demand an unbounded amount of compute and memory, so every model ships with the cap baked in.
Two neighbours handle what this page doesn't. Long-context extension asks how a trained window gets widened after the fact. Context degradation asks why recall gets uneven inside very long windows. Here you get the frame itself: what counts as inside, what slides out, and what bigger frames charge.
The one idea you need first: models read tokens, not words. Tokenization chops your text into those pieces, and those pieces outnumber your words, so count tokens against the limit, never words.
Think of It Like This
A ticket rail with exactly eight clips
Imagine you are cooking the pass in a busy kitchen. Above you hangs a ticket rail with exactly eight clips. Each new order goes up on the right, everything shifts one slot left, and the oldest ticket slides off the end onto the floor. Nobody on the line can read it after that. It isn't faded or blurry. It's gone.
Every ticket still clipped to the rail is fully readable, first slot to last. That's the window: a fixed number of slots, binary membership, oldest out first.
Where the picture stops: kitchen tickets ignore each other, but tokens don't. Each token is weighed against every other one, which is why doubling the rail roughly quadruples the work instead of doubling it. And a cook reads the middle ticket as easily as either end, while a model sometimes fumbles the middle. That unevenness is a separate problem with its own page.
How It Actually Works
Call the number of tokens in one pass , where is a plain count ( means eight tokens). Call the trained ceiling , the largest the model ever saw in training. The window is the frame that holds at most tokens at a time.
The frame
Membership is binary. A token with position index , where counts from the start of the pass, is either inside the frame and visible to attention, or past an edge and simply not input. There is no half-visible state. Positional encoding tells the model where each inside token sits, but only positions up to carry meanings the model learned, which is why text past the edge can't just be squeezed in.
When the frame is full and generation continues, it slides: each fresh token takes the newest slot and the oldest token leaves. During a long chat this is silent. Nothing warns you. The start of the conversation just stops being input.
Why the frame can't just grow
Dense self-attention forms one query per token and scores it against every key, which is ordered pairs. At that's 64 comparisons. At it's 256. Twice the length, four times the pairs. The KV cache adds its own bill: every extra token stores its key and value vectors in every layer for as long as generation runs, so memory climbs with alongside the compute.
So a bigger window buys one thing, recall of more distant context, and sells two, latency and memory. That's the trade it always charges. When only a handful of passages actually matter, stuffing the whole library into the frame wastes both, and retrieving the passages instead wins.
Worked example
Fix the frame at slots, where is just the worked size for this example. Your prompt fills it exactly:
You generate . The frame slides:
Token isn't deprioritized. It's gone.
You generate :
Same length, same 64 pairs. The diagram above draws exactly this state: and off the left edge, and waiting on the right.
Now compare a frame twice as wide. At the pairs are , and . Twice the tokens, four times the comparisons. That ratio is why context length is a headline specification instead of a footnote.
The assumption underneath: the material you need fits inside the recent tokens. When it doesn't, the slide drops whatever is oldest, however important it was.
Code
The point of this snippet is the eviction rule, not the arithmetic. Watch the oldest tokens leave and the length refuse to grow.
def slide_window(tokens: list[int], incoming: list[int], window: int) -> list[int]: """Keep only the newest `window` tokens; oldest leave first.""" kept = tokens + incoming return kept[-window:]
def attention_pairs(n: int) -> int: """Ordered query-key comparisons in dense self-attention.""" return n * n
window = list(range(1, 9)) # [t1 .. t8], the full 8-slot frameprint(window) # -> [1, 2, 3, 4, 5, 6, 7, 8]window = slide_window(window, incoming=[9, 10], window=8)print(window) # -> [3, 4, 5, 6, 7, 8, 9, 10]print(attention_pairs(8)) # -> 64print(attention_pairs(16)) # -> 256Three things to notice. The length never exceeds 8. The survivors keep their order, first and last. And the pair counts, 64 and 256, match the hand computation above, which is the whole point of printing them.
Watch Out For
Losing your instructions halfway through a long chat
Symptom: the model starts strong, then drifts. It drops the output format, forgets the role you gave it, or asks for information you provided at the start.
Cause: those instructions sat in the oldest tokens, and the slide evicted them. Nothing warns you because eviction is silent by design.
Fix: keep instructions out of the sliding region. Pin them where your framework supports it, re-inject a short reminder as the chat grows, or compact old turns into a summary instead of letting raw history fill the frame. If early tokens keep getting evicted, read how attention sinks keep the first few slots pinned.
Treating fits in the window as read from the window
Symptom: your token count sits safely under the advertised limit, yet the model misses a fact buried in the middle of the prompt.
Cause: the hard limit is binary but recall isn't uniform. Text in the middle of a long window can get used weakly even though every token is technically inside. That's a quality problem layered on top of the frame, not the frame itself.
Fix: test retrieval at several positions instead of trusting the headline number. Place decisive facts near the start or the end, and fetch needle facts with retrieval rather than stuffing. The full story lives on the context degradation page.
The Quick Version
- A context window is the fixed number of tokens a model reads in one pass, capped at training time. Nothing outside it reaches the model.
- When the window is full, each new token evicts the oldest one first. The length never exceeds the cap.
- Attention compares every token pair, so doubling the length roughly quadruples the comparison work. Eight tokens need 64 pairs and sixteen need 256.
- Cached keys and values grow with length too, so long windows tax memory as well as compute.
- Fitting inside the limit isn't the same as being used well. Placement inside the window still affects recall.
- When only a few passages matter, retrieve them instead of widening the window.