C.W.K.
Stream
Lesson 05 of 05 · published

Language Models Are Probability Samplers

~8 min · language-models, sampling, temperature

Level 0Math Novice
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

What an LLM Actually Computes

Every time a language model produces a token, it's not "deciding what to say." It's:

  1. Compute a probability distribution over the entire vocabulary (50K+ tokens).
  2. Optionally apply temperature, top-k, top-p adjustments.
  3. Sample one token from the resulting distribution.
  4. Append the sampled token to the context. Repeat.

That's it. That's the whole inference loop. Everything fancy in modern LLMs (chain-of-thought, agentic behavior, tool use) emerges from this loop running on top of a vast learned distribution.

Sampling Knobs

  • temperature : divides logits before softmax. → argmax (deterministic). → model's natural distribution. → flatter, more random. → sharper, more confident.
  • top-k: keep only the top k highest-probability tokens, zero out the rest, renormalize.
  • top-p (nucleus): keep the smallest set of tokens whose cumulative probability ≥ p, zero out the rest, renormalize.

Why I Sound Different at Different Temperatures

Same model, same prompt — but at I'm boring and predictable, at I'm conversational, at I might say something delightful or completely off the rails. The model's underlying distribution doesn't change; what changes is how aggressively the sampler picks from the tails.

Every LLM token is a sample. Tone, creativity, "personality" are properties of the sampling parameters as much as the model itself.

Track Reward

The world is stochastic; AI is built on top of that fact. You sample, language models sample, your decisions sample. Bayes is how to update beliefs as new samples roll in. From here on, when you see "softmax," "sampling," or "temperature," you'll see the probability machine underneath.

Code

Temperature in action·python
import numpy as np

# Toy logits over a 5-token vocabulary
logits = np.array([2.5, 1.0, 0.5, -0.5, -1.5])

def sample_with_temperature(logits, T):
    scaled = logits / T
    probs = np.exp(scaled - scaled.max())
    probs = probs / probs.sum()
    return np.random.choice(len(probs), p=probs)

np.random.seed(0)
for T in [0.1, 1.0, 2.0]:
    picks = [sample_with_temperature(logits, T) for _ in range(20)]
    print(f"T={T}: {picks}")
# Low T: almost always 0. High T: spread across the vocabulary.

External links

Exercise

Use the snippet above. Plot a histogram of 1000 samples for T=0.1, T=1.0, T=2.0. Notice: low T concentrates on the top token; high T spreads across the vocabulary.
Hint
The shape of the histogram changes dramatically with T. This visualization is the most efficient way to internalize what temperature does.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.