LLM Context Window Guide: How Much RAM Does 128K Context Need?

A model's advertised context window tells you the maximum input length — but running it requires substantial VRAM for the KV cache. A 128K context window sounds impressive until you realize it needs 20-40GB VRAM on top of the model weights.

KV Cache Memory Calculator

KV cache size formula: 2 × num_layers × num_kv_heads × head_dim × ctx_len × precision

ModelContextKV Cache (FP16)KV Cache (Q8)
Llama 3.1 8B128K~24 GB~12 GB
Llama 3.1 8B32K~6 GB~3 GB
Qwen3.6 32B128K~96 GB~48 GB
Qwen3.6 32B16K~12 GB~6 GB

Practical Context Settings

# 8B model on RTX 4090 (24GB): model=5GB, context budget=19GB
# At Q8 KV cache: 19GB / 0.1 KB per token = ~190K tokens possible
./build/bin/llama-server   -m ./models/llama3.1-8b-q5.gguf   --n-gpu-layers 999   --flash-attn   --cache-type-k q8_0   --cache-type-v q8_0   --ctx-size 65536 \    # 64K is well within budget
  --host 0.0.0.0 --port 8080

When Do You Actually Need 128K Context?

  • Analyzing whole codebases: A typical Python project is 50-200K tokens
  • Long document Q&A: A 300-page PDF is ~150K tokens
  • Multi-hour conversations: Average conversation uses 10-30K tokens
  • Most tasks: 8-16K context is enough for 95% of queries

For hardware recommendations specifically for long-context inference workloads, see LLMHardware.io's long-context hardware guide comparing different VRAM tiers.

Comments

Popular posts from this blog

AMD RX 9070 XT for Local LLMs: ROCm Performance Guide (2026)

llama.cpp flash attention: how to enable and benchmark

SillyTavern with Local LLMs: Complete Setup for AI Companions