# For each

Run one operation, or a body of steps, once for every item in a list. Items run in parallel, and each item’s result is kept under its position in the list.

Loop step, For each:

- Step kind: `ForEach`
- Items: `Trigger.Invoices`
- Each: `Invoice`
- Operation: `PurchaseOrders`

## Inputs

- `Items` (expression, required): The list to run over, read from the run’s values, such as { Field: Trigger.Invoices }. A value that is not a list fails the attempt.
- `Each` (string): The name each item is read under inside Input or the body. Required with Steps, and with an Input that reads the item.
- `Operation` (string): A declared tool or workflow run once per item. Give Operation or Steps, not both.
- `Input` (mapping): What each item’s operation is called with. Where absent, the item itself is the whole input.
- `Steps` (step[]): A body run once per item, starting from the item.
- `Output` (output contract): What each item’s value is narrowed to.
- `Attempts` (integer, default `1`): How many attempts the step may take. A retry runs only the items that failed.
- `OnExhausted` (policy): What happens when the attempts run out.

## Outputs

- `<index>` (any): Each item’s value under its position in the list: the operation’s result, or the last value its body produced. An empty list completes at once with an empty result.

## One operation per item

```yaml
- ForEach: Orders
  Items: { Field: Trigger.Invoices }
  Each: Invoice
  Operation: PurchaseOrders
  Input:
    number: { Field: Invoice.PurchaseOrder }
```

## A body of steps per item

```yaml
- ForEach: Review
  Items: { Field: Trigger.Documents }
  Each: Doc
  Steps:
    - ExecuteTool: Read
      Operations: [FetchDocument]
      Input:
        path: { Field: Doc.Path }
    - DeliverResults: HandOver
    - ModelCall: Judge
      Instructions: Judge the document against the review checklist.
      Output:
        Structured:
          type: object
          properties:
            Verdict: { type: string }
```

> **Every item must succeed**
> One failed item does not stop the others; they all run to an end. The step completes only when every item has succeeded, and a retry reruns only the failed items. Inside a body, `Exit` ends that item’s pass and the other items carry on.

## Related steps

- [Call a tool](/docs/workflows/steps/call-a-tool/): Call one tool once, with input you map.
- [Run a workflow](/docs/workflows/steps/run-a-workflow/): Run a whole workflow per item as the operation.
- [Invoice matching](/docs/workflows/examples/invoice-matching/): ForEach over a batch of invoices.
