# Top-K, Top-P, and Min-P Sampling by [kirupa](https://www.kirupa.com/me/index.htm) | filed under [AI Fundamentals](https://www.kirupa.com/ai_fundamentals/index.htm) **TL;DR:** Top-k, top-p, and min-p trim the list of tokens a model can pick from before it samples. Top-k keeps a fixed number, top-p keeps tokens until their odds add up to p, and min-p keeps anything at least some fraction as likely as the favorite, so it trims hard when the model is sure. - Ten oddballs at 1.5% each take 15% of Biscuit's wheel, and over a long reply, thin slivers like those sneak in surprisingly often. - With top-k at 3, Biscuit keeps two wrong opening times and cuts three good loaf names, because one fixed number can't fit both wheels. - Min-p at 0.1 puts the cutoff at 10% of the favorite: 8.8% when 7 holds 88%, and 2.2% when Swirl holds 22%, which cuts every oddball. - Order matters. Hugging Face applies temperature before trimming and llama.cpp applies it last, and heating first let all 10 oddballs back in. Every time a language model writes a token, it spins a prize wheel. In [Temperature for Predictable Output](https://www.kirupa.com/ai_fundamentals/temperature_predictable_output.htm), we saw how that wheel works: the model gives every token in its vocabulary a slice sized by its probability, and temperature makes the big slices bigger or the small slices bigger before the spin. There's a detail that article only touched on. The wheel doesn't have four slices. It has one slice for every token the model knows, which is often 100,000 or more. Most of those slices are so thin we'd need a microscope to see them, but there are a LOT of them, and every so often the pointer lands on one. That's how a perfectly sensible reply suddenly says something bizarre halfway through a sentence. Top-k, top-p, and min-p are three different ways of trimming the wheel before it spins. Each one throws away some slices and lets the model pick only from what's left. In the following sections, we'll look at how each one decides what to keep, where each one goes wrong, how they combine with temperature (the order matters more than you'd think!), and what values make good starting points. Onwards! ## Why Trim the Wheel at All? We'll use **Biscuit**, the help desk bot for the Crumb & Co. bakery. The bakery is launching a new cinnamon loaf, and the owners ask Biscuit to brainstorm names for it. Biscuit starts writing ***Let's call it the Cinnamon*** and now needs the next token. To keep things readable, we'll pretend the model's wheel has just 16 slices. Six of them are lovely names, and ten of them are what we'll call **oddballs**: ![A prize wheel for the next token after Let's call it the Cinnamon. Six good names take most of the wheel: Swirl 22 percent, Twist 19 percent, Cloud 16 percent, Dream 12 percent, Hug 9 percent, and Comet 7 percent. Ten oddballs, such as Tuesday, Gravel, and Lawsuit, are thin slivers at 1.5 percent each, 15 percent in total. A handwritten note points at the slivers: 10 oddballs.](https://www.kirupa.com/ai_fundamentals/images/top_k_top_p_min_p_tail.png) No single oddball is likely. Each one is a 1.5% sliver. But together they add up to 15% of the wheel, which means roughly one spin in seven lands on something like ***Cinnamon Lawsuit***. Real vocabularies make this worse, because the tail is thousands of tokens long instead of ten. The tail also gets more chances than it looks like it does, because the model spins once for every token. Let's say the oddballs only add up to 2% at each step. Over a 100-token reply, the chance that at least one oddball sneaks in is 1 minus 0.98^100, which is about 87%. And once one strange token is in the reply, the model keeps writing from there, so one bad pick can drag the rest of the sentence along with it. Ari Holtzman and his coauthors call this region the **unreliable tail** in [The Curious Case of Neural Text Degeneration](https://arxiv.org/abs/1904.09751), and it's the reason trimming exists. So why not just always take the biggest slice? That's greedy decoding (what temperature 0 does), and it has the opposite problem. The same paper found that always picking the top token tends to produce bland, repetitive text that can get stuck saying the same thing over and over. We want to keep the variety in the head of the wheel and cut off the tail. The three methods disagree on where that cut should go. ## Top-K: Keep a Fixed Number Top-k is the simplest idea. We sort the tokens from most to least likely, keep the first *k*, throw away the rest, and resize the survivors so they fill the whole wheel again. Angela Fan, Mike Lewis, and Yann Dauphin used it in their 2018 paper [Hierarchical Neural Story Generation](https://arxiv.org/abs/1805.04833), sampling from the 10 most likely candidates at each step. To see where top-k struggles, we need a second moment from Biscuit's day. A customer asks what time the bakery opens, and Biscuit has written ***We open every morning at***. The bakery opens at 7, and the model is pretty sure about it. Here are both of Biscuit's wheels, drawn as bars, with top-k set to 3: ![Top-k of 3 applied to two lists. On the left, after We open every morning at, the model gives 7 88 percent, 8 5 percent, 6 3 percent, 9 2 percent, noon 1 percent, and dawn 1 percent. Top-k keeps 7, 8, and 6, so two wrong opening times stay in play. On the right, after Let's call it the Cinnamon, the model gives Swirl 22 percent, Twist 19 percent, Cloud 16 percent, Dream 12 percent, Hug 9 percent, Comet 7 percent, and 10 oddballs at 1.5 percent each. Top-k keeps Swirl, Twist, and Cloud, so three good names get cut.](https://www.kirupa.com/ai_fundamentals/images/top_k_top_p_min_p_top_k.png) On the left, the model is confident, yet top-k still keeps three tokens. That leaves ***8*** and ***6*** in play, and after resizing, Biscuit tells a customer the wrong opening time about 8% of the time. On the right, the model sees six good names, and top-k throws half of them away. One number can't fit both situations. Holtzman and his coauthors made exactly this point: a small *k* is a problem when the odds are spread out, and a large *k* is a problem when they're peaked. ## Top-P: Keep Until the Odds Add Up Top-p (aka **nucleus sampling**) is the fix that Holtzman and his coauthors proposed. Instead of keeping a fixed number of tokens, we walk down the sorted list, adding up probabilities as we go, and keep the smallest set of tokens whose probabilities add up to at least *p*. The number of survivors now changes with the shape of the wheel. Here is top-p set to 0.9: ![Top-p of 0.9 applied to the same two lists. On the left, 7 alone reaches 88 percent, which is short of 90, so top-p also keeps 8 and reaches 93 percent. On the right, the six good names add up to 85 percent, so top-p keeps going and takes 4 oddballs to reach 91 percent, while the other 6 oddballs get cut.](https://www.kirupa.com/ai_fundamentals/images/top_k_top_p_min_p_top_p.png) That's a lot better on the right. All six names survive, and most of the oddballs are gone. But look closely at both sides. On the left, ***7*** holds 88% of the wheel, which is just short of 90%, so top-p has to grab one more token to reach its total, and that token is the wrong opening time. On the right, the good names only reach 85%, so top-p digs four oddballs deep to make up the difference. What's happening here is that top-p only cares about the running total. It never asks whether the next token is any good. It keeps adding tokens until the total is reached, and whatever happens to be next in line gets in. ## Min-P: Keep What's Close to the Favorite Min-p takes a different approach. It looks at the single most likely token and keeps every token that is at least some fraction of that one's probability. The fraction is the setting we choose. With min-p at 0.1, a token survives if it's at least one tenth as likely as the favorite. The method comes from Minh Nguyen and his coauthors in [Turning Up the Heat: Min-p Sampling for Creative and Coherent LLM Outputs](https://arxiv.org/abs/2407.01082), published at ICLR 2025. Here is min-p at 0.1 on Biscuit's two wheels: ![Min-p of 0.1 applied to the same two lists. On the left, the favorite, 7, is at 88 percent, so the cutoff is 8.8 percent, and only 7 survives. On the right, the favorite, Swirl, is at 22 percent, so the cutoff is 2.2 percent. All six good names stay, and all 10 oddballs, at 1.5 percent each, get cut.](https://www.kirupa.com/ai_fundamentals/images/top_k_top_p_min_p_min_p.png) On the left, the favorite is at 88%, so the cutoff is 8.8%. Only ***7*** clears it, and Biscuit gets the opening time right every time. On the right, the favorite is only at 22%, so the cutoff drops to 2.2%. Every good name clears it, and every oddball falls under it. This is the property that makes min-p nice to work with. The cutoff moves with the model's confidence. When the model is sure, the cutoff rises and trims hard. When the model sees lots of reasonable options, the cutoff falls and lets them all through. Unlike top-p, min-p never has to reach a total, so it never has to pull in a token just to make the numbers add up. ## Side by Side Here is what each setting kept on Biscuit's two wheels: | Setting | How it decides | Opening time | Naming the loaf | |---|---|---|---| | Top-k = 3 | Keep the 3 most likely tokens | 7, plus two wrong times | 3 of the 6 names | | Top-p = 0.9 | Keep tokens until they add up to 90% | 7, plus one wrong time | All 6 names, plus 4 oddballs | | Min-p = 0.1 | Keep tokens at least 10% as likely as the favorite | Just 7 | All 6 names, no oddballs | Min-p wins both rounds here, and that's partly because I picked wheels that show the differences clearly. With a different wheel, top-p would keep exactly the right set too. The pattern holds up in general, though. Top-k ignores the shape of the wheel, top-p cares about the shape but only through a running total, and min-p judges each token against the favorite. ## Which Comes First, Temperature or Trimming? In real use, trimming almost never runs alone. It runs together with temperature, and that raises a question that sounds like a nitpick: does the software apply temperature before it trims the wheel, or after? As it turns out, popular tools disagree. Hugging Face's transformers library applies temperature first and then runs top-k, top-p, and min-p on the reshaped wheel (its [generation code](https://github.com/huggingface/transformers/blob/main/src/transformers/generation/utils.py) notes that min-p is applied after temperature scaling). llama.cpp does the reverse by default: its [default sampler chain](https://github.com/ggml-org/llama.cpp/blob/master/common/common.h) runs top-k, top-p, and min-p first and applies temperature last. To see why this matters, let's say we turn the temperature up to 1.5 for Biscuit's loaf names, because we want more adventurous picks, and keep min-p at 0.1. Here is the same request under both orders: ![Two pipelines for the loaf names with temperature 1.5 and min-p 0.1. In the first, temperature runs first and flattens the wheel: Swirl drops to 16.5 percent and each oddball grows to 2.75 percent. Min-p then cuts at 1.65 percent, which every oddball clears, so all 10 oddballs stay and take 27.5 percent of the wheel. In the second, min-p runs first on the original wheel and cuts all 10 oddballs. Temperature then spreads the six good names more evenly, from Swirl at 22.7 percent to Comet at 10.6 percent, with no oddballs.](https://www.kirupa.com/ai_fundamentals/images/top_k_top_p_min_p_order.png) When temperature goes first, it fattens the oddballs from 1.5% to 2.75% each and shrinks the favorite to 16.5%, which drops the min-p cutoff to 1.65%. Every oddball now clears the bar, and they take up more than a quarter of the wheel. When trimming goes first, min-p cuts the oddballs while they're still thin, and temperature only spreads the odds among the six good names. We get more adventurous names and no lawsuits. The same thing happens to top-p, because heating the wheel spreads the probability out and the running total has to reach further down the list. Top-k is the only one that doesn't care about the order, because temperature never changes which tokens are in the top *k*. It only changes their sizes. Neither order is wrong. They just behave differently, so when a setting doesn't do what we expect, the order is one of the first things to check. If we're stuck with temperature first, a higher min-p gives some of the cut back. The min-p paper itself ran its experiments with temperature first and suggests values as high as 0.5 to 0.7 for very high temperatures. ## How Sampling Settings Go Wrong Most problems with these settings come from a handful of mistakes: - Trimming too hard. A top-k of 1, a top-p near 0, or a high min-p all leave a single token standing, which is greedy decoding in disguise. The output gets bland and can start repeating itself. - Trimming too little while adding lots of heat. A top-p of 1.0 (which keeps everything) combined with a high temperature hands the oddballs a bigger slice and nothing to stop them, and replies drift into gibberish. - Stacking everything. Turning on top-k, top-p, and min-p together, each with its own value, makes it very hard to tell which one is doing the cutting. Pick one trimming method and leave the others off (top-k at 0, top-p at 1.0, and min-p at 0 usually mean off). - Trimming on top of temperature 0. At temperature 0, the model already takes the top token, so trimming has nothing left to do. - Sending settings the API doesn't take. Hosted APIs are steadily removing these knobs, which we'll look at next. ## Good Starting Points Where these settings matter most today is with open models we run ourselves, through tools like llama.cpp, Ollama, vLLM, or transformers ([Running AI Models Locally](https://www.kirupa.com/ai_fundamentals/running_models_locally.htm) covers those). Hosted APIs give us fewer and fewer of these knobs. At the time of writing, OpenAI's API offers `temperature` and `top_p` but no top-k or min-p. [Anthropic's 2026-10-21 API version](https://platform.claude.com/docs/en/api/migration-2026-10-21) removed `temperature`, `top_p`, and `top_k` entirely, and sending any of them returns an error. And [Google's Gemini API](https://ai.google.dev/api/generate-content) silently ignores `temperature`, `topP`, and `topK` on Gemini 3.6 Flash and newer models. With that in mind, here's where I'd start: - On a hosted API that still takes these settings, adjust temperature or top-p but not both, which is what OpenAI's API reference recommends. Temperature is the easier one to reason about, so I'd leave `top_p` at 1. - On a local model, start with the tool's defaults. llama.cpp ships with top-k 40, top-p 0.95, min-p 0.05, and temperature 0.8, which is a reasonable all-around mix. - For one-right-answer jobs like extraction, tagging, or opening hours, keep the temperature low and trust the model's favorite. A min-p around 0.1 adds a safety net if you do want a little sampling. - For creative jobs like brainstorming loaf names, use min-p as the one trimming setting. The min-p paper found that values between 0.05 and 0.1 balance creativity and coherence, and that leaves room to raise the temperature for more variety. Whatever we pick, we should test it the same way we'd test a prompt change, by running the same requests many times and reading what comes back. [Regression Tests on Every Prompt Change](https://www.kirupa.com/ai_fundamentals/prompt_regression_tests.htm) shows how to set that up. ## Conclusion Temperature reshapes the model's prize wheel, and trimming decides which slices are allowed on the wheel at all. Top-k keeps a fixed number of slices, which is too many when the model is sure and too few when it has lots of good options. Top-p keeps slices until their probabilities add up to a target, which adapts to the wheel but sometimes pulls in a bad token just to reach the total. Min-p keeps every slice that is close enough to the favorite, so it trims hard when the model is confident and gently when the model isn't. For Biscuit, min-p kept the bakery's opening time honest and the loaf names interesting, and running it before temperature let us turn up the heat without letting Cinnamon Lawsuit back in. If you're curious about where all those tokens on the wheel come from in the first place, [What Is Tokenization?](https://www.kirupa.com/ai_fundamentals/what_is_tokenization.htm) covers how text gets chopped up into them. ## Test your knowledge! [Take the Top-K, Top-P, and Min-P Sampling Quiz](https://www.kirupa.com/quizzes/top_k_top_p_min_p.htm). Practice with 5 multiple-choice questions and an explanation after every answer.