Sampling parameters
Sampling parameters such as top-k and top-p are settings that limit which candidate tokens a language model may choose from at each step, working alongside temperature to control how predictable or varied its output is.
At every step a language model produces a probability for each possible next token. When it samples, it draws one at random according to those probabilities. Most of the probability usually sits on a handful of sensible tokens, but thousands of unlikely tokens share the rest, and an occasional draw from that long tail can derail a sentence. Sampling parameters trim the tail before the draw is made.
Top-k keeps only the k most probable tokens and shares the probability out among them. Hugging Face's explainer credits Angela Fan and colleagues with introducing it in 2018 and notes that OpenAI's GPT-2 adopted it. Its weakness is that k is fixed. When the model is confident, a k of 50 still lets in poor choices; when many words are plausible, the same k may cut good ones. A top-k of 1 is the same as greedy decoding.
Top-p, also called nucleus sampling, was proposed by Ari Holtzman and colleagues in 2019. It keeps the smallest set of tokens whose probabilities add up to at least p, so the pool shrinks when the model is confident and grows when it is not. Google's documentation gives an example: with top-p set to 0.5 and three candidate tokens at probabilities of 0.3, 0.2 and 0.1, the model chooses between the first two and never picks the third.
Temperature works differently: it reshapes the probabilities themselves, sharpening them towards the likeliest token at low values and flattening them at high values. In Google's Vertex AI, top-k narrows the candidates first, top-p filters them next and temperature governs the final draw; Gemini models accept temperatures from 0 to 2, with 1.0 as the default. APIs also commonly offer a maximum output length, stop sequences and a random seed, which Google describes as giving best-effort repeatability rather than guaranteed identical output.
In practice, low values suit extraction, classification and code, where consistency matters, and higher values suit brainstorming and fiction. Published settings help as a starting point: DeepSeek, for example, evaluated its R1 model with a temperature of 0.6 and top-p of 0.95. Change one setting at a time, because adjusting several at once makes it hard to tell which one caused a change, and remember that the same number can behave differently on different models.
Top-k always keeps a fixed number of the most likely tokens, whatever their probabilities. Top-p keeps however many tokens are needed to reach a set share of the total probability, so the pool adapts: small when the model is confident, larger when many tokens are plausible. Temperature changes the shape of the probability distribution, making likely tokens more or less dominant. Top-p removes unlikely tokens from consideration altogether. They are often used together, with top-p cutting off the long tail and temperature controlling how adventurous the choice is among what remains. There is no universal setting, and defaults differ between providers. Low temperature and low top-p suit factual and structured tasks; higher values suit creative ones. Model makers sometimes publish their own settings, as DeepSeek did when it evaluated R1 at a temperature of 0.6 and top-p of 0.95. It makes the model always choose the most probable token, which is effectively greedy decoding. Identical output on every run is still not guaranteed by every service; Google, for example, describes even a fixed random seed as offering best-effort repeatability.Common questions
What is the difference between top-k and top-p?
What is the difference between temperature and top-p?
What top-p and temperature values should I use?
Does a temperature of 0 make a model's output deterministic?
Search for "top-p vs top-k", "nucleus sampling" or "LLM sampling parameters" alongside the name of the API you use. Provider documentation from Google, Anthropic, OpenAI and others is the authority on which settings a service supports and their defaults. For the reasoning behind the methods, read the original papers on arXiv and library explainers such as Hugging Face's.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.