{"slug": "estimate-kv-cache-cost-before-doubling-a-local-models-context", "title": "Estimate KV cache cost before doubling a local model’s context", "summary": "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.", "body_md": "*AI-generated example, checked by executing the arithmetic below. It uses a fictional model configuration. TIHMLO publishes the linked planning worksheet.*\n\nA 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.\n\nIn 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:\n\n```\n32 layers × 8 KV heads × 128 values × 2 (key and value)\n× 2 bytes/value = 131,072 bytes per cached token\n```\n\nA 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.\n\n``` js\nconst gib = 2 ** 30;\nconst example = { layers: 32, kvHeads: 8, headDim: 128, cacheBytes: 2 };\nfunction cacheGiB({ layers, kvHeads, headDim, cacheBytes }, tokens) {\n  return layers * kvHeads * headDim * 2 * cacheBytes * tokens / gib;\n}\nconst weightGiB = (8e9 * 4 / 8) / gib;\nconsole.log(cacheGiB(example, 4096)); // 0.5\nconsole.log(cacheGiB(example, 8192)); // 1\nconsole.log(weightGiB.toFixed(2));  // 3.73\nconsole.log((weightGiB + cacheGiB(example, 8192)).toFixed(2)); // 4.73\n```\n\nThat 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.\n\nRun 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.\n\nThe [TIHMLO local model memory worksheet](https://tihmlo.com/decision-tools/local-model-memory-budget/?utm_source=devto&utm_medium=editorial&utm_campaign=distribution_wave2&utm_content=kv_cache_budget) is a starting point for deciding what to measure. See the [Hugging Face cache guide](https://huggingface.co/docs/transformers/en/kv_cache) for cache implementations and [quantization overview](https://huggingface.co/docs/transformers/quantization/overview) for weight formats.", "url": "https://wpnews.pro/news/estimate-kv-cache-cost-before-doubling-a-local-models-context", "canonical_source": "https://dev.to/tihmlo_90/estimate-kv-cache-cost-before-doubling-a-local-models-context-km1", "published_at": "2026-11-24 18:10:28+00:00", "updated_at": "2026-11-24 18:21:22.806298+00:00", "lang": "en", "topics": ["ai-infrastructure", "large-language-models", "mlops"], "entities": ["TIHMLO", "Hugging Face"], "also_reported_by": [], "takeaway": "For a 32-layer, 8-KV-head, 128-dim, 16-bit cache, budget 131,072 bytes per cached token — 0.5 GiB at 4K context and 1 GiB at 8K — on top of ~3.73 GiB of 4-bit weights for an 8B model.", "recommendation": {"advice": "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.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/estimate-kv-cache-cost-before-doubling-a-local-models-context", "markdown": "https://wpnews.pro/news/estimate-kv-cache-cost-before-doubling-a-local-models-context.md", "text": "https://wpnews.pro/news/estimate-kv-cache-cost-before-doubling-a-local-models-context.txt", "jsonld": "https://wpnews.pro/news/estimate-kv-cache-cost-before-doubling-a-local-models-context.jsonld"}}