Run Until • AI Agent SDK
run_until: Run agent iteratively until output meets quality criteria.
run_until
Method
</div>
Run agent iteratively until output meets quality criteria.
This method implements the “Ralph Loop” pattern: run agent → judge output → improve based on feedback → repeat until threshold met.
When `goal is provided, delegates to the tool-using goal loop (:meth:run_goal`) so the acceptance-criteria completion judge gates a real tool-iteration loop (rather than re-generating a whole answer).
Signature
def run_until(prompt: str, criteria: str, threshold: float, max_iterations: int, mode: str, on_iteration: Optional[Callable[[Any], None]], verbose: bool, goal: Optional[str], goal_criteria: Optional[Any], judge_model: Optional[str]) -> 'EvaluationLoopResult'Parameters
strThe prompt to send to the agent
strEvaluation criteria for the Judge (e.g., “Response is thorough”)
floatScore threshold for success (default: 8.0, scale 1-10)
intMaximum iterations before stopping (default: 5)
str“optimize” (stop on success) or “review” (run all iterations)
OptionalOptional callback called after each iteration
boolEnable verbose logging
Optional[str]Optional goal text — enables the tool-using goal loop
OptionalOptional structured GoalCriteria for the goal loop
Optional[str]Optional independent judge model for the goal loop
Returns
'EvaluationLoopResult'when `goal is provided this delegates to the tool-using goal loop and returns an :class:~praisonaiagents.agent.autonomy.AutonomyResult instead (success/output/completion_reason), which is a different shape from the default EvaluationLoopResult`.
Usage
agent = Agent(name="analyzer", instructions="Analyze systems")
result = agent.run_until(
"Analyze the auth flow",
criteria="Analysis is thorough and actionable",
threshold=8.0,
)
print(result.final_score) # 8.5
print(result.success) # TrueUses
GoalCriteriarun_goalEvaluationLooploop.run
Source
View on GitHubpraisonaiagents/agent/agent.py at line 5697