Out-of-memory (OOM) errors when you raise a local LLM's context window happen because the KV cache grows linearly with context length, and it competes with the model weights for the same VRAM (or RAM). The fix is not one setting but a sequence: first measure what is actually running out, then reduce the memory the KV cache demands (quantize the cache, shrink batch size, cap context to what you truly need), then offload what does not fit to system RAM or disk, and only then consider a smaller model or a bigger GPU. Below is the order that works, plus the mistakes that make people think it cannot be done.

What You'll Need

  • A local LLM runtime that exposes context, batch and cache settings (llama.cpp or a front end built on it, Ollama, LM Studio, text-generation-webui, or vLLM) and its current version.
  • A way to watch memory live: nvidia-smi on NVIDIA, Task Manager or rocm-smi on AMD, and your OS memory monitor for system RAM.
  • Roughly 1–2 GB of free disk space per model file if you plan to swap to a smaller quantization.
  • A note of your GPU's total VRAM and your system RAM, since every decision below is a budget against those two numbers.

Step-by-Step

  1. Reproduce the failure and note the exact number. Load the model at the context size that fails and watch memory as it loads. Most runtimes print an allocation failure with the requested size in MB or GB. That number tells you whether you are short by a little (a few hundred MB, fixable with cache quantization) or by a lot (you need a smaller model or offloading).
  2. Confirm what else is using the GPU. A browser with hardware acceleration, a game launcher, another model still resident from a previous run, or a display server can hold a gigabyte or more. Close them, then reload. On Windows, check the GPU tab of Task Manager for shared versus dedicated memory; a model that spills into shared memory runs but is dramatically slower and can still fail at large contexts.
  3. Lower the context to a realistic value first. Set the context window to what your prompts actually need rather than the model's maximum. A 32K-context model asked to run at 32K needs a KV cache several times larger than at 4K. Find the largest value that loads reliably, then work upward in steps (for example 4K, 8K, 16K) until it fails again. That ceiling is your real starting point.
  4. Quantize the KV cache. This is the single biggest win for context length. In llama.cpp-based tools the cache type options are usually labelled F16 (default), Q8_0 and Q4_0 or similar; moving from F16 to Q8_0 roughly halves KV cache memory, and Q4_0 cuts it further at some quality cost. In Ollama this is set per model with a num_kv_cache_type style parameter in a Modelfile or through the runtime's environment variables; in LM Studio and text-generation-webui it appears as a KV cache quantization dropdown in the model load settings. Expect modest quality degradation at Q4 and near-none at Q8.
  5. Reduce batch and ubatch size. Prompt processing allocates a scratch buffer proportional to batch size. If the failure happens while ingesting a long prompt rather than during generation, lower the batch size (often 512 or 2048 by default) to 128 or 256. This slows prompt ingestion slightly but can be the difference between loading and not.
  6. Offload layers instead of fighting for VRAM. If the model plus cache still will not fit, move some layers to the CPU. In llama.cpp that is the -ngl / n_gpu_layers setting; in LM Studio it is the GPU offload slider; in Ollama it is num_gpu in the Modelfile. Fewer GPU layers means slower tokens per second but a working large context. A hybrid split is often the only way to run 16K+ context on a mid-range card.
  7. Enable flash attention if your runtime supports it. Flash attention reduces the memory overhead of the attention computation and is a prerequisite for some KV cache quantizations in llama.cpp. It is usually a checkbox or a -fa flag; if enabling it causes errors on your GPU, disable it and rely on the steps above.
  8. Shrink the model itself as the last lever. Dropping from a Q5 or Q6 quantization to Q4 frees enough memory for a noticeably larger cache. This trades model quality for context, so test a few representative prompts afterward. If even Q4 at your target context will not fit, the honest answer is that the context is too large for this hardware and you should either accept a smaller window or use a smaller model.
  9. Persist the working configuration. Once you find a combination that loads, save it: a Modelfile in Ollama, a preset in LM Studio or text-generation-webui, or a launch script with flags for llama.cpp. Otherwise the next launch reverts to defaults and the OOM returns.
  10. Verify stability under a real long prompt. Loading successfully is not the same as running successfully. Paste a prompt near your new context limit and generate a few hundred tokens while watching memory. If it climbs steadily and then fails, you are at the edge; drop one step in context size or cache quantization.

Common Mistakes

  • Setting the context to the model's advertised maximum and assuming it will fit. The maximum is a capability of the architecture, not a promise about your GPU.
  • Ignoring system RAM. When VRAM is exhausted, some runtimes silently spill to system RAM and appear to work until they crash; check both pools.
  • Changing three settings at once, then not knowing which one helped or broke quality.
  • Forgetting that the KV cache scales with concurrent requests. Serving multiple users or parallel slots multiplies cache memory, so a context that works for one request can fail for four.
  • Leaving a previous model loaded in another window or terminal. Two half-loaded models use more memory than one full one.

Tips

  • Write down your VRAM total and subtract the model file size plus roughly 10–15% overhead; what remains is your cache budget. Divide by the per-token cache cost your runtime reports to get a realistic context ceiling.
  • Q8 KV cache quantization is usually the best quality-per-byte trade and is a safe default for most people.
  • If your prompts are long documents rather than long conversations, retrieval or chunking often beats a huge context window and costs far less memory.
  • Restart the runtime between experiments; freed memory is not always returned to the pool promptly.

Troubleshooting

If the model still fails to load after cache quantization and a smaller batch, check whether the runtime is actually applying your settings — some front ends override the context value from the UI, and a Modelfile parameter can be silently ignored if the launch command sets it too. If it loads but crashes mid-generation, lower context by 25% and retry; generation allocates as it goes. If tokens per second collapse to single digits, you are spilling to system RAM, which means the configuration technically works but is impractical. If enabling flash attention throws an error, disable it; not every GPU and driver combination supports it. If you are on a laptop with a shared GPU memory pool, the integrated and discrete GPU may both be reported, and the model may be loading onto the wrong one — set the preferred GPU in the driver control panel.

Frequently Asked Questions

Does a bigger context window always need more VRAM?

Yes. The KV cache grows with context length and number of layers, so doubling context roughly doubles that portion of memory, on top of the fixed model weights.

Is quantizing the KV cache safe for quality?

At Q8 the loss is usually negligible for everyday use. At Q4 it is measurable and worth testing on your own prompts before committing.

Can I use system RAM instead of VRAM for context?

You can offload layers to CPU, which moves both weights and their cache to system RAM, but it is much slower. There is no setting that puts only the cache in RAM while keeping all layers on the GPU in most runtimes.

Why does it work at 8K but fail at 16K on the same model?

Because the cache is the variable. The weights fit either way; the extra 8K of context is what pushed you over the limit.

Do parallel requests share one cache?

They share the allocated cache pool, so each concurrent slot consumes part of it. Serving several users effectively reduces the context available to each.