Sampling & Decoding

Gen AI interview temperature top-p top-k

How does the model pick the next token?

At each step the model outputs a probability distribution over the whole vocabulary (logits → softmax). Decoding is the strategy for choosing one token from that distribution — always the top one (greedy) or a sample.

Follow-ups
  • “What are logits?” — The raw pre-softmax scores; softmax turns them into probabilities that sum to 1.

What does temperature do?

Temperature divides the logits before softmax. Low T sharpens the distribution (the top token dominates → focused, deterministic). High T flattens it (more options get a chance → diverse, creative, riskier).

Follow-ups
  • “What is T = 0?” — Effectively greedy decoding — always the most likely token, fully deterministic.

Greedy vs top-k vs top-p (nucleus)?

They differ in which tokens are eligible to sample from — i.e. how they truncate the distribution before sampling.

Greedy / beam pick the top

Greedy takes the single best token; beam search keeps several candidate sequences. Safe but repetitive.

Top-k k best only

Sample only from the k highest-probability tokens — cuts off the long tail of junk.

Top-p (nucleus) smallest set ≥ p

Keep the smallest set of tokens whose probability sums to p (e.g. 0.9) — adapts to how peaked the distribution is.

Follow-ups
  • “Top-k vs top-p?” — Top-p is usually preferred: it widens when the model is unsure and narrows when it's confident.

When do you use low vs high temperature?

Low for correctness, high for creativity.

Low temperature (≈0–0.3)
  • Facts, code, extraction, classification.
  • Reproducible, fewer hallucinations.
High temperature (≈0.8–1.2)
  • Brainstorming, story writing, variety.
  • More surprising — and more likely to drift.
Follow-ups
  • “Lower temperature to reduce hallucinations?” — It helps a bit, but grounding/RAG matters far more. (See Hallucinations.)
  • “Why isn't output reproducible even at T=0?” — Hardware/floating-point non-determinism and batching can still cause small variations.