# What happens when I press enter?

> The model first reads your whole prompt, then writes the answer one token at a time. Reading is limited by compute and writing is limited by memory bandwidth.

Every request runs in two phases. The model reads your whole prompt in one go, then writes the answer one [token](https://localagi.co/tokens) at a time. The two phases hit different limits in your hardware, which is why a machine can be quick at one and slow at the other.

## Two phases with different bottlenecks

### Prefill: reading the prompt

Prefill is the reading phase. The runtime turns your prompt into tokens and pushes all of them through the model together. The chip does a huge amount of maths in parallel, so the limit is raw compute.

Prefill is fast per token. A GPU reads a long prompt many times faster than it can write. But the work grows with the prompt, so pasting in a 30-page document means a real wait before anything happens.

### Decode: writing the answer

Decode is the writing phase. The model produces one token, adds it to the text, and runs again for the next one. Each step needs every weight in the model, read out of memory, to make a single token.

That makes decode limited by memory bandwidth, the speed at which weights move from memory to the chip. The maths per token is small. Fetching the weights is the slow part. [What makes a model fast?](https://localagi.co/what-makes-it-fast) turns this into a formula.

**Prefill.** Reads all prompt tokens at once. Limited by compute, so it is slow when the prompt is long.

**Decode.** Writes the answer one token at a time. Limited by memory bandwidth, so it is slow when the model is big.

## Time to first token versus tokens per second

Two numbers describe how a request feels. Time to first token, or TTFT, is how long you wait before the first word appears. That is mostly prefill.

Tokens per second, written tok/s, is how fast the words stream after that. That is decode.

For a short chat message, TTFT is a fraction of a second and only tok/s matters. For a coding agent that sends 20,000 tokens of files with every request, TTFT can be the larger wait. A machine with high bandwidth and modest compute writes quickly but makes you wait at the start.

**Rule of thumb.** 
Short prompts and long answers depend on bandwidth. Long prompts and short answers depend on compute. Know which kind of work you do before you buy hardware.

## The KV cache

While reading, the model computes two sets of numbers for every token, called keys and values. Later tokens look back at them to decide what matters. The runtime keeps them in memory in the KV cache, so it never computes them twice.

This is why the second message in a chat starts faster than the first. The cache already holds your earlier conversation. The runtime only has to prefill the new message, then carry on writing.

The cache is not free. It grows with every token in the conversation and takes memory alongside the model itself. [How much can the model keep in mind?](https://localagi.co/context-window) covers how big it gets and [Will the model fit in my memory?](https://localagi.co/memory) covers how to budget for it.

**The catch.** 
The cache only helps when the start of the prompt stays the same. If an app edits an early part of the conversation, such as a system prompt that changes every turn, the runtime throws the cache away and reads everything again. Some agent tools do this and feel slow for that reason.

## A timeline of one request

Here is one chat message from the moment you press enter.

1. Tokenize the prompt
2. Prefill: read all tokens, fill the KV cache
3. First token appears
4. Decode: one token per pass
5. Stop token, answer ends

On a typical laptop with a small model, steps 1 to 3 take well under a second for a short message. Step 4 then runs at the machine's decode speed until the model emits a stop token or hits a length limit.

**Try it.** 
Start a model in Ollama with timing turned on. First paste a long document and ask for a one-line summary. Then ask a one-line question that needs a long answer.

```bash
ollama run qwen3:8b --verbose
```

Compare `prompt eval rate` with `eval rate`. The first is prefill speed, the second is decode speed.

## Go deeper

### Why decode cannot run in parallel

Each new token depends on the one before it. The model cannot start token 51 until it knows token 50. So decode is a strict loop of one pass per token, and the full weights are read once per pass.

Prefill has no such chain. All prompt tokens are known up front, so the chip processes them together and reuses each weight it fetches across hundreds of tokens. That reuse is what moves the bottleneck from bandwidth to compute.

### Prefill gets slower as context grows

Each token in the prompt looks back at every token before it. Doubling the prompt more than doubles the attention work, so very long prompts slow down towards the end. Newer models use tricks such as sliding-window attention to soften this, but long prefill is still the main cost of large contexts.

### Prompt caching across requests

Runtimes such as [llama.cpp](https://localagi.co/llama-cpp) and [vLLM](https://localagi.co/vllm-and-sglang) can keep the KV cache for a shared prefix between separate requests. If every request starts with the same long system prompt, only the first one pays for it. This matters most when several people share one server, covered in [What changes when many people use one model?](https://localagi.co/one-user-to-many).

### Speculative decoding

A small draft model guesses the next few tokens, and the big model checks them all in one pass. Checking is a prefill-style job, so it is cheap. When the guesses are right, you get several tokens for the price of one pass. [How do I make it faster?](https://localagi.co/tuning-for-speed) covers when it helps.
