EvaluationLoop implements the “Ralph Loop” pattern: run an agent, judge the output, provide feedback, and repeat until the quality threshold is met.
How It Works
Quick Start
- Agent.run_until()
- EvaluationLoop Class
- With Callback
- Goal-gated variant
Modes
Optimize Mode
Stops as soon as the threshold is met. Best for production use.
Review Mode
Runs all iterations regardless of score. Best for analysis.
Configuration
The Agent instance to evaluate
Evaluation criteria for the Judge (e.g., “Response is thorough and accurate”)
Score threshold for success (1-10 scale)
Maximum number of iterations before stopping
"optimize" (stop on success) or "review" (run all iterations)Optional callback called after each iteration with
IterationResultEnable verbose logging
Results
EvaluationLoopResult
Whether the loop achieved the threshold
Score from the last iteration (1-10)
All scores across iterations
Output from the last iteration
All findings/suggestions collected
Number of iterations completed
Total time taken
IterationResult
Each iteration produces anIterationResult:
Async Support
Best Practices
Write Specific Criteria
Write Specific Criteria
Be specific in your criteria to get consistent results:
Set Appropriate Thresholds
Set Appropriate Thresholds
- 8.0 (default): Good for most use cases
- 9.0+: High quality, may require more iterations
- 7.0: Acceptable quality, faster completion
Limit Iterations for Cost Control
Limit Iterations for Cost Control
Each iteration makes LLM calls. Set
max_iterations based on your budget:Use Callbacks for Monitoring
Use Callbacks for Monitoring
Track progress in real-time:
Related
Judge
LLM-as-judge for evaluating outputs
Evaluator-Optimizer
Multi-agent evaluator-optimizer pattern
Goal Loop
Gate a tool-using loop on an independent acceptance-criteria judge
EvaluationLoop uses lazy loading - the Judge is only imported when you actually run an evaluation, ensuring zero performance impact when not in use.

