Why a model that fits at 4K crashes at 32K context
Weights stay fixed, while the KV cache grows linearly with context length and batch size. For Qwen3 8B (Q4_K_M), total model workload at 4K with default FP16 KV cache is 5.81 GB (KV is only 0.56 GB). Raise the window to 32K and FP16 KV balloons to 4.50 GB, pushing total workload to 10.14 GB — instantly overflowing an 8 GB card and crowding a 12 GB desktop GPU once Windows reserves ~1.5 GB. On Qwen3 32B (Q4_K_M), 4K requires 21.9 GB (fitting a 24 GB card), while 32K with FP16 KV adds 8.00 GB of cache (29.55 GB total), forcing slow PCIe spillover into system RAM.
Fix 1: Quantize KV cache to q8_0 or q4_0 with Flash Attention
In ggml, q8_0 stores 32 values in 34 bytes (~53% of FP16) and q4_0 stores 32 values in 18 bytes (~28% of FP16). Switching Qwen3 8B at 32K from f16 to q8_0 drops KV cache from 4.50 GiB to 2.39 GiB (total 7.82 GiB), and q4_0 drops it to 1.27 GiB (total 6.58 GiB — fitting even an 8 GB GPU). On Qwen3 32B at 32K, q8_0 lowers total workload from 29.55 GiB to 25.43 GiB, while q4_0 brings it down to 23.23 GiB. Quantizing the V cache requires Flash Attention enabled; otherwise llama.cpp refuses the flag or silently falls back.
# llama.cpp (llama-server): Q8_0 KV cache + Flash Attention at 32K
./build/bin/llama-server \
-m ./models/Qwen3-8B-Q4_K_M.gguf \
-ngl 99 -c 32768 --flash-attn on \
-ctk q8_0 -ctv q8_0 --host 127.0.0.1 --port 8080
# Ollama: set environment variables before starting `ollama serve`
export OLLAMA_FLASH_ATTENTION=1
export OLLAMA_KV_CACHE_TYPE=q8_0
# Set num_ctx=32768 in your client/API request (or Modelfile PARAMETER num_ctx 32768)
ollama run qwen3:8bFix 2: Why generation slows down as context fills (and when to pick hybrid-attention models)
Even when KV cache fits in VRAM, autoregressive decode reads BOTH active weights AND the entire filled KV cache on every single generated token. When KV reaches 4–8 GiB on an 8B–14B model, memory traffic nearly doubles and decode tok/s drops sharply, while prompt prefill (TTFT) scales linearly-to-quadratically with input length. For 64K–128K coding agents on a 24 GB GPU, choose hybrid-attention architectures (like Qwen3.8 27B, where only 16 of 64 layers hold full growing KV): at 128K context with q8_0 KV, Qwen3.8 27B Q4_K_M uses just 4.25 GiB of KV cache (22.90 GiB total) — roughly 4× smaller KV than a uniform 64-layer transformer.
# Inspect the exact VRAM climb, decode speed decay & TTFT curve for your GPU:
https://quantized.uk/tools/vram-calc/?mode=forward&model=qwen3-8b&quant=Q4_K_M&gpu=rtx4090&ctx=32768&cache=q8_0Common questions
Does q8_0 KV cache hurt coding or reasoning accuracy?
In practice q8_0 KV cache is nearly indistinguishable from FP16 on code, JSON, and multi-turn chat while cutting KV VRAM nearly in half. Reserve q4_0 KV for tight cards where 32K+ would otherwise spill into system RAM, as aggressive 4-bit V quantization can slightly degrade needle-in-a-haystack recall on very long prompts.
Why does Ollama still OOM after I set OLLAMA_KV_CACHE_TYPE=q8_0?
Two common causes: (1) OLLAMA_FLASH_ATTENTION=1 was not set alongside OLLAMA_KV_CACHE_TYPE, or the Ollama server daemon was not restarted after exporting the variables (on Linux systemd, add Environment= lines via `sudo systemctl edit ollama`); (2) OLLAMA_NUM_PARALLEL > 1 multiplies the KV cache pool by the number of parallel slots.
What this guide uses
- Picks for it
- Best local LLM for 24GB
Next steps
See everything that fits 🟢 RTX 4090Reverse lookup — 24GB at 32768 context, ranked by qualityModels covered in this guide
Did this actually run?
Copying a command is not the same as it working, so this is the only place the site asks. Nothing is collected beyond the answer itself.
Related guides
Nginx Reverse Proxy for Local LLM APIs
Put Ollama or llama.cpp behind Nginx with TLS, rate limiting, and a stable /v1 endpoint for your apps.
Tune llama.cpp on a CPU-Only Linux PC or VPS
Check physical cores on a Linux PC or CPU-only VPS. Run a GGUF with llama.cpp and use llama-bench to test thread counts and where OpenBLAS helps.
Speculative Decoding, MTP & Multi-Slot (-np) Serving in llama-server
Speed up single-user generation 1.3×–1.8× with built-in MTP or a draft GGUF, and size -np parallel KV slots without out-of-memory errors.