6 min read

Ollama Context Length: How to Set It and What It Costs

How to set Ollama's context length with num_ctx or OLLAMA_CONTEXT_LENGTH, why the default differs by machine, and how much memory each setting reserves.

By Cogniq Labs ResearchEvidence policy

InferenceLanguage Models
Isometric illustration of a long track of memory slots, a short glowing section in use at the front and the rest reserved but empty, stretching into the distance.

Ollama's context length decides how much of a conversation or document the model can see at once, and how much memory Ollama sets aside to hold it. Set it with OLLAMA_CONTEXT_LENGTH for the whole server, or with num_ctx per request or per model, and choose the length you need, not the largest the model allows. A context that is too long wastes memory and can push the model off your GPU. One that is too short hides part of your input from the model.

This note covers how to set it, why the default is different on different machines, and how to work out what it costs.

The default depends on your machine

The answer to "what is Ollama's default context length?" has changed over time, and Ollama's own pages disagree:

Source Default it gives
Context length page Based on VRAM: 4K below 24 GiB, 32K from 24 to 48 GiB, 256K at 48 GiB or more
FAQ 4,096 tokens
Modelfile reference 2,048 for num_ctx

The context length page is the most specific, so it most likely describes recent versions. Two consequences follow. A machine with a large GPU can now get a very long context, and a lot of reserved memory, without anyone asking for it. And the same model can behave differently on two computers, because one quietly gets 4K tokens and the other 32K.

Don't guess. Load the model and run ollama ps. The CONTEXT column shows the length actually in use.

How to set it

There are four places to set the context length. Use the one that matches how you run Ollama.

For the whole server. Set an environment variable when starting Ollama:

OLLAMA_CONTEXT_LENGTH=16384 ollama serve

On a desktop install, the Ollama app also has a context length slider in its settings.

For one interactive session. Inside ollama run:

/set parameter num_ctx 16384

This lasts until you leave the session.

For one API request. Put num_ctx in the options object. The FAQ gives this example:

curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "Why is the sky blue?",
  "options": { "num_ctx": 4096 }
}'

Saved with a model. Add a line to a Modelfile and create a new model from it:

FROM llama3.2
PARAMETER num_ctx 16384
ollama create llama3.2-16k -f Modelfile

This is the most reliable option when another tool calls Ollama for you, because the setting travels with the model name.

Why your setting seems to be ignored

If you set a context length and ollama ps shows something else, one of the other settings is winning. A value sent with a request applies to that request, and some tools that call Ollama send their own num_ctx with every request, which overrides what you set on the server. A Modelfile parameter applies to models created from it. The server default applies when nothing else does.

When in doubt, use the Modelfile approach and point your tool at the new model name. Then check ollama ps while the tool is running, not afterwards.

What it costs in memory

Every token of context needs space in the KV cache, and Ollama reserves that space for the full context length when it loads the model. The size per token comes from the model's configuration:

bytes per token = 2 × layers × key-value heads × head dimension × bytes per value

For a Llama 3.1 8B style model, with 32 layers, 8 key-value heads and a head dimension of 128, at 16-bit, that is 131,072 bytes, or 128 KiB, per token. As illustrative arithmetic:

Context length KV cache at 16-bit At 8-bit (q8_0)
2,048 0.25 GiB 0.13 GiB
8,192 1 GiB 0.5 GiB
32,768 4 GiB 2 GiB
131,072 16 GiB 8 GiB
262,144 32 GiB 16 GiB

The weights of that model at 4-bit are about 5 GB, so at 128K tokens the reserved cache is about three times the size of the model. Ollama can only use as much context as the model supports; the 256K row applies to models of this shape that allow it, and shows how quickly the reservation grows. Larger models need more per token: a 70B model of the Llama 3 shape needs about 320 KiB per token, as we work through in how much VRAM you need for a 70B model.

Two settings multiply or divide these numbers:

  • Parallel requests. The FAQ says parallel processing increases the context size by the number of parallel requests, giving the example that a 2K context with 4 parallel requests results in an 8K context. Memory scales with OLLAMA_NUM_PARALLEL times the context length.
  • Cache precision. OLLAMA_KV_CACHE_TYPE=q8_0 uses about half the memory of the default f16, with what the FAQ calls a very small loss in precision. q4_0 uses about a quarter, with a small to medium loss.

Choosing a length

The context length page suggests at least 64,000 tokens for tasks such as web search, agents and coding tools, which pass large amounts of text to the model. For chat, short documents and most question answering, 8K to 16K is usually enough. The right number is the longest input you actually send, plus room for the reply, with a margin.

A longer context is not free in quality either. Models use the middle of a long prompt less well than the start and the end, which we cover in your context window is smaller than advertised. A 128K window that is mostly filled with loosely relevant text can give worse answers than a well-chosen 16K one.

Context length is not output length

Two settings are easy to confuse. num_ctx is the size of the whole window: the prompt, the conversation so far and the reply together. num_predict is the most the model may write in one reply. The Modelfile reference describes num_predict as the maximum number of tokens to predict, with a default of -1, meaning no limit.

They interact. A long prompt leaves less room in the window for the answer, so a 4K context filled by a 3,500-token document leaves the model only a few hundred tokens to reply before it runs out of space. If answers stop short on long inputs, the window is the likely limit, not num_predict. Size num_ctx for the longest prompt plus the longest answer you expect.

Different lengths for different jobs

There is no need for one context length to fit everything. Because a Modelfile setting travels with the model name, you can create two versions of the same model and choose by task:

# Modelfile.chat
FROM llama3.1:8b
PARAMETER num_ctx 8192
# Modelfile.agent
FROM llama3.1:8b
PARAMETER num_ctx 65536
ollama create llama3.1-chat -f Modelfile.chat
ollama create llama3.1-agent -f Modelfile.agent

Both use the same weights on disk. Only the one you load reserves its context, so everyday chat stays light and the long window is there for the jobs that need it. Remember that a model stays loaded for 5 minutes after use by default, according to the FAQ, so switching between the two in quick succession can hold both in memory. ollama stop unloads one straight away.

A longer window does not make short prompts slower

A larger num_ctx reserves more memory, but a short prompt still takes about the same time to process, because the model only works on the tokens you actually send. What slows down is a long prompt: reading 50,000 tokens takes far longer than reading 500, whatever the window size. The exception is the one above. If the larger reservation pushes part of the model onto the CPU, every request gets slower, short or long.

Check that it still fits

After changing the context length, run the model and check ollama ps again:

  • PROCESSOR shows 100% GPU. Everything fits. A longer context costs memory, and more work on long prompts, but generation speed should stay about the same.
  • PROCESSOR shows a CPU/GPU split. The extra memory pushed part of the model into system memory, and generation will be much slower. Lower the context, use an 8-bit cache, or reduce parallel requests until it shows 100% GPU again.

If Ollama fails to load the model at all, see why Ollama runs out of memory for the full list of fixes.

If a tool cannot reach Ollama at all after you change settings, see Ollama connection refused.

What we do not know

We have not measured these settings; the memory figures are arithmetic from published configurations, and the real reservation also includes some overhead. Ollama's defaults have changed between versions and its pages disagree, so the CONTEXT column on your own machine is the number to trust. Some newer model architectures reduce the per-token cache in ways this formula does not capture, so check the model's own documentation for those.

Sources

  1. Context length — Ollama
  2. Ollama FAQ — Ollama
  3. Modelfile reference — Ollama

Frequently asked questions

What is the default context length in Ollama?

It depends on the version and your machine. The current context length page says Ollama picks a default from your VRAM: 4K tokens below 24 GiB, 32K from 24 to 48 GiB, and 256K at 48 GiB or more. Older docs give 4,096 or 2,048. Run ollama ps to see the context a loaded model is actually using.

How do I change the context length in Ollama?

For the whole server, start it with OLLAMA_CONTEXT_LENGTH set. For one session, use /set parameter num_ctx in ollama run. For one API request, put num_ctx in the options object. To save it with a model, add PARAMETER num_ctx to a Modelfile and run ollama create.

How much memory does a longer context use in Ollama?

Memory grows in proportion to the context length. For a Llama 3.1 8B style model at 16-bit, the cache is about 128 KiB per token: 0.5 GiB at 4K, 4 GiB at 32K and 16 GiB at 128K. Parallel requests multiply it, and an 8-bit cache halves it.

Does a bigger num_ctx slow Ollama down?

It does when the extra memory pushes part of the model off the GPU. Then the model runs partly on the CPU and generates much more slowly. ollama ps shows this as a CPU/GPU split. If it shows 100% GPU, a larger context mostly costs memory, plus more work on long prompts.

Why does Ollama ignore my num_ctx setting?

Usually because a different setting takes effect: a value in the request, a Modelfile, or the server default. A tool that calls Ollama may also send its own num_ctx with requests. Check the CONTEXT column in ollama ps to see what was actually loaded.

Related reading