Skip to content
LLMs / Deep dive

How a language model picks its next token

Logits, temperature, top-k and top-p — and why the same prompt gives different answers. With runnable Python.

Narjis Zekhnini · 3 Nov 2026 · 3 min read

Read the articleMore on LLMs
sampling.py
import numpy as np

def sample(logits, temperature=0.8):
    # scale, softmax, then draw
    z = logits / temperature
    p = np.exp(z - z.max())
    p /= p.sum()
    return np.random.choice(len(p), p=p)
"the"
"a"
"this"

Latest posts

View all →

The week in AI, in one email.

New tutorials, the releases that matter, and new AI products. Every Friday.