GEPAOptimizer
is an adapter for the powerful, state-of-the-art GEPA (Genetic-Pareto) library. It uses an evolutionary algorithm that treats prompts like DNA, iteratively mutating them based on rich, reflective feedback from a “teacher” model to find highly optimized solutions.
This cookbook will guide you through setting up and running the GEPAOptimizer
for production-grade prompt optimization.
This optimizer requires the
gepa
library. If you haven’t already, install it with: pip install gepa
.When to Use GEPA
GEPA is your most powerful tool, ideal for scenarios where achieving the absolute best performance is critical.✅ Best For
- Critical, production-grade applications
- Complex, multi-component systems (e.g., RAG)
- High-stakes tasks where small improvements matter
- When you have a larger evaluation budget
❌ Not Ideal For
- Quick, simple experiments
- Very small budgets or datasets
- Initial exploration (use Random Search first)
How It Works
OurGEPAOptimizer
acts as a clean adapter to the external gepa
library, handling the complex setup for you. The core evolutionary loop proceeds in steps:
1
Evaluate
GEPA first tests the performance of the current best prompt(s) on a sample of your dataset to establish a baseline.
2
Reflect
It uses a powerful “reflection” model to analyze the results, especially the failures. It generates rich, textual feedback on why the prompt failed.
3
Mutate
Based on this reflection, the reflection model rewrites the prompt to create new, improved “offspring” prompts (mutations). This step also includes paraphrasing to increase diversity.
4
Select & Repeat
GEPA uses a sophisticated method called Pareto-aware selection (powered by a UCB bandit algorithm) to efficiently choose the most promising new prompts to carry forward to the next generation. The cycle then repeats.
1. Prepare Your Dataset and Initial Prompt
A high-quality dataset is crucial for GEPA. For this example, we’ll aim to optimize a summarization prompt. A good dataset should contain a diverse set of articles and their ideal, “golden” summaries.2. Configure the GEPA Optimizer
GEPA requires two key models and an evaluation budget.3. Run the Optimization
With everything configured, call the.optimize()
method. The most important parameter is max_metric_calls
, which defines your total budget for the entire evolutionary process.
Important:
max_metric_calls
includes all evaluations, even for initial prompt outputs. If your dataset has 300 rows and max_metric_calls
is 200, the budget will be exhausted just evaluating the first prompt, preventing any actual optimization. Ensure max_metric_calls
is significantly larger than your dataset size.4. Analyze the Results
Theresult
object contains the best prompt found, its score, and the history of the run. GEPA’s strength is finding highly optimized prompts that often contain specific, nuanced instructions learned from analyzing failures.
Performance Tips
Provide a Sufficient Budget
Provide a Sufficient Budget
GEPA is powerful but data-hungry. Its evolutionary process shines with a larger budget. A
max_metric_calls
of 150-300 is a good starting point for real tasks. A small budget (< 50) may not be enough for the algorithm to evolve past the initial prompt.Use a High-Quality Reflection Model
Use a High-Quality Reflection Model
The quality of the optimization is heavily dependent on the
reflection_model
. Using a top-tier model like gpt-5
or claude-4.5-sonnet
or gemini-2.5-pro
for this role is highly recommended for generating insightful critiques and high-quality mutations.Start with a Decent Initial Prompt
Start with a Decent Initial Prompt
While GEPA can work from a very simple prompt, providing a reasonably well-structured initial prompt gives the evolutionary process a better starting point and can lead to faster convergence on a high-quality solution.