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

# 06.5 loops

# Loops

Loop nodes allow a workflow to repeat part of its execution until a specified condition is no longer satisfied.

Instead of duplicating workflow steps, loops execute the same sequence multiple times while evaluating a predicate after each iteration.

Loops are useful for iterative processing, polling operations, and retry-like business logic.

***

# What is a Loop?

A loop repeatedly executes a section of a workflow.

Conceptually:

```text id="loop1" theme={null}
Loop

↓

Execute Body

↓

Evaluate Condition

↓

Repeat or Exit
```

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

***

# Why Use Loops?

Many business processes require repeated execution.

Examples include:

* processing collections
* polling external services
* waiting for a condition
* batch operations
* iterative validation

Without loops, the workflow would require duplicated nodes.

***

# Loop Flow

A loop creates a cycle within the workflow.

```text id="loop2" theme={null}
Loop

↓

Body

↓

Condition

↙        ↘

Repeat    Exit
```

Each iteration uses the current workflow context.

***

# Loop Predicate

Like condition nodes, loop nodes evaluate a predicate.

Conceptually:

```python id="loop3" theme={null}
lambda context: True
```

The predicate determines whether another iteration should execute.

***

# Using Workflow Variables

Loop predicates typically evaluate workflow variables.

Example:

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

↓

Loop

↓

counter = 1

↓

Loop

↓

counter = 2

↓

Loop

↓

counter = 3

↓

Exit
```

Variables are updated during each iteration.

***

# Example

Suppose a workflow processes items in a collection.

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

↓

Loop

↓

Process Item

↓

Increment Index

↓

Repeat
```

The workflow continues until every item has been processed.

***

# Polling Example

Loops can also monitor external systems.

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

↓

Completed?

↓

No

↓

Wait

↓

Repeat
```

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

***

# Loops vs Retry

Although they appear similar, loops and retries solve different problems.

| Loop                             | Retry                          |
| -------------------------------- | ------------------------------ |
| Business logic                   | Failure recovery               |
| Controlled by workflow variables | Controlled by execution errors |
| Unlimited possibilities          | Limited retry attempts         |
| Part of workflow design          | Reliability mechanism          |

Retries automatically recover from failures.

Loops intentionally repeat workflow logic.

***

# Nested Loops

Large workflows may contain multiple loops.

```text id="loop7" theme={null}
Outer Loop

↓

Inner Loop

↓

Continue
```

While supported conceptually, deeply nested loops should be used carefully to maintain readability.

***

# Loop Exit

Every loop must eventually terminate.

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

↓

False

↓

Continue Workflow
```

A loop without a terminating condition may execute indefinitely.

***

# Preventing Infinite Loops

A good loop should always modify the state that controls its predicate.

Example:

```text id="loop9" theme={null}
counter += 1

↓

Condition

↓

Eventually False
```

Without state changes, the loop may never exit.

***

# Workflow Context

Each iteration shares the same workflow context.

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

↓

Workflow Context

↓

Iteration 2

↓

Workflow Context

↓

Iteration 3
```

Variables accumulate across iterations unless explicitly reset.

***

# Common Use Cases

Loop nodes are useful for:

* processing batches
* iterating over records
* pagination
* scheduled polling
* incremental processing
* approval retries
* repeated validation

***

# Best Practices

* Ensure every loop has a clear exit condition.
* Update workflow variables during each iteration.
* Keep loop bodies focused on one task.
* Avoid deeply nested loops.
* Consider retries instead of loops for transient failures.
* Monitor long-running loops in production workflows.

***

# Summary

Loop nodes enable BindAI workflows to repeat execution while a predicate remains true.

They provide a clean, reusable mechanism for iterative business logic while sharing workflow context across every iteration.

Well-designed loops simplify workflows and eliminate unnecessary duplication.
