Compare the per-token compute cost of greedy decoding versus temperature sampling, then explain why callers nevertheless see real cost differences between the two strategies in production traces.
Per token, greedy and temperature sampling cost essentially the same: one forward pass, one KV-cache read, then a trivial argmax or sample.
Imagine two writers told to write a story. Both take the same time to think of what word to put down next. The first writer always picks the single most likely word; the second rolls a small weighted die and picks one from a few likely words. The thinking is the same; only the final pick differs. The dice-rolling takes a tiny moment, way too small to notice. But the two stories often turn out different lengths because they walk down different paths, and the longer story costs more in total only because it has more words, not because each word was harder to write. If you compare bills you have to compare on cost-per-word, not total-bill, or you mix up the trajectory with the per-word price.
Detailed answer & concept explanation~6 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.
3 min: name the shared forward pass + KV read, note the post-softmax argmax versus sample is microseconds and irrelevant, identify output length as the real cost driver, and prescribe length-normalized cost comparison as the diagnostic discipline.
| Property | Greedy | Temperature sampling |
|---|---|---|
| Forward passes per token | 1 | 1 |
| KV-cache reads per token | 1 | 1 |
| Post-softmax operation | argmax (~3 us) | softmax + sample (~10 us) |
| Per-token compute | 1x (baseline) | 1x (within noise) |
| Determinism | Yes (with same input) | No (modulo seed) |
| Typical effect on output length | Often shorter, more terminal | Often longer, more exploratory |
Real products, models, and research that use this idea.
- OpenAI's chat completion billing is strictly per-token (input + output); switching temperature from 0 (greedy-equivalent) to 0.7 does not change the rate.
- vLLM and SGLang report identical per-step decode latency for greedy and sampling configurations on the same model and batch size.
- Production A/B tests of greedy versus sampling on chat routes typically show cost differences explainable by mean-output-length differences, not by per-token cost.
- Anthropic's Claude API exposes temperature as a quality knob, not a cost knob; the docs note that decoding-strategy choice does not affect billing rates.
- Best-of-n and beam search are the only single-knob decoding choices that genuinely multiply per-token cost; greedy versus sampling does not.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the LM head's logits projection not the cost difference between greedy and sampling?
QCould a clever implementation skip the softmax for greedy and save real time?
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 is meaningfully slower or more expensive per token than greedy. The forward pass is identical and the only difference is a trivial post-softmax operation. The real cost gap is in output length, not per-token cost.
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.