Originally published on kuryzhev.cloud
Failure scenario: the GPU is "in use" but the model crawls
A typical case of Ollama CPU offload goes like this. A developer has a 16 GB GPU and a mid-sized quantized model that answered quickly all week. Then they point a coding agent or IDE plugin at it and raise the context window to 32k so it can read more files. Generation drops from fluent to a token every few hundred milliseconds. Nothing errors, and the model still answers, only slowly.
nvidia-smi shows VRAM nearly full, so everything looks fine. But GPU utilization is low and a few CPU cores are pinned. Part of the model is running on the CPU, and Ollama did not announce it.
Short answer: run ollama ps and read the PROCESSOR column. If it says anything other than 100% GPU, the model plus its context cache no longer fits in VRAM. Lower num_ctx, quantize the KV cache, cut parallel slots, or pick a smaller model or quant until it reads 100% GPU again. This applies whenever a model that used to be fast slows down after a context, model or tooling change.
The symptom is easy to misdiagnose because nothing breaks. The server stays up, the API returns 200s, and quality is unchanged. Only latency gets worse. CPU-bound token generation can be several times slower than a fully GPU-resident run, and the exact ratio depends on your hardware.
Why it happens: weights are not the only thing in VRAM
Ollama loads model weights into VRAM, and it also allocates a KV cache for the context window. That cache grows linearly with context length, so doubling num_ctx roughly doubles it. A model whose weights fit with room to spare at a small context can overflow once the context is raised.
When the total does not fit, Ollama does not refuse to load. It splits the model by layers and keeps what fits on the GPU. The rest runs on the CPU from system RAM. Every token then crosses that boundary, and the slow CPU layers set the pace for the whole pass. This is the documented fallback behavior. It is also why a 10% spill can feel much worse than 10% slower.
Several things inflate memory without you touching the model:
- Client-side context overrides. Many tools send
options.num_ctxper request, overriding whatever you set in a Modelfile or on the server. Check what the client actually sends. - Parallel slots.
OLLAMA_NUM_PARALLELmultiplies the context memory, because each slot needs its own share. - A second loaded model. Models stay resident for the keep-alive period (five minutes by default). A chat model and an embedding model can share the card and push each other into offload.
- Default context changes. Recent Ollama releases may choose a default context based on available VRAM. This is version-dependent, so verify against the official Ollama docs for your installed version.
- No GPU at all. A container started without
--gpus=all, or a driver that stopped loading after a kernel update, means 100% CPU.ollama psshows that too.
Watch out for: nvidia-smi alone will not tell you this. Full VRAM is normal in both the healthy and the offloaded case. The split lives in Ollama's own view.
The fix: size the context to the card, then verify
First, confirm the diagnosis. Load the model the way your tool does by sending a real request, then check:
# Which processor is each loaded model on? ollama ps # Expected shape when healthy: # NAME ID SIZE PROCESSOR CONTEXT UNTIL # ... 100% GPU ... # Offloaded shape: a split like "NN%/MM% CPU/GPU" in PROCESSOR # Watch VRAM while you test nvidia-smi --query-gpu=memory.used,memory.total --format=csv -l 2 # On a systemd install, the server log reports how many layers went to the GPU journalctl -u ollama --since "10 min ago" | grep -i -E "offload|layers" If the processor column shows a split, apply server-level settings. On Linux with systemd, run sudo systemctl edit ollama and add an override:
[Service] # Default context for every model; a Modelfile or a per-request num_ctx overrides it Environment="OLLAMA_CONTEXT_LENGTH=8192" # Flash attention is required for KV cache quantization Environment="OLLAMA_FLASH_ATTENTION=1" # Roughly halves KV cache memory versus f16; small quality cost Environment="OLLAMA_KV_CACHE_TYPE=q8_0" # One slot unless you really serve concurrent users Environment="OLLAMA_NUM_PARALLEL=1" # Do not keep idle models fighting for VRAM Environment="OLLAMA_MAX_LOADED_MODELS=1" Then run sudo systemctl restart ollama. If you run the Ollama container, pass the same variables with -e flags. You must recreate the container for new values to take effect.
For a model you use constantly, pin its default context in a Modelfile. Tools that send their own num_ctx will still override it, so audit those clients too:
cat > Modelfile <<'EOF' FROM your-model:tag PARAMETER num_ctx 8192 EOF ollama create your-model-8k -f Modelfile ollama stop your-model:tag # unload the old instance ollama run your-model-8k "hello" && ollama ps Verify it worked: ollama ps should now report 100% GPU, and the CONTEXT column should show the value you set. Run ollama run --verbose your-model-8k and compare the reported eval rate before and after. Use your own numbers, since they vary by card and model.
What this costs: a smaller context means agents see less of your repository per request, and long prompts get truncated. Quantizing the KV cache saves memory but can slightly degrade output on long contexts, and q4_0 is more aggressive than q8_0. If you truly need a 32k+ window, shaving settings will not get you there. The honest fix is a smaller model, a lower weight quantization, or a card with more VRAM. Partial offload can still be acceptable for batch jobs where latency does not matter.
Watch out for: server environment variables are read only when the server starts. Restart the service (or recreate the container) after changing them, which also unloads models. Modelfile changes need the model unloaded with ollama stop and reloaded. Otherwise you are testing the old allocation.
Prevention checklist
Offload is easiest to catch before it reaches users. Make the processor split a visible check, not something you discover through complaints. The block below can live in a team README or a pre-flight script:
#!/usr/bin/env bash # preflight-ollama.sh: fail if any loaded model is not fully on GPU set -euo pipefail out=$(ollama ps | tail -n +2) [ -z "$out" ] && { echo "no model loaded; send a request first"; exit 2; } if grep -q "CPU" <<<"$out"; then echo "CPU offload detected:"; echo "$out"; exit 1 fi echo "all loaded models are 100% GPU" # Keepable alias: # alias ops='watch -n2 ollama ps' The checklist to adopt:
- Record the VRAM budget for each shared GPU: weights, KV cache at your chosen context, and headroom for a second model.
- Set
OLLAMA_CONTEXT_LENGTHexplicitly on the server instead of relying on a default that may change between releases. - Pin
num_ctxin Modelfiles for models you depend on, and audit what your IDE and agent tools send per request. - Keep
OLLAMA_NUM_PARALLELat 1 on small cards. Raise it only after measuring. - Re-run
ollama psafter every model pull, context change, driver update or kernel update. - In containers, confirm GPU access once with
nvidia-smiinside the container and again withollama ps. - Treat a sudden tokens-per-second drop as a capacity signal. Check for CPU in the processor column before blaming the model.
When this approach is not enough: if several people share one GPU, tuning flags only delays the problem. You need a scheduling layer or dedicated cards per workload. On Kubernetes, that means requesting GPUs explicitly rather than hoping a single node's card is free. You can do this through the device plugin's nvidia.com/gpu resource or, since Kubernetes v1.37, DRA-backed extended resources, which are now GA.
Flag names and defaults move between Ollama releases, so cross-check this article against the Ollama documentation for your installed version. For more practical notes on running AI tooling in production, see kuryzhev.cloud.

