Model and Inference EngineeringKV cache and capacityStaff
Why can prefill run out of GPU memory even when the attention-cache budget fits?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A cache budget appears to support 32,000 tokens, but concurrent long prompts cause allocation failure. Explain persistent versus temporary memory, design peak measurements for the actual batch shape and compare workspace headroom, concurrent-prefill limits and chunking. Distinguish an admission rejection from allocator failure and actual live-memory exhaustion.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
The key-value (KV) cache is attention state we keep for future tokens. It is not the entire peak memory of a forward pass. Prefill also needs temporary tensors and kernel workspaces while it processes prompt tokens. Weight storage, cached KV, runtime buffers and allocator reservations all share the device. A spreadsheet that subtracts weights and KV from total VRAM and calls the remainder "free" may undercount a shape-specific temporary peak. vLLM's worker memory profiling measures peak model memory to determine what can be allocated to KV, precisely because KV cannot take the whole remaining device.
For a hypothetical 80 GiB device,20 GiB of weights,32 GiB of resident cache and 8 GiB of other persistent state occupy 60 GiB. A concurrent prefill workspace peak of 28 GiB would require 88 GiB and exceed capacity, even though the cache alone fits. These categories must not double-count the same allocation. Real measurements also include allocations outside the framework’s allocator.
Suppose short prompts fit under a KV pool sized for 32K tokens, but a 32K prefill OOMs when several prompts run together. I would measure allocated and reserved memory before, during and after prefill for that batch shape. Use PyTorch's peak-allocation and memory snapshot tools where the stack uses them, and account separately for allocations invisible to that allocator. Profile per worker and rank. The peak might come from attention or MLP intermediates, image encoders, a fallback kernel, graph capture or another concurrent request. Do not announce the cause from nvidia-smi idle memory alone.
Chunked prefill can reduce the number of prompt positions processed simultaneously, trading extra scheduling steps for a smaller temporary peak. Limiting concurrent prefills and reserving a measured workspace budget can help too. But if a kernel switches implementation at a shape threshold, the right fix may be to repair or route around that fallback. Re-run quality checks after a chunking change because positional or mask bugs can make the same prompt produce different answers, as The prompt is identical. Why does chunked prefill change the model's answer? explores.
An interviewer might say FlashAttention has linear memory, so prefill cannot OOM. It avoids materializing the quadratic attention matrix in the usual implementation, but it does not remove all activations or workspace. Also distinguish an allocation failure from actual live memory exhaustion and from a request rejected by an admission limit. The capacity contract needs peak memory for real batch shapes, not only steady-state KV slots. KV memory is allocated in blocks. Why can short requests waste so much of it? addresses wasted slots inside KV blocks. This one addresses memory outside the KV budget during computation.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux