How a large language model answers¶
Note
In one minute
- A large language model (LLM) writes its answer one token at a time: it predicts the next piece of text, adds it, and starts again.
- To write each token, the GPU reads all the model's weights once. That is why serving is limited by how fast GPU memory can be read.
- The model keeps a memory of the conversation, the KV cache, that grows with every token and takes GPU memory.
Before you start. What a model is. This page is background (Conventions).
Tokens: the pieces of text¶
A model does not read letters or words. It reads tokens: pieces of text from a fixed vocabulary of tens of thousands of entries. A common word is often one token; a rare word is cut into several. In English, a token is on average a little shorter than a word, so 1,000 words make roughly 1,300 tokens. The exact count depends on the model.
Everything is counted in tokens: the size of a question, the length of an answer, the speed of a server (tokens per second) and often the price of a hosted service.
One token at a time¶
An LLM is trained to do one thing: given a text, give the probability of every possible next token. To write an answer, the serving software repeats a loop:
- Give the model the text so far.
- The model returns the probabilities of the next token.
- Pick one token (the most likely, or one drawn at random among the likely ones).
- Append it to the text, and go back to step 1, until the model produces an "end" token or a length limit is reached.
An answer of 500 tokens is 500 turns of this loop. Each turn runs the whole model.
Two phases: prefill and decode¶
Serving software splits the work in two phases, and they stress the GPU differently:
| Phase | What happens | What limits it |
|---|---|---|
| Prefill | The model reads the whole prompt (the question, the documents given, the conversation so far) in one pass, all tokens in parallel. | Compute: many operations on many tokens at once. |
| Decode | The model writes the answer, one token per turn of the loop. | Memory bandwidth: each turn reads all the weights to produce a single token. |
This is why users see a short wait before the first word (prefill, the time to first token), then words arriving at a steady pace (decode, tokens per second).
Worked example: why bandwidth sets the speed¶
During decode, for one user alone, each token requires reading every weight once. So a rough upper limit is:
tokens per second ≈ memory bandwidth ÷ size of the weights
| Weights | GPU memory bandwidth | Upper limit for one user |
|---|---|---|
| 16 GB (8B model, BF16) | 3,350 GB/s | about 210 tokens per second |
| 140 GB (70B model, BF16) | 3,350 GB/s per GPU, model spread over 2 GPUs | about 48 tokens per second |
3,350 GB/s is the memory bandwidth NVIDIA gives for the H100 SXM DOC (NVIDIA H100). Real servers reach less than this limit, but the order of magnitude holds. Serving software raises the total throughput by processing many users' tokens in the same pass (batching): the weights are read once for all of them.
The context window and the KV cache¶
The context window is the maximum number of tokens the model can take into account at once: prompt plus answer. Recent models accept tens of thousands to hundreds of thousands of tokens.
To avoid recomputing the whole conversation at every turn of the loop, the model keeps intermediate results for every token already processed: the KV cache (keys and values of its attention layers). It grows by a fixed amount per token, for every user being served. A long context, or many users at once, can need more GPU memory for the KV cache than for the weights themselves (How much memory a model needs).
Common mistakes¶
- "A faster GPU means faster answers." For one user, decode speed depends mostly on memory bandwidth, not on the compute figure in the GPU's marketing.
- "The context window is free." Every token in the context uses KV cache memory while the request runs. A 128,000-token context per user limits how many users a GPU can serve.
- "The model remembers yesterday's conversation." It only sees what is in its context window now. Any "memory" between sessions is the application putting old text back into the prompt.
Check yourself¶
An answer is 300 tokens long. How many times does the model run?
About 300 times in the decode phase, once per generated token, after one prefill pass over the prompt.
Why is decode limited by memory bandwidth rather than compute?
Because producing one token for one user needs to read every weight, but does little arithmetic with each one. The GPU spends its time waiting for memory.
What grows in GPU memory as a conversation gets longer?
The KV cache: a fixed amount per token in the context, per user being served.
Go further¶
- Next page: Four uses, four infrastructures.
- The numbers behind this page: How much memory a model needs.