The Loop node is used to repeatedly execute a series of tasks. It orchestrates loop logic via a dedicated sub-canvas (the loop body), supporting two modes: array-based iteration and fixed-count iteration.
Usage Example
Consider the scenario of generating one-sentence introductions for multiple cities in batch. This workflow accepts an array of city names, passes each city name sequentially to a large language model (LLM) through the Loop node, and collects all generated introductions into an output array.
Adding the Node
Method 1: Add via the + Button
-
Hover your mouse over any node on the canvas (e.g., the Start node); a
+button automatically appears on its right side. -
Click the
+button and select Logic > Loop from the pop-up node list. The Loop node is then automatically added to the canvas and connected to the upstream node. - After adding the Loop node, the canvas automatically generates a dedicated loop body sub-canvas, containing a fixed Loop Start node and a fixed Iteration End node.
Method 2: Drag from the Node Library
- In the left-hand node library, locate and select Logic > Loop.
- Drag the node onto the canvas at your desired position, then draw a connection line from the edge of an upstream node to the Loop node.
- Upon addition, the canvas automatically generates the loop body sub-canvas, including the fixed Loop Start and Iteration End nodes.
Core Configuration
Loop Type
The loop type determines how iterations are executed. Two modes are supported: Array-based Loop (default) and Fixed-count Loop.
Array-based Loop
In this mode, the loop iterates over each element in an input array. The number of iterations equals the array’s length. Input must be of array type, and elements are processed in index order. Multiple arrays may be provided; the loop runs for as many iterations as the shortest array contains.
Within the loop body, the current array element and its index can be accessed using predefined variables:
| Variable Name | Type | Description |
|---|---|---|
item | Same as array element type | Current array element being processed |
index | Number | Zero-based array index |
| Example: | — Array ["Beijing", "Shanghai", "Guangzhou"] | — Iteration 1: item = "Beijing", index = 0 |
— Iteration 2: item = "Shanghai", index = 1 | — Iteration 3: item = "Guangzhou", index = 2 |
Fixed-count Loop
This mode executes the loop a predetermined number of times—ideal for scenarios requiring exact repetition.
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| Loop Count | Number | Yes | Number of iterations; valid range: 1–1000 |
Intermediate Variables
By default, each iteration is isolated: Iteration 1 has no knowledge of Iteration 2, and Iteration 2 cannot access results from Iteration 1. Intermediate variables solve this by persisting across all iterations—they retain their values between iterations and can be both read and updated (e.g., via a Variable Assignment node), enabling state transfer from one iteration to the next.
Core Mechanism:
- Before the loop starts, intermediate variables are initialized with their specified initial values.
- During each iteration, nodes inside the loop body can read the current value of any intermediate variable.
- Using a Variable Assignment node, you can update an intermediate variable’s value within the loop body.
- At the start of the next iteration, the variable retains its most recently assigned value.
Typical Use Cases:
| Scenario | Purpose of Intermediate Variable | Example |
|---|---|---|
| Long-text generation | Store the previous paragraph so the LLM can reference it when generating the next, preserving context continuity | Variable last_paragraph, initialized as empty string; updated after each iteration to hold the latest paragraph |
| Counting & early termination | Track occurrences of a specific condition (e.g., errors); exit loop when threshold is reached | Variable error_count, initialized to 0; incremented on each error; termination condition set to error_count >= 3 |
| Result aggregation | Accumulate outputs incrementally to build a final concatenated result | Variable full_text, initialized as empty string; each iteration appends new content to it |
Termination Condition
A termination condition is a user-defined rule that causes the loop to exit early. The system evaluates this condition before each iteration begins. If the condition evaluates to true, the loop exits immediately.
Important constraints:
- Termination conditions may only reference intermediate variables.
- They cannot reference array elements (
item,index), global workflow variables, or other external variables.
- You must first declare the corresponding intermediate variable in the Loop node configuration.
- You must update that variable inside the loop body (e.g., using a Variable Assignment node).
-
Between groups: Logical OR — the loop terminates if any group evaluates to
true. -
Within a group: Logic is configurable via the dropdown “When the following X conditions are met”:
- All (default): Logical AND — all conditions in the group must be satisfied.
- Any: Logical OR — any one condition in the group being satisfied suffices.
Output Variable
The output variable stores the loop’s results. Its value is always an array, where each element corresponds to the output of one completed iteration.
If the loop terminates early (e.g., due to a termination condition), the output array contains only the results from successfully completed iterations.
Loop Body
The loop body is a dedicated sub-canvas nested within the Loop node, used to orchestrate the core logic executed during each iteration. During runtime, the workflow executes all nodes inside this sub-canvas once per iteration.
Node connections inside the loop body follow the same rules as the main workflow canvas, but with the following restrictions:
- No nested loops: You cannot add another Loop node inside the loop body.
- No batch processing nodes: Batch Processing nodes are not allowed inside the loop body.
- No cross-boundary dragging: Nodes from outside the loop body cannot be dragged into it.
- No outward movement: Nodes inside the loop body cannot be moved outside of it.