Explain what happens at the model head when a caller requests top-k logprobs per token. Identify what is essentially free, what genuinely costs something, and how that cost scales with k and with streaming.
Logprobs are near-free on the GPU because the softmax already exists; the real cost is bytes on the wire, JSON serialization per token, and amplified streaming latency.
Imagine a chef who already chops twenty vegetables for every dish but only puts five on the plate. If you ask to see all twenty, the chopping work was already done; the only extra effort is wrapping the extras and shipping a heavier box to your table. That is what logprobs feel like at the model head. Every time the model picks the next word, it has already produced a probability score for every possible candidate word whether you ask for them or not. Pulling out the top few is a quick partial sort. What actually costs something is packing those extra (word, score) pairs into JSON for every single streamed word and sending a fatter package over the network. The bigger your k, the heavier each package, and on a word-by-word stream that adds up to real bandwidth and packaging time.
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.
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.
Logprobs sit at an interesting boundary in serving cost. Their compute footprint is essentially zero, because the forward pass already produces the full probability distribution over the vocabulary at every decode step. Yet their realized cost on a production system can be substantial, because of where the work moves: from the GPU, where there is plenty of headroom, to the CPU and the network, where there often is not.
This question tests whether you reason about cost in terms of where the bottleneck actually sits rather than reaching for the familiar GPU-FLOPs framing. The candidates who get it answer in three layers: compute is free, serialization is real, streaming is the amplifier. The candidates who miss it claim the model has to do extra work to compute probabilities, which is the wrong mental model.
The deep dive walks the request path end to end, explains why each layer behaves the way it does, and lays out the practical guidance an operator needs to decide when logprobs are an acceptable cost and when they are not.
What the forward pass already produces
Every decoder forward pass ends with a head that projects the final hidden state through a lm_head matrix to produce a logit vector of size V, the vocabulary. V is typically 32k for Llama-family models, 50k to 100k for GPT-family, up to a few hundred thousand for some multilingual models. A softmax then turns that vector into a probability distribution over the next token.
The sampler needs that distribution to pick a token. Greedy sampling reads the argmax. Top-p or top-k sampling reads a sorted slice. Even temperature scaling reads the full logit vector before re-normalizing. There is no execution path in modern decoding that skips the softmax; it is structurally part of the step.
Returning logprobs to the caller, then, is not asking the GPU to do new work. It is asking the server to forward information that already exists in device memory. The top-k extraction is the only new operation, and it is a partial sort over V entries that fused kernels run in microseconds. Compare that to the matmul cost (billions of parameter multiplies) and the HBM bandwidth (gigabytes per token), and the partial sort is in the noise.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- OpenAI Chat Completions exposes top_logprobs up to 20 and bills logprobs at an output-token-equivalent rate on most tiers, reflecting the serialization and wire load.
- Anthropic Claude Opus 4.7 and Sonnet 4.6 surface logprobs through a per-token logprobs field in streamed responses, with the same per-event payload growth pattern.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhere exactly does the top-k extraction happen, and is it on the GPU or the CPU?
On modern serving stacks (vLLM, TensorRT-LLM, TGI) the partial sort is a fused GPU kernel that runs on the logit tensor before it leaves device memory. Only the top-k slice is copied host-side, which keeps the wire cost between GPU and CPU minimal.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Saying logprobs cost extra GPU compute because the model has to 'compute probabilities for every token.' It already does. The softmax is part of every forward pass; logprobs are a free side effect.
60 second bullets to scan on the way to the call.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.