> ## Documentation Index
> Fetch the complete documentation index at: https://enterprise-docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Loop

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

<table>
  <thead>
    <tr>
      <th>Type</th>
      <th>Dependencies</th>
      <th>Use Cases</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td><strong>Loop</strong></td>
      <td>Each iteration depends on previous results</td>
      <td>Recursive operations, optimization problems</td>
    </tr>

    <tr>
      <td><strong>Iteration</strong></td>
      <td>Iterations execute independently</td>
      <td>Batch processing, parallel data handling</td>
    </tr>
  </tbody>
</table>

## Configurations

<table>
  <thead>
    <tr>
      <th>Parameter</th>
      <th>Description</th>
      <th>Example</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Loop Termination Condition</td>
      <td>Expression that determines when to exit the loop</td>
      <td><code>x \< 50</code>, <code>error\_rate \< 0.01</code></td>
    </tr>

    <tr>
      <td>Maximum Loop Count</td>
      <td>Upper limit on iterations to prevent infinite loops</td>
      <td>10, 100, 1000</td>
    </tr>

    <tr>
      <td>Loop Variables</td>
      <td>Values that persist across iterations and remain accessible to nodes after the loop completes</td>
      <td>A counter <code>x \< 50</code> increases by 1 each iteration. Use it for calculations inside the loop, then access its final value in later workflow steps.</td>
    </tr>

    <tr>
      <td>Exit Loop Node</td>
      <td>Immediately ends the loop when reached</td>
      <td>Caps execution at 10 iterations, regardless of other conditions.</td>
    </tr>
  </tbody>
</table>

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

## Example 1: Basic Loop

**Goal: Generate random numbers between 1-100 until getting a number less than 50.**

**Steps**:

1. Set up a **Loop** node by configuring its **Loop Termination Condition** to trigger when the **Template** node returns `done`.

2. Set up a **Code** node that generates random integers between `1` and `100`.

3. Set up an **IF/ELSE** node with the following logic:

* For numbers ≥ 50: Output the `Current Number` and continue the loop

* For numbers \< 50: Output the `Final Number`, and then use the **Template** node to return `done`

4. The workflow terminates automatically once a number below `50` is generated.

![Basic loop workflow](https://assets-docs.dify.ai/2025/04/282013c48b46d3cc4ebf99323da10a31.png)

![Steps](https://assets-docs.dify.ai/2025/04/9d9fb4db7093521000ac735a26f86962.png)

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

1. 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`

2. 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

3. Set up an **LLM** node to generate poems.

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

The first iteration begins with the initial verse value `I haven't started creating yet`. Each subsequent iteration builds upon the previous output, with the new poem replacing the verse variable’s content.

4. Set up a **Variable Assigner** node to manage state:

* Increment num by 1 after each iteration

* Update verse with the newly generated poem

5. When executed, the workflow will produce four versions of your poem, each iteration building upon its previous output.

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

<CardGroup cols="2">
  <Card title="Edit this page" icon="pen-to-square" href="https://github.com/langgenius/dify-docs-mintlify/edit/main/en/guides/workflow/node/loop.mdx">
    Help improve our documentation by contributing directly
  </Card>

  <Card title="Report an issue" icon="github" href="https://github.com/langgenius/dify-docs-mintlify/issues/new?title=Documentation%20Issue%3A%20&body=%23%23%20Issue%20Description%0A%3C%21--%20Please%20briefly%20describe%20the%20issue%20you%20found%20--%3E%0A%0A%23%23%20Page%20Link%0Ahttps%3A%2F%2Fgithub.com%2Flanggenius%2Fdify-docs-mintlify%2Fblob%2Fmain%2Fen/guides/workflow/node%2Floop.mdx%0A%0A%23%23%20Suggested%20Changes%0A%3C%21--%20If%20you%20have%20specific%20suggestions%20for%20changes%2C%20please%20describe%20them%20here%20--%3E%0A%0A%3C%21--%20Thank%20you%20for%20helping%20improve%20our%20documentation%21%20--%3E">
    Found an error or have suggestions? Let us know
  </Card>
</CardGroup>
