How much can the model keep in mind?
The context window is everything the model can see at once: the system promptSystem promptStanding instructions placed before the conversation.Read more, the whole conversation, any files you pasted and the answer it is writing. It is measured in What is a token?. Anything outside the window does not exist for the model.
Working memory, in tokens
Two numbers matter, and they are often confused. The model's maximum context is what it was trained to handle, often 128K tokensTokenA chunk of text, usually part of a word. Models read and write in tokens.Read more or more. The runtimeRuntimeThe software that loads a model file and runs it.Read more's context setting is how much memory it actually sets aside, and that is often far smaller.
The second number is the one you live with. A model that supports 128K, loaded with a 4K setting, has a 4K window.
| Context | Roughly holds |
|---|---|
| 4K tokens | A long chat, or about 3,000 words |
| 32K tokens | A short codebase tour, or a 60-page report |
| 128K tokens | A small book |
More context costs memory and prefill time
The runtime keeps every token in the window in the KV cacheKV cacheSaved work from earlier tokens, so the model does not reread the whole conversation for every new token.Read more, which sits in memory next to the model. The cache grows in a straight line with the context. Double the window and you double the cache.
For a typical 8B model, each token takes about 128 KB at 16-bit. A 32K window is then about 4 GB on top of the model's own 5 GB. For a 70B model the same window is about 10 GB. Will the model fit in my memory? shows how that fits into your budget.
A long context also costs time. Every token you send has to be read during prefillPrefillThe phase where the model reads your prompt. Limited by compute.Read more before the first word appears. A full 64K window can mean a wait of many seconds, or minutes on slow hardware. What happens when I press enter? explains why.
The default-too-small trap
Runtimes pick a small default context so that models load on modest machines. That is sensible for a first chat. It quietly breaks anything that needs more, and nothing warns you.
Agents and coding tools are the usual victims. They send a long system prompt, tool definitions and file contents with every request. The tool fills the window before your question even arrives.
For agents and coding tools, set the context to at least 32K tokens. 64K tokens is comfortable.
How to raise it
Each runtime calls the setting something different. Set it before loading the model, because the memory is reserved at load time.
Ollama. The setting is num_ctx. Set it for one session from inside ollama run, or for every model with an environment variable before starting the server.
# inside an ollama run session
/set parameter num_ctx 32768
# for the whole server
OLLAMA_CONTEXT_LENGTH=32768 ollama serveApps that call Ollama's API can pass num_ctx in the request options. Many coding tools do not, so the server default is the one that counts. How do I use Ollama? covers making it permanent with a Modelfile.
LM Studio. Open the model's load settings before loading it and raise Context Length. LM Studio shows the model's maximum next to the slider. How do I use LM Studio as a server? has the details.
llama.cpp. Use -c, also written --ctx-size, on llama-server or llama-cli.
llama-server -m model.gguf -c 32768What is llama.cpp? covers the other flags that affect memory.
A bigger window needs more memory. If the model only just fits, raising the context can push it into swapping or partial offloadOffloadRunning part of a model on the CPU because it does not fit in VRAM.Read more. Then you choose between a smaller quantizationQuantizationStoring each weight with fewer bits so the model takes less memory.Read more, a smaller model or a quantized KV cache, covered in How do I make it faster?.
What happens when you overflow
When the conversation outgrows the window, something has to go. Most runtimes drop or cut the oldest tokens to make room. The model then forgets the start: your instructions, the system prompt, the file you pasted first.
The symptoms are confusing because the model keeps going.
An agent forgets which tools it has and starts making up commands. A chat ignores rules you set at the beginning. A summary covers only the last part of a document. Why does my agent forget things or fail tool calls? lists these patterns.
Some runtimes stop with an error instead. That is the better failure, because at least you know.
Paste a long document into a chat with a small context, then ask what the first paragraph said. If the answer is wrong or vague, the start fell out of the window. Raise the context and ask again.
Go deeperThat's enough
Where the 128 KB per token comes from
Each layer stores a key and a value for every token. Their size is the number of key-value heads times the head size. A Llama-class 8B model has 32 layers, 8 key-value heads and a head size of 128.
That is 32 layers times 8 heads times 128 numbers times 2 for keys and values, times 2 bytes at 16-bit. The result is 131,072 bytes, or 128 KB per token. Other models use different shapes, so check the model card.
Quantizing the cache
Some runtimes store the KV cache at 8-bit instead of 16-bit, which halves its size. The quality cost is usually small at 8-bit and more noticeable below it. It is a separate setting from the model's own quantization.
Advertised context versus useful context
A model trained for 128K does not use all of it equally well. Recall is usually best near the start and the end of the window and weaker in the middle. For important material, put it near the end of the prompt, close to your question.
Sliding windows and other tricks
Some newer models only look back over a fixed recent window in most layers, with a few layers seeing everything. This cuts the cache size a lot. Runtimes have to support the scheme to get the saving, so the same model can use very different memory in two runtimes.