Concepts / Generating Outputs with AI Models: Sampling Multiple Candidates

Generating Outputs with AI Models: Sampling Multiple Candidates

Because every token adds a negative term, a raw summed log probability always gets lower as an output gets longer. The number records both how likely the tokens were and how many tokens there were.

  • Programming

Why a Model Needs a Selection Rule

A language model rarely has to produce only one answer. When you sample several candidate outputs for the same prompt, a practical question appears: which candidate should the system return? There are two broad options. One is to show several candidates to a person and let that person pick the one that suits them. The other is to build an automatic rule that ranks the candidates and keeps the top one. This article is about the second option, the scoring rules that let software choose.

Every rule covered here starts from the same raw material. Each token in a candidate has a probability that the model computed while generating it, so every candidate carries a trail of numbers. The methods differ in how they combine that trail into a single score, and in one case they replace the trail with a separate scoring model. As you work through the trace below, notice where your own expectation about which candidate wins starts to change.

Probability of a Whole Output

The most direct selection rule is to pick the output with the highest probability. An output is a sequence of tokens, and the probability of the whole sequence is built from the per-token probabilities. The rule is that the probability of an output equals the product of the probabilities of all the tokens in it. Each new token multiplies the running total by one more factor.

Formula
Formula, step by step
  1. 1
    P(y) is the probability of the whole output y, which is a sequence of T tokens.
  2. 2
    The product symbol means you multiply one factor for each token position t from 1 to T.
  3. 3
    Each factor P(y_t given the earlier tokens) is the probability the model assigned to token t, given every token that came before it.
  4. 4
    The result is one number between 0 and 1 for the whole output.

The later factors are conditional. The probability of the second token is written as a probability given the first, and the probability of the third is given the first two. The product therefore reflects the whole chain of choices that produced the sequence, not three independent guesses. This is why the same word can carry a different factor depending on what the model had already written.

Turning Products Into Sums with Logs

Multiplying many small numbers is awkward in practice, so working with probabilities on a log scale is easier. The key fact is that the logarithm of a product equals the sum of the logarithms. Taking the log of a sequence probability therefore turns the chain of multiplications into a chain of additions. The log probability of a sequence is the sum of the log probabilities of its tokens.

Formula

Notice what the transformation does to the comparison. Ranking candidates by their probability products and ranking them by their log-probability sums gives the same order, because the logarithm is an increasing function. The sum is simply a more convenient bookkeeping device. Each token adds one more term, and the running total is easy to inspect.

Why are the log values negative? A probability lies between 0 and 1, and the logarithm of any number in that range is negative. Each token therefore contributes a negative amount to the sum, and the more tokens there are, the more negative the total tends to become. This sign fact drives the bias discussed in the next section.

Key point

Because every token adds a negative term, a raw summed log probability always gets lower as an output gets longer. The number records both how likely the tokens were and how many tokens there were.

Correcting the Length Bias with Averages

Once outputs are scored by summed log probability, longer outputs face a structural disadvantage. Each additional token adds another negative term, so a long candidate's total usually sits lower than a short candidate's total. If the selection rule simply took the highest sum, it would keep favoring brevity, even when a longer output is the better answer.

The fix is to normalize by length. Dividing the summed log probability by the number of tokens gives the average log probability. This measures how strongly each token contributed on average rather than how many tokens there were. The method then picks the candidate with the highest average. The source reports that this is the approach the OpenAI API uses, as of its writing.

Rule

Formula, step by step
  1. 1
    Compute the log probability of each token given the tokens before it.
  2. 2
    Add those log values to get the summed log probability of the whole candidate.
  3. 3
    Divide by T, the number of tokens, so that the score is an average per token.
  4. 4
    Compare candidates by this average, not by the raw sum.

Worked Comparison of Two Candidates

The numbers below are invented for illustration to show the bias. They use the natural logarithm. Suppose a short candidate has two tokens, each with probability 0.5, and a longer candidate has five tokens, each with probability 0.7. Both candidates are plausible, and the question is which one the selection rule should keep.

Worked example

Raw sum versus average for a short and a long candidate

Short candidate: two tokens with probabilities 0.5 and 0.5. Long candidate: five tokens with probabilities 0.7 each. Which candidate wins under a raw log-probability sum, and which wins under the average?

  1. 1
    Short candidate product

    $0.5 \times 0.5 = 0.25$, so the whole-output probability is 0.25.

  2. 2
    Short candidate raw sum

    $\log(0.5) + \log(0.5) = 2 \times (-0.6931) \approx -1.3863$.

  3. 3
    Short candidate average

    $-1.3863 / 2 \approx -0.6931$.

  4. 4
    Long candidate product

    $0.7^{5} \approx 0.1681$, so the whole-output probability is about 0.168.

  5. 5
    Long candidate raw sum

    $5 \times \log(0.7) = 5 \times (-0.3567) \approx -1.7834$.

  6. 6
    Long candidate average

    $-1.7834 / 5 \approx -0.3567$.

  7. 7
    Compare the raw sums

    $-1.3863$ is higher than $-1.7834$, so the raw sum keeps the short candidate.

  8. 8
    Compare the averages

    $-0.3567$ is higher than $-0.6931$, so the average keeps the long candidate.

Result

The raw sum favors the short candidate because it has fewer negative terms. The per-token average favors the long candidate because each of its tokens was, on average, more probable.

Raw log sum -1.386Raw log sum -1.783Average -0.693Average -0.357divide by 2divide by 5
How do the raw log-probability sums and the per-token averages of candidates of different lengths compare, and why does the raw sum favor short outputs while the average does not?

Reward Models as Selection Judges

Probability-based rules depend only on the numbers the model already produced. A different family of methods uses a separate model, called a reward model, to score each candidate. The reward model examines each output and assigns it a score, and the system selects the output with the highest score. The source describes this as the same idea as the verifiers discussed in the previous section.

The flow has four steps. The prompt is sent to the model, several candidates are sampled, the reward model assigns each candidate a score, and the candidate with the highest score is returned. The reward model is what decides the winner, so the candidates' own token probabilities are not needed for the comparison.

PromptCandidate Ascore 0.42Candidate Bscore 0.91Candidate Cscore 0.67Reward modelHighest scoreselectedCandidate Bsamplesamplesamplescorescorescorekeep maximum
How do multiple sampled candidates flow through the reward model, and how does the highest score determine which candidate is selected?

The example scores in the diagram are invented for illustration. The source does not give scores for specific candidates. Its point is the structure: several candidates go in, one score per candidate comes out, and the maximum is kept.

Where Scoring Is Used in Practice

The source gives several industry illustrations. Stitch Fix and Grab both select the outputs their reward models or verifiers score highly. Nextdoor reported that using a reward model was the key factor in improving the performance of its application in 2023. These examples show that the scoring step is used in production, not only in research.

OpenAI trained verifiers to help models pick the best solutions to math problems. The source reports that a verifier significantly boosted performance, and that the boost was roughly the same as a 30 times increase in model size. In the source's illustration, a 100-million-parameter model with a verifier could perform on par with a 3-billion-parameter model without one.

These selection methods connect to a broader idea called test time compute, which refers to the computational resources used during inference, when the model is producing outputs rather than being trained. Allocating more compute during inference can mean generating more candidate outputs and then selecting among them. The source notes that scaling test time compute can be more efficient than scaling model parameters, citing DeepMind's research.

The source frames this with a question: if a language model is allowed a fixed but nontrivial amount of inference-time compute, how much can it improve its performance on a challenging prompt? Every selection method in this article is one way of spending that budget, because each one needs candidates to compare.

Common Errors When Comparing Candidates

  • Common mistake

    Comparing raw summed log probabilities across candidates of different lengths

    What it looks like

    Ranking the 2-token candidate (sum -1.386) above the 5-token candidate (sum -1.783) only because its sum is higher.

    Why it fails

    Each token adds a negative term, so longer candidates have lower sums almost by construction. The ranking mostly reflects length.

    Do this instead

    Compare average log probabilities, which divide the sum by the number of tokens.

  • Common mistake

    Treating a product of probabilities as an average

    What it looks like

    Multiplying 0.5 and 0.5 and concluding that a token-level average of 0.5 was achieved.

    Why it fails

    The product 0.25 is the probability of the whole output. It is not the average probability per token.

    Do this instead

    Keep the whole-output probability and the per-token average as separate quantities.

  • Common mistake

    Assuming a higher reward score means a more probable output

    What it looks like

    Choosing Candidate B because it has the highest reward score and then assuming its token probabilities were the highest.

    Why it fails

    A reward model scores candidates with its own judgment. It does not read the token probabilities that produced them.

    Do this instead

    Read the reward score as the selection criterion and check the scoring model's behavior separately.

  • Common mistake

    Mixing scores from different scoring rules

    What it looks like

    Comparing a raw log sum for one candidate against a reward score for another.

    Why it fails

    The two numbers measure different things on different scales, so the comparison has no meaning.

    Do this instead

    Apply one selection rule to all candidates before comparing their scores.

  • Common mistake

    Forgetting that the summed log probability is negative and treating a less negative number as a worse one

Trace the Scores Yourself

What do you think happens?

A candidate has three tokens, each with probability 0.9. What is the probability of the whole output?

Reveal answer
Answer:

0.729

The whole-output probability is the product of the token probabilities: $0.9 \times 0.9 \times 0.9 = 0.729$.

MEDIUM

Candidate X has two tokens with probabilities 0.7 and 0.7. Candidate Y has three tokens with probabilities 0.8, 0.8, and 0.8. First compute the whole-output probability of each candidate. Then decide which candidate a raw product would select, and which candidate would be selected if you reasoned only about the average per-token probability.

Hints
  • Multiply the token probabilities in each candidate to get the whole-output probability.
  • Candidate X has a product of 0.49. Candidate Y has a product of $0.8^{3} = 0.512$.
  • The average per-token probability is the geometric mean of the token probabilities, so both candidates have an average of about 0.7 and 0.8 respectively, which favors Candidate Y.

Key Takeaways

  • The probability of a whole output is the product of its token probabilities, so each added token multiplies the total by another factor no larger than 1.
  • Taking logs turns that product into a sum of log probabilities, which is easier to work with and gives the same ranking as the product.
  • Each log term is negative, so a raw sum favors short outputs. Dividing by the number of tokens gives the average log probability, which corrects that bias.
  • A reward model scores each sampled candidate and the highest-scoring output is selected. Verifiers used this way improved math performance in the source's examples.
  • The most common comparison errors are ranking raw sums across different lengths, confusing a product with an average, and mixing scores from different scoring rules.

More concepts starting with G