Thanks to visit codestin.com
Credit goes to docs.sim.ai

Reference

Loop

The Loop block is a container that runs the blocks inside it repeatedly — over each item in a collection, a fixed number of times, or while a condition holds. Drop blocks inside it, and they run once per iteration. Iterations run one after another; for concurrent work, use a Parallel block.

Loop Types

A For loop runs a fixed number of times. Set the iteration count.

A ForEach loop runs once per item in an array or object, exposing the current item as <loop.currentItem>.

A While loop runs as long as a condition is true. The condition is checked before each iteration, so the body may run zero times.

A Do-While loop runs the body once, then repeats while a condition is true. The condition is checked after each iteration, so the body always runs at least once.

Configuration

Set the loop type, then the value it needs:

  • Iterations — how many times to run (For loops)
  • Collection — the array or object to iterate (ForEach loops)
  • Condition — the boolean expression to check (While and Do-While loops)

Referencing loop data

References differ inside the loop and after it.

Inside the loop, read the current iteration with <loop.*>:

  • <loop.index> — the iteration number, starting at 0
  • <loop.currentItem> — the current item (ForEach only)
  • <loop.items> — the full collection (ForEach only)
const idx = <loop.index>;        // 0, 1, 2, ...
const item = <loop.currentItem>; // the current item

These are only available to blocks inside the loop container.

After the loop, read the collected results by the loop block's name — not <loop.*>:

// a loop named "Process Items"
const all = <processitems.results>; // [result1, result2, ...]

The name is normalized — lowercase, no spaces. <loop.*> only works inside.

Examples

A ForEach loop scores each review and collects the results; after the loop finishes, an Agent summarizes <loop.results>. The same container holds the blocks for any loop type:

  • Process API resultsAPI (Fetch) → Loop (ForEach) → Agent (Analyze) → Function (Store)
  • Generate variationsLoop (5×) → Agent (Generate) → Evaluator (Score) → Function (Select best)
  • Counter with WhileVariables (i=0) → Loop (While i<10) → Agent (Process) → Variables (i++)

Nesting and limits

Containers nest. You can place loops inside loops, parallels inside loops, and any combination, to build multi-dimensional workflows.

For and ForEach loops are capped at 1,000 iterations. While and Do-While loops run until their condition is false, with no hard cap — make sure the condition eventually becomes false so the loop ends.

Best Practices

  • Use ForEach for collections. When you have an array or object, ForEach is clearer than a For loop with manual indexing.
  • Keep iteration counts reasonable. Each iteration adds to total run time, since the loop runs sequentially.
  • Reach for Parallel when order doesn't matter. Independent work over a collection runs faster in a Parallel block.
  • Guarantee an exit. For While and Do-While loops, make sure the condition will become false.

Common Questions

For and ForEach loops are capped at 1,000 iterations. While and Do-While loops with a condition have no hard cap — they run until the condition is false. A Do-While without a condition falls back to a fixed count, also capped at 1,000. Always ensure While/Do-While conditions eventually become false to avoid infinite loops.
Sequentially, one after another. For concurrency across items, use the Parallel block, or nest a Parallel inside a Loop if you need both patterns.
Use <loop.currentItem> for the current item and <loop.index> for the zero-based iteration number. These are only available to blocks inside the loop container.
Reference the loop block by its normalized name (lowercase, no spaces) with <blockname.results>, which returns an array of all iteration results in order. Do not use <loop.*> outside the loop — that only works inside.
Yes. Loops and Parallels fully support nesting in any combination, and each nested container keeps its own scope and iteration context.
A While loop checks its condition before each iteration, so it can run zero times. A Do-While loop runs the body once, then checks the condition, so it always runs at least once.
The body is skipped and the loop outputs an empty results array. The workflow continues to whatever is connected after the loop.

On this page