Skip to main content

Overview

Best-of-N is a simple yet effective algorithm that reuses the same parent program for N consecutive iterations, generating N different variants and keeping the best. This allows thorough exploration of variations from a single starting point.

Focused Exploration

Generates multiple variants from the same parent

Automatic Reset

Switches to a new parent after N iterations

Simple Logic

Easy to understand and configure

Efficient Sampling

No complex selection or archive management

How It Works

Iteration Cycle

  1. Parent Selection: Select the best program as parent (if starting fresh or after N iterations)
  2. Variant Generation: Generate a variant of the parent
  3. Evaluation: Score the variant
  4. Counter Increment: Increment iteration counter
  5. Check Reset: If counter reaches N, reset and select new parent
  6. Repeat: Continue with same or new parent
1

Iteration 1

Select best program as parent, generate variant #1
2

Iterations 2-N

Reuse same parent, generate variants #2 through #N
3

Iteration N+1

Select new best program (which might be one of the N variants), reset counter

Context Programs

While the parent stays fixed for N iterations, context programs are sampled fresh each time from the current top programs, providing updated examples.

Configuration

Basic Usage

Configuration File

Python API

Configuration Options

int
default:"5"
Number of consecutive iterations to reuse the same parent before selecting a new one.Recommended values:
  • 3-5: Quick iteration, frequent parent updates
  • 5-10: Balanced exploration/update
  • 10-20: Deep exploration of each parent
int
default:"4"
Number of top programs to include as context (updated each iteration)

When to Use Best-of-N

  • Problems where each parent has many possible improvements
  • Stochastic or creative generation (gives LLM multiple tries)
  • When you want to thoroughly explore variations
  • Limited iteration budgets where you want multiple attempts
  • Deterministic generation (LLM produces same output each time)
  • Problems requiring diverse exploration of solution space
  • Very short runs where N > total iterations

Example

Creative Text Generation

Best-of-N works well for creative tasks with high LLM variance:
Result: Every 10 iterations, the algorithm picks the best prompt so far and generates 10 more variants.

Choosing N

The optimal value of N depends on several factors:

LLM Variance

If the LLM produces very different outputs each time (creative tasks, underspecified problems):
More attempts = higher chance of finding a good variant

Iteration Budget

  • Total iterations = 30: best_of_n: 3 (10 parent updates)
  • Total iterations = 100: best_of_n: 5-10 (10-20 parent updates)
  • Total iterations = 500: best_of_n: 10-25 (20-50 parent updates)
Avoid setting best_of_n too high relative to total iterations. You need multiple parent updates to make progress.

Monitoring Progress

Track Parent Switches

Analyze Variants

After a run, analyze which variants were best:

Comparison with Other Algorithms

Advanced Strategies

Adaptive N

Adjust N based on improvement:

Diversity Sampling

Vary the context programs more:

Tips for Best Results

Use Temperature

Enable LLM temperature > 0 to get diverse variants from the same parent

Monitor Variance

Track score variance of variants. Low variance = reduce N

Balance N and Budget

Ensure at least 5-10 parent updates in your iteration budget

Combine with Restarts

Periodically reset to explore from different starting points
  • Top-K - Similar but updates parent every iteration
  • Beam Search - Maintains multiple parents simultaneously
  • GEPA Native - Uses acceptance gating for variant selection