Compare greedy, sampling and beam search at inference: cost and best use case.
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.
Detailed answer & concept explanation~8 min readEverything 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. 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.
- OpenAI's chat completions API exposes temperature and top_p but quietly dropped the legacy best_of beam-style parameter for chat models.
- vLLM and SGLang default to sampling with continuous batching; enabling beam search collapses their throughput because each beam claims a separate paged KV allocation.
- Google Translate and classic summarization stacks historically used beam search width 4 to 5, where low-entropy targets reward highest-probability decoding.
- Self-consistency on Gemini 3.1 Pro and DeepSeek V4 samples many chains of thought at temperature, then majority-votes the final answer, paying n times the decode cost.
- Claude Opus 4.7 and GPT-5.5 serve chat with temperature sampling; the post-RLHF distribution is sharp enough that a single sample reads cleanly.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does beam search multiply KV memory and not just compute?
QHow does best-of-n differ from beam search in both cost and quality behavior?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
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.
Same topic, related formats. Practice these next.