How to set the context length on Windows

There are three places to set it. Pick the one that matches how your app talks to Ollama. Ollama reads the variable only when it starts, so after any change, quit it completely (including the icon in the system tray) and open it again.

Three ways to raise the context (PowerShell)
  1. For the whole server, installed app: [Environment]::SetEnvironmentVariable('OLLAMA_CONTEXT_LENGTH', '16384', 'User') and then quit Ollama from the tray and open it from the Start menu
  2. For the whole server, portable zip: $env:OLLAMA_CONTEXT_LENGTH = '16384'; Start-Process C:\ollama\bin\ollama.exe -ArgumentList serve -WindowStyle Hidden
  3. For one request (native API /api/generate or /api/chat): add options = @{ num_ctx = 16384 } to the request body
  4. For a model with its own name, a Modelfile with three lines: FROM gemma4:12b · PARAMETER num_ctx 16384 · PARAMETER temperature 0
  5. Create and use that model: ollama create gemma4-16k -f .\Modelfile-example.txt and then ollama run gemma4-16k

The server variable and num_ctx were tested with Ollama 0.40.14. The Modelfile route is the one in Ollama’s documentation; we didn’t test it. The app’s context slider wasn’t tested either.

The trap: num_ctx doesn’t work through the OpenAI-compatible API

Many apps don’t call Ollama’s native API but the OpenAI-compatible one at http://127.0.0.1:11434/v1. OpenCode does, many editor extensions do, and so does almost any program that says “OpenAI-compatible”. Ollama’s documentation says it plainly: that API has no way to set the context. Whatever you put in num_ctx never arrives, and the model keeps loading with 4,096.

With those apps you have two options: raise the context for the whole server with OLLAMA_CONTEXT_LENGTH, which is what worked with OpenCode, or create a model with a Modelfile and pick that model name in the app, which is the route the documentation recommends. If OpenCode is the reason you’re here, read OpenCode with Ollama not working too.

How to check that the new context is really in use

Don’t trust the setting; check what Ollama loaded. Run these with the model loaded from your own app, not from a separate console.

  • The log: the “server config” line must show OLLAMA_CONTEXT_LENGTH:16384 (or your number). If it shows 0, the variable isn’t set for the Ollama process: you set it in another window, or Ollama didn’t fully restart.
  • ollama ps: the CONTEXT column must show the new number. If it still says 4096, the app isn’t getting the context you set.
  • A real request: the tokens read (prompt_eval_count in the API response) should match what you sent. If tokens read plus tokens written add up exactly to the context, it was cut.
  • The trap question: ask about a fact that appears only in the last paragraph of your document. If the model finds it, it’s reading the whole thing.
Read the value Ollama actually loaded (PowerShell)
  1. Select-String -Path "$env:LOCALAPPDATA\Ollama\server.log" -Pattern 'OLLAMA_CONTEXT_LENGTH:\d+' | Select-Object -Last 1 | ForEach-Object { $_.Matches.Value }
  2. ollama ps
  3. On startup the log also says what it chose on its own: msg="vram-based default context" total_vram="15.9 GiB" default_num_ctx=4096

Real line from the Ollama 0.40.14 log on a 16 GB card.

How you recognize a cut-off context

There is no error message. The model answers with confidence, but from only part of what you gave it. In the log, a “truncating input prompt” warning or truncated = 1 at the end of a request is the proof.

  • You ask about something at the end of a document and the model says it isn’t there, or makes it up.
  • With several documents, it answers well about the first one and badly about the rest.
  • An agent behaves as if it never read your request: it plans another task, says it has no access to files, or repeats the same tool over and over.

Which number to choose, and what it costs

Ollama sets the default from your graphics card memory: 4k tokens below 24 GiB, 32k between 24 and 48 GiB, and 256k from 48 GiB up (context length in the Ollama documentation). To pick yours, count your document plus your instructions, add what you expect in reply (a few hundred tokens for a summary, more than a thousand for a table or a report), add a lot more if the model reasons, and round up to 8,192, 16,384 or 32,768. For agents, the documentation recommends at least 64,000; we saw OpenCode work with 32,768.

Raising it barely costs speed. With 32,768 of context and a text of about 9,400 tokens, qwen3.5:4b wrote at 86 tokens per second, qwen3.5:9b at 55 and gemma4:12b at 80. What it costs is graphics card memory: go too far and the model stops fitting. How much memory each extra token takes depends on the model; we measured it in how much VRAM context uses in Ollama.

A real example of why 4,096 is too short: fourteen short internal policy documents took between 4,200 and 4,350 tokens depending on the model, so they no longer fit by default. With 16,384 of context, the two local models answered 17 and 18 of the 18 test questions correctly.

Frequently asked questions

What is the default context length in Ollama?On a graphics card with less than 24 GiB of memory, 4,096 tokens. The log says so on startup in the “vram-based default context” line.

Why doesn’t num_ctx change anything in my app?Because your app probably uses the OpenAI-compatible API (/v1), which can’t carry num_ctx. Use OLLAMA_CONTEXT_LENGTH or a Modelfile model instead.

I set OLLAMA_CONTEXT_LENGTH and ollama ps still shows 4096. Why?Ollama only reads it at startup. Quit it completely, including from the tray, and open it again; if you set it with $env: in a window, it only applies to an Ollama started from that same window.

Does a bigger context make Ollama slower?Barely, in our tests; it mostly costs graphics card memory. If the model no longer fits entirely on the card, then it does get much slower.