Loops
Any step can loop back to an earlier step, forming a loop body. Set
loop_back_to and loop_config on the tail step of the loop.
After the tail step completes successfully, the executor resolves
loop_config.condition_key. While it resolves truthy and the iteration budget
isn’t exhausted, the loop body (every step numbered from loop_back_to to the
tail, inclusive) is reset and runs again. A failed tail step does not loop.
LoopConfig
Section titled “LoopConfig”| Field | Type | Default | Purpose |
|---|---|---|---|
condition_key | str | "" | A context reference ($metadata.step_N, $memory.ns.key, $history[-1], $outer_context, …) whose resolved value is cast to bool. Empty = always loop, up to max_iterations. |
max_iterations | int | 10 | Budget guard: max re-executions of the loop body (≥ 1). |
negate_condition | bool | False | False = while-loop (continue while truthy). True = until-loop (continue while falsy). |
When the budget is exhausted, the chain simply continues past the tail step;
reaching max_iterations is not an error.
Example
Section titled “Example”from mmar_carl import ToolStepDescription, ToolStepConfig, LoopConfig
# Steps 1–2 form the loop body; step 2 drives iteration.ToolStepDescription( number=2, title="Refine answer", dependencies=[1], config=ToolStepConfig(tool_name="refiner", input_mapping={}), loop_back_to=1, loop_config=LoopConfig( condition_key="$memory.loop.needs_retry", # truthy → loop again max_iterations=5, ),)For an until-loop (run until the flag becomes truthy), set negate_condition=True.
ChainBuilder loop helpers
Section titled “ChainBuilder loop helpers”ChainBuilder.add_while_loop(body_steps, condition_key, max_iterations=10, start_number=None)
and add_until_loop(...) wrap the manual API. They renumber the body steps from
start_number (default: the next free number), set loop_back_to on the last
step, and attach a LoopConfig. add_while_loop continues while the value is
truthy; add_until_loop sets negate_condition=True and continues until it is
truthy.
The repo’s loop example (no API key) shows a research loop that keeps searching until enough facts are gathered:
body = [ ToolStepDescription(number=0, title="Search Facts", config=ToolStepConfig(tool_name="search_facts", input_mapping={})), ToolStepDescription(number=0, title="Evaluate Research", config=ToolStepConfig(tool_name="evaluate_research", input_mapping={})), ToolStepDescription(number=0, title="Check Done", config=ToolStepConfig(tool_name="check_done", input_mapping={})),]
chain = ( ChainBuilder() .add_until_loop(body_steps=body, condition_key="$metadata.step_3", max_iterations=10) .build())The example also covers a retry-until-success loop, the manual loop_back_to /
loop_config API, and an add_while_loop queue drainer.
Loop history
Section titled “Loop history”Each restart records the previous pass in
context.metadata["loop_iteration_history"], keyed by "<loop_back_to>-<tail>"
(e.g. "1-3"). Every entry is {"iteration": n, "results": [...]} with the
dumped step results of that pass. Only the latest pass’s results stay in
ReasoningResult.step_results.
At most one loop restarts per batch.
See also
Section titled “See also”- Conditional step for one-shot branching.
- Caching: memoize loop-body steps that see the same inputs.