dspy-best-of-n

Installation
SKILL.md

Pick the Best Output with dspy.BestOfN

Guide the user through using DSPy's BestOfN module to run a program multiple times and keep the highest-scoring result. This is rejection sampling -- generate N candidates, score each one, return the winner.

What is BestOfN

dspy.BestOfN wraps any DSPy module and calls it up to N times with temperature=1.0 (each attempt uses a different rollout ID to get diverse outputs). A reward function scores every result, and BestOfN returns the single best prediction.

If any attempt hits a score threshold you set, execution stops early -- no need to burn through all N attempts when you already have a great result.

Your module ──> Run N times ──> Score each with reward_fn ──> Return best

When to use BestOfN

  • You have a cheap, fast metric that can score outputs automatically (test suite passes, regex match, word count check, etc.)
  • Quality variance is high -- the same prompt sometimes produces great output and sometimes doesn't
  • You'd rather spend tokens than engineering time -- BestOfN is the simplest way to boost quality without optimization
Related skills

More from lebsral/dspy-programming-not-prompting-lms-skills

Installs
4
GitHub Stars
5
First Seen
Mar 17, 2026