Temperature and Top-p Explained: A Visual AI Beginner’s Guide

AI BASICS TO ADVANCED · LESSON 07
Same request.
Different possibilities.
Meet two settings that shape how AI selects its next token.

Raju asks his AI helper for a name for his ginger tea. One time it suggests “Ginger Comfort.” Another time it suggests “The Ginger Pause.” Why can the same request produce different wording? Today, we explore temperature and top-p—two generation settings that influence how a language model chooses among possible continuations.

Raju is fictional. These AI-generated cartoons are visual metaphors; the robot represents software. Names and outputs are teaching examples, not measured results.

01. First, remember the next-token idea

A language model produces scores for possible next tokens based on the context. A decoding method turns those scores into a choice. After a token is added, the model considers the updated context and repeats. A token can be part of a word, a whole word, or punctuation.

ONE STEP INSIDE GENERATION
01
Context
Raju’s ginger tea is…
02
Candidates
Possible next tokens have scores
03
Selection
Choose a token, then continue
A complete sentence is built through many such steps. The model does not simply pick one finished answer from a list.

02. What does sampling mean?

Imagine a weighted draw: more likely tokens get a larger share of the chances. Sampling chooses using those probabilities, so a less likely token can sometimes be selected. Greedy decoding instead chooses the highest-scoring available token at each step.

TWO WAYS TO CHOOSE
01
Greedy
Pick the highest-scoring token
02
Sampling
Draw according to probabilities
Chance is weighted. Sampling does not mean that every candidate is equally likely.

03. What is temperature in AI?

Temperature changes how concentrated the next-token probabilities are. Lower positive values sharpen the distribution toward favored tokens; higher values flatten it, giving alternatives more opportunity. Temperature 1 leaves the original softmax distribution unchanged before other filters. It is a mathematical setting, not the computer’s physical temperature.

THE TEMPERATURE EFFECT
01
Lower
More concentrated choices
02
T = 1
Unscaled reference distribution
03
Higher
More spread among alternatives
Higher temperature can increase variation, but does not guarantee useful creativity or better answers.


04. A small numerical example

Suppose only two tokens are possible: A has probability 80% and B has 20% at temperature 1. At temperature 0.5 their probabilities become about 94.1% and 5.9%. At temperature 2 they become about 66.7% and 33.3%. A remains favored, but the gap changes.

SAME TWO CANDIDATES · DIFFERENT WEIGHTS
01
T = 0.5
A: 94.1% · B: 5.9%
02
T = 1
A: 80% · B: 20%
03
T = 2
A: 66.7% · B: 33.3%
Exact toy calculation, rounded to one decimal place. No other filters are applied; these are not measured model probabilities.
Optional: the simple math behind the example

For positive temperature T, raise each original probability to the power 1/T, then divide each result by their sum. For T = 0.5: 0.8² ÷ (0.8² + 0.2²) ≈ 0.941. Equivalently, temperature divides model scores called logits before softmax converts them to probabilities. You can understand the lesson without memorizing this formula.

05. What is top-p, or nucleus sampling?

Top-p limits the candidate pool by cumulative probability. Rank tokens from most to least likely. Keep the smallest leading group whose probabilities reach or exceed p, then sample within that group using rescaled probabilities. The token that crosses the threshold stays in the pool.

BUILD THE CANDIDATE POOL
01
Rank
Highest probability first
02
Accumulate
Stop when the total reaches p
03
Sample
Choose among retained tokens
Top-p = 0.9 does not mean “keep 90% of all words.” It refers to probability mass, not the number of tokens.

06. Walk through top-p = 0.8

Consider four hypothetical tokens: A = 50%, B = 25%, C = 15%, and D = 10%. A alone reaches 50%; A + B reaches 75%, still below 80%. Adding C reaches 90%, so A, B, and C remain. D is excluded.

CUMULATIVE PROBABILITY
01
A · keep
50% total
02
B · keep
75% total
03
C · keep
90% total: threshold reached
04
D · exclude
Outside the retained pool
After rescaling the retained 90% to 100%: A ≈ 55.6%, B ≈ 27.8%, C ≈ 16.7%. Rounded values may not sum to exactly 100%.

07. Temperature and top-p do different jobs

Temperature reshapes relative probabilities. Top-p removes the lower-probability tail beyond a cumulative cutoff. They can interact because changing probabilities can change which tokens fit inside the cutoff. Exact processing order and other filters depend on the implementation.

COMPARE THE TWO CONTROLS
01
Temperature
Changes probability concentration
02
Top-p
Limits the cumulative candidate pool
03
Top-p = 1
No nucleus truncation by this setting
Top-p = 1 does not make the distribution uniform. Other settings can still constrain candidates.

08. Zero temperature is not a truth switch

Some services interpret temperature 0 as greedy or near-greedy decoding; other libraries require a separate greedy option and a positive sampling temperature. Even when repeated answers are more stable, that does not establish correctness. Reproducibility can also depend on model version and serving details.

KEEP THESE IDEAS SEPARATE
01
Consistency
Does wording repeat?
02
Correctness
Are the claims supported?
03
Reproducibility
Can the run be recreated?
Check the specific model’s documentation. A stable wrong answer remains wrong.

09. Raju still needs to check the facts

Suppose an output describes his tea as “award-winning,” although he supplied no award information. Lowering the temperature does not verify that claim. Raju should compare the draft against confirmed facts and remove unsupported details, whatever settings were used.

FACTS NEED EVIDENCE
01
Source
Confirmed ingredients and shop details
02
Draft
Check each factual claim
03
Decision
Verify, revise, or remove
Sampling settings do not retrieve missing facts, update model knowledge, or establish that a citation is real.



10. Where can you change these settings?

Some developer playgrounds, APIs, and local model tools expose temperature and top-p. A normal chat interface may hide them. Available controls, accepted ranges, and recommended defaults vary by model. Writing “temperature = 0.2” in a message does not itself change an API parameter.

PROMPT VS. GENERATION SETTING
01
Prompt
The task, facts, and output requirements
02
Parameter
A supported setting in the tool or API
03
Documentation
Which settings this model accepts
Start with the model’s documented defaults. Do not copy another model’s configuration without checking compatibility.

11. Run a useful beginner experiment

Keep the model, prompt, input facts, and output limit fixed. Generate five responses with the documented defaults. If the model supports tuning, change only temperature to another supported value and generate five more. Compare variety, instruction-following, and unsupported claims. Leave top-p unchanged for this first experiment.

A SMALL CONTROLLED COMPARISON
01
Baseline
5 responses with default settings
02
One change
5 responses with one supported change
03
Compare
Variety + compliance + factual support
Five trials are a learning exercise, not a reliable benchmark. More important applications need broader evaluation.
Use the same prompt for every trial
Suggest three short menu names for ginger tea. Confirmed ingredients: black tea, milk, ginger, and sugar. Use simple English. Do not invent awards, prices, ingredients, or health benefits. Return only the three names.

No settings in your chat app? You can still practice writing clear prompts and checking answers. Asking for several alternatives is useful, but it is not the same experiment as changing a sampling parameter.

12. Choose settings by testing the task

For a menu-name brainstorm, compare how many distinct, usable ideas you get. For extracting a price, compare whether the answer exactly matches the supplied source. Begin with recommended defaults and improve the prompt or evidence when those are the problem. There is no universal best temperature for every model and task.

DEFINE SUCCESS FIRST
01
Brainstorming
Distinct ideas that fit the brief
02
Extraction
Exact match to source facts
03
Publishing
Useful writing with checked claims
If you later test top-p, keep temperature fixed. Change one variable at a time so the comparison is interpretable.

Check your understanding

1. With probabilities 50%, 25%, 15%, and 10%, which tokens remain at top-p = 0.7?
2. Does higher temperature guarantee a more accurate answer?
3. Does top-p = 1 give every token an equal chance?

Reveal the answers

1. A and B: together they reach 75%, crossing 70%.
2. No. It changes the probability distribution; accuracy requires evaluation.
3. No. It disables nucleus truncation, while the remaining distribution can still be highly uneven.

YOUR ONE-MINUTE RECAP

Temperature reshapes probabilities.
Top-p restricts the candidate pool.
Neither guarantees truth.

Use documented defaults, test one change at a time, and judge the actual results.

Next lesson: Why AI hallucinates

We will explore why AI can produce confident but unsupported answers, how to recognize them, and how source-based checking helps.

Join the conversation: For Raju’s menu names, would you prioritize variety, consistency, or factual support? Explain your choice.


Sources and further reading

Examples and numerical exercises were created for this lesson. Consult the current documentation for your chosen model.

Comments

Popular posts from this blog

Beyond Standard RAG: The Architecture of Agentic Retrieval and Self-Correction Loops

The Architecture of Vision-Language Models: How Multimodal AI Sees and Understands the World

Java Collection Framework: A Practical Guide with Examples