In this article
  1. Before you start
  2. Why interviewers ask this
  3. How an LLM picks the next word from first principles
  4. The Staff-level answer
  5. The rubric
  6. Three variants of this question
  7. Common mistakes
  8. Follow-up questions
  9. Sources

Before you start

This page sits inside the LLM inference guide and builds on how a transformer produces its scores.

Why interviewers ask this

A candidate who cannot explain decoding treats a language model as a black box that "just answers." Interviewers ask about it because the same model produces dull, repetitive text or wandering nonsense depending on a few settings, and an engineer must know which knob fixes which problem. Holtzman et al. show that decoding choices alone can change text quality dramatically, even with exactly the same model.

Candidates report being asked this in many forms: the difference between top-k and top-p, what temperature zero means, how to adjust creativity, and how to stop repeated phrases. A weak answer lists names. A strong answer says what each setting does to the probability list. A Staff-level answer also says what each one cannot fix and where the settings interact. For example, temperature changes which tokens fall inside top-p's set, because top-p is applied after temperature in the usual order.

How an LLM picks the next word from first principles

Decoding strategy
A decoding strategy is the rule that turns a language model's next-token probabilities into one chosen token at each step, repeated until the text ends. The model stays the same; only the choosing rule changes.

In plain words

Imagine a person finishing your sentence at a party. In their head, some endings feel very likely and many feel unlikely. They do not say the single likeliest ending every time, or the conversation would be boring; they also would not say a random word. Decoding is the habit they use to choose. The analogy breaks in one place: a person chooses from feeling, while a model chooses by a fixed arithmetic rule you can change.

In one precise sentence, a language model produces a probability for every token in its vocabulary, and the decoding strategy selects one token from that list, again and again, with each chosen token fed back in as input. This repeated sampling of the next word is how Conformal Nucleus Sampling describes generation. Below, a toy list of six tokens shows each stage; the numbers are illustrative.1

Each sampled token passes four stages in orderFlow of one generation step: the model scores every token, temperature reshapes the probabilities, top-k or top-p trims the list, and one token is chosen and fed back in.Probabilitiessoftmax ofscoresTemperaturereshapeTop-k ortop-ptrim the tailChoose onethen repeatDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/how-an-llm-picks-the-next-word.html

Every sampled token passes the same four stages, and the decoding strategy lives in the last three.CC BY 4.0 — source: How an LLM picks the next word, step by step
Step-by-step description
  1. Softmax turns scores into probabilities.
  2. Low sharpens, high flattens.
  3. Top-k or top-p cuts the tail.
  4. One token is chosen, then repeat.
  • The model scores every token and softmax makes probabilities.
  • Temperature reshapes the probabilities.
  • Top-k and top-p cut the unreliable tail.
  • One token is chosen, then the loop repeats.

Step 1: The model scores every token and softmax makes probabilities

At each step the model outputs one logit per token in its vocabulary, and softmax turns the logits into probabilities that sum to one. The logits come from the final layer of the transformer after it reads all tokens so far.

Input: the text so far. Output: one probability per possible next token. In the peaked toy case (illustrative), the token "the" gets 0.8246 and "sat" gets 0.002; in the flat toy case, the best token gets only 0.2308.1 Notice that the model has not chosen anything yet: it has only produced a list. You now know what the later steps act on, and why a confident model and an unsure model give very different lists to the same rule.

Step 2: Temperature reshapes the probabilities

Temperature divides every logit by a number T before softmax, so a low T widens the gap between tokens and a high T narrows it. With logits zi, the probability of token i is:

pi=ezi/T∑jezj/T

In the peaked toy case the top token's probability is 0.9793 at T of 0.5, 0.8246 at 1.0 and 0.6569 at 1.5; in the flat case it is 0.3046, 0.2308 and 0.2082.1 The Hugging Face documentation lists a default of 1.0, which leaves the model's own probabilities unchanged. Because softmax of z divided by T is proportional to pi1/T, dividing the logits is the same as reshaping the probabilities directly. Temperature adds no knowledge; it only reshapes the same list.

As T shrinks, the top token takes almost everything: at T of 0.1 the peaked toy case gives it 1.0 to four places.1 That limit is called greedy decoding: greedy decoding is a rule that always takes the highest-probability token. Exactly zero cannot be computed with this formula, so software normally treats it as a special case that takes the top token directly.

Step 3: Top-k and top-p cut the unreliable tail

Top-k and top-p remove unlikely tokens before the draw, because a long tail of poor options adds up to a real chance of a bad pick. Top-k keeps a fixed count; the Hugging Face documentation lists a default k of 50. Top-p, introduced by Holtzman et al., keeps the smallest set of most probable tokens whose probabilities add up to p or higher, the same definition the Hugging Face documentation gives, so its pool adapts to the model's certainty. These defaults apply only when sampling is turned on (do_sample=True); otherwise generate() decodes greedily.

Top-k keeps 3 tokens in both toy cases, while top-p at 0.9 keeps 2 tokens when the model is confident and 6 when it is not (illustrative numbers).
Toy caseTop-k with k = 3Top-p with p = 0.9
Peaked: tokens kept32
Peaked: probability mass kept0.97730.9362
Flat: tokens kept36
Flat: probability mass kept0.59071.0

Top-k with k of 3 keeps 3 tokens in both toy cases, while top-p at 0.9 keeps 2 tokens when peaked and 6 when flat. The flat case shows the weakness of a fixed k: it discards 41% of the mass the model considers plausible.1 After trimming, the remaining probabilities are rescaled to sum to one. You now know why top-p is the more flexible rule.

Top-p adapts its kept set while top-k stays fixedGrouped bars of tokens kept in an illustrative six-token toy: top-k with k of 3 keeps 3 tokens in both cases, while top-p at 0.9 keeps 2 for a peaked distribution and 6 for a flat one.Tokens kept0246Top-k, k = 3Top-p, p = 0.9RulePeaked distributionFlat distribution3236Diagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/how-an-llm-picks-the-next-word.html
Top-k keeps a fixed count, while top-p keeps few tokens when the model is confident and many when it is not.CC BY 4.0 — source: How an LLM picks the next word, step by step
Text description of the diagram
  • Top-k with k of 3 keeps 3 tokens in both toy cases.
  • Top-p at 0.9 keeps 2 tokens when peaked and 6 when flat.

Step 4: One token is chosen, then the loop repeats

The last stage chooses one token from the trimmed list, either by taking the top one or by a random draw weighted by the probabilities. The chosen token joins the text and the model runs again, reusing the key-value (KV) cache; servers interleave many loops with continuous batching.

Greedy decoding takes the top token every time, so a locally best token can mislead. Beam search is a search rule that keeps the w best partial sequences at each step. In a two-step toy (illustrative), greedy takes A with probability 0.6 and then a 0.5 continuation, for 0.3, while beam search with width 2 also keeps B and finds B then z at 0.36.1

Beam search can still loop: a 2025 International Conference on Computational Linguistics (COLING) study of decoding hyperparameters reports lower diversity as the beam widens, from 12.78 at width 3 to 6.17 at width 50. Beam search and greedy decoding suit tasks with one right answer, such as translation or structured output; sampling suits open-ended text, where maximizing likelihood gives bland, repetitive output (Holtzman et al.).

Knowing it is half the job. Saying it is the other half.

Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.

Start Free Trial →

The Staff-level answer

Interviewer asks

How does an LLM pick the next word?

A strong answer

The model outputs a score for every token, and softmax turns those into probabilities. A decoding strategy then chooses one. Temperature divides the scores first, so low values sharpen the distribution and high values flatten it. Top-k keeps a fixed number of tokens, and top-p keeps the smallest set reaching a probability threshold, so it adapts to confidence. Then I sample from what is left. For closed-ended tasks with one right answer, such as translation or structured output, I use greedy decoding or beam search instead. Always taking the top token on open-ended text gives flat, repetitive output, so there I sample, and I tune one knob at a time.

What makes this Staff-level is the last move: it names the failure of the obvious rule, and it treats the settings as separate levers on one distribution rather than as a list of names.

The rubric

The rubric separates answers by whether they describe what each setting does to the probability list, not by how many setting names they recite.
DimensionWeak answerStrong answerStaff answer
Correctness"The model picks the most likely word."Probabilities, then a draw from them.Separates the model's distribution from the choosing rule.
DepthNames temperature, top-k, top-p.Explains how each reshapes or trims the list.Gives the formula and the limit as temperature falls.
Trade-offsNone given.Low is safe but dull; high is varied but erratic.Notes fixed k fails on flat distributions; beam can still repeat.
CommunicationJargon in a list.One idea per sentence, with an example.Orders the answer along the pipeline.
Follow-up readinessStuck at temperature zero.Says it matches greedy decoding.Adds the divide-by-zero special case.

Three variants of this question

What is the difference between top-k and top-p (nucleus) sampling?

Top-k keeps a fixed number of tokens, while top-p keeps the smallest set whose probabilities reach a threshold, so the set size changes with the model's confidence. In the toy flat case top-k with k of 3 keeps 3 tokens and loses 41% of the mass, while top-p at 0.9 keeps all 6.1

When you set the temperature to 0.0, which decoding strategy are you using?

You are effectively using greedy decoding, because as temperature falls toward zero the top token takes almost all the probability. Say that exactly zero is usually handled as a special case, since the formula would divide by zero. For a Staff-level nuance, do not call it perfectly deterministic: on shared hardware, floating-point rounding and batching can change which of two near-tied tokens wins, so two runs may occasionally differ. Check your provider's documentation.

How do you adjust the creativity of generative AI models?

Raise temperature or widen top-p or top-k for more varied wording, and lower them for steadier output. Temperature only reshapes probabilities; it adds no knowledge, so "creative" output can also be wrong. Change one setting at a time and compare outputs.

Common mistakes

  • "The model picks the most likely word." (wrong) The usual rule samples from a trimmed distribution, and always taking the top token gives flat, repetitive text. (right) "The model produces probabilities, and the decoding strategy chooses from them."
  • "Beam search fixes repetition." (wrong) Holtzman et al. report that using likelihood as the decoding objective leads to bland, strangely repetitive text, and beam search still seeks high likelihood. (right) "Beam search reduces the risk of one poor early choice but can still loop."
  • "Top-k and top-p are the same." (wrong) One keeps a count and the other a probability mass. (right) "Top-p adapts the pool size; top-k does not."
  • "Decoding changes the model." (wrong) The weights stay the same. (right) "Decoding acts on the same model's output distribution."
  • "Higher temperature makes the model smarter." (wrong) It only flattens the probabilities. (right) "Temperature trades predictability for variety."

Follow-up questions

What are decoding strategies in LLMs (temperature, top-k, top-p, beam search)?

Decoding strategies are the rules that choose a token from the model's probabilities: greedy decoding takes the top token, beam search tracks several sequences, and temperature, top-k and top-p shape a random draw. The Hugging Face documentation lists top_k, top_p and repetition_penalty among its generation parameters.

Your model repeats phrases and gets stuck in loops on long generations. How do you fix degeneration?

Move away from pure highest-probability decoding toward sampling with top-p, since Holtzman et al. tie degeneration to maximum-likelihood decoding. A repetition penalty is another lever; in Hugging Face, a value of 1.0 means no penalty. Verify on your own prompts, because no single setting is a universal fix.

Why not always pick the most likely word?

Human text has fluctuating per-token probability, while always taking the top token gives unnaturally flat, high probability and dull, looping text. Sampling restores variety.

Does a decoding strategy change the model?

No. The weights are untouched; the strategy only changes how a token is chosen from the same probabilities. Serving costs such as quantization or paged attention are separate from this choice.

What happens if you raise the temperature very high?

The probabilities approach equal chances, so rare tokens get drawn often and the text turns incoherent. In the toy flat case the top token already falls from 0.3046 at T of 0.5 to 0.2082 at 1.5.1

Key takeaways
  • A decoding strategy chooses one token from the model's probabilities, repeatedly, without changing the model.
  • Temperature divides the logits: low sharpens, high flattens, and the limit toward zero is greedy decoding.
  • Top-k keeps a fixed count; top-p keeps a cumulative-probability set whose size adapts.
  • Greedy and beam search suit closed-ended tasks; beam search can still repeat, so open-ended text uses sampling.
  • Change one setting at a time and say what it cannot fix.

Sources

  1. The Curious Case of Neural Text Degeneration (arXiv 1904.09751) — arXiv (Holtzman et al.), ICLR 2020, 22 Apr 2019
  2. Text generation: GenerationConfig documentation (Transformers main branch, read 2026-10-26) — Hugging Face Transformers, 26 Oct 2026
  3. Decoding Decoded: Understanding Hyperparameter Effects in Open-Ended Text Generation — ACL Anthology (Garces Arias et al., COLING 2025), 19 Jan 2025
  4. Conformal Nucleus Sampling (Findings of ACL 2023) — ACL Anthology, 1 Jul 2023