Search

Search papers and lessons

Best-of-N Sampling

Level 3 · 10 min read

Best-of-N draws N independent answers from a model, scores each with a reward model or verifier, and keeps the highest-scoring one, trading extra compute for better answers.

On this page

  • Colour = topic mix
  • Probability
  • Neural network basics
  • Calculus
  • Linear algebra
  • Discrete math
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Best-of-N is the simplest way to spend extra compute to get a better answer. You ask a language model for N independent answers to the same prompt, score each one with a reward model or a checker, and return the one with the highest score. The model itself is never changed.

The cost is linear in N, but the gain is not. The first few extra samples help a lot and the returns shrink, and a weak scorer can lead you to a bad answer that merely looks good.

Where you'll see it: as an inference-time alignment step in chat systems, as a baseline that every reward model is judged against, and as a baseline for test-time compute scaling, and for maths and code, where a checker can verify an answer.

Intuition

Buying more tries

Suppose you are hiring for a job and you can interview candidates one at a time. Each candidate is drawn from the same applicant pool, so you cannot control who shows up, but you can keep the best one after interviewing N of them. The pool does not get better. You simply get to look at more people before you pick. Best-of-N is that process applied to model outputs: the model is the applicant pool, the reward model is the interviewer, and N is how many people you see.

Two things follow. First, if the pool is bad at a task, picking the best of a bad pool is still bad. Second, the value of each extra interview falls off: going from one candidate to two is a big jump in the quality of the pick, while going from ninety-nine to one hundred barely changes it.

Why it works at all

A language model does not give one answer. Sampling with a non-zero temperature gives a spread of answers, some better and some worse. A reward model gives each answer a score that is correlated with quality. If the scores were perfect, you would only need N large enough to hit one good answer. Best-of-N exploits the spread that sampling already gives you, so no training is needed at the moment of use.

flowchart LR
  P[Prompt x] --> S[Sample N answers from the base model]
  S --> Y1[answer 1]
  S --> Y2[answer 2]
  S --> YN[answer N]
  Y1 --> R[Reward model scores each one]
  Y2 --> R
  YN --> R
  R --> B[Return the highest score]

The math

Setup

Let πbase(y∣x)\pi_{\text{base}}(y \mid x) be the probability that the base language model writes response yy to prompt xx. Let r(x,y)r(x, y) be the score that a reward model gives that response. Best-of-N draws NN responses independently from the base model and returns

y^=arg⁡max⁡i∈{1,…,N}  r(x,yi),yi∼πbase(⋅∣x).\hat{y} = \arg\max_{i \in \{1, \dots, N\}} \; r(x, y_i), \qquad y_i \sim \pi_{\text{base}}(\cdot \mid x).

Here yiy_i is the ii-th sample and y^\hat{y} is the one returned. Nothing about πbase\pi_{\text{base}} changes. The only control is NN.

How the best score is distributed

Write F(t)F(t) for the cumulative distribution function of the reward r(x,Y)r(x, Y) when YY is drawn from the base model. Because the NN samples are independent, the best score is at most tt exactly when every score is at most tt:

P(max⁡ir(x,yi)≤t)=∏i=1NP(r(x,yi)≤t)=F(t)N.P\big(\max_i r(x, y_i) \le t\big) = \prod_{i=1}^{N} P\big(r(x, y_i) \le t\big) = F(t)^N.

This is the key identity. Differentiating gives the density of the best score, and the expected best score follows:

E[max⁡iri]=∫t  N f(t) F(t)N−1 dt,\mathbb{E}\big[\max_i r_i\big] = \int t \; N \, f(t) \, F(t)^{N-1} \, dt,

where f=F′f = F' is the density. Each extra sample multiplies the CDF by another factor of F(t)≤1F(t) \le 1, so the maximum moves right, but by less each time.

How far the answer moves from the base model

The distribution of the returned answer is πN\pi_N, where the density of picking yy is the base probability times a weight that depends only on how good yy is relative to the rest. Let U=F(r(x,y))U = F(r(x, y)). Under the base model UU is uniform on [0,1][0, 1]. The returned answer has UU distributed as the maximum of NN uniforms, which has density NuN−1N u^{N-1}. So the likelihood ratio between πN\pi_N and πbase\pi_{\text{base}} is NuN−1N u^{N-1}, and the KL divergence is

KL(πN ∥ πbase)=EπN[log⁡(NUN−1)]=log⁡N+(N−1) E[log⁡U].\mathrm{KL}(\pi_N \,\|\, \pi_{\text{base}}) = \mathbb{E}_{\pi_N}\big[\log(N U^{N-1})\big] = \log N + (N-1)\,\mathbb{E}[\log U].

Under πN\pi_N, UU has a Beta(N,1)(N, 1) distribution, and its mean log is −1/N-1/N. That gives the closed form

KL(πN ∥ πbase)=log⁡N−N−1N.\mathrm{KL}(\pi_N \,\|\, \pi_{\text{base}}) = \log N - \frac{N-1}{N}.

This is the cost of the selection measured in nats. It grows with log⁡N\log N, so it is very cheap in KL terms, and it is the quantity that makes the reward-hacking story below precise.

Worked examples

Example
Picking from four answers by hand

Four samples come back with reward scores 0.20.2, 1.31.3, −0.5-0.5 and 0.90.9.

  1. Compare the scores: the largest is 1.31.3.
  2. Return the answer that produced 1.31.3. The other three are discarded.
  3. Cost: four forward passes of the base model plus four reward-model calls.

Interpretation: the chosen answer is not guaranteed to be correct. It is only the best of the four under the scorer. If the scorer is wrong about that answer, best-of-N returns the mistake with confidence.

Example
Expected best score for standard normal rewards

Suppose rewards are standard normal, so F=ΦF = \Phi, the standard normal CDF. Plug into the formula above and evaluate the integral numerically.

  1. N=1N = 1: the expected best score is 00, the mean of one draw.
  2. N=2N = 2: E[max⁡]≈0.5642\mathbb{E}[\max] \approx 0.5642, which is exactly 1/π1/\sqrt{\pi}.
  3. N=10N = 10: E[max⁡]≈1.5388\mathbb{E}[\max] \approx 1.5388.
  4. N=100N = 100: E[max⁡]≈2.5076\mathbb{E}[\max] \approx 2.5076.

Interpretation: going from N=10N = 10 to N=100N = 100 is ten times the compute and buys about 11 extra standard deviation. Each step gives less. The growth is roughly like 2log⁡N\sqrt{2 \log N} for large NN, which is why a fixed budget of samples is never a complete answer.

Example
Finding one correct solution with a perfect checker

A model solves a hard maths problem with probability p=0.1p = 0.1 per sample, and a checker can tell correct from incorrect perfectly. The chance that at least one of NN samples is correct is

P(at least one correct)=1−(1−p)N.P(\text{at least one correct}) = 1 - (1 - p)^N.
  1. N=1N = 1: 1−0.9=0.11 - 0.9 = 0.1.
  2. N=10N = 10: 1−0.910=1−0.3487=0.65131 - 0.9^{10} = 1 - 0.3487 = 0.6513.
  3. N=100N = 100: 1−0.9100≈0.999971 - 0.9^{100} \approx 0.99997.

Interpretation: with a checker you can turn a 10 percent solver into one that is almost always right, given enough samples. These values are analytic, not from a run. The catch is that real checkers are not perfect, and the next sections explain what happens then.

Tradeoffs and failure modes

Best-of-N is attractive because it needs no training and no change to the model. It is also easy to misuse.

SituationWhat goes wrongBetter choice
Strong, well-matched reward modelWorks well, gains are realUse best-of-N and tune N to your budget
Reward model trained on a different distribution than the outputs it scoresThe model learns to produce answers the scorer likes but that are not better, so the scorer is exploitedRetrain the scorer on samples from the policy, or switch to an objective that stays close to the base model
Large N with a noisy scorerSelection picks the luckiest error, so quality can fall as N grows past some pointCap N, or use [[process-reward-model
Verifiable task (unit tests, exact answers)Little risk from scorer errorBest-of-N is cheap and strong here
Open-ended writing with no reliable scorerScores are weak proxies for qualityHuman preference training (Reinforcement Learning from Human Feedback (RLHF)), or Direct Preference Optimization
Budget for many samples, but a repeated taskPaying N times per request foreverDistil the best-of-N behaviour into the model with rejection-sampling fine-tuning, or use Beam Search over steps

The most important failure is reward overoptimisation. Gao et al. (2022) studied how the true quality of a best-of-N policy changes as N grows while the proxy score keeps rising. The proxy score keeps improving, while the gold score eventually stops tracking it and can fall. The KL formula above is a useful yardstick: as KL grows with log⁡N\log N, you are moving further from the base model, and the scorer's errors have more room to matter.

Latency is less of a problem than cost. The N samples can run in parallel, so wall-clock time need not grow with N, but the compute bill does.

History

Choosing the best of several candidates is much older than language models. Machine translation and speech recognition have long used n-best lists: the decoder produces several hypotheses and a second model reranks them. Best-of-N is the same idea with a learned reward in place of a reranker.

In the era of learned reward models for text, best-of-N was used as a simple way to get better summaries and answers without further training. Stiennon et al. (2020) trained reward models from human comparisons to judge summaries. Gao et al. (2022) then measured how far selection against such a scorer can go before the scorer is exploited.

In 2024, Snell et al. studied best-of-N as a baseline among several ways to spend compute at inference time, such as beam search against a process reward model. They found that the most effective method depends on how hard the prompt is, and that a process reward model trained on the PRM800k data was easy to exploit.

Check yourself

QuestionIn one sentence, what does best-of-N change, and what does it leave alone?Show answer

It changes which answer is returned: N samples are drawn and the highest-scoring one is kept. It leaves the model's weights and its sampling distribution untouched, so the base model's capabilities are not improved, only exploited.

QuestionA scorer gives standard normal rewards. What is the expected best score of N = 4 samples, to about two decimal places?Show answer

Numerical integration of ∫t Nϕ(t)Φ(t)N−1 dt\int t \, N\phi(t)\Phi(t)^{N-1}\,dt with N=4N = 4 gives about 1.031.03. Compare with 0.560.56 for N=2N = 2 and 1.541.54 for N=10N = 10. The increments shrink, which is the diminishing-returns pattern.

QuestionWhy does the KL divergence from the base model grow like log N, and why does that matter for reward hacking?Show answer

The returned answer has a likelihood ratio of NuN−1N u^{N-1} against the base model, and its expected log is log⁡N−(N−1)/N\log N - (N-1)/N. KL grows slowly, so selection stays close to the base model in a precise sense. But the scorer's errors are concentrated in exactly the tail that selection picks from, so a fixed small KL can still expose a large error in the proxy score.

QuestionA coding model solves a task with probability 0.2 per sample, and unit tests act as a perfect checker. How many samples do you need for at least a 90 percent chance of a correct answer?Show answer

Solve 1−0.8N≥0.91 - 0.8^N \ge 0.9, so 0.8N≤0.10.8^N \le 0.1. Taking logs, N≥log⁡0.1/log⁡0.8≈10.3N \ge \log 0.1 / \log 0.8 \approx 10.3, so N=11N = 11 suffices. Check: 0.811≈0.08590.8^{11} \approx 0.0859, giving 1−0.0859≈0.9141 - 0.0859 \approx 0.914. Interpretation: with a perfect checker the cost is linear in N, so the question becomes how much compute the task is worth.

QuestionYour reward model was trained on outputs from a different model. Why might best-of-N with a large N make answers worse, not better?Show answer

Selection finds the sample the scorer rates highest. If the scorer is wrong in a way that correlates with features the policy happens to produce, the largest N finds the most extreme case of that error. Quality can then fall as N rises, which is the overoptimisation curve. The fix is a scorer that matches the outputs it judges, or a cap on N.

Sources