A how-to, four ways
The task is setting the context length in Ollama, from excerpts of three of Ollama’s own documentation pages. Every version below got the same brief.
The brief
Every model got this text and nothing else.
Write a how-to article for our documentation site: how to set the context length in Ollama. The readers are developers who already have Ollama installed. Use these excerpts from our existing documentation pages as your source. Reply with the article only, in Markdown.
From the context length page:
- Context length is the maximum number of tokens that the model has access to in memory.
- Ollama defaults to these context lengths based on VRAM: under 24 GiB VRAM, 4k context; 24 to 48 GiB, 32k context; 48 GiB or more, 256k context.
- Tasks that need a large context, like web search, agents, and coding tools, should be set to at least 64000 tokens.
- A larger context length increases the memory needed to run a model. Make sure you have enough VRAM.
- Cloud models are set to their maximum context length by default.
- In the Ollama app: change the context length slider under Settings.
- When serving Ollama: OLLAMA_CONTEXT_LENGTH=64000 ollama serve
- Check the allocated context length and whether the model is offloaded to the CPU with ollama ps. Its output has PROCESSOR and CONTEXT columns, for example "100% GPU" and "131072".
From the FAQ ("How can I specify the context window size?"):
- By default, Ollama uses a context window size of 4096 tokens.
- Override it with the OLLAMA_CONTEXT_LENGTH environment variable, for example OLLAMA_CONTEXT_LENGTH=8192 ollama serve
- In ollama run, use: /set parameter num_ctx 4096
- In the API, set "num_ctx" in "options", for example: curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "Why is the sky blue?", "options": {"num_ctx": 4096}}'
From the Modelfile reference:
- PARAMETER num_ctx 4096 sets the context window size.
- Parameter table: num_ctx, "Sets the size of the context window used to generate the next token. (Default: 2048)"
Four versions
Set the context length in Ollama
This procedure sets the context length for your models, which is the maximum number of tokens a model has access to in memory. When you finish, ollama ps shows the context length Ollama allocated.
Before you begin
- Install Ollama.
- Find out how much video memory (VRAM) your GPU has.
Cloud models are set to their maximum context length by default, so you don't need to change it for them.
Choose a context length
Ollama picks a default context length from your VRAM:
- Under 24 GiB: 4k tokens
- 24 to 48 GiB: 32k tokens
- 48 GiB or more: 256k tokens
Tasks that need a large context, such as web search, agents, and coding tools, need at least 64,000 tokens. The examples below use 64,000.
Set the context length
A larger context length increases the memory a model needs. Make sure you have enough VRAM before you raise it.
Use one of these methods.
In the Ollama app
- Go to Settings.
- Move the context length slider to the value you want.
When you serve Ollama
Set the OLLAMA_CONTEXT_LENGTH environment variable when you start the server:
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
In a chat with ollama run
- Start a chat with
ollama run <model>. Replace<model>with the name of the model you want to use. - At the chat prompt, set the parameter:
text /set parameter num_ctx 64000
In an API request
Set num_ctx in options in your request:
curl http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "Why is the sky blue?", "options": {"num_ctx": 64000}}'
Confirm the context length
- Run
ollama ps. - Find the CONTEXT column. It shows the context length Ollama allocated, for example
131072. Check it against the value you set. - Find the PROCESSOR column. It shows whether the model is offloaded to the CPU, for example
100% GPU.
Next steps
- To set the context length in a Modelfile, see the
PARAMETER num_ctxentry in the Modelfile reference. - For more on context length, see the context length page.
Show the Check and Review Log
Log: how-to (Watership standard)
Route: start.md, Task type (types/task.md, types/task.rules.md), voice.md, slop-guard.md, never.md.
(a) Check counts
- First draft: 0 errors, 2 warnings, 0 suggestions.
- Final: 0 errors, 0 warnings, 0 suggestions (markdown format).
(b) Main changes
SpellOutFirst(check): "VRAM" is now "video memory (VRAM)" at first use.ThousandsSeparators(check): "64000" in prose is now "64,000". Code samples keep 64000 as typed.proc-warning-at-point(review): the memory warning moved from the prerequisites to the top of "Set the context length", right before the steps that raise the value.- Lists rule on one-item lists (review): the serve and API methods had one-item numbered lists, so each is now one sentence plus a code block. The app and ollama run methods keep numbered steps.
proc-verify-end(review): the last section confirms the result withollama psand names both columns.floor-writing-about-the-documentand claims (review): dropped a draft bullet that said which settings "your VRAM supports", which the brief does not state.proc-one-action-per-step(review): app step split into "Go to Settings" and "Move the slider".
(c) Findings kept
- None flagged by the check remain. Two judgment calls: the tier list keeps the brief's "4k, 32k, 256k" wording rather than converting to exact token counts, because the brief gives no exact figures; "131072" in the confirm step is the brief's own example output, not the value the steps set.
(d) Questions for the documentation owner
- Source conflict on the default. The context length page gives 4k, 32k, or 256k by VRAM. The FAQ says the default is 4096. The Modelfile reference table says "Default: 2048". The article states only the VRAM tiers (the most specific source, and consistent with the FAQ for under 24 GiB) and tells the reader to confirm with
ollama ps. It does not mention 2048. Which is correct, and should the Modelfile table be updated? - Is "4k" exactly 4096 tokens, and are 32k and 256k exactly 32768 and 262144?
- What is the exact label of the context length slider in Settings? The article calls it "the context length slider" without bold.
- Does
/set parameter num_ctxand the APInum_ctxapply only to that chat or request, and does a Modelfile needollama createto take effect? The brief does not say, so the article does not claim either. - Does
OLLAMA_CONTEXT_LENGTHneed a server restart, and what is the syntax on Windows? Not in the brief, so omitted. - Does
ollama pslist only loaded models? The step assumes a model is running; the brief does not say. - Link targets for the context length page and Modelfile reference. The article names them without URLs because none were supplied.
How to set the context length in Ollama
Context length is the maximum number of tokens a model can access in memory. A larger context lets a model work with more information, but it also uses more memory. Make sure you have enough VRAM for the model and context length you choose.
Choose a context length
Ollama’s default context length depends on available VRAM:
| VRAM | Default context length | |---|---:| | Under 24 GiB | 4k tokens | | 24–48 GiB | 32k tokens | | 48 GiB or more | 256k tokens |
For tasks that need a large context, such as web search, agents, or coding tools, set the context length to at least 64,000 tokens. Cloud models use their maximum context length by default.
Set the context length
Choose the method that matches how you use Ollama.
In the Ollama app
Open Settings and adjust the context length slider.
When serving Ollama
Set OLLAMA_CONTEXT_LENGTH when you start the server:
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
Use a value that suits your task and available VRAM.
In an interactive session
In ollama run, set the num_ctx parameter:
/set parameter num_ctx 4096
In an API request
Set num_ctx in the request’s options object:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Why is the sky blue?",
"options": {
"num_ctx": 4096
}
}'
In a Modelfile
Add a PARAMETER num_ctx line to the Modelfile:
PARAMETER num_ctx 4096
Check the allocated context length
Run ollama ps to check the allocated context length and whether the model is offloaded to the CPU. In the output, the PROCESSOR column shows where the model is running, and CONTEXT shows the context length. For example, 100% GPU and 131072 indicate GPU processing with a context length of 131,072 tokens.
How to set the context length in Ollama
Context length is the maximum number of tokens a model can hold in memory at once. This guide shows how to change it in the Ollama app, when serving Ollama, in an interactive session, through the API, and in a Modelfile. It also shows how to confirm the value that was actually allocated.
Before you begin
Ollama picks a default context length based on your available VRAM:
| VRAM | Default context length | | --- | --- | | Under 24 GiB | 4k | | 24 to 48 GiB | 32k | | 48 GiB or more | 256k |
Cloud models are set to their maximum context length by default.
Keep two things in mind before you raise the value:
- Tasks that need a large context, such as web search, agents, and coding tools, should be set to at least 64000 tokens.
- A larger context length increases the memory a model needs. Make sure you have enough VRAM.
Set the context length
Choose the method that matches how you run Ollama.
In the Ollama app
Open Settings and move the context length slider to the value you want.
When serving Ollama
Set the OLLAMA_CONTEXT_LENGTH environment variable when you start the server:
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
This applies to every model the server loads.
In an ollama run session
Use the /set parameter command inside the session:
/set parameter num_ctx 4096
Through the API
Set num_ctx in the options object of your request:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Why is the sky blue?",
"options": {
"num_ctx": 4096
}
}'
This applies to that request only.
In a Modelfile
Add a PARAMETER line to your Modelfile:
PARAMETER num_ctx 4096
num_ctx sets the size of the context window used to generate the next token.
Verify the context length
With a model loaded, run:
ollama ps
The output includes a PROCESSOR column and a CONTEXT column. For example, 100% GPU and 131072 mean the model is fully on the GPU with a 131072-token context. If PROCESSOR shows part of the model on the CPU, the context length you chose may be larger than your VRAM can hold. Lower the value or free up memory.
How to Set the Context Length in Ollama
This article walks you through every supported method for configuring the maximum number of tokens that an Ollama model can keep in memory. It assumes you already have Ollama installed and are comfortable with the command line, API calls, or the Ollama app.
---
Why Context Length Matters
- Context length is the maximum number of tokens a model can “see” at once.
- A larger window lets your model handle longer documents, conversations, or multi‑step reasoning.
- The trade‑off: the larger the context, the more VRAM (GPU memory) the model consumes.
---
VRAM‑Based Defaults
| VRAM | Default Context Length | |------|------------------------| | < 24 GiB | 4 k tokens | | 24–48 GiB | 32 k tokens | | ≥ 48 GiB | 256 k tokens |
Tip: For tasks that need a very long history (web search, agent workflows, coding assistants) set the context to ≥ 64 k tokens.
Caution: Cloud models are always set to their maximum context length by default, regardless of local VRAM.
---
Methods to Override the Context Length
| Method | How to Use | Example | |--------|------------|---------| | Environment Variable | OLLAMA_CONTEXT_LENGTH | OLLAMA_CONTEXT_LENGTH=64000 ollama serve | | Ollama Run | /set parameter num_ctx | ollama run llama3.2 /set parameter num_ctx 8192 | | API Call | "num_ctx" inside options | curl http://localhost:11434/api/generate -d '{"model":"llama3.2","prompt":"Why is the sky blue?","options":{"num_ctx":4096}}' | | Modelfile | PARAMETER num_ctx <size> | PARAMETER num_ctx 4096 | | Ollama App | Slider under Settings | (See screenshot in docs) |
Note: The Modelfile default is 2048 tokens, while the runtime default is 4096 tokens. The environment variable and API options override both.
1. Using OLLAMA_CONTEXT_LENGTH
This is the quickest way to apply a context length for all models you start in that terminal session:
# 64 k tokens for every model you serve
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
You can also set it globally in your shell profile (e.g., .bashrc or .zshrc):
export OLLAMA_CONTEXT_LENGTH=64000
2. Using ollama run
If you only want to change the context for a single invocation:
ollama run llama3.2 /set parameter num_ctx 8192
This sends the
num_ctxchange to the model after it’s loaded, so the new context length is used only for that run.
3. Using the API
When calling the generate endpoint, specify the desired context in the request payload:
{
"model": "llama3.2",
"prompt": "Explain quantum entanglement.",
"options": {
"num_ctx": 8192
}
}
curl http://localhost:11434/api/generate \
-H "Content-Type: application/json" \
-d '{"model":"llama3.2","prompt":"Explain quantum entanglement.","options":{"num_ctx":8192}}'
4. Setting it in a Modelfile
If you want the context length to be baked into a model you’ll reuse frequently:
# MyModel.modelfile
PARAMETER num_ctx 4096
Build or pull the model normally, and the num_ctx setting will be applied automatically.
5. Through the Ollama App
- Open the Ollama desktop app.
- Go to Settings → Advanced.
- Move the Context Length slider to the desired token count.
(The slider respects your GPU’s VRAM capacity; it won’t allow you to exceed what your hardware can handle.)
---
Verify the Current Context Length
After starting a model, run:
ollama ps
You’ll see columns like:
| NAME | PROCESSOR | CONTEXT | |------|-----------|---------| | llama3.2 | 100% GPU | 131072 |
PROCESSORtells you whether the model is on GPU or CPU.CONTEXTshows the actual context length in use.
If a model is offloaded to CPU, you’ll see 100% CPU – you may need more VRAM or a smaller context.
---
Tips & Best Practices
| ✅ | Recommendation | |---|----------------| | 1 | Match context length to VRAM – Don’t set 256 k tokens on a 4 GiB GPU. | | 2 | Use the environment variable for dev/test – It applies globally, reducing repetition. | | 3 | Document the context setting – Add a comment to your Modelfile or README so teammates know the limit. | | 4 | Check ollama ps after every change – Ensure the model didn’t offload to CPU unexpectedly. | | 5 | For cloud models – They’ll always run at the maximum context; you can’t reduce it locally. |
---
Quick Reference Cheat Sheet
# Set globally for this session
export OLLAMA_CONTEXT_LENGTH=64000
# Start the server with 64k context
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
# Single run with 8k context
ollama run llama3.2 /set parameter num_ctx 8192
# API call with 8k context
curl http://localhost:11434/api/generate \
-d '{"model":"llama3.2","prompt":"Text","options":{"num_ctx":8192}}'
# Modelfile example
PARAMETER num_ctx 4096
---
Happy modeling! If you run into memory issues or need help tuning other parameters, feel free to consult the rest of the Ollama documentation or reach out on the community forums.
What to look for
- The sources disagree, and the brief doesn’t say so. Ollama’s context length page gives a default of 4k, 32k, or 256k tokens depending on the GPU’s memory. Its FAQ says the default is 4096. Its Modelfile reference says 2048.
- GPT-6 and Claude Sonnet picked one answer without saying there was a question. Both present the memory-based table as the default, and neither mentions that the FAQ and the Modelfile reference say something else. A reader can’t tell a fact was in doubt.
- gpt-oss 20B noticed half of it. It shows the same table, then says a few paragraphs later that the Modelfile default is 2048 and the runtime default is 4096, without reconciling the three. It also adds a cheat sheet, tips, and a Modelfile example the notes don’t contain.
- The standard version doesn’t settle what it can’t know. It gives the memory-based table, the most specific source, and tells the reader to confirm the real value with
ollama ps. Its log opens with the conflict as a question for the documentation owner, followed by six more things the excerpts leave open. Under the standard, a writer can’t decide a fact. Only the fact’s owner can. - It follows the task template. The title is the goal, the requirements come first, each method is a short set of steps, and the last section shows how to confirm the change worked.