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

# Steps

> Run several agents in order, pass results between them, and fan out over a list.

A definition with `steps` runs other agents in order. Each step is a run of its own, with its own page, its own Learnings and its own questions for a person. The parent run holds the steps together and reports the output of the last step that ran.

A definition does the work itself or runs steps. It cannot have both `instructions` and `steps`.

## An example

```yaml theme={null}
agent: support-triage
input:
  ticket: number
steps:
  - id: triage
    agent: ./triage.yaml
    input:
      ticket: $input.ticket

  - id: refund
    agent: refund-requests
    when: $triage.output.kind == "late-order"
    input:
      ticket: $input.ticket

  - id: reply
    agent: draft-reply
    input:
      ticket: $input.ticket
      decision: $refund.output.decision
```

The `refund` step runs when triage called the ticket a late order. If it is skipped, `$refund.output.decision` reads as nothing in the `reply` step and is not an error.

## What a step names

`agent` is a saved agent's name, an inline definition, or a file path such as `./triage.yaml`. The `sensei` command and the SDK read a file in before they send the definition, and resolve relative paths against the file that names them. The service itself never reads files.

A step's `id` is a short lower-case name that starts with a letter. Later steps use it to read the step's output.

## Pass values between steps

In a step's `input`, a string that starts with `$` is a reference. Any other value is taken as written.

| Reference | Reads |
| - | - |
| `$input.ticket` | A field of the parent's input |
| `$triage.output.kind` | A field of an earlier step's output |
| `$item` | The current item, under `for_each` |

A step can read `$input` and the steps before it. A reference to a step that comes later is refused when the definition is checked.

## Run a step on a condition

`when` takes a condition. The step runs when the condition is true.

```yaml theme={null}
when: $triage.output.kind == "late-order" and not $triage.output.spam
```

Conditions compare references, quoted strings, numbers, `true`, `false` and `null` with `==` and `!=`, and combine with `and`, `or`, `not` and parentheses.

## Fan out over a list

`for_each` names a list from the input or from an earlier step. The step runs once per item, and `$item` is the item.

```yaml theme={null}
agent: translate-app
input:
  language: text
steps:
  - id: list
    agent: list-screens

  - id: translate
    agent: translate-screen
    for_each: $list.output.screens
    parallel: 4
    input:
      screen: $item
      language: $input.language
```

`parallel` is how many items run at once, from 1 to 10. It defaults to 1. A list can have up to 200 items.

## Steps or a batch

Steps and a [batch](https://docs.sense-lab.ai/sensei/runs/batches.md) do different work, and a batch can be one of the steps.

| | Steps | A batch |
| - | - | - |
| What it is for | Agents that run in order and pass results on | Many changes to one repository at once |
| What one unit is | Any agent | A change to the repository |
| Where each starts | Where the step before it left off | All from the same commit |
| What it adds | `when`, `for_each`, values passed between steps | The check that the changes fit together, and the pull requests |

`for_each` over an agent that changes a repository runs the agent once per item. It does not check the changes against each other. For several changes to one repository, use a batch.

## A batch as a step

A step's agent can be a batch. The batch names the input field that holds its tasks, and the step fills that field from an earlier step:

```yaml theme={null}
agent: weekly-chores
input:
  label: text
limit: { usd: 30, minutes: 120 }
steps:
  - id: list
    agent: list-chores            # its output has: changes: list
    input:
      label: $input.label

  - id: changes
    agent:
      repo: acme/api
      input:
        changes: list
      tasks: $input.changes
      parallel: 4
    when: $list.output.changes
    input:
      changes: $list.output.changes

  - id: note
    agent: post-note
    input:
      pull_requests: $changes.output.pull_requests
      summary: $changes.output.summary
```

The step is a batch like one started on its own. Every task starts from the same commit, the verified changes are tested together, and the pull requests say how each change fits with the rest. The step's row on the run's page says how the tasks went and opens the batch.

The list is read when the step starts. It has 1 to 200 entries and each is text. A list with anything else in it fails the step, and the run says which entry. `when: $list.output.changes` skips the step when the list is empty.

The agent that lists the tasks is an agent with an `output`, such as one that reads your tracker with its tools. A step that changes a repository has no output of its own shape, so it cannot be the one that lists them.

### What the batch hands on

| Reference | Reads |
| - | - |
| `$changes.output.summary` | How it went, in a sentence: "6 of 8 verified. 5 fit together on main at a1b2c3d." |
| `$changes.output.status` | `verified` or `unverified` (part verified) |
| `$changes.output.pull_requests` | The pull requests, as a list of addresses |
| `$changes.output.tasks` | One entry for each task: `index`, `task`, `status`, and where there is one `pr_url`, `together` and `note` |
| `$changes.output.verified`, `.total`, `.fit` | How many verified, how many there were, and how many fit together |

A batch where no task verified fails, and so does the run. A batch that is part verified does not: the steps after it run, and can read which tasks made it.

### What it may spend

The run's `limit` covers all of its steps, the batch and its tasks included: at most $50 and 120 minutes. The batch's own ceiling is its `total` or what the run has left, whichever is lower, less up to $0.50 kept for the check together. At the batch's ceiling its unfinished tasks stop and what verified is kept and delivered. At the run's limit the whole run stops.

A batch that needs more than a run with steps may spend is started on its own.

## When a step waits

A step that reaches an `ask_first` action waits the same way any agent run does. The parent shows **Waiting for you** and links to the step. Your answer goes to the step, and the parent carries on from there.

## Limits

| Limit | Value |
| - | - |
| Steps in one definition | 20 |
| Depth of steps inside steps | 3 |
| Items under `for_each` | 200 |
| `parallel` | 10 |

A `limit` on the parent covers the whole run. Each step is capped by what the parent has left.

Keys that describe one computer belong on the agent a step runs: `computer`, `repo`, `ref`, `image`, `tools`, `ask_first` and `check` are refused beside `steps`.

## Mark a run with steps

**Right** applies to every step. **Wrong** asks which step was wrong, and applies to the last step that ran when you leave it at **Not sure**. From the terminal:

```bash theme={null}
sensei outcome <run> wrong --step refund --note "Refunded a gift card order"
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.