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
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
Post a Comment