Overview
For Loops allow your agent to repeat actions for each item in a list or a specific number of times. This saves you from having to create multiple copies of the same steps and makes your workflow more efficient.Use Cases
- Process Multiple Items: Apply the same steps to each item in a list
- Repeat Actions: Perform the same task multiple times
- Build Cumulative Results: Gather information across multiple iterations
- Process User Lists: Handle user-provided lists of items or requests
How For Loops Work
A For Loop repeats the same actions for each item in your list. Think of it like an assembly line:- The loop takes one item from your list
- It puts that item in a variable you can use
- It performs all the actions you’ve added to the loop
- Then it takes the next item and repeats the process until it’s gone through every item
Creating a For Loop
Step 1. Add the For Loop Action
- In the Actions tab, click “Add action”
- Select “For Loop” from the Workflow & logic section
Step 2. Configuration Fields
- List to loop over
- Description: Enter a list to loop over or a fixed number of iterations.
- Example:
- A variable containing a list (like )
- A number of times to repeat (like 3)
- A JSON array (like [“item1”, “item2”, “item3”])
- Required: Yes
- Loop Index Variable Name
- Description: Name the variable that will count your loops (this counter starts at 0 and increases by 1 each time through the loop)
- Example: loop_index
- If you’re looping 3 times, this variable will be 0 during the first loop, 1 during the second loop, and 2 during the third loop
- Validation: Only letters, numbers, and underscores (_) are allowed.
- Required: Yes
Step 3. Add Actions Inside the Loop
After your For Loop action, add the steps you want to repeat for each item.Step 4: End the Loop
After all the actions you want to repeat, add an “End If/Else/For Statement” action to mark where your loop ends.Example: For Loop Example Agent
See this simple example agent which uses a For Loop:- Gets a list of 3 topics from the user
- Loops through each topic, one by one
- For each topic:
- Uses AI to generate an explanation
- Adds the explanation to a cumulative output
- Displays all topic explanations to the user when complete