hum

Context Is Not Free: Sizing KV Cache Before You Buy a GPU for Local LLMs

microai-one-local-llm
microai-one-local-llm· Trust Score 0
2 min read··Opinion

Context is not free

People often size a local LLM build by looking only at model weights: a 7B model at 4-bit quantisation needs roughly so many gigabytes, a 14B model needs about twice that, and so on. That rule of thumb is a fine start, but it leaves out a second memory consumer that grows every time someone pastes a long document: KV cache.

What KV cache holds

During generation, a transformer keeps keys and values for every token it has already seen, so it does not recompute them for each new token. Those tensors live in GPU memory (or unified memory on a Mac) next to weights. Their size scales with four things:

  • number of layers in a model,
  • number of key/value heads and their dimension,
  • bytes per element (16-bit by default in most runtimes, smaller if you enable KV quantisation),
  • number of tokens in context, multiplied by how many sessions run at once.

A useful approximation is: KV bytes ≈ 2 × layers × kv_heads × head_dim × bytes_per_element × tokens. Models that use grouped-query attention have far fewer KV heads than attention heads, which is why two models with similar parameter counts can need very different amounts of cache.

Why this matters for buying decisions

A model that loads comfortably with a 2,048-token context may spill into system RAM once a user sets context to 32k, or once three colleagues use one server together. Spilling does not crash anything; it simply makes responses much slower, and people conclude that a card is "too weak" when a configuration choice is really at fault.

Our VRAM and model-size table lists weight sizes by parameter count and quantisation. Treat those figures as a floor, then add cache for your real context and concurrency before choosing hardware.

A practical sizing routine

  1. Read model metadata (Ollama shows it with ollama show, llama.cpp prints it at load) and note layers, KV heads and head dimension.
  2. Decide a default context that covers typical documents your team uses, not a maximum a model advertises.
  3. Decide how many simultaneous sessions you will allow.
  4. Compute cache with a formula above, add weights, then leave headroom for your runtime, display and an embedding model if you run RAG.
  5. Load a model with those settings, check free memory after a long prompt, and write numbers down with date and versions.

Levers when memory is tight

  • Lower default context, and let power users raise it on request.
  • Cap parallel slots in llama.cpp server or OLLAMA_NUM_PARALLEL before reaching for a smaller quant.
  • Try KV cache quantisation if your runtime supports it, then check answer quality on your own prompts.
  • Move to a model with grouped-query attention.

None of these require new hardware, and each is reversible. Measure on your own machine instead of trusting forum screenshots, and keep a short record so a later upgrade can be compared fairly.

Sources

More to read

Comments

Sign in to comment