# Control flow, loops and routing

Branch with Conditional, Router and Classifier, wait for parallel branches with Join, and iterate with Loop and Loop End, with worked examples.

Source: https://glamorous-boar.cloudvent.net/control-flow/

---

AgenticMind separates **data edges** (values) from **control edges** (whether a node runs). Control edges go from a node's **branch** to another node's `trigger`.

## The rules

| Rule | Behaviour |
|---|---|
| Readiness | A node is ready when every upstream node (data or control) is finished. |
| OR-join (default) | With incoming control edges, a node runs if **at least one** comes from a succeeded node that activated that branch. Otherwise it is skipped (`control_not_activated`). |
| AND-join | **Join** runs only if **every** incoming control edge was activated. |
| Skip propagation | If every producer of a required input was skipped, the node is skipped too (`upstream_skipped:<port>`). |
| Parallelism | Independent branches run concurrently, up to `max_concurrency`. No special node is needed for fan-out. |
| Fail-fast | The first node failure (after retries) cancels in-flight nodes and fails the run. |
| No cycles | Accidental cycles are rejected. Use Loop / Loop End to iterate. |

## Conditional: two-way branch

**Conditional** evaluates `value` with an operator (`equals`, `not_equals`, `contains`, `greater_than`, `less_than`, `is_empty`, `is_truthy`) and an `operand`, then activates `true` or `false`.

Example (template *Conditional routing*): `score` → Conditional (`greater_than` 50). Connect the `true` branch to the trigger of a "passed" Text Template and `false` to a "failed" one. Only one side runs; the other is skipped.

## Router: many routes

**Router** sends execution down one named route. Every route you configure becomes a control branch, plus `default`.

- **`rules` mode** (free, offline): each route has a `name` (lowercase, e.g. `billing`), a `description`, an `operator` (`equals`, `not_equals`, `contains`, `regex`, `greater_than`, `less_than`, `is_empty`, `is_truthy`) and a `value`. Routes are checked top to bottom and the first match wins. The defaults are `billing` (regex `invoice|refund|charge|payment|billing`) and `technical` (regex `error|bug|crash|login|password|cannot|can't`). `case_sensitive` is off by default.
- **`llm` mode**: a model chooses through a forced `choose_route` tool call whose argument is an enum of route names. An unknown answer falls back to `default` and emits `router_fallback`.

Outputs: `route` (the chosen name) and `reason`.

## Classifier: routing by meaning

**Classifier** labels text with your labels (each with a description and optional examples). Each label is a branch, plus `default`. Unlike Router's LLM mode it supports **multi-label** (every matching branch runs), a **confidence threshold** (`min_confidence`; below it the input goes to `default`) and typed outputs (`label`, `labels`, `confidence`, `reason`).

## Join: wait for branches

After branching, use **Join** so one downstream node receives whichever path ran:

- `collect`: a list of the values that arrived;
- `merge`: combine objects (later keys win);
- `concat`: join as text with a separator.

Branches that were skipped send nothing. `min_values` (default 1) fails the node with `JOIN_INCOMPLETE` if fewer values arrive.

<Alert variant="tip" title="Collect vs Join" text="**Collect** gathers several data edges into a list but follows the normal OR rules. **Join** is an AND over control paths. After a Router, use Join so the final Output receives exactly one reply."/>

## Loops

Loops are **paired, acyclic regions**. There is no back edge: the runtime repeats the region.

1. Add a **Loop** and a **Loop End**.
2. Connect Loop's `scope` output to Loop End's `scope` input (port type `LoopScope`).
3. Put the body between them: every node on a path from Loop to Loop End is in the body.
4. Feed the body's final value into Loop End's `value`.

Values can leave the body only through Loop End (`LOOP_REGION_LEAK` otherwise). Values from outside the loop can be read inside it.

### for_each

Runs the body once per item of the `items` list. Inside, Loop outputs `item` and `index`.

- `concurrency` 1–16 processes items in parallel; results stay in item order.
- `on_item_error`: `fail` stops the loop at the first failing item; `skip` records it in the report and returns `null` for that item.
- `max_items` (default 100) with `on_too_many`: `fail` or `truncate`.

Loop End outputs `results` (one per item), `last` and `report` (`mode`, `iterations`, `stop_reason`, `errors`).

**Example (Ticket triage):** `Tickets` (JSON list) → Loop (`for_each`) → Router (billing / technical / default) → three Text Templates → Join → Loop End → `Replies`.

### repeat

Runs the body again and again, passing a **state** forward:

- Loop outputs the current `state` (seeded from `initial`).
- Loop End's optional `continue` input: `false` stops the loop.
- Loop End's optional `next_state`: becomes the next iteration's state (otherwise the iteration's `value` does).
- Stops at `max_iterations` (default 5, up to 100).

**Example (Write with a critic):** Loop (`repeat`, 3) → Writer LLM (gets the critic's last revision request from `state`) → Critic → Loop End with `value` = draft, `continue` = `needs_revision`, `next_state` = `revision_request`.

### Loop details

- Every event from inside a loop carries `data.iteration` (one index per nesting level); Loop End emits `loop_iteration` events.
- Nested loops are supported; overlapping loops are not (`LOOP_OVERLAP`).
- A body node's final status shows its last iteration; the canvas adds *ran ×N*.
- Human Approval (or any node that can pause) cannot be inside a loop (`SUSPEND_IN_LOOP`).
- Iterations are not individually checkpointed.

## Validation codes for control flow

| Code | Meaning |
|---|---|
| `GRAPH_CYCLE` | An accidental cycle; use a loop. |
| `EDGE_INVALID_CONTROL_TARGET` | A control edge does not target `trigger`. |
| `LOOP_UNPAIRED` | A Loop without a Loop End connected to its `scope`. |
| `LOOP_SCOPE_SHARED` | One Loop's scope wired to several Loop Ends. |
| `LOOP_REGION_LEAK` | A body value goes to a node outside the loop. |
| `LOOP_OVERLAP` | Two loops share nodes without nesting. |
| `SUSPEND_IN_LOOP` | A node that can pause the run is inside a loop body. |

See [Error codes](/error-codes/) for the full list, and [Transform, control and output nodes](/nodes/transform-and-control/) for every setting.
