> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trycordage.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Iterators & Batch Processing

> Process multiple inputs efficiently with text and media iterators

Iterators allow you to run a workflow multiple times with different inputs, enabling powerful batch processing capabilities.

## How Iterators Work

An iterator node takes a collection of items (text lines or media files) and executes the downstream workflow once for each item.

```mermaid theme={null}
flowchart LR
    A[Iterator<br/>N items] --> B1[Process<br/>Iteration 1]
    A --> B2[Process<br/>Iteration 2]
    A --> BN[Process<br/>Iteration N]
    B1 --> C[Collect Results]
    B2 --> C
    BN --> C
```

## Text Iterator

Processes each line of a multi-line text input as a separate workflow run.

### Setup

1. Drag a **Text Iterator** node onto the canvas
2. Enter multiple lines of text (one item per line)
3. Connect the iterator's output to downstream nodes
4. The workflow runs once per line

### Example: Multiple Prompts

Generate images for multiple prompts:

```mermaid theme={null}
flowchart LR
    subgraph Iterator["Text Iterator"]
        direction TB
        P1["A sunset beach"]
        P2["A mountain lake"]
        P3["A forest path"]
        P4["A city skyline"]
    end
    Iterator -->|current_text| Model[Model]
    Model --> Export[Export]
```

This generates 4 images, one for each prompt.

### Example: Style Variations

Apply different styles to the same base prompt:

```mermaid theme={null}
flowchart LR
    Input["Text Input<br/>'A portrait'"] --> Concat[Prompt Concatenator]
    subgraph Iterator["Text Iterator"]
        direction TB
        S1["in watercolor"]
        S2["as oil painting"]
        S3["in pencil sketch"]
        S4["in pop art style"]
    end
    Iterator --> Concat
    Concat --> Model[Model]
    Model --> Export[Export]
```

Generates 4 portraits with different artistic styles.

## Media Iterator

Processes each uploaded file (image or video) as a separate workflow run.

### Setup

1. Drag a **Media Iterator** node onto the canvas
2. Upload multiple files via the upload interface
3. Connect the iterator's output to downstream nodes
4. The workflow runs once per file

### Example: Batch Enhancement

Enhance a folder of images:

```mermaid theme={null}
flowchart LR
    subgraph Iterator["Media Iterator"]
        direction TB
        F1["photo1.jpg"]
        F2["photo2.jpg"]
        F3["photo3.jpg"]
        F4["..."]
    end
    Iterator -->|current_image| Upscale[Upscale Model]
    Upscale --> Export[Export]
```

### Example: Batch Processing Pipeline

Apply multiple operations to all images:

```mermaid theme={null}
flowchart LR
    A["Media Iterator<br/>(50 images)"] --> B[Resize]
    B --> C[Crop]
    C --> D[Filter]
    D --> E[Export]
```

All 50 images are processed through the same pipeline.

## Combining Iterators

### Text + Fixed Image

Apply multiple prompts to a single reference image:

```mermaid theme={null}
flowchart LR
    Image[Image Input] --> Model[Img2Img Model]
    subgraph Iterator["Text Iterator"]
        direction TB
        T1["make it blue"]
        T2["add sunset"]
        T3["winter scene"]
    end
    Iterator --> Prompt[Prompt]
    Prompt --> Model
    Model --> Export[Export]
```

### Media + Fixed Parameters

Process multiple images with the same settings:

```mermaid theme={null}
flowchart LR
    A[Media Iterator] --> C[Model]
    B["Text Input: 'enhance'"] --> C
    C --> D[Export]
```

## Batch Output

### Collected Results

Iterator runs produce multiple outputs. The Export node collects all results:

```
Export Results:
├── output_001.png (from iteration 1)
├── output_002.png (from iteration 2)
├── output_003.png (from iteration 3)
└── output_004.png (from iteration 4)
```

### Naming Patterns

Configure export filename patterns:

* `{index}` - Iteration number (001, 002, ...)
* `{input}` - Original input filename (for media)
* `{timestamp}` - Generation timestamp

Example: `batch_{index}_{timestamp}.png`

## Iterator Settings

### Iteration Control

| Setting               | Description                           |
| --------------------- | ------------------------------------- |
| **Max Iterations**    | Limit total runs (useful for testing) |
| **Batch Size**        | Process N items in parallel           |
| **Continue on Error** | Skip failed items instead of stopping |

### Performance

Iterators can run iterations:

* **Sequentially**: One at a time, safer for rate-limited APIs
* **In Parallel**: Multiple at once, faster but uses more resources

<Tip>
  Start with sequential execution to verify your workflow works, then enable parallel for production.
</Tip>

## Common Patterns

### Dataset Generation

Create training data:

```mermaid theme={null}
flowchart LR
    subgraph Iterator["Text Iterator<br/>(1000 prompts)"]
        direction TB
        P1["cat sitting"]
        P2["dog running"]
        P3["bird flying"]
        P4["..."]
    end
    Iterator --> Model[Generate Model]
    Model --> Export["Export<br/>→ 1000 labeled images"]
```

### Content Pipeline

Process user uploads:

```mermaid theme={null}
flowchart LR
    A["Media Iterator<br/>(user photos)"] --> B[Remove BG]
    B --> C[Resize]
    C --> D[Optimize]
    D --> E[Export]
```

### A/B Testing

Generate variations:

```mermaid theme={null}
flowchart LR
    Input["Text Input:<br/>'base prompt'"] --> ModelA[Model A]
    Input --> ModelB[Model B]
    ModelA --> ExportA[Export A]
    ModelB --> ExportB[Export B]
```

Run this with different base prompts using Text Iterator.

## Best Practices

<Tip>
  **Test with 1-2 items first** - Verify your workflow before processing hundreds of items.
</Tip>

<Tip>
  **Monitor credit usage** - Batch processing can consume credits quickly. Calculate estimated costs before large runs.
</Tip>

<Tip>
  **Use descriptive inputs** - Name your input files or text lines meaningfully for easier output tracking.
</Tip>

<Tip>
  **Handle errors gracefully** - Enable "Continue on Error" for large batches to avoid losing progress.
</Tip>

## Limits

| Limit                   | Value           |
| ----------------------- | --------------- |
| Max text lines          | 1000            |
| Max media files         | 500             |
| Max parallel iterations | 10              |
| Max total items per run | Depends on plan |

## Troubleshooting

### "Iterator timeout"

Large batches may exceed time limits:

* Reduce batch size
* Process in smaller chunks
* Contact support for higher limits

### "Partial results"

Some iterations failed:

* Check the error log for failed items
* Verify input data quality
* Retry failed items separately

### "Memory exceeded"

Too many parallel iterations:

* Reduce batch size
* Process sequentially
* Use smaller input files
