Using bounded loops
Use a loop step when a fixed sequence needs to repeat against shared Flow variables until a verdict passes. Common examples are draft → review → revise, generate → validate → repair, and retrieve → assess → refine.
A bounded loop has do-while semantics: Runtype runs the body once, evaluates until after the body, and repeats only while the expression is false. maxIterations is always a hard backstop and must be between 1 and 10.
The body reads and writes the same Flow-level variable scope on every round. In this example, each revision replaces draft, and the next review reads the revised value. When iterationVariable is set, Runtype writes the current 1-based round before running the body.
Build a loop with the TypeScript SDK
Both TypeScript Flow builders expose .loop():
MCP flow tools accept the same type: "loop" step shape in inline, create, and update requests.
Design safe loop bodies
- Make every round move one shared working variable toward a measurable verdict. Avoid repeatedly redrafting from the original input.
- Keep
untildeterministic. Have a review or validation step write a boolean or compact verdict, then test that value. - Choose the smallest useful bound. Reaching
maxIterationscompletes the loop and continues the Flow; it does not imply that the verdict passed, so add a conditional after the loop when exhaustion needs a separate path. - Nested
loopsteps are rejected. Put the inner work in the same body, call a separate Flow, or use a conditional inside the body.
Next steps
- Flow variables and templates: share working context between rounds
- Using conditional steps: route based on the final verdict
- Timeouts and long-running work: use asynchronous handles for runs that may outlive a client request