# Constant Loop

Executes a nested set of nodes a fixed number of times. Unlike the Loop node which iterates over an array, Constant Loop iterates N times based on a configured count. Each iteration receives the current index (0 to N-1) as input.

Use this for fixed-repeat operations like generating N samples, retrying an operation a specific number of times, or paginating through API results a known number of pages.

## How It Works

You configure a `count` value. The node executes the connected body nodes exactly `count` times, passing the iteration index (0, 1, 2, ...) to each iteration. The `input` socket is required for graph flow; the body receives the current index from the `loop` output.

After all iterations complete, the collected results flow to the `output` socket as an array.

**Example**:
```
Count: 3
Loop body: Multiply index by 10
Output: [0, 10, 20]
```

Iterations are **strictly sequential**. Iteration 1 doesn't start until iteration 0 finishes. This ensures predictable execution order.

## Configuration

**Count** (required): The number of iterations to execute. Must be at least 1. Each iteration receives its index (0 to count-1) as input.

## Inputs & Outputs

**Input**: `input` (required) - Graph-flow input that starts the fixed loop. It is not passed to the body.

**Outputs**:
- **loop**: The current iteration index (0, 1, 2, ...). Connect your processing logic here.
- **output**: An array of results after all iterations complete.

**Example flow**:
```
Count: 3
Loop body: Generate report for week {index + 1}
Output: ["Week 1 Report", "Week 2 Report", "Week 3 Report"]
```

## Comparison with Loop Node

| Feature | Loop Node | Constant Loop Node |
|---------|-----------|-------------------|
| Input | Array of items | `input` (required, starts loop) |
| Iteration value | Array item | Index (0, 1, 2, ...) |
| Use case | Process each item in data | Fixed N operations |
| Config | none | count |

Choose **Loop Node** when you have an array of data to process. Choose **Constant Loop Node** when you need to execute something exactly N times.

Use While Loop when the number of iterations is unknown and each iteration decides whether to continue.

## Example: Generate Weekly Reports

```
[Constant Loop: count=4]
    ↓ (loop - index 0, 1, 2, 3)
[Template: "Week {index + 1} Summary"]
    ↓
[AI Agent: Generate report content for that week]
    ↓ (output)
[Table: Insert] → 4 reports inserted
```

Generates exactly 4 weekly reports (Week 1 through Week 4). The loop index determines which week to generate.

## Example: API Pagination

```
[Constant Loop: count=5]
    ↓ (loop - index 0, 1, 2, 3, 4)
[HTTP GET: /api/items?page={index}]
    ↓
[Table: Insert] → store page results
    ↓ (output)
[Merge] → combine all 5 pages of results
```

Fetches exactly 5 pages of API results. Each iteration fetches a different page using the index.

## Example: Retry with Max Attempts

```
[Constant Loop: count=3]
    ↓ (loop - attempt 0, 1, 2)
[Branch: Check if already succeeded]
    ↓ (if not succeeded)
[HTTP POST: Submit data to external API]
    ↓ (output)
[Filter: Only keep successful results]
    ↓
[Limit: 1] → stop after first success
```

Attempts an operation up to 3 times. Combined with Branch and Filter nodes, you can implement retry logic with early termination on success.

## Data Piping Invariant

A downstream node only sees what is explicitly wired into it. The `loop` socket emits the iteration index; the `output` socket emits an array of collected results. Neither socket carries unrelated upstream data. If you need to upsert the original row after a side-effect inside the loop body, keep a parallel wire from the original data source to the write node, or reconverge branches with a `Merge` node in `select` mode. Do not place write/upsert nodes in thin air expecting data to appear from an unrelated upstream branch.

## Tips

- **Index starts at 0.** The first iteration receives index 0, the second receives 1, and so on. Use `{index + 1}` if you need 1-based numbering.
- **Iterations are sequential.** Each iteration completes before the next starts. No parallelism.
- **Count must be at least 1.** The minimum count is 1. Use the Loop node if you need zero-iteration behavior.
- **Results are always collected.** Even if your loop body produces side effects (like sending emails), the output array collects all return values.
- **Use with Limit node.** For retry scenarios, combine with Limit node to stop early when a condition is met.
- **Connect the loop output.** You must connect the `loop` output to at least one node for the body to execute. The workflow engine auto-detects the loop body from this connection.
- **Index as array accessor.** Since the index is just a number, you can use it with the Loop node's output to access specific items: `loop_output[index]`.

## Skipping an Iteration

A loop body that returns `skip(reason)` contributes nothing to the collected array. The iteration ran and decided it had nothing to add, so skip works as a filter and the output array comes back shorter than the iteration count.
