KatInterview

Life of a Prompt in a Block-Paged LLM Serving Engine

Machine LearningModel answer
MediumModel answerMachine Learningllm-inferencekv-cachepaged-attention

Last updated

Walk one request through an LLM serving engine that stores attention state in fixed-size memory blocks, covering scheduling, prefill, decode, block tables, memory reclamation and latency trade-offs.

You are asked to explain how a modern LLM serving engine handles a single request from the moment it arrives until the last token is returned. Use an engine built around paged key/value (KV) memory as your reference design.

In your answer, cover:

  1. How a request is accepted and queued.
  2. What happens during the prefill phase and how it differs from decode.
  3. How the attention cache is divided into fixed-size blocks, and how a per-request block table maps logical token positions to physical GPU memory.
  4. How the scheduler forms batches at every model step (continuous batching) instead of per request.
  5. Who owns each cache block, when it is released, and what happens when the cache is full.
  6. How these choices affect throughput, time to first token, and per-token latency.

Expectations

  • Talk about the architecture and its invariants, not a specific library version or internal class names.
  • State your assumptions where they matter: model size, maximum context length, GPU memory, and the latency goal.
  • Mention the clarifying questions you would ask first, for example: typical prompt and output lengths, what the latency target actually measures, whether prompts share prefixes, whether responses are streamed, and what should happen when memory runs out.

Hints

Hint 1

Separate the two phases: prefill processes all prompt tokens in parallel and is compute-bound, while decode produces one token per step per sequence and is memory-bandwidth-bound.

Hint 2

Think of the block table like a page table in an operating system: logical block index maps to a physical block id, so a sequence's cache need not be contiguous and waste is limited to the last partially filled block.

Hint 3

The scheduler runs once per model iteration, not once per request. Ask what it must decide each step: which waiting requests to admit, which running ones to continue, and which to preempt when free blocks run out.

Hint 4

Do not forget lifetime: blocks are reference-counted, freed on completion or cancellation, and shared prefixes can be copy-on-write.