Skip to content

for each

for each

Iterate over a collection, executing one or more steps for each item. The for each block processes each element of a list and collects the results into an output array. Inside the loop body, loop.item references the current element and loop.index gives the zero-based position.

When to use

Use for each when you need to:

  • Process each item in a list with the same step sequence
  • Transform a collection by running LLM classification on each element
  • Send notifications to a list of recipients
  • Apply the same effect operation to multiple inputs

Use compute with .map() or .filter() for pure in-memory transformations. Use launch with items for fire-and-forget parallel processing where you do not need to collect results. Use together for running independent steps concurrently (not iterating over a collection).

Syntax

for each <variable> in <collection_expression>
<steps>

The collection expression is evaluated once before iteration begins. Each iteration executes all nested steps sequentially with loop.item (or the named variable) bound to the current element.

Alternative syntax (legacy)

for_each <collection_expression>
<steps>

In this form, use loop.item and loop.index to access the current element and position.

Configuration

ConfigRequiredDescription
<variable>Yes (in for each var in expr form)Name bound to the current item in each iteration.
<collection>YesExpression that evaluates to a list.

Accessing loop data

ReferenceWhat it accesses
loop.itemCurrent element in the iteration
loop.indexZero-based position of the current element
Named variableSame as loop.item when using for each var in expr syntax

Examples

Classify each item in a list

machine batch_classifier
accepts
emails as list, is required
responds with
classifications as list
implements
for each email in input.emails
ask classify, using: "anthropic:claude-haiku-4"
with task "Classify this email as spam or not_spam.\n\nEmail: ${email.body}"
returns
label as text
confidence as number
assuming
label: "not_spam"
confidence: 0.95
compute collect_results
{classifications: steps.classify.results}

Process with compute steps

for each item in input.orders
compute process_order
{
order_id: item.id,
total: item.price * item.quantity,
index: loop.index
}

Mixed steps in loop body

machine document_processor
accepts
documents as list, is required
implements
for each doc in input.documents
ask extract_summary, using: "anthropic:claude-haiku-4"
with task "Summarize this document in one sentence.\n\nDocument: ${doc.content}"
returns
summary as text
assuming
summary: "A brief summary of the document."
remember store_summary
content: doc.content
collection: "document_summaries"
metadata: {
title: doc.title,
summary: steps.extract_summary.summary,
processed_at: now()
}

Parallel processing

for each url in input.urls
action fetch http
url: loop.item
timeout: 5000

By default, iterations run sequentially. The runtime may parallelize iterations when steps within the loop body are independent (no cross-iteration data dependencies).

Governance

Steps inside a for each block are governed individually on each iteration. An ask step inside a loop checks permissions, records to the behavioral ledger, and tracks costs for every single invocation. This means:

  • A loop over 100 items with an ask ... using step produces 100 governance decisions and 100 ledger entries
  • Token budgets are checked before each LLM call, not once for the entire loop
  • If a permission check fails mid-iteration, the loop halts (unless wrapped in on failure)
  • Cost tracking accurately reflects the total cost of all iterations

The for each block itself is a control-flow construct and does not require its own permissions. The governance burden comes from the steps inside it.

Translations

LanguageKeyword
Englishfor each
Spanishpara cada
Frenchpour chaque
Germanfur jedes
Japaneseそれぞれ
Chinese每个
Korean각각

See also

  • compute - Use .map() and .filter() for pure transformations
  • launch - Fire-and-forget for collections (no result collection)
  • together - Run independent steps concurrently
  • implements - The section where for each blocks live