Skip to main content

The Loop node executes repetitive workflows where each cycle builds on the results of the previous one. Unlike iteration, which processes array elements independently, loops create progressive workflows that evolve with each repetition.

Loop vs Iteration#

Understanding when to use each repetition pattern:

Sequential Processing - Each cycle depends on previous results

Progressive Refinement - Outputs improve or evolve over iterations

State Management - Variables persist and accumulate across cycles

Use Cases - Content refinement, problem solving, quality assurance

Configuration#

Loop Variables#

Define variables that persist across loop iterations and remain accessible after the loop completes. These variables maintain state and enable progressive workflows.

Termination Conditions#

Configure when the loop should stop executing:

Loop Termination Condition - Expression that determines when to exit (e.g., quality_score > 0.9)

Maximum Loop Count - Safety limit to prevent infinite loops

Exit Loop Node - Immediate termination when this node is reached

Info

The loop terminates when either the termination condition is met, the maximum count is reached, or an Exit Loop node executes. If no conditions are specified, the loop continues until the maximum count.

Basic Loop Example#

Generate random numbers until finding one less than 50:

Basic Loop Workflow for Random Number Generation

Basic Loop Workflow for Random Number Generation

Workflow Steps:

  1. Code node generates random integers between 1-100
  2. If-Else node checks if number is less than 50
  3. Template node returns “done” for numbers < 50 to trigger loop termination
  4. Loop continues until termination condition is met

Loop Execution Steps and Results

Loop Execution Steps and Results

Advanced Loop Example#

Create a poem through iterative refinement, with each version building upon the previous one:

Loop Variables:

  • num - Counter starting at 0, incrementing each iteration
  • verse - Text variable holding the current poem version

Workflow Logic:

  1. If-Else node checks if num > 3 to determine when to exit
  2. LLM node generates improved poem based on previous version
  3. Variable Assigner updates both counter and poem content
  4. Exit Loop node terminates after 4 refinement cycles

The LLM prompt references both the current verse and iteration context: