Compare greedy, sampling and beam search at inference: cost and best use case.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Compare greedy decoding, sampling (temperature / top-p), and beam search at inference time along three axes: per-step compute cost, per-step KV-cache cost, and what use case each is appropriate for. Why has beam search nearly disappeared from modern chat APIs?
Greedy and sampling are both 1x cost on one KV trajectory; beam search at width B costs roughly Bx compute and Bx KV memory, which is why chat APIs dropped it.
Imagine writing a story one word at a time. Greedy decoding always picks the single most likely next word, like a writer who never second-guesses. Sampling rolls a weighted die instead, so the story varies and feels more natural, but it still writes just one story. Beam search is different: it keeps several half-finished stories alive at once, extends each, and keeps only the best few. That is smarter for short, low-creativity tasks like translation. But keeping many stories alive means many notebooks of running notes, and that gets expensive fast. For a long, chatty answer those extra notebooks cost a lot and the result often turns repetitive, so chat systems stopped bothering.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: 1x cost of greedy and sampling, then beam's Bx compute and Bx KV cost, then why chat dropped beam (entropy, serving cost, RLHF), then best-of-n versus self-consistency cost tiers.
| Strategy | Per-step compute | Per-step KV cost | Best use case |
|---|---|---|---|
| Greedy (argmax) | 1x | 1x (one cache) | Deterministic, reproducible output |
| Sampling (temp / top-p) | 1x | 1x (one cache) | Open-ended chat, diversity |
| Beam search (width B) | ~Bx | ~Bx (one cache per beam) | Translation, summarization |
| Best-of-n / self-consistency | ~nx (parallel) | ~nx across streams | Reasoning quality boost |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Claiming temperature sampling costs more than greedy. The forward pass is identical; only a cheap random draw differs. The real cost multiplier is beam width, not the sampling logic.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.