IT Vizag
← How LLMs Really Work

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

How LLMs Really Work · Lesson 7 of 15 · 2 min read · 4 min video

Temperature, sampling and why answers vary

How the next word is picked, and which settings to use for which job.

📄 Download slides (PDF)· free sign-up

Ask the same question twice and you may get two different answers. That's not a bug. At every step the model produces probabilities for the next token, and a sampling method picks one.

The probability list

After "The best time to visit Araku Valley is", the model might give:

  • " October": 34%
  • " winter": 22%
  • " between": 15%
  • " during": 9%
  • … thousands of others, each tiny

Now something has to choose.

Greedy

Always pick the top token. It's predictable, but long answers can turn repetitive or flat.

Temperature

Temperature reshapes the probabilities before picking:

  • Low (0 to 0.3): sharpens them, so the top choices win almost always. Focused and consistent
  • Around 0.7 to 1: keeps the model's natural spread. Varied and natural
  • High (above 1): flattens them, so unlikely tokens get chosen. Creative at first, then increasingly odd

Temperature doesn't make a model smarter or more truthful. It changes how adventurous the picking is.

Top-p and top-k

These trim the list before picking:

  • Top-k: keep only the k most likely tokens, say 40
  • Top-p (nucleus): keep the smallest set whose probabilities add up to p, say 0.9

Both cut off the long tail of nonsense options. Usually you tune either temperature or top-p, not both aggressively.

Which setting for which job

  • Extracting data, classifying, writing code, answering from documents: low temperature, 0 to 0.3
  • Everyday chat and explanations: the default, often around 0.7 to 1
  • Brainstorming names, taglines, story ideas: higher, around 1, and ask for many options
🔒 Code example: sign up free (above) to unlock it.

Even temperature 0 can vary

At temperature 0, outputs are usually the same, but not always guaranteed: tiny numerical differences in GPU maths and server batching can flip a close call. If you need exact repeatability, store the answer, don't regenerate it. Some newer reasoning models also fix or ignore these settings, so check your model's documentation.

Tip: For apps, consistency beats creativity. Start at a low temperature, and raise it only for tasks where variety is the point.

In one line

The model outputs probabilities; temperature and top-p decide how boldly it picks from them. Low for accuracy, higher for ideas.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend