Continuous improvement uses repeated cycles of producing, reviewing, and revising a result. It is useful when quality can be measured and another pass can improve the output. section 9.3.4
A Loop workflow coordinates each cycle:
- Run agents in sequence, passing the first agent’s input only at the start and carrying context through later executions. section 9.3.4
- Check a stop condition after each iteration; cap the number of iterations to prevent an unbounded loop. section 9.3.4
Writer and reviewer
A writer drafts marketing copy, then a reviewer scores it and gives feedback. The workflow repeats until the score reaches 80 or the iteration limit is reached. The threshold is a configurable quality bar, not a universal standard. section 9.3.4
Listing 9.7 LoopWorkflow implementation and stop condition
# Stop condition type: (current result, iteration count) -> whether to stop
StopCondition = Callable[[AgentResult, int], bool]
class LoopWorkflow(Agent):
def __init__(
self,
agents: List[Agent],
stop_condition: Optional[StopCondition] = None,
max_iterations: int = 10,
name: str = "loop_workflow",
):
self.agents = agents
self.stop_condition = stop_condition
self.max_iterations = max_iterations
self.name = name
async def run(
self,
user_input: Optional[str] = None,
context: Optional[ExecutionContext] = None,
verbose: bool = False,
) -> AgentResult:
result = None
is_first_agent = True # Whether this is the first agent overall
for iteration in range(1, self.max_iterations + 1):
# One iteration: run all agents sequentially
for agent in self.agents:
if context is not None:
context.final_result = None
context.current_step = 0
if is_first_agent:
# Very first: pass user_input
result = await agent.run(user_input, context=context, verbose=verbose)
is_first_agent = False
else:
# After: pass only context
result = await agent.run(context=context, verbose=verbose)
context = result.context
# Check stop condition
if self.stop_condition and self.stop_condition(result, iteration):
break
return result
Use the LoopWorkflow listing to inspect the iteration structure and stop-condition interface. section 9.3.4





























