Learn AI Math

Topic 7 of 20 · 36 words

LLM Inference: Prefill & Decode

The two phases of running an LLM—compute-heavy prefill and memory-bound decode—plus KV caching, batching, and latency metrics.

✎ Napkin Diagram · Topic 07How an AI Replies: 1. Read Your Question → 2. Type the Answer
PNG ↗
How an AI Replies: 1. Read Your Question → 2. Type the Answer Simple two-step cartoon: Step 1 (Prefill) shows the AI scanning your entire question in one flash (causing the brief pause before it starts talking); Step 2 (Decode) shows it typing out the reply one word at a time. STEP 1: READ YOUR PROMPT (PREFILL)👀 Reads your whole messageALL AT ONCE in one big gulp!Saves notes in short-term memory(called the "KV Cache" bookmark)⏱️ This is the pause before the first word appearsSTEP 2: TYPE THE REPLY (DECODE)✍️ Writes the answer ONE word at a time:"Here""is""why…"Checks its bookmark on every single word⚡ This controls how fast words stream on screen getaimath.com
Every AI reply has two halves: gulping down your whole prompt at once, then speaking the answer one word at a time.

The 36 words, in learning order

Start at 1. Each word builds on the ones before it.

  1. InferenceInference is when a trained AI model is actually used to give answers, instead of still learning.
  2. Inference EngineAn inference engine is software built to run trained AI models quickly and efficiently.
  3. Inference PipelineAn inference pipeline is the full chain of steps from receiving a request to returning the model's answer.
  4. PrefillPrefill is the first step of answering, where the model reads your whole prompt at once and builds its memory of it.
  5. DecodeDecode is the second step of answering, where the model writes its reply one token at a time.
  6. Time to First Token (TTFT)Time to First Token is how long you wait between sending a request and seeing the first token of the answer.
  7. Time Per Output Token (TPOT)Time Per Output Token is the average time it takes to generate each new token after the first one.
  8. Inter-Token Latency (ITL)Inter-token latency is the time gap between one generated token and the next.
  9. Time to First Byte (TTFB)Time to First Byte is how long it takes from sending a request until the first bit of data comes back.
  10. Tokens Per Second (TPS)Tokens per second is how many tokens a model produces, or processes, every second.
  11. Prompt ProcessingPrompt processing is the model reading and understanding your input before it starts writing an answer.
  12. Token GenerationToken generation is the model producing new tokens one after another to form its answer.
  13. Autoregressive GenerationAutoregressive generation is creating output one step at a time, where every new token depends on all the tokens before it.
  14. KV CacheThe KV cache is the model's short-term memory of the keys and values it already computed for earlier tokens.
  15. Key CacheThe key cache is the part of the KV cache that stores the keys for all previous tokens.
  16. Value CacheThe value cache is the part of the KV cache that stores the values for all previous tokens.
  17. KV Cache SizeKV cache size is how much memory the KV cache needs, which grows with context length, layers, heads and number of users.
  18. KV Cache QuantizationKV cache quantization stores the KV cache in fewer bits to save memory.
  19. PagedAttentionPagedAttention is a way to store the KV cache in small fixed-size blocks, like pages in a book, to avoid wasting memory.
  20. Continuous BatchingContinuous batching lets a server add new requests to a running batch as soon as others finish, instead of waiting for the whole batch.
  21. Static BatchingStatic batching groups requests together and runs them as one fixed batch until all of them are finished.
  22. Dynamic BatchingDynamic batching waits a short moment to collect incoming requests and then runs them together as a batch.
  23. Speculative DecodingSpeculative decoding speeds up generation by letting a small fast model guess several tokens ahead, then having the big model check them all at once.
  24. Medusa DecodingMedusa decoding adds extra small prediction "heads" to a model so it can guess several future tokens at once, then verify them.
  25. Parallel DecodingParallel decoding is any method that produces or checks several tokens at the same time instead of strictly one by one.
  26. Draft ModelA draft model is the small, fast model that proposes tokens in speculative decoding.
  27. Target ModelThe target model is the large, high-quality model that checks and accepts the draft model's guesses in speculative decoding.
  28. Greedy DecodingGreedy decoding always picks the single most likely next token at every step.
  29. Beam SearchBeam search keeps several possible sentences going at once and picks the one with the best overall score at the end.
  30. SamplingSampling is how a model picks the next token from its list of possible options, using their probabilities.
  31. ThroughputThroughput is the total amount of work a system completes over time, like the number of tokens or requests per second across all users.
  32. LatencyLatency is the delay between asking for something and getting a response.
  33. Memory BandwidthMemory bandwidth is how fast data can move between a chip's memory and its processors, usually measured in gigabytes per second.
  34. Compute-BoundA task is compute-bound when its speed is limited by how fast the chip can do math.
  35. Memory-BoundA task is memory-bound when its speed is limited by how fast data can be read from memory, not by math speed.
  36. Prefill-Decode DisaggregationPrefill-decode disaggregation runs the prefill and decode phases on separate machines, each tuned for its job.

How LLM Inference: Prefill & Decode fits together

Every LLM reply has two distinct halves. First comes prefill: the GPU digests your entire prompt in parallel and builds the KV cache, determining Time to First Token (TTFT). Second comes decode: the model generates output one token at a time, re-reading its weights and KV cache on every step, determining Time Per Output Token (TPOT) and Tokens Per Second.

Techniques like PagedAttention, continuous batching, speculative decoding, and KV cache quantization exist to eliminate wasted memory and keep GPUs busy across dozens of simultaneous requests.