Build a workflow
A workflow runs a list of steps in order, passing results forward — and can fan a step out over a list, skip a step, or retry one that fails.
A workflow is a list of steps that run one after another. Each step can use the result of the steps before it. Use a workflow when you always want the same sequence to happen, in the same order.
Three optional step settings add control flow without turning the file into a
program: for_each repeats a step for every item in a list, when skips a
step unless a condition holds, and retry runs a flaky step again.
Make a workflow
workflow estimate_age
description: Find the year behind a clue, then guess the age.
input:
clues: string
output:
estimate: object
steps:
- agent: researcher
prompt: "What year does this clue point to: {clues}"
into: year
- prompt: |
Guess the person's age.
Clues: {clues}
Year: {year}
into: estimateHow steps connect
Each step runs a prompt and can save its answer with into. Later steps can use
that saved answer by writing its name in curly braces.
In the example above:
- The first step asks the researcher for a
yearand saves it asyear. - The second step uses both the original
{clues}and the{year}from step one.
So {clues} comes from the workflow's input, and {year} comes from the step
that ran before. This is how a workflow passes results forward. When a saved
value is a list or a structured result, the placeholder shows it as JSON.
Repeat a step for every item
for_each names a list — a workflow input declared as array, or an earlier
step's into — and runs the step once per item. Inside the step, {item} is
the current element and {index} is its position, counting from zero.
workflow estimate_ages
agent: age
input:
clues: array
steps:
- agent: researcher
for_each: clues
prompt: "What year does this clue point to: {item}"
into: yearsThe items run up to four at a time, and into receives a list of the
results in the same order as the input, however quickly each one finished. An
empty list makes the step do nothing and saves []. A list longer than 100
items is refused before anything runs.
Give the workflow a list on the command line:
nt run estimate_ages -i '{"clues":["bought the first iPhone at 22","retired last year"]}'Skip a step
when runs a step only if a condition holds. The condition is checked once,
before the step, against the values saved so far. A skipped step leaves its
into unset, so a later step can tell that it did not run.
- when: years is not empty
prompt: "Estimate an age from these years: {years}"
into: estimateThere are exactly six shapes of condition, and no others:
| Condition | Runs the step when… |
|---|---|
name | name is set and not blank, empty, false, or 0. |
not name | the opposite. |
name is empty | name is unset, blank, or an empty list or map. |
name is not empty | the opposite. |
name == value | name rendered as text equals value (quotes optional). |
name != value | the opposite. |
name must be a workflow input or an earlier step's into — nt validate
checks this before anything runs.
Retry a flaky step
retry gives a step extra attempts, up to five. An attempt fails when the
model call errors, or when the step's agent declares output and does not
return a valid structured result. Under for_each, each item retries on its
own. When the budget is spent, the workflow stops with an error that names the
step.
- when: years is not empty
retry: 2
prompt: "Estimate an age from these years: {years}"
into: estimateWith retry set, a missing structured result is treated as a failure even on
the last attempt. Without it, a step keeps today's forgiving behavior and saves
the model's plain text instead.
What a workflow needs
| Setting | What it does |
|---|---|
description | A short note about the workflow. |
agent | The default agent for steps that do not name one. |
input | What you give the workflow to start. |
output | The shape of the final result. |
steps | The list of steps to run, in order. |
Each step can have:
| Step setting | What it does |
|---|---|
agent | Which agent runs this step. |
prompt | What to ask, with {names} filled in from earlier. |
skill | An optional skill to add for this step. |
into | A name to save this step's answer under. |
for_each | A list to repeat the step over; {item} and {index} inside. |
when | A condition that must hold for the step to run. |
retry | Extra attempts, 0 to 5, when the step fails. |
See the flow
nt graph shows every step with its control flow:
▶ workflow estimate_ages
1. researcher [for each clues] → years
2. age [when years is not empty · retry 2] → estimateAnd nt run … --show-tool-calls names each item, skip, and retry on the
thinking line as it happens.
Run a workflow
Workflows run with the same command as agents:
nt run estimate_age -i '{"clues":"bought the first iPhone at 22"}'