Building with AILesson 4 of 80

Module 1 · The call underneath everything

Temperature, sampling, and why the same input gives a different answer

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 4 of 10 in this chapter

This chapter’s goalSend the same request to a hosted model and to a model running on your own machine, read every field of the response including token counts and stop reason, and predict within a factor of two what a proposed feature will cost per thousand users before you write it.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 7Free · no account needed
Lesson steps

What the model actually produces

A text-generation system scores possible next tokens and uses a decoding method to select one. Some APIs expose sampling settings. Others manage decoding without exposing those controls. The following is a simplified sampler for a model that supports these settings.

A common sampling pipeline works as follows:

  1. Divide every logit by the temperature.
  2. Apply softmax to turn the scores into probabilities summing to 1.
  3. Optionally cut the tail: top_k keeps the k highest, top_p (nucleus) keeps the smallest set whose probabilities sum to at least p.
  4. Draw one token at random from what is left.

Temperature divides before the softmax, so it stretches or flattens the distribution. At temperature=2 the gaps between candidates shrink and unlikely tokens become reachable. At temperature=0.1 the top candidate dominates. At exactly 0, most implementations skip sampling and take the argmax.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Python, from Zero, for AI exam.

Stuck on something?Ask Sayali, your AI tutor

How to cite: Addaly (2026) ‘Temperature, sampling, and why the same input gives a different answer’, in Building with AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/building-with-ai/sampling-and-determinism (Accessed: 29 November 2026).