Ask a chatbot the same question twice and you can get two different answers. Ask it with “temperature 0” and you usually get the same answer every time. So where does the variety come from, and what are all those other dials, like top-k, top-p and min-p, actually doing?
A language model never writes a word directly. At every step it gives a score to every token it knows, and a separate piece of code, the sampler, turns those scores into one choice. The model is the same every time you run it. The sampler is where the randomness lives, and its settings are yours to change.
By the end of this page you will be able to look at a set of sampler settings and predict what they do: which candidates survive, how much randomness is left, and why the same settings that make a story lively will break a line of code.
Before this: new to tokens? Start with How text becomes tokens. It explains the pieces this page chooses between.
Next-token sampler
Recent tokens (mock window for the penalties)
Outlined tokens are also candidates, so the penalties lower their scores.
Sampler chain, llama.cpp order · pick a step to inspect it
- Sampleone token
Four readouts sit under the bars. Entropy, in bits, measures how spread out the choice is: 0 bits means one certain answer, 1 bit is a fair coin toss. Perplexity turns that into “how many equally likely options this feels like”. Probability kept is the share of the full distribution that survives the filters, and candidates kept counts the survivors. After Sample 1,000, the gap between expected and observed adds up how far each observed share is from its expected share and halves the total: 0 means a perfect match.
The scores are invented to be plausible, so treat them as a picture of the mechanism, not as facts about a model. One quiet example: ␣Te sits in the capital list (0.25%) because the city’s te reo name is Te Whanganui-a-Tara. In the Kia preset each te reo word is shown as one token for readability; a real tokeniser usually splits words like tūpato into several pieces, as How text becomes tokens shows.
Scores first, then probabilities
The raw scores are called logits. They are just numbers: ␣Wellington scores 16.0 and ␣Auckland 13.8 in our example. On their own they mean little; what matters is the gap between them. Softmax turns them into probabilities by exponentiating each score and dividing by the total, so a gap of 2.2 becomes a ratio of about 9 to 1 (e2.2 ≈ 9). The same softmax turns attention scores into weights inside the model; see How attention decides what matters.
In code, softmax always subtracts the largest logit before exponentiating. That changes nothing mathematically but stops e1000 from overflowing to infinity. The tests for this page check exactly that case.
The capital example at T = 1 has an entropy of 0.95 bits, a perplexity of about 1.9: nearly a coin toss between one strong answer and a scatter of others. The story opening has 3.87 bits, like choosing from about 15 equal options.
In maths: entropy and perplexity
Temperature in maths
Temperature divides every logit by T before softmax:
Dividing by a small T stretches every gap. At T = 0.5, the 2.2 gap between ␣Wellington and ␣Auckland becomes 4.4, a ratio of about 81 to 1. As T approaches 0 the gaps become infinite and all the probability lands on the top token: that is greedy decoding, and most tools treat temperature 0 as exactly that. Dividing by a large T squashes the gaps towards zero; as T grows without limit every token gets 1/N, the uniform distribution, and the model’s knowledge is thrown away.
Temperature reshapes everything
| Token | Logit | Probability |
|---|---|---|
| n | 14.5 | 78.7% |
| len | 13.2 | 15.5% |
| 10 | 12.0 | 3.5% |
| 1 | 11.0 | 1.0% |
| 0 | 10.6 | 0.6% |
T = 0.80: top token n 78.7%, entropy 1.04 bits
Temperature never changes the ranking. It only changes how much the ranking matters. That is why it is often paired with a filter that removes the tail entirely.
Three ways to cut the tail
A real vocabulary has tens of thousands of tokens, and most of them are nonsense in context. Each gets a tiny probability, but tens of thousands of tiny probabilities add up. The filters exist to cut that tail before the dice roll.
- Top-k keeps the k highest-scoring tokens. Simple, but blind: k = 40 is generous for “The capital of New Zealand is” and stingy for a story.
- Top-p, or nucleus sampling, from Holtzman and colleagues (2020), keeps the smallest set of top tokens whose probabilities add up to at least p. The token that crosses the line is kept.
- Min-p keeps any token whose probability is at least a fraction of the top token’s:
With the default min-p of 0.05 and ␣Wellington at 84%, the bar is 4.2%. ␣Auckland (9.3%) clears it; ␣a (2.8%) does not.
Why min-p and top-p adapt
The difference between the three only shows when you compare a confident distribution with a flat one.
Fixed count, fixed mass, or relative to the best
Confident: “The capital of New Zealand is”
Flat: “Once upon a time, on a small island,”
Confident versus flat: Top-k keeps 5 and 5; Top-p keeps 2 and 14; Min-p keeps 2 and 18.
Top-p and min-p adapt in different ways. Top-p fixes the mass it keeps, so in a very flat distribution it can still keep dozens of weak tokens to reach 90%. Min-p fixes a bar relative to the best, so when the model is confident the bar is high, and when it is unsure the bar drops.
Min-p keeps all 18 of 18 in the flat row only because this toy has 18 candidates and even the weakest is about 8 times less likely than the top one, inside min-p’s factor of 10. A real vocabulary has a tail of around 100,000 tokens, most of them far less likely than that, and min-p’s bar cuts that tail even where top-p would keep a long run of it.
Nguyen and colleagues, who proposed min-p, report that it holds up well at high temperatures. Their paper applies temperature before min-p, so the bar moves with T. Our generator, like llama.cpp, applies temperature last: min-p cuts first and the heat only reshuffles the survivors. Both orders help at high temperature, for different reasons. A 2025 follow-up by Schaeffer and colleagues argues the advantage over other samplers is smaller than first reported.
Penalties: pushing back on repeats
Models like to copy. If the context already says “The weather in Wellington is windy”, the most likely next token after a second “The weather in Wellington is” is ␣windy again, at 85% in our example. Repetition penalties lower the score of any token that appeared in the recent window (64 tokens by default in llama.cpp). The repeat penalty r divides a positive score by r and multiplies a negative one by r, so the token always becomes less likely. The presence penalty subtracts a fixed amount from any token that appeared at all.
A repeat penalty of 1.3 drops ␣windy from 85% to 15%. The same 1.3 on the Python loop drops the correct n to 7%. Penalties cannot tell a boring repeat from a necessary one, which is why many people leave them off for code.
In maths: the llama.cpp penalties
Here c is how many times the token appeared in the window, and [c > 0] is 1 if it appeared at all. The frequency penalty grows with each repeat; it works like the presence penalty, so the widget leaves it out.
Order matters
The sampler is a chain, and the steps do not commute. llama.cpp’s default chain is penalties, DRY, top-n-σ, top-k, typical-p, top-p, min-p, XTC and temperature last (common/common.h). With default settings, DRY, top-n-σ, typical-p and XTC do nothing, which leaves the five steps in the main figure. Because temperature comes last, top-p and min-p decide who survives using the T = 1 distribution, and temperature only reshuffles the odds among the survivors.
Other software applies temperature first. Hugging Face transformers, for example, runs temperature, then top-k, top-p and min-p. Flip the Order switch in the main figure at T = 2 on the capital context: temperature-last keeps 2 tokens, temperature-first keeps 7, and ␣Wellington drops from 75% to 53%. Same numbers on the dials, noticeably different behaviour.
From one word to a passage
Picking one word is a small decision. Picking a hundred in a row, each conditioned on the last, is where sampling settings show their character. The generator below uses a tiny word-level n-gram model: it counts which word followed which pair of words in about 800 words of text we wrote about Aotearoa’s harbours and weather, then predicts from those counts. It is nothing like a transformer, but it produces a probability for every next word, so the same sampler chain works on it unchanged.
It also shows two other ways to choose words. Greedy always takes the top word. Beam search keeps the B best partial passages at every step and returns the one with the highest total log-probability, the sum of the natural log of each word’s probability (closer to zero is more likely).
Generate a passage
- Log-prob
- −43.6
- Per token
- −0.62
- Distinct
- 21%
The harbour is calm again, and the sea wall. Children jump off the wharf, and the sea wall. Children jump off the wharf, and the sea wall. Children jump off the wharf, and the sea wall. Children jump off the wharf, and the sea wall. Children jump off the wharf, and the sea wall. Children jump off the wharf,
- Log-prob
- −51.2
- Per token
- −0.73
- Distinct
- 87%
The harbour at the wharf into short white waves and bends the trees on the water, the rain comes in from the south. When the weather is softer. The Waitematā Harbour is shallow and wide, and the day, and the port is busy with a narrow entrance. The Waitematā Harbour is shallow and wide, and a red sky in the morning and come back when
- Log-prob
- −23.8
- Per token
- −0.34
- Distinct
- 66%
The harbour is calm again, so people carry a jacket and leave the umbrella at home. On the west coast the Manukau Harbour is wide and busy with ships that carry logs, coal and cars. Whakaraupō is the Māori name for the harbour is calm again, so people carry a jacket and leave the umbrella at home. On the west coast the Manukau Harbour is wide
Hover over or tab to any generated word to see its probability.
The Model switch sets how much context the n-gram sees. A trigram looks at the two previous words; a bigram looks at only one, so it loses the thread sooner. On “The harbour” with seed 1 and the defaults, the sampled bigram passage scores about −115 against the trigram’s −51. A modern model looks back over thousands of tokens; the next step, Watch a neural network learn, shows where those scores come from.
What this means for local AI
When you run a model on your own machine, these dials are in your hands rather than a provider’s. The defaults differ between tools. Checked November 2026:
| Setting | llama.cpp | Ollama | LM Studio |
|---|---|---|---|
| Temperature | --temp 0.8 |
temperature 0.8 |
Temperature 0.8 |
| Top-k | --top-k 40 |
top_k 40 |
Top K 40 |
| Top-p | --top-p 0.95 |
top_p 0.9 |
Top P 0.95 |
| Min-p | --min-p 0.05 |
min_p 0.0 (off) |
Min P 0.05 |
| Repeat penalty | --repeat-penalty 1.0 (off) |
repeat_penalty 1.0 (off) |
Repeat Penalty 1.1 |
| Seed | --seed −1 (random) |
seed random by default (code uses −1; the Modelfile docs table says 0) |
off (random) |
Sources: llama.cpp common/common.h and common/arg.cpp; Ollama’s Modelfile reference and api/types.go; LM Studio’s configuration schema. These are the tools’ own fallbacks. Many models ship their own recommended values, which override them: an Ollama model can carry parameters in its Modelfile, and LM Studio can store settings per model. Check what is actually in effect before tuning.
Some starting points, as trade-offs rather than rules:
- Code, maths, extraction, tool calls. Low temperature (0 to 0.3) or a tight min-p (0.1 or more). Keep repetition penalties off, because code repeats names on purpose. You are buying correctness with variety.
- Chat and general writing. The llama.cpp defaults (0.8, top-p 0.95, min-p 0.05) are a sensible middle.
- Creative writing and brainstorming. Temperature 1.0 to 1.5 with min-p 0.05 to 0.1 and top-p off. Min-p lets you raise temperature without falling into the tail. Expect more surprises, good and bad.
Seeds make runs repeatable. A sampler draws from a pseudo-random number generator, and all three tools pick a fresh random seed for each run unless you set one. Fix the seed and the same model, prompt and settings produce the same “random” draws, so the same text. That is useful for comparing settings fairly, for tests, and for reproducing a bug. It is only exact on the same software and hardware, though: different GPU kernels, batch sizes or quantisations can shift logits slightly, and a tiny shift can flip one close choice and change everything after it.
What we simplified
- The scores are invented. The logits in the first three figures are hand-written to be plausible. They are not from a real model and should not be read as facts about any model.
- Twenty candidates, not a vocabulary. Real samplers work over 32,000 to 260,000 tokens. The tail is far longer, which makes the filters matter more.
- Whole words as tokens. Te reo words in the Kia preset are shown whole; a real tokeniser would split most of them.
- Only the default steps. llama.cpp also has DRY, XTC, typical-p, top-n-σ and mirostat samplers, and grammars that restrict output to valid JSON or code. We show only the steps that are active by default.
- An n-gram model is not a transformer. It sees two words of context; a modern model sees thousands of tokens. Its loops and its word salad are exaggerated versions of what real models do.
- Our beam search is bare. No end-of-text token, no length normalisation, fixed length. Chat models rarely use beam search at all; it is more common in translation and speech recognition.
- Penalties use a mock window. In the first figure the recent tokens are a fixed list per example, not a real conversation.
Next: where do the logits come from? A trained network. See Watch a neural network learn.
Inspired by, and worth your time
- Seeing Theory by Brown University
- The Curious Case of Neural Text Degeneration (ICLR 2020) by Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes and Yejin Choi
- Turning Up the Heat: Min-p Sampling for Creative and Coherent LLM Outputs (ICLR 2025) by Minh Nhat Nguyen and colleagues
- LLM Samplers Explained by kalomaze
- How to generate text: using different decoding methods for language generation with Transformers by Patrick von Platen, Hugging Face
- Transformer Explainer by Aeree Cho, Polo Chau and colleagues, Georgia Tech