Loop
Controlled node
Overview
The Loop node executes a workflow branch repeatedly for a specified number of iterations. It is useful for batch processing, repeating operations, or iterating through a fixed number of steps.
On each iteration, the node fires the loop event and outputs the current index (0-based). When all iterations are complete, it fires the finished event.
Output events
There are two output event options for this node:
- loop: Fires for each iteration while the loop is running. Connect this to the nodes you want to execute repeatedly.
- finished: Fires once when the loop has completed all iterations. This is useful for triggering cleanup actions or subsequent steps after the loop finishes.
Inputs
| Input | Type | Description | Default |
|---|---|---|---|
| run | Event | Triggers the loop to start. | - |
| times | Number | Specifies the number of times to repeat the loop. | 0 |
Outputs
| Output | Type | Description |
|---|---|---|
| loop | Event | Fires on each iteration of the loop. |
| index | Number | Contains the current iteration index (0, 1, 2, ..., times-1). |
| finished | Event | Fires when the loop has completed all iterations. |
Runtime behavior and defaults
When the run event fires, the Loop node begins executing. It maintains an internal counter starting at 0. While the counter is less than the times value, the node:
- Sets the index output to the current counter value
- Fires the loop event
- Repeats (increments the counter)
When the counter reaches the times value, the node fires the finished event and stops. If times is 0 or not a valid number, the node will fire finished immediately without firing loop.
The index output starts at 0 and increments to times - 1. For example, if times is set to 5, the index will output 0, 1, 2, 3, and 4 on successive iterations.
Example
Scenario: Generate 5 variations of a text prompt using an AI Write node.
- Connect a Start node to the run input of the Loop node.
- Set the times input to
5. - Connect the loop output to an AI Write node.
- Connect the index output to the AI Write node's prompt input using template syntax (e.g.,
"Generate paragraph {{index}}: ..."). - Connect the finished output to a Show node to display a "Generation complete" message.
The workflow will run the AI Write node 5 times with indices 0 through 4, then trigger the Show node when finished.