How-To Guides

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.

Build a workflow

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

workflows.nt
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: estimate

How 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:

  1. The first step asks the researcher for a year and saves it as year.
  2. 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: years

The 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: estimate

There are exactly six shapes of condition, and no others:

ConditionRuns the step when…
namename is set and not blank, empty, false, or 0.
not namethe opposite.
name is emptyname is unset, blank, or an empty list or map.
name is not emptythe opposite.
name == valuename rendered as text equals value (quotes optional).
name != valuethe 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: estimate

With 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

SettingWhat it does
descriptionA short note about the workflow.
agentThe default agent for steps that do not name one.
inputWhat you give the workflow to start.
outputThe shape of the final result.
stepsThe list of steps to run, in order.

Each step can have:

Step settingWhat it does
agentWhich agent runs this step.
promptWhat to ask, with {names} filled in from earlier.
skillAn optional skill to add for this step.
intoA name to save this step's answer under.
for_eachA list to repeat the step over; {item} and {index} inside.
whenA condition that must hold for the step to run.
retryExtra 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] → estimate

And 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"}'

Next