3. Branching with decisions
A decision picks one of several paths. Write it with curly braces, and give each arrow leaving it a label. Those arrows are its branches.
Code:
mermaid
agentflow TB
flow support["Support"]
read["Read ticket"]@{ instruction: "Read the ticket and decide whether it is a bug or a question." }
kind{"Bug or question?"}
fix["File a bug"]@{ instruction: "Open a bug report with steps to reproduce." }
answer["Answer"]@{ instruction: "Reply to the question using the help center." }
read --> kind
kind -- bug --> fix
kind -- question --> answer
end
The decision chooses a branch by its label, so:
- Every branch needs a label.
- Each label is different.
- Each branch goes to a different node.
- A decision has at least two branches.
A branch with no label can't be chosen.
Code:
mermaid
agentflow TB
flow support["Support"]
read["Read ticket"]@{ instruction: "Read the ticket and decide whether it is a bug or a question." }
kind{"Bug or question?"}
fix["File a bug"]@{ instruction: "Open a bug report with steps to reproduce." }
answer["Answer"]@{ instruction: "Reply to the question using the help center." }
read --> kind
kind -- bug --> fix
kind --> answer
end
The branch from "kind" to "answer" has no label, so the decision can't choose it. Add a label.
Only branches can have labels. A label on any other arrow is an error.
Guidance on a branch
A branch can also carry an instruction. Give the arrow an id (e1@) and set the instruction on that id. The label is still required.
Code:
mermaid
agentflow TB
flow support["Support"]
read["Read ticket"]@{ instruction: "Read the ticket and decide whether it is a bug or a question." }
kind{"Bug or question?"}@{ instruction: "Treat crashes and wrong results as bugs." }
fix["File a bug"]@{ instruction: "Open a bug report with steps to reproduce." }
answer["Answer"]@{ instruction: "Reply to the question using the help center." }
read --> kind
kind e1@-- bug --> fix
kind -- question --> answer
e1@{ instruction: "Include the customer's app version." }
end
The decision above also has its own instruction, which guides the choice.
Next: 4. Loops.