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. 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. js 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 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.