Skip to content
AI360XpertBeta

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.

A fixed 8-token window slides forward as each new token arrives, pushing the oldest token out while everything still inside stays readable
A fixed 8-token window slides forward as each new token arrives, pushing the oldest token out while everything still inside stays readable

Why Does This Exist?

A transformer never reads your whole document. It reads a fixed run of tokens, at most nmax⁡n_{\max} 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 nn tokens does work that grows with n2n^2, not nn. The KV cache held during generation grows with nn 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 nn, where nn is a plain count (n=8n = 8 means eight tokens). Call the trained ceiling nmax⁡n_{\max}, the largest nn the model ever saw in training. The window is the frame that holds at most nmax⁡n_{\max} tokens at a time.

The frame

Membership is binary. A token with position index ii, where ii 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 nmax⁡n_{\max} 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 n×n=n2n \times n = n^2 ordered pairs. At n=8n = 8 that's 64 comparisons. At n=16n = 16 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 nn 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 W=8W = 8 slots, where WW is just the worked size for this example. Your prompt fills it exactly:

[t1,t2,t3,t4,t5,t6,t7,t8],n=8,82=64 ordered pairs[t_1, t_2, t_3, t_4, t_5, t_6, t_7, t_8], \quad n = 8, \quad 8^2 = 64 \text{ ordered pairs}

You generate t9t_9. The frame slides:

[t2,t3,t4,t5,t6,t7,t8,t9][t_2, t_3, t_4, t_5, t_6, t_7, t_8, t_9]

Token t1t_1 isn't deprioritized. It's gone.

You generate t10t_{10}:

[t3,t4,t5,t6,t7,t8,t9,t10][t_3, t_4, t_5, t_6, t_7, t_8, t_9, t_{10}]

Same length, same 64 pairs. The diagram above draws exactly this state: t1t_1 and t2t_2 off the left edge, t11t_{11} and t12t_{12} waiting on the right.

Now compare a frame twice as wide. At n=16n = 16 the pairs are 162=25616^2 = 256, and 256/64=4256 / 64 = 4. 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 nmax⁡n_{\max} 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))  # -> 256

Three things to notice. The length never exceeds 8. The survivors keep their order, t3t_3 first and t10t_{10} 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.