# Prefill vs Decode Phases

> What prefill and decode mean in an AI request, why each is slow in a different way, and how caching helps, explained for your AI interview.

**In short:** Prefill is the first stage of an AI request, where the model reads all the input at once. Decode is the second, where it writes the answer one token at a time. Prefill sets how long you wait for the first word, and decode sets how fast the rest arrives.

AI and LLM interview question Q065, Transformer architecture. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/prefill-vs-decode-phases

## Say it in 30 seconds

When I send a prompt to a model, it works in two stages. First is prefill: it reads all my input at once, in one parallel pass, and saves its working notes, the keys and values. Then comes decode: it writes the answer one token at a time, reusing and adding to those notes. Prefill sets how long I wait for the first word. Decode sets how fast the rest comes out. Prefill is limited by raw maths, decode by how fast data moves from memory, so the fixes differ. Caching a shared start skips repeated prefill work.

## Say it in 2 minutes

A language model handles a request in two stages. The first is prefill. The model reads the whole input, the instructions and the document you pasted, all at once. It does that in one big parallel pass, and it keeps the keys and values it computes, which are its working notes on each token. The first new word comes out at the end of this stage.

The second stage is decode. The model writes the answer one token at a time, and each new token needs the notes from every token before it. NVIDIA describes decode as memory-bound, which means the speed of moving data from memory sets the pace, and the chip's maths waits for it. Prefill keeps the chip busy with maths. Decode often leaves much of that compute idle.

So the two stages shape the user's wait differently. Prefill sets how long you wait before the first word. Decode sets how fast the rest arrives. For an insurance claims assistant reading a long policy, most of the wait is prefill, so the fix is to skip repeated reading, for example by caching the policy text that starts every prompt. Some serving systems even run the two stages on separate machines and move the saved notes between them.

In an interview, name the two stages, say which one sets the wait, and say that caching only skips repeated prefill. That answer shows you understand where the time goes.

## What are prefill and decode, in plain words?

When you send a model a prompt, it works in two stages. First it reads everything you sent. Then it writes the reply, one piece at a time. The reading stage is called prefill, and the writing stage is called decode. Both run on a GPU, the graphics chip that runs the model.

NVIDIA puts it in one line: "This process involves two phases: the prefill phase and the decode phase." Both happen on every request, but they take different amounts of time and need different kinds of work from the chip.

Picture a triage nurse. She reads the whole patient chart before she says anything. Then she explains the plan one sentence at a time while she checks her notes. For an AI claims assistant reading a 40-page policy, the same two steps happen: it reads the policy first, then writes the summary.

- **Prefill:** The first stage: the model reads all the input at once, in one parallel pass.
- **Decode:** The second stage: the model writes the output one token per step.
- **Token:** A small piece of text, often part of a word. Models read and write in tokens, so one long word can be several.
- **KV cache:** The saved working notes (keys and values) for the tokens so far. Prefill fills it, and decode adds to it as each new token is written.
- **TTFT:** Time to first token: how long until the first word of the reply appears.

## How do the two phases differ in work and speed?

Prefill is efficient because all the input is known up front. NVIDIA calls it "a matrix-matrix operation that's highly parallelized", which means one big grid-of-numbers multiplication done all at once. That keeps the chip's maths units busy. Decode cannot do that. Each new token depends on the one before, so the steps must run in order.

- **Prefill: Lots of maths at once.** Every input token goes through one parallel pass. The chip's maths units stay busy, so the work is limited by compute.
- **Decode: Little maths, many steps.** One new token per step. Each step must read the model's stored numbers and the saved notes from memory, so memory speed sets the pace.
- **What you feel: Two different waits.** Prefill sets the wait before the first word. Decode sets how fast the rest streams out.

Researchers saw the same split in 2023. The Splitwise paper describes a compute-heavy phase and a memory-heavy phase, and its design splits the two across machines.

> "we find that there are two main phases during an LLM inference request: a compute-intensive prompt computation, and a memory-intensive token generation" (Splitwise research paper (2023))
>
> "The speed at which the data (weights, keys, values, activations) is transferred to the GPU from memory dominates the latency, not how fast the computation actually happens." (NVIDIA developer blog (2023))

The second quote explains decode. The pace is set by how fast data arrives from memory, and the chip's maths waits for it. That is why a faster compute chip alone does not fix a slow decode. NVIDIA also says batching is the simplest way to raise GPU use and throughput. Batching means running several requests together on the same chip. It raises total throughput, but Splitwise notes that "the token generation phase underutilizes compute resources" even with state-of-the-art batching.

## Where does the wait come from in a real request?

Take an insurance claims assistant. It reads a policy wording of about 30,000 tokens, plus the claim note (made-up numbers), and writes a 250-token summary for the claims handler. Nothing shows on screen until prefill finishes, so a 6-second reading pass (made up) is 6 seconds of blank screen.

*Diagram, One request, two phases: The reading pass runs once and saves its keys and values. The writing steps then reuse them, one token at a time. The arrows show that order from left to right.*
1. All the input goes in together.
2. One pass saves the keys and values for every input token.
3. Each new token reuses the saved keys and values.
4. The answer streams out one token at a time.

Once the first word appears, the 250 decode steps stream out one by one. So the fix for a slow first word is to cut the reading. The policy wording is the same for every claim on that policy, so it can sit at the start of each prompt and be cached. Anthropic describes it like this: "Prompt caching optimizes your API usage by letting the API resume from specific prefixes in your prompts." For long documents, Anthropic says you generally see improved time-to-first-token. The repeated reading is skipped, so the first word comes sooner.

> **Tip:** Caching skips repeated prefill work for a shared start. It does not change the decode steps, so say that when you explain it.

### How would you split prefill and decode across machines?

Some serving systems (the software that answers user requests) run prefill and decode on separate machines. The reason: prefill needs strong compute, while decode is limited by memory. Splitwise makes the same point: the writing step does not need the newest chips.

*Diagram, Two machines, one answer: A simplified view of the split: the first machine reads the input, the saved state moves across, and the second machine writes the answer.*
1. The prompt goes to the first machine.
2. That machine hands over its saved keys and values.
3. The second machine picks up that state.
4. Tokens stream back to the user.

The catch is the handover: the saved state has to move from the prefill machine to the decode machine. The paper says it can achieve "1.4x higher throughput at 20% lower cost than current designs". Those results describe that paper's setup, and they do not carry over to every system.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Two stages: prefill reads the input, decode writes the output.
- [ ] Prefill is one parallel pass, and it sets time to first token.
- [ ] Decode is one token per step, and memory speed sets its pace.
- [ ] The KV cache links them: prefill fills it, decode reuses it and adds to it.
- [ ] Caching skips repeated prefill, and it does not speed up decode.
- [ ] Serving trade-offs: batching, and splitting the phases across machines.

**Common traps**

- Trap: "Prefill is when the model writes the answer." Better: Prefill reads the input. It ends with the first new token, then decode writes the rest.
- Trap: "A faster GPU makes the whole answer faster." Better: Decode is limited by memory speed, so a faster compute chip alone does not fix it. Faster memory helps, and batching serves more users per chip.
- Trap: "Caching makes every answer faster." Better: Caching skips repeated prefill on a shared start. It leaves the decode steps as they were.

## Can you spot the slow part?

**Spot the fix:** Your claims assistant takes 9 seconds (made up) before the first word, then streams quickly. Which change cuts that wait the most?

A. Ask for a shorter summary.
B. Cache the policy text that starts every prompt.
C. Lower the temperature to make answers calmer.
D. Add a longer system prompt with more rules.

**Answer:** B. The wait is the reading pass, prefill. Caching the shared start skips repeated reading. A shorter summary shortens decode, and a longer system prompt adds more to read first.

**Quick check:** Which statement about the decode phase is right?

A. It reads all the input tokens in one parallel pass.
B. It writes one token per step, and each step needs the tokens before it.
C. It runs only once per request, at the start.
D. It is the step that first builds the KV cache.

**Answer:** B. Decode writes the output one token at a time, and each step depends on what came before. Reading all the input in one pass is prefill, and prefill builds the KV cache.

## Can you answer these from memory?

**Flash cards**

- **What is prefill?** Reading all the input in one parallel pass. It sets the wait for the first word.
- **What is decode?** Writing the output one token per step, reusing the saved keys and values.
- **What is the KV cache?** Saved keys and values for the tokens so far. Decode reuses them and adds to them.
- **Which phase sets TTFT?** Prefill. TTFT is timed from when you send, so any delay before the first word counts too.
- **Why is decode memory-bound?** Memory speed sets its pace, because each step reads stored data.
- **What does caching skip?** Repeated prefill work on a shared start. Decode is unchanged.

## Likely follow-up questions

- Q055 What is the KV cache, and why does it speed up generation? https://designervenkat.online/interview/ai/what-is-the-kv-cache
- Q316 TTFT vs tokens/sec vs total latency? https://designervenkat.online/interview/ai/ttft-vs-tokens-per-second-vs-latency
- Q326 Prompt cache vs semantic cache vs KV cache? (full answer coming)
- Q335 What is prefill/decode disaggregation? (full answer coming)
- Q073 Walk through one decoder block's forward pass. https://designervenkat.online/interview/ai/walk-through-one-decoder-block-forward-pass

## Learn it in depth

- [Prompt Caching](https://designervenkat.online/learn/ai-engineering/prompt-caching)
