Zenaique

Greedy decoding and temperature sampling: does one cost more per token than the other?

Short answer·Medium·4.0 · 0·~3 min·Asked atAlibabaCharacter AiIntuit·Relevant atOpenAI
Attempt it

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.

Free · 2 AI evals / day
TL;DR

Per token, greedy and temperature sampling cost essentially the same: one forward pass, one KV-cache read, then a trivial argmax or sample.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

The cost question for decoding strategies is one of the most consistently misunderstood corners of LLM economics. Engineers often reach for greedy decoding as a cost-saving lever, or worry that high temperature makes generation more expensive. Both intuitions are essentially wrong, and untangling them requires looking carefully at what actually executes on the GPU during a decode step.

The central fact: a decode step's cost is dominated by the forward pass, and the forward pass is identical for greedy and temperature sampling. Both strategies require the model to compute the next-token distribution; only the final pick differs. That final pick is microseconds of work, completely dwarfed by the milliseconds-to-tens-of-milliseconds of the forward pass.

So where does the cost gap that production teams sometimes observe come from? Almost always from output length. Different decoding strategies walk different trajectories through token space, and trajectories of different lengths cost proportionally different amounts. The per-token cost is invariant; the token count varies. Conflating these two is the source of the misintuition.

This deep dive walks through what each step actually computes, why the post-softmax pick is negligible, where real cost gaps come from, and how to do honest cost comparisons across decoding strategies.

What a decode step actually computes

A single decode step on an autoregressive transformer does the following work:

  1. Take the current sequence's last token (just generated) and its embedding.
  2. Run the embedding through every transformer block: attention (read the entire KV cache for this request from HBM, compute attention scores, weighted sum over values), feed-forward (up-projection, activation, down-projection), residual adds and norms.
  3. Apply the LM head (a final linear projection from the hidden dimension to the vocabulary size V).
  4. Now we have a logits vector of length V. The decoding strategy picks one token from it.

Steps 1-3 are the forward pass. They involve billions of operations: reading the model weights (tens of billions of parameters) from HBM, doing attention against the cached keys and values, etc. At small batch the forward pass is bandwidth-bound and takes milliseconds to tens of milliseconds depending on model size.

Step 4 is the decoding choice. It operates on a vector of size V (typically 100k-300k elements in modern frontier models). This is a tiny tensor compared to the forward-pass intermediates.

Greedy and temperature sampling are both step-4-only differences. They share steps 1-3 completely.

Why the post-softmax pick is negligible
Where real cost gaps come from: output length
How to do honest cost comparisons
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
PropertyGreedyTemperature sampling
Forward passes per token11
KV-cache reads per token11
Post-softmax operationargmax (~3 us)softmax + sample (~10 us)
Per-token compute1x (baseline)1x (within noise)
DeterminismYes (with same input)No (modulo seed)
Typical effect on output lengthOften shorter, more terminalOften 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.
Sign in to see more production examples.

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?
A

Both strategies require the full logits vector to make any decision. Greedy picks the argmax of the vector; sampling picks a weighted random element. Neither strategy can skip the projection. So the LM head computation is shared cost, not strategy-differentiating cost.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

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.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Why both strategies do exactly one forward pass per token

  • Why both strategies read the same KV cache identically

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy