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.
Count the cache for one stream
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.
Compare 4,096 with 8,192 cached tokens
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.
Use it as a boundary check
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.
Top comments (0)