Loop

Repeat blocks multiple times with iteration control

The Loop block is a container that executes the blocks inside it repeatedly. It is not a regular block with its own source file -- it is a special wrapper node managed by the executor's LoopBlockHandler. Drag blocks into the loop container and they will run once per iteration.

Loop blocks are container nodes. Drag other blocks inside them on the canvas to define what runs each iteration.

Loop Types

For Loop -- runs a fixed number of times. The default iteration count is 5 if not specified.

Example: Run 3 times
- Iteration 1 (index 0)
- Iteration 2 (index 1)
- Iteration 3 (index 2)

ForEach Loop -- iterates over each item in an array or object. The collection must be non-empty; an empty collection throws an error.

Example: Process ["apple", "banana", "orange"]
- Iteration 1: currentItem = "apple"
- Iteration 2: currentItem = "banana"
- Iteration 3: currentItem = "orange"

For objects, each iteration receives a [key, value] pair.

While Loop -- evaluates a condition before each iteration. If the condition is false on the first check, the body never executes.

While and Do-While loops have no built-in iteration cap. They rely on the condition becoming false (or the workflow timeout) to stop.

Do-While Loop -- executes the body at least once, then evaluates the condition after each iteration. Useful when you need at least one pass before checking.

How It Works

The executor's LoopBlockHandler manages iteration control:

  1. Initialize -- the loop counter starts at 1. For forEach loops, the collection is resolved (JSON, variable references, or expressions).
  2. Check continuation -- for/forEach compare the counter against the collection length or iteration count. while/doWhile evaluate the condition expression using the same engine as the Condition block.
  3. Activate children -- blocks connected to the loop's internal start handle are placed on the active execution path.
  4. Collect results -- each iteration's outputs are aggregated into loop.results.
  5. Increment and repeat -- the counter advances and step 2 runs again. When the loop finishes, blocks connected to the loop's exit handle are activated.

Iteration Variables

Inside the loop body, the following variables are available:

VariableTypeDescription
loop.indexnumberZero-based index of the current iteration
loop.currentItemanyThe current item (ForEach loops) or loop context (While loops)
loop.itemsarray/objectThe full collection being iterated (ForEach only)

After the loop completes, downstream blocks can access:

VariableTypeDescription
loop.resultsarrayOrdered array of all iteration outputs

Configuration

ParameterApplies toDescription
Loop TypeAllfor, forEach, while, or doWhile
IterationsForNumber of times to repeat (default: 5)
CollectionForEachArray or object to iterate. Supports JSON literals, <block.output> references, and expressions.
ConditionWhile / Do-WhileA boolean expression evaluated each iteration (e.g., <variable.i> < 10)

Example Use Cases

Processing API Results

  1. An API block fetches a list of customer records.
  2. A ForEach loop iterates over each customer.
  3. Inside the loop, an Agent analyzes the customer data and a Function stores the result.
  4. After the loop, a Response block returns the aggregated loop.results.

Iterative Refinement

  1. A For loop set to 5 iterations generates content variations.
  2. Inside the loop, an Agent produces a draft and an Evaluator scores it.
  3. After the loop, a Function selects the highest-scoring variation from loop.results.

Limitations

Container blocks (Loops and Parallels) cannot be nested inside each other. You cannot place a Loop inside another Loop, or a Parallel inside a Loop. If you need multi-dimensional iteration, restructure your workflow to use sequential loops or process data in stages.

Loops execute iterations sequentially. For concurrent execution of independent tasks, use the Parallel block instead.

Best Practices

  • Set reasonable iteration counts for For loops to avoid long-running workflows. The default is 5.
  • Use ForEach for collections -- it automatically sizes to the collection length and gives you currentItem.
  • Always update your condition variable inside While/Do-While loops. Forgetting to change the variable the condition depends on creates an infinite loop (bounded only by the workflow timeout).