4. Loops β
An arrow back to an earlier step makes a loop. Every loop needs a way out: a decision in the loop with a branch that leaves it. A smaller loop inside a bigger one needs a decision branch of its own too.
Code:
agentflow TB
flow editor["Editor"]
draft["Draft"]@{ instruction: "Write a 300-word product description." }
check{"Good enough?"}
publish["Publish"]@{ instruction: "Publish the approved description." }
draft --> check
check -- no --> draft
check -- yes --> publish
end
Here check -- no --> draft repeats the draft, and check -- yes --> publish leaves the loop.
A loop without a decision never ends.
Code:
agentflow TB
flow editor["Editor"]
draft["Draft"]@{ instruction: "Write a 300-word product description." }
review["Review"]@{ instruction: "List what to improve in the draft." }
draft --> review
review --> draft
end
The loop through "draft" never ends, because it has no decision with a branch leading out. Add a decision with a branch that leaves the loop.
Leave from the decision β
A task continues along every arrow that leaves it. If a task in the loop also points outside it, that target runs on every lap. Start the arrow from the decision instead.
Code:
agentflow TB
flow editor["Editor"]
draft["Draft"]@{ instruction: "Write a 300-word product description." }
check{"Good enough?"}
publish["Publish"]@{ instruction: "Publish the approved description." }
notify["Notify"]@{ instruction: "Tell the team a new draft is ready." }
draft --> check
check -- no --> draft
check -- yes --> publish
draft --> notify
end
Task "draft" has an arrow leaving its loop to "notify", so "notify" runs on every lap. To run it once after the loop, start that arrow from the loop's decision.
Next: 5. Using connectors.