What is Loop Node?
A Loop node executes repetitive tasks that depend on previous iteration results until exit conditions are met or the maximum loop count is reached.Loop vs. Iteration
| Type | Dependencies | Use Cases |
|---|---|---|
| Loop | Each iteration depends on previous results | Recursive operations, optimization problems |
| Iteration | Iterations execute independently | Batch processing, parallel data handling |
Configurations
| Parameter | Description | Example |
|---|---|---|
| Loop Termination Condition | Expression that determines when to exit the loop | x < 50, error_rate < 0.01 |
| Maximum Loop Count | Upper limit on iterations to prevent infinite loops | 10, 100, 1000 |
| Loop Variables | Values that persist across iterations and remain accessible to nodes after the loop completes | A counter x < 50 increases by 1 each iteration. Use it for calculations inside the loop, then access its final value in later workflow steps. |
| Exit Loop Node | Immediately ends the loop when reached | Caps execution at 10 iterations, regardless of other conditions. |
The loop can be terminated by either the Exit Loop Node or the Loop Termination Condition. When either condition is met, the loop will immediately exit.If no exit conditions are specified, the loop will continue executing (similar to
while (true)) until it reaches the Maximum Loop Count.Example 1: Basic Loop
Goal: Generate random numbers between 1-100 until getting a number less than 50. Steps:-
Set up a Loop node by configuring its Loop Termination Condition to trigger when the Template node returns
done. -
Set up a Code node that generates random integers between
1and100. - Set up an IF/ELSE node with the following logic:
-
For numbers ≥ 50: Output the
Current Numberand continue the loop -
For numbers < 50: Output the
Final Number, and then use the Template node to returndone
- The workflow terminates automatically once a number below
50is generated.
Example 2: Advanced Loop (with Variables and Exit Node)
Goal: Design a workflow that generates a poem through four iterative refinements, with each version building upon the previous one. Steps:- Set up a Loop node with those Loop Variables:
- num: A counter starting at 0, incrementing by 1 per iteration
-
verse: A text variable initialized with
I haven't started creating yet
- Set up an IF/ELSE node that evaluates the iteration count:
- When num > 3: Proceed the Exit Loop node
- When num ≤ 3: Proceed to the LLM node
- Set up an LLM node to generate poems.
Example Prompt:You are a European literary figure who can create poetic verses based on
sys.query.verse is your last creation. You can progress based on your previous work.I haven't started creating yet. Each subsequent iteration builds upon the previous output, with the new poem replacing the verse variable’s content.
- Set up a Variable Assigner node to manage state:
- Increment num by 1 after each iteration
- Update verse with the newly generated poem
- When executed, the workflow will produce four versions of your poem, each iteration building upon its previous output.