> For the complete documentation index, see [llms.txt](https://docs.empowergpt.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.empowergpt.ai/workflows/block-types/loop.md).

# Loop

The **Loop** block allows you to repeat a set of workflow steps for each item in an array.

Use a Loop when you have multiple items—such as files, records, or other values—and want to apply the same workflow logic to each item individually.

When a **Start Loop** block is added to the Canvas, an **End Loop** block is automatically added. Place the blocks that should be repeated between the Start Loop and End Loop blocks.

<div data-with-frame="true"><figure><img src="https://2192711725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fc2KRGWrkyRO0iZN5Lyeg%2Fuploads%2FSoPadCuYkVO2ZBq1s8hU%2Fimage.png?alt=media&amp;token=aa8ec2e1-269a-4b06-bf27-1ce99778acfe" alt=""><figcaption></figcaption></figure></div>

### When to Use a Loop

Use a Loop whenever the same set of actions needs to be performed for multiple items.

Common use cases include:

* Processing multiple uploaded files
* Summarizing multiple documents individually
* Analyzing a list of records
* Processing multiple items returned by a previous block
* Performing the same AI operation for every item in an array

For example, if a user uploads multiple documents through a Form Trigger, a Loop can process each uploaded document separately:

**Form Trigger → Start Loop → Agent → End Loop → Output**

The Agent inside the Loop runs once for each file being processed.

***

## How a Loop Works

A Loop consists of two blocks:

**Start Loop**\
Defines the array that should be processed and controls the number of iterations.

**End Loop**\
Marks the end of the steps that should be repeated.

When you add a **Start Loop** block to the Canvas, EmpowerGPT automatically adds the corresponding **End Loop** block.

Any blocks placed between these two blocks become part of the Loop.

For example:

**Start Loop**\
↓\
**Agent**\
↓\
**End Loop**

If the selected array contains five items, the blocks inside the Loop are executed for each item in the array, subject to the configured maximum number of iterations.

***

## Configuration

The Start Loop block contains two main configuration fields:

<div data-with-frame="true"><figure><img src="https://2192711725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fc2KRGWrkyRO0iZN5Lyeg%2Fuploads%2FIXkgqNlUqfR5AqA49Gff%2Fimage.png?alt=media&amp;token=0d6bc89b-2c49-432b-9a43-59f540064c4d" alt="" width="329"><figcaption></figcaption></figure></div>

| **Field**              | **Description**                                                              |
| ---------------------- | ---------------------------------------------------------------------------- |
| **Array to Loop Over** | Selects the array containing the items that should be processed by the Loop. |
| **Max Iterations**     | Defines the maximum number of times the Loop is allowed to execute.          |

### Array to Loop Over

Use **Array to Loop Over** to select the collection of items that the Loop should process.

The array is typically provided by a previous block in the workflow.

For example, a Form Trigger with **Allow Multiple Files** enabled can provide multiple uploaded files. This array can then be selected in **Array to Loop Over**.

The Loop processes the items individually:

**Array**

If we have a field in the form trigger named as 'File' which allows multiple uploads and user wants to loop an agent through all the files, he can use loop array as:

{{form-trigger.file}}

**Loop execution**

`Iteration 1 → File 1`

`Iteration 2 → File 2`

`Iteration 3 → File 3`

Blocks inside the Loop can use the current item being processed during each iteration.

***

### Max Iterations

**Max Iterations** defines the maximum number of items that the Loop can process during a workflow run.

This acts as a limit on the number of times the blocks inside the Loop can execute.

For example, suppose:

* The selected array contains **20 items**
* Max Iterations is set to **10**

The Loop will execute for a maximum of **10 iterations**.

Set Max Iterations according to the expected size of your input array and the amount of processing required for each item.

> **Tip:** Use an appropriate Max Iterations value to prevent unexpectedly large arrays from causing unnecessary workflow executions.

***

## Example: Summarize Multiple Files

Suppose you want users to upload multiple documents and generate a summary for each document.

The workflow can be configured as:

**Form Trigger → Start Loop → Agent → End Loop → Output**

#### 1. Form Trigger

Add a **File** field and enable:

**Allow Multiple Files:** Enabled

The uploaded files are provided to the workflow as an array.

#### 2. Start Loop

Configure:

**Array to Loop Over:** Select the file field from the Form Trigger by pressing {{}}.

**Max Iterations:** Set the maximum number of files that should be processed.

#### 3. Agent

Place an Agent between the Start Loop and End Loop blocks.

Provide the current file from the Loop to the Agent and configure instructions such as:

> Review the current document and provide a concise summary of its main topics and key information.

The Agent executes for each item processed by the Loop.

#### 4. End Loop

The **End Loop** marks the end of the repeated workflow steps.

Once the Loop has finished processing the required items, the workflow continues to the next connected block.

The resulting structure is:

**Form Trigger**\
↓\
**Start Loop – Uploaded Files**\
↓\
**Agent – Summarize Current File**\
↓\
**End Loop**\
↓\
**Output**

***

## Example: Multiple Steps Inside a Loop

A Loop is not limited to a single block. Multiple blocks can be placed between Start Loop and End Loop when each item requires several processing steps.

For example:

**Start Loop**\
↓\
**Agent – Analyze Document**\
↓\
**Condition – Check Document Type**\
↓\
**Additional Processing**\
↓\
**End Loop**

Each item passes through the workflow logic contained within the Loop before the next item is processed.

***

## Best Practices

**Select an array as the input**\
The value configured in **Array to Loop Over** should contain multiple items that can be processed individually.

**Set an appropriate Max Iterations value**\
Choose a limit appropriate for your workflow to prevent unexpectedly large inputs from creating excessive iterations.

**Keep repeated processing focused**\
Only place blocks inside the Loop when they need to execute for every item.

**Consider AI usage**\
If an Agent is placed inside a Loop, the Agent may execute once for every processed item. A workflow processing many items can therefore consume significantly more AI credits than a single Agent execution.

**Test with a small array first**\
When building a Loop, test it with a small number of items before running it against larger arrays. This makes it easier to verify that each item is being processed as expected.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.empowergpt.ai/workflows/block-types/loop.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
