Decoding

Decoding is the procedure used to turn next-token probabilities into an output sequence. The model gives a distribution; the decoding rule chooses or samples tokens from it.

At step , a language model predicts:

Greedy decoding chooses the most likely token:

Sampling draws from a temperature-scaled distribution:

where is the token logit and is temperature.

Key Ideas

  • Greedy decoding is deterministic but can be brittle or repetitive.
  • Higher temperature increases randomness; lower temperature makes outputs more concentrated.
  • Top- and nucleus sampling restrict the candidate token set before sampling.
  • Beam search explores multiple high-probability sequences but can favor generic outputs.
  • Decoding changes output behavior without changing model weights.

Common Decoders

DecoderRuleUse whenRisk
Greedychoose each stepdeterministic extraction, simple constrained outputlocal optimum, repetition
Temperature samplingsample from softened logitsbrainstorming or diverse textvariance and unsupported claims
Top-sample from highest-probability tokenslimit tail noisefixed ignores distribution shape
Nucleus top-sample from smallest set with cumulative mass adaptive diversityunstable with poor calibration
Beam searchkeep multiple high-probability partial sequencestranslation or short sequence searchgeneric output, length bias

Top- defines a candidate set:

Nucleus sampling defines the smallest set whose probability mass exceeds :

Sampling then renormalizes over the chosen set.

Length and Stop Rules

Decoding is also controlled by maximum tokens and stop criteria.

ControlPurposeFailure mode
max tokensbound cost and context usetruncates reasoning or JSON
stop sequenceend at known delimiterstops inside quoted text
schema validationaccept only valid structurevalid syntax with wrong content
repetition penaltyreduce loopscan distort technical wording
multiple samplesexpose uncertaintyincreases verification cost

Decoding vs Verification

Decoding parameters affect variability, but they do not verify truth.

TaskTypical decodingStill needs
JSON extractionlow temperature, schema constraintsemantic validation against source
factual answerlow temperature or deterministiccitation and source support
code generationlow-to-moderate samplingtests and review
brainstorminghigher diversityfiltering and human selection
agent tool callconstrained outputtool result handling and side-effect check

Practical Checks

  • Is the task deterministic extraction or creative generation?
  • Are temperature, top-, top-, max tokens, and stop rules documented?
  • Does the decoder preserve required structure or schema?
  • Are multiple samples needed to estimate variability?
  • Is the selected answer verified after decoding?
  • Is output variability part of the claim, or just hidden noise?
  • Are stop rules tested against the intended output format?