# Agents and turns

> An agent works in turns. It starts when its inputs are ready, gets more turns from the board or from continue turns, and ends by calling finish.

An **agent** is one model session run by a **runner** process. It works in **turns**. One turn is one call into the SDK: the model reads its input, calls tools, and stops. Between turns the agent is Idle and costs nothing. The SDK session persists across turns, so the agent remembers what it has already read and done.

## Starting

- An agent with no `inputs` and no `depends_on` starts as soon as the run starts.
- An agent with `inputs` starts only after every agent it references has finished. The referenced outputs are passed into its first turn as JSON.
- `depends_on` works the same way but passes nothing.

Every reference in `inputs` or `depends_on` is an edge in the dependency graph, which must have no cycles. References in `writes`, `watches` and `done_when` are not edges. An agent also waits in Pending when `convoyd` is at `--max-processes`.

A `count` agent expands into instances named `worker[0]`, `worker[1]`, and so on; a `for_each` agent into instances named by key, such as `researcher[nvidia]`. Referencing `agent.researcher` in `inputs` or `depends_on` waits for all instances.

## Taking more turns

After its first turn, an agent gets another turn in exactly two cases:

1. A message is published to a topic in its `watches`.
2. A message is published with `to` set to the agent's name, on any topic.

The input of that turn is an unread summary of the board, plus any messages addressed to the agent, in full. The agent pulls what else it needs with `read_topic`. See [topics and the board](/docs/concepts/board/).

### Continue turns

An agent with an empty `watches` list works until it calls `finish`. Whenever it ends a turn without finishing, it immediately gets a **continue turn**, carrying any messages addressed to it, until it calls `finish` or reaches `max_turns`. This is how a single agent works through a long job, and it is the same rule in a team or alone. It also means an agent with empty `watches`, no `output` and the default `max_turns` can loop for 50 turns; `convoy validate` warns, and long jobs should set their own limit.

## Finishing

Calling `finish(output)` ends the agent. If the agent declares an `output` shape, the payload is checked against it and a mismatch is returned to the model as an error, so it can correct itself. After `finish` the agent is Done, `agent.<name>.output` resolves to the payload, and its runner exits on its next `wake`, which revokes its token.

Agents that watch topics usually never call `finish`. The server stops them when the run ends. An agent is also stopped when it reaches `max_turns`.

## Output shapes

`output` is an object type: `{ approved = bool, score = number }`. It is required when `done_when` or another agent's `inputs` reads a field of this agent's output. Reading `.approved` from an agent that declares no `output`, or a field not in its shape, is a validation error. Without `output`, `finish` accepts any JSON.

```hcl
output = { approved = bool, score = number }
```

## Agent states

| State | Meaning |
| --- | --- |
| Pending | Waiting for `inputs` or `depends_on`, or for a free process slot |
| Running | In a turn |
| Idle | Between turns, waiting on the board |
| Stuck | Running, but no `report_progress` for 20 minutes. Not terminal; the overseer is woken |
| Done | Called `finish` |
| Stopped | Stopped by `convoy stop`, its parent, the overseer, the run ending, or `max_turns` |
| Failed | The runner exited without `finish`. No restart in the MVP |

The server treats a runner's `wake` call as "turn ended" and its response as "turn started", so it always knows whether an agent is Running or Idle. Details are in [the controller](/docs/runtime/controller/) and [the runner](/docs/runtime/runner/).

## Progress

Agents call `report_progress(summary)` after each meaningful step. The summary is what [`convoy status`](/docs/cli/status/), other agents and the overseer see, and it resets the Stuck timer.
