Interview Q&A / AI & Generative AI

IntermediateIn-depth3 min readUpdated 2 Oct 2026

What do temperature and top-p control in an LLM?

SHORT ANSWER

Both control how the next token is picked from the model's probabilities. Temperature sharpens or flattens the probabilities (low means predictable, high means varied), and top-p limits sampling to the smallest set of tokens whose probabilities add up to p.

Explain it simply

At every step an LLM gives a score to each possible next token. Imagine a canteen menu where each dish has a popularity score. You could always order the most popular dish, or you could pick with some randomness so you sometimes try something else.

  • Temperature changes how strongly you favour the popular dishes. Low temperature: you almost always pick the top dish. High temperature: the less popular dishes get a real chance.
  • Top-p (also called nucleus sampling) first throws away the unpopular tail of the menu. With top-p of 0.9, you keep only the most popular dishes until their shares add up to 90%, and pick only among those.

Neither setting makes the model smarter or adds knowledge. They only change how it chooses among options it already considered likely.

Example

The model's raw scores (logits) are turned into probabilities with a function called softmax. Temperature divides the scores before softmax:

js
function softmax(logits, temperature) {
  const exps = logits.map((x) => Math.exp(x / temperature));
  const sum = exps.reduce((a, b) => a + b, 0);
  return exps.map((x) => (x / sum).toFixed(2));
}

const logits = [2, 1, 0]; // scores for three candidate tokens

console.log(softmax(logits, 1)); // Output: ["0.67","0.24","0.09"]
console.log(softmax(logits, 0.5)); // Output: ["0.87","0.12","0.02"]
console.log(softmax(logits, 2)); // Output: ["0.51","0.31","0.19"]

At temperature 0.5 the top token gets 87% of the probability, so output is very predictable. At temperature 2 the choice is much more spread out.

With top-p = 0.9 on the temperature-1 numbers, the model keeps the first two tokens (0.67 + 0.24 = 0.91, which crosses 0.9), drops the third, rescales the two survivors, and samples between them.

How it works

  1. The model outputs one score per token in its vocabulary.
  2. Scores are divided by the temperature, then passed through softmax to get probabilities.
  3. If top-p is set, tokens are sorted by probability and only the smallest group that reaches p is kept.
  4. One token is sampled from what remains.

A temperature of 0 is usually treated as "always pick the highest-probability token" (greedy decoding). Even then, outputs may not be perfectly identical across runs on some systems because of how the computations are batched and rounded.

Real-world use

  • Extracting fields from an invoice or classifying support tickets: low temperature, because you want consistent answers.
  • Brainstorming taglines for a college fest: higher temperature, for variety.
  • Many teams change either temperature or top-p, not both, so the effect of each change is easy to understand.

Common mistakes

  • Thinking temperature 0 removes hallucinations. It makes output repeatable, not correct.
  • Setting temperature very high and getting incoherent text, then blaming the model.
  • Confusing top-p with top-k. Top-k keeps a fixed number of tokens; top-p keeps a variable number based on cumulative probability.

What to say in the interview

"Temperature and top-p are decoding settings. Temperature scales the logits before softmax: below 1 it sharpens the distribution so the top tokens dominate, above 1 it flattens it so output is more varied. Top-p, or nucleus sampling, keeps only the smallest set of tokens whose probabilities add up to p and samples from that set, which cuts off the unlikely tail. For factual or structured tasks I use a low temperature; for creative tasks a higher one. Neither one improves accuracy."

Follow-up questions

  • What is the difference between top-p and top-k sampling?
  • Why might you still see different outputs at temperature 0?
  • Which setting would you use for a JSON extraction task, and why?

#llm#sampling#temperature#top-p#generative-ai

Related questions

Practise more AI & Generative AI questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open AI & Generative AI quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.