Skip to main content
Repeat a step — or a fixed list of steps — until a condition is met. This pattern is ideal for iterative improvement, quality checking, and optimization workflows.
Steps inside this pattern inherit the same max_retries, guardrails, and output_file policies as top-level steps. See Nested workflows → Retry, guardrails, and output_file.
The user requests repeated refinement; the repeat helper runs the step N times.

Quick Start

1

Define generator and stop condition

2

Run repeat workflow

Output:

Repeating a list of steps

Pass a list to run several steps in order, once per iteration. Each step sees the previous step’s output.

Ordering guarantees

API Reference

repeat()

Parameters

Condition Function

The until function receives WorkflowContext and returns bool:

Result Variables

After repeat completion:

Examples

Quality-Based Stopping

Counter-Based Stopping

With Agents

Evaluator-Optimizer Pattern

Classic pattern with separate generator and evaluator:

With Early Stop

Two-agent back-and-forth

The simplest N-agent turn-taking pattern: alternate two agents for a fixed number of rounds.
For a fixed rotation of more than two speakers, prefer Discussion — it exists for exactly this.

Use Cases

How It Works


Best Practices

Cap repeat loops to prevent runaway cost when exit conditions never trigger.
Evaluator functions should return explicit booleans — avoid ambiguous string matching.
Repeat improves a single artefact; loop processes many items — do not confuse the two.
Inspect intermediate outputs when tuning evaluator-optimiser workflows.

Comparison with Loop

Repeat vs. Discussion

repeat() now handles both a single step and a fixed list of steps — pick the right tool:
  • Use repeat(step, until=...) when the same step should iterate to improve.
  • Use repeat([a, b, ...], max_iterations=N) for a light back-and-forth of a small, fixed list.
  • Use Discussion when you want a first-class N-agent conversation with round-robin/custom selectors, until, and per-turn logging.

Workflow Patterns

Overview of routing, parallel, loop, and repeat

Workflow Loop

Iterate over lists and files

Workflow Routing

Decision-based branching

Workflow Parallel

Run independent steps concurrently

Workflow Discussion

N-agent conversation loop