How the context window works
/AI/4 min read
The context window is everything a model can see at once — about 100,825 words, or 306 pages, at 128k tokens. Filling it costs 256 times the attention arithmetic of an 8k context and 42.9 GB of cache on a 70B model, and a 20-turn chat re-sends 105,000 tokens to hold a 10,000-token conversation.
A language model has no memory between requests. Everything it can use to produce an answer — the system prompt, the conversation so far, retrieved documents, tool results, and the answer it is writing — has to be present, as tokens, in a single input. That input has a maximum size: the context window.
What fits
Context windows are measured in tokens. At roughly 1.3 tokens per English word and about 330 words to a printed page:
| context window | words | pages |
|---|---|---|
| 8,192 tokens | 6,302 | 19 |
| 131,072 tokens | 100,825 | 306 |
| 1,000,000 tokens | 769,231 | 2,331 |
A 128k window holds a long novel or a substantial codebase module. A million-token window holds several books. The output counts against the same budget: if the window is 128k and the prompt is 120k, at most 8k tokens remain for the reply.
Why it is limited
Two costs grow with context length.
Attention arithmetic. Each token attends to every earlier token, so the number of pairs grows with the square of the length. Going from 8k to 128k tokens multiplies the pairs by 256 — from 67 million to 17 billion per layer and head. Kernels like Flash Attention make this fast in practice, but the growth is still quadratic.
Memory for the KV cache. Every token in context leaves keys and values at every layer, held for the rest of the generation. For a 70B-class model with grouped-query attention, about 320 KB per token: 42.9 GB for a single 131k-token conversation. That memory decides how many conversations a GPU can serve at once, and therefore the cost.
Training matters too. A model trained mostly on short sequences does not automatically handle long ones well; long-context ability has to be trained, often in a late stage with position-encoding adjustments.
Bigger is not the same as better
A model that accepts 128k tokens does not necessarily use all of them equally well.
- Lost in the middle. Many models recall information near the start and end of a long context better than information buried in the middle.
- Distraction. Irrelevant material competes for attention. Adding loosely related documents can lower accuracy compared with adding only the relevant one.
- Latency. Processing a long prompt takes time before the first output token appears, even with fast hardware.
- Cost. Input tokens are billed. A full window on every request is expensive.
The practical lesson is to treat context as a scarce resource to be spent deliberately, not a container to be filled.
Conversations re-send everything
Because the model is stateless, a chat application sends the entire conversation with every new message. If each turn adds 500 tokens, turn 20 sends 10,000 tokens — and the total sent over the whole conversation is 500 × (1 + 2 + … + 20) = 105,000 tokens, more than ten times what is finally in context.
Prompt caching softens this: a provider can reuse the computation for a prefix it has already processed, charging a fraction of the price and skipping most of the prefill time. Keeping the stable part of the prompt — system instructions, tool definitions, early history — unchanged and at the front is what makes caching effective.
Managing context
When conversations or documents outgrow the window, or simply grow expensive, systems choose what to keep:
- Retrieval — store documents outside the context and bring in only the passages relevant to the current question.
- Summarisation and compaction — replace old turns with a summary that preserves decisions, facts and open tasks.
- Truncation — drop the oldest turns, keeping the system prompt and recent exchanges.
- Structured memory — save key facts to an external store and reinsert only those that matter.
- Ordering — place the most important material at the start or end, where it is most reliably used.
What to take away
The context window is the single input that holds everything a model can see: instructions, history, documents, tool results and its own answer. At 128k tokens it is about 306 pages, but filling it costs 256 times the attention arithmetic of an 8k context and about 42.9 GB of cache on a 70B-class model.
Models do not use long contexts uniformly, and chats re-send their whole history each turn — 105,000 tokens over 20 turns for 10,000 in context. Spend context deliberately: retrieve, summarise, order carefully and keep stable prefixes cacheable.