Context Windows
The short version: a context window is the number of tokens a model can attend to at once. It is a ceiling, not a budget. Models advertise 128K, 256K, or 1M tokens; what they can actually use is usually smaller, and what your machine can hold is smaller still.
What counts as context
Everything the model sees in one forward pass:
- the system prompt and instructions
- the conversation history
- documents, code, and retrieved chunks
- tool outputs and search results
- reasoning traces, if the model exposes them
The window is shared by all of it — and by the output. A 128K window with a 20K system prompt and 80K of documents leaves 28K for the conversation and the answer. If you ask for a 4K response, your input budget is smaller still.
Token math
A rough English conversion is 1 token ≈ 0.75 words.
| Window | Rough words | Rough pages |
|---|---|---|
| 8K | 6,000 | ~12 |
| 32K | 24,000 | ~48 |
| 128K | 96,000 | ~190 |
| 1M | 750,000 | ~1,500 |
Code, JSON, and non-English text tokenize differently — sometimes much worse. Treat these as order-of-magnitude, not accounting.
Advertised vs. effective
The spec sheet is the maximum the model can accept. The effective window is how much it can actually use.
- RULER tested models claiming 32K or more; only about half held up at 32K. GPT-4’s advertised 128K behaved like roughly 64K.
- NoLiMa removed the literal word overlap between question and answer. At 32K, 10–11 of the 12–13 models tested dropped below 50% of their short-context baseline. GPT-4o fell from 99.3% to 69.7%.
- Lost in the Middle found a U-shaped curve: models do best when the relevant fact is at the start or end, and worst when it is buried in the middle.
- Context rot is the broader pattern: as the input grows, models get distracted, give up early, or answer confidently wrong — often before the window is full.
So “1M context” means “accepts up to 1M tokens,” not “reliably uses 1M tokens.” The gap is the whole story.
What context costs
The KV cache grows linearly with context. For a Llama 3.1 8B — 32 layers, 8 KV heads, 128-dim, FP16 — that’s about 128 KiB per token. At 128K tokens, the cache alone is ~16 GiB, roughly twice the model’s weights. A 70B model with 80 layers and 8 KV heads is ~320 KiB per token: ~40 GiB at 128K.
That’s why the window is not free. Use the VRAM Fit Calculator to see the memory cost, and KV Cache Compression to reduce it.
What context does to speed
Growing context hits two different phases.
Prefill — time to first token. Before the model writes anything, it processes the whole prompt. With full attention, every token compares against every other token, so attention compute grows roughly quadratically with prompt length; the MLP part grows linearly. In practice, doubling the prompt often more than doubles TTFT. Modern architectures soften this — sliding-window and sparse attention cut the number of tokens each token attends to, and GQA/MLA shrink the cache — but they don’t make long prompts free.
Decode — tokens per second. Every new token attends to the KV cache. That cache is read from memory on every step, so decode is memory-bandwidth-bound: a bigger cache means more bytes per token and higher inter-token latency. Generation doesn’t get quadratically slower, but it gets steadily slower as the cache grows.
Throughput and concurrency. Long contexts consume more KV cache per request, leaving less room for batching. On a server, that means fewer concurrent users and a higher cost per token. On a local machine, it means less headroom and more swapping.
| As context grows | Time to first token | Decode speed | Memory | Quality |
|---|---|---|---|---|
| 2× prompt | Often superlinear with full attention | Slower per token | ~2× KV cache | Often worse in the middle |
| 10× prompt | Can dominate total latency | Noticeably slower | ~10× KV cache | Context rot becomes visible |
What helps:
- Prompt caching for repeated prefixes — avoids recomputing the same prompt.
- Retrieval — keep the prompt short instead of stuffing the window.
- KV cache quantization — reduces the bytes read per token, which can speed decode.
- Sparse/sliding-window attention — cuts attention compute, though the cache still grows unless the architecture compresses it.
- Compaction — summarize old turns in agents instead of carrying the whole history.
Rule of thumb: context length is a speed dial as well as a quality dial. A 128K prompt is not just more expensive in VRAM; you pay for it once during prefill and then pay again on every generated token.
What happens when the context limit is reached
There are three different limits, and they fail differently:
| Limit | Set by | What happens when you cross it |
|---|---|---|
| Architectural max | The model | The engine rejects the request or refuses to load the context; there is no way around it except a different model |
Runtime context (num_ctx, max-model-len) | The engine or user | The engine truncates, slides the window, or errors — often silently |
| Effective context | Model quality | The model still answers, but accuracy, recall, and instruction-following degrade |
The architectural max is the spec sheet. The runtime context is what you configured. The effective context is what the model can actually use. Most local failures happen at the runtime limit, not the architectural one.
Before generation: the prompt doesn’t fit
If the prompt alone exceeds the limit, the runtime has two choices: reject the request or truncate the prompt. Hosted APIs usually reject it with a context-length error. Local runtimes often drop the oldest tokens and proceed — which means the model never sees the system prompt or the start of the document. That’s the dangerous one.
During generation: input + output hit the ceiling
The model doesn’t enforce the limit; the runtime does. When the prompt plus the generated tokens reach the configured context, one of these happens:
- The response stops. Hosted APIs typically return
finish_reason: "length"; the text ends mid-sentence or mid-JSON. The model doesn’t know it was cut off, so it may not warn you. - The request fails. Some servers reject the request outright if the prompt alone exceeds the limit.
- The window slides. Some local runtimes drop the oldest tokens and keep generating. The answer continues, but the system prompt and early context are gone.
- The agent loop breaks. A truncated tool call or JSON object can’t be parsed; the agent retries, loops, or fails.
After generation: the output budget is gone
If you reserved too little output space, the answer is cut off even though the prompt fit. The model may stop mid-code-block, mid-table, or mid-tool-call. This is why you reserve output tokens up front: context is shared between input and output.
What it looks like in practice
- The model ignores a constraint you set at the start.
- It contradicts an earlier answer.
- It forgets a name, a number, or a tool result.
- It repeats itself or starts hallucinating details.
- It returns truncated JSON or an incomplete tool call because the output budget was eaten by the input.
Detect it: compare the prompt token count (prompt_eval_count in Ollama, usage fields in API responses) to your configured context. Put a canary instruction at the top — “If you can read this, start your answer with OK.” If the canary disappears, you’ve been truncated.
Handle it: reserve output tokens, pin the system prompt, summarize or retrieve instead of stuffing, and prefer runtimes that fail loudly over ones that truncate silently.
The key distinction: hitting the limit is not a model event; it’s a runtime policy. Two engines running the same model can behave differently at the same token count.
Your runtime probably isn’t using the full window
Local runtimes often default to a much smaller context than the model supports:
- Ollama defaults to 4K below 24 GB VRAM, 32K for 24–48 GB, and 256K for 48 GB+ (v0.15.5+). Check with
ollama ps. - llama.cpp uses
-c/--ctx-size. - vLLM uses
--max-model-len. - LM Studio exposes a context-length setting per model.
If you don’t set it deliberately, you may be running a 128K model at 4K and wondering why it forgets the start of the conversation.
How to use context well
- Retrieve, don’t dump. Pull the relevant 5–20K tokens instead of pasting 200K.
- Put key facts at the edges. Instructions first, the question last; the middle is where facts go to die.
- Compact history. Summarize older turns, keep the recent ones verbatim.
- Use prompt caching. It doesn’t enlarge the window, but it avoids recomputing the same prefix.
- Measure your effective window. Run a needle test, a multi-hop question, and a real task at your actual prompt length. Test facts in the middle, not just the start.
Myths
- “1M context means 1M usable tokens.” No — it means 1M accepted tokens.
- “More context is always better.” No — it costs memory, latency, and attention.
- “Context window equals memory.” No — the window is a token count; the KV cache is the memory bill.
- “Long context replaces RAG.” Sometimes; for large corpora, retrieval is still cheaper and more reliable.
- “I should fill the window.” No — a tight, well-ordered context usually beats a stuffed one.
Bottom line: advertised context is a ceiling. Effective context is what the model can actually use, memory is what you can afford, and speed is what you pay at prefill and decode. Set your runtime’s context deliberately, leave room for the output, and don’t confuse “accepts 1M tokens” with “thinks clearly across 1M tokens.”
