---
title: "Loop"
description: "Execute repetitive workflows with progressive refinement"
icon: "infinity"
---

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:

<Tabs>
  <Tab title="Loop">
    **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
  </Tab>
  
  <Tab title="Iteration">
    **Independent Processing** - Each item processed separately
    
    **Parallel Execution** - Items can be processed simultaneously
    
    **Batch Operations** - Same operation applied to multiple data points
    
    **Use Cases** - Data transformation, bulk processing, parallel analysis
  </Tab>
</Tabs>

## 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.
</Info>

## Basic Loop Example

Generate random numbers until finding one less than 50:

<Frame caption="Basic loop workflow for random number generation">
  <img src="https://assets-docs.dify.ai/2025/04/282013c48b46d3cc4ebf99323da10a31.png" alt="Basic loop workflow" />
</Frame>

**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

<Frame caption="Loop execution steps and results">
  <img src="https://assets-docs.dify.ai/2025/04/9d9fb4db7093521000ac735a26f86962.png" alt="Loop execution steps" />
</Frame>

## Advanced Loop Example

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

<video controls src="https://assets-docs.dify.ai/2025/04/7ecfc04458aa38e721baaa5f6355486c.mp4" width="100%" />

**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:

```text
You are a European literary figure creating poetic verses.

Current verse: {{verse}}

Refine and improve this poem based on your previous work.
```

## Common Use Cases

**Content Refinement** - Progressively improve text, code, or designs through multiple LLM reviews until quality standards are met.

**Problem Solving** - Break complex problems into iterative steps where each cycle tackles the next logical piece based on previous progress.

**Research Workflows** - Iteratively search, analyze, and refine research queries based on findings from each search cycle.

**Quality Assurance** - Repeatedly test and validate outputs, making improvements until all criteria are satisfied.

## Best Practices

**Define Clear Exit Conditions** - Ensure loops have specific, measurable conditions for termination to prevent infinite execution.

**Set Reasonable Limits** - Use appropriate maximum iteration counts based on expected complexity and processing requirements.

**Manage State Effectively** - Use loop variables to maintain necessary information across iterations while avoiding unnecessary data accumulation.

**Monitor Progress** - Include logging or progress indicators to track loop execution, especially for long-running processes.