Skip to main content
How LLM serving worksLesson 1 of 9

How an Answer Is Built One Token at a Time

An LLM does not write its answer all at once. It turns the question into tokens (the text pieces a model reads), picks one next token, appends it, and computes again, repeating until the answer is done. That is why chat answers appear as if someone were typing.

Input tokens—
Generated tokens0
Answer received—
Inference engine (e.g. vLLM)Usersends a questionTokenizertext ↔ token IDsModelscores next tokenProbabilitiesnext-token candidatesSamplingpicks one candidateAnswer so fartoken sequencequestion text
RequestResponse / reportAppears in a later step

What you will learn

  • The model reads token IDs, not characters
  • The model outputs a probability table for the next token
  • Generation is a loop: pick a token, append it, compute again
  • Why longer answers cost more time and money
  • When generation stops

Running it on Kubernetes​

  • Inference servers such as vLLM and KServe usually expose this process through an OpenAI-compatible API (/v1/chat/completions and similar). With stream: true, tokens arrive over SSE as they are generated.
  • If streamed tokens arrive in bursts or the stream breaks midway, check the Ingress or gateway in front first. Response buffering, or a read timeout shorter than a long answer, causes exactly this.
  • vLLM exposes cumulative input tokens (vllm:prompt_tokens_total) and output tokens (vllm:generation_tokens_total) on /metrics. Comparing their rates shows whether load comes from long prompts or long answers.
  • When users report answers cut off mid-sentence, check the client’s max_tokens and the server’s maximum context length first.

Try it yourself​

curl -s http://<engine>:8000/v1/models # Served model name, e.g. vLLM
curl -sN http://<engine>:8000/v1/completions -H 'Content-Type: application/json' -d '{"model":"<model>","prompt":"What is Kubernetes?","max_tokens":50,"stream":true}' # One token per SSE event (step 6)
curl -sN http://<engine>:8000/v1/completions -H 'Content-Type: application/json' -d '{"model":"<model>","prompt":"What is Kubernetes?","max_tokens":5}' | grep -o '"finish_reason":"[a-z]*"' # "length": cut by max_tokens (step 8)
curl -s http://<engine>:8000/metrics | grep -E 'vllm:(prompt|generation)_tokens_total' # Input vs output tokens so far

Check yourself

  1. Users report that answers stop abruptly in the middle of a sentence. What should you suspect first?
  2. Why does a 300-token answer take much longer than a 30-token answer to the same question?
  3. The same question gets a slightly different answer each time. Why?

Further reading: vLLM documentation · KServe documentation