cd /news/ai-infrastructure/estimate-kv-cache-cost-before-doubli… · home › topics › ai-infrastructure › article
[ARTICLE · art-175602] src=dev.to ↗ pub= topic=ai-infrastructure verified=true sentiment=· neutral

Estimate KV cache cost before doubling a local model’s context

A developer published a planning method for estimating KV cache memory before increasing a local model's context window, working through a fictional 8-billion-parameter configuration with 32 layers, 8 KV heads and 128 values per head. The arithmetic puts a 16-bit cache at 131,072 bytes per cached token — 0.5 GiB at 4,096 tokens and 1 GiB at 8,192 tokens — on top of roughly 3.73 GiB of nominally 4-bit weights. The writeup cautions that the estimate is idealized and recommends measuring peak memory on the actual backend at the longest realistic request and intended concurrency, then varying one parameter at a time.

recommendation

Estimate KV cache as layers × KV heads × head dim × 2 × bytes per value × tokens, then measure peak memory on your real backend at your longest request and target concurrency before raising context.

more in playbook: Inference and self-hosting
by read2 min views2 publishedNov 24, 2026

AI-generated example, checked by executing the arithmetic below. It uses a fictional model configuration. TIHMLO publishes the linked planning worksheet.

A file containing nominally 4-bit weights for an 8-billion-parameter model suggests a first-pass weights estimate near 4 GB. A running model also needs memory for its context. To estimate one part of that difference, model the key and value cache separately from the weights.

In a simplified dense cache, each token stores one key and one value for each KV head in each layer. A configuration with 32 layers, 8 KV heads, 128 values per head and a 16-bit cache therefore uses:

32 layers × 8 KV heads × 128 values × 2 (key and value)
× 2 bytes/value = 131,072 bytes per cached token

A grouped-query model can have fewer KV heads than query heads. Use the KV count from the model configuration. The weight quantization format also does not automatically determine the cache precision.

const gib = 2 ** 30;
const example = { layers: 32, kvHeads: 8, headDim: 128, cacheBytes: 2 };
function cacheGiB({ layers, kvHeads, headDim, cacheBytes }, tokens) {
  return layers * kvHeads * headDim * 2 * cacheBytes * tokens / gib;
}
const weightGiB = (8e9 * 4 / 8) / gib;
console.log(cacheGiB(example, 4096)); // 0.5
console.log(cacheGiB(example, 8192)); // 1
console.log(weightGiB.toFixed(2));  // 3.73
console.log((weightGiB + cacheGiB(example, 8192)).toFixed(2)); // 4.73

That last number just adds idealized packed weights and this hypothetical cache. Runtime allocation, quantization metadata, temporary tensors and allocator headroom can change actual peak memory. A runtime may reserve the full configured cache ahead of time, allocate lazily, use a different cache type, offload it or share a prompt prefix across requests. Two unrelated streams with full-length caches can need roughly twice the cache memory in this simplified model.

Run the longest realistic request and the intended concurrency on the actual backend. Record the model, weight format, cache type, context and peak memory together. Then change one of those variables and run the same workload again. The difference can show whether a shorter context or fewer concurrent requests helps before you switch the model.

The TIHMLO local model memory worksheet is a starting point for deciding what to measure. See the Hugging Face cache guide for cache implementations and quantization overview for weight formats.

── more in #ai-infrastructure 4 stories · sorted by recency
── more on @tihmlo 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/estimate-kv-cache-co…] indexed:0 read:2min 2026-11-24 · —