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

# 08.3 workflow loop

# Workflow Loop Template

The **Workflow Loop** template demonstrates how a workflow can repeat a sequence of nodes until a specified condition becomes false.

Instead of duplicating workflow steps, a Loop node repeatedly executes the same workflow branch while updating the workflow context after each iteration.

This template introduces iterative execution into BindAI workflows.

***

# Purpose

This template demonstrates how to:

* repeat workflow execution
* evaluate a loop predicate
* update workflow variables
* exit the loop safely
* build iterative automation

It extends the Condition template by allowing execution to repeat instead of choosing only one path.

***

# Workflow Structure

A loop creates a cycle in the workflow.

```text id="wl1" theme={null}
Start

↓

Loop

↓

Agent

↓

Update Variables

↓

Loop
```

Execution continues until the loop condition evaluates to **False**.

***

# Execution Flow

Each iteration follows the same sequence.

```text id="wl2" theme={null}
Evaluate Loop

↓

Execute Body

↓

Update Context

↓

Evaluate Again
```

The workflow exits only when the predicate returns `False`.

***

# Loop Predicate

The Loop node evaluates a predicate before each iteration.

Conceptually:

```text id="wl3" theme={null}
counter < 3
```

If the predicate returns:

* **True** → execute another iteration
* **False** → continue to the workflow exit

***

# Workflow Variables

Loops usually depend on workflow variables.

Example:

```text id="wl4" theme={null}
counter = 0

↓

Loop

↓

counter = 1

↓

Loop

↓

counter = 2

↓

Loop

↓

counter = 3

↓

Exit
```

Each iteration updates the shared workflow context.

***

# Example

Suppose an AI agent processes one item per iteration.

```text id="wl5" theme={null}
Initialize

↓

Loop

↓

Process Item

↓

Next Item

↓

Repeat
```

The workflow continues until every item has been processed.

***

# Shared Context

Every iteration shares the same workflow context.

```text id="wl6" theme={null}
Iteration 1

↓

Workflow Context

↓

Iteration 2

↓

Workflow Context

↓

Iteration 3
```

Variables accumulate across iterations unless explicitly reset.

***

# Loop Exit

Eventually the predicate evaluates to false.

```text id="wl7" theme={null}
Condition

↓

False

↓

Continue Workflow

↓

End
```

Execution leaves the loop and proceeds normally.

***

# Polling Example

Loops are useful when waiting for an external process.

```text id="wl8" theme={null}
Check Status

↓

Completed?

↓

No

↓

Wait

↓

Repeat
```

Once the external operation finishes, the workflow exits the loop.

***

# Batch Processing

A common use case is processing collections.

```text id="wl9" theme={null}
Load Items

↓

Loop

↓

Process Current Item

↓

Next Item

↓

Finished
```

This avoids creating separate workflow branches for every item.

***

# Loop vs Retry

Although they both involve repeated execution, loops and retries solve different problems.

| Loop                     | Retry                         |
| ------------------------ | ----------------------------- |
| Business logic           | Failure recovery              |
| Controlled by predicates | Triggered by execution errors |
| Updates workflow state   | Re-executes failed operation  |
| Part of workflow design  | Reliability mechanism         |

Retries recover from temporary failures.

Loops intentionally repeat workflow logic.

***

# Preventing Infinite Loops

Every loop should eventually terminate.

A good pattern is:

```text id="wl10" theme={null}
Modify State

↓

Evaluate Condition

↓

Eventually False
```

Without updating the state used by the predicate, the workflow may never exit.

***

# Related Templates

This template prepares you for:

* Workflow Parallel
* Workflow Human
* Workflow Retry
* Workflow Timeout

Many production workflows combine loops with these patterns.

***

# Best Practices

* Always define a clear exit condition.
* Update workflow variables during each iteration.
* Keep loop bodies focused on one responsibility.
* Avoid deeply nested loops.
* Use retries instead of loops for transient failures.
* Monitor long-running loops in production.

***

# Summary

The **Workflow Loop** template demonstrates how BindAI workflows can repeat execution until a predicate becomes false.

By combining shared workflow context with iterative execution, loops provide a clean and reusable way to process collections, poll external systems, and implement business logic without duplicating workflow nodes.
