# swarm

> The swarm block names the swarm, states its goal, and sets the rules that end it, including done_when, max_runtime, max_cost_usd, and the overseer.

The `swarm` block declares the goal and the stopping rules for a run. Every configuration has exactly one, in any file. Its label is the swarm's name, which `convoy run`, `convoy status`, and the API use.

## Minimal example

```hcl title="main.cvy"
swarm "hello" {
  goal        = "Write a haiku about convoys"
  done_when   = agent.poet.output.done == true
  max_runtime = "10m"
}
```

This validates with one warning: `max_cost_usd` is not set ([TS061](/docs/dsl/validation/#ts061)). It also needs a `poet` agent that declares `output = { done = bool }`.

## Full example

```hcl title="main.cvy"
swarm "vending_year" {
  goal         = <<-EOT
    Run the Vending-Bench 2 machine for a full simulated year and end with the
    highest bank balance possible without going bankrupt.
  EOT
  done_when    = agent.operator.output.day >= 365 || agent.operator.output.bankrupt == true
  max_runtime  = "168h"
  max_cost_usd = 300
  overseer = {
    provider  = provider.anthropic
    model     = "claude-sonnet-5"
    can_spawn = false
  }
}
```

## Attributes

| Attribute | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `goal` | string | yes | | The goal in plain language. Shown to every agent and to the overseer. |
| `done_when` | bool expression | yes | | When true, the run is Done. Evaluated each time a referenced agent finishes. |
| `max_runtime` | duration string | yes | | Wall-clock limit for the whole run. |
| `max_cost_usd` | number greater than 0 | no | unset | Dollar budget for the whole run, counting every agent and the overseer. |
| `overseer` | object with `provider`, `model`, `can_spawn` | no | convoyd's flags | The overseer's provider and model, and whether it may create agents. |

### goal

A string that states what the swarm is for. It is placed in every agent's system prompt and in the overseer's, so write it for a model that has no other context. Use a heredoc for anything longer than a sentence. `goal` is static: it cannot reference other blocks.

### done_when

A boolean expression that decides when the swarm is finished. It may only read agent outputs: `agent.<name>.output.<field>`, combined with comparison and logic operators (`==`, `!=`, `>=`, `&&`, `||`, `!`).

```hcl
done_when = agent.critic.output.approved == true
done_when = agent.reporter.output.held_out_gain >= 0.10
done_when = agent.operator.output.day >= 365 || agent.operator.output.bankrupt == true
```

To read one instance of a `for_each` agent, index it by key: `agent.runner["shard_a"].output.passed`. See [References and functions](/docs/dsl/expressions/#for_each-agents).

How it is evaluated:

- The server evaluates it every time an agent that it references finishes. It is not polled.
- Agents that have not finished are unknown values. An expression that depends on an unknown value is unknown, and the server treats unknown as "not yet".
- A known `true` ends the run with status Done, and every running agent is stopped.
- A known `false` means not done.
- An evaluation error, such as comparing a number with a string, is reported once as a swarm-level event and treated as `false`.
- If every agent finishes and `done_when` is still false, the run ends as Failed.

Every agent whose output `done_when` reads must declare an `output` shape that includes the field you read. Otherwise validation fails ([TS021](/docs/dsl/validation/#ts021), [TS022](/docs/dsl/validation/#ts022)). Referencing anything other than an agent output, such as a topic or a provider, is [TS023](/docs/dsl/validation/#ts023).

`done_when` references are not dependency-graph edges. The swarm does not wait for the referenced agents to start; it just checks the condition when they finish.

### max_runtime

A [duration](/docs/dsl/#durations) string such as `"4h"`. When the limit passes, every agent is stopped and the run's status is Stopped with reason `max_runtime`. It is required so that every run has an upper bound even when no budget is set.

### max_cost_usd

A number greater than zero (US dollars) bounding spend across every agent and the overseer. Spend is computed by the server from the token usage each runner reports after each turn, priced with Convoy's built-in table or the provider's own [`price`](/docs/dsl/provider/#price).

When spend reaches the limit, every agent is stopped, the run is Stopped with reason `budget`, and outputs already produced are kept. Turns already in flight finish first, so actual spend can exceed the cap by up to one turn per running agent. `convoy status` reports actual spend.

If `max_cost_usd` is set, every model used by an agent, and the overseer's model if declared, must have a price, or validation fails with [TS060](/docs/dsl/validation/#ts060). If it is not set, validation warns ([TS061](/docs/dsl/validation/#ts061)) because only `max_runtime` bounds the run. A value of zero or less, or a non-number, is [TS062](/docs/dsl/validation/#ts062). See [Budgets and limits](/docs/concepts/budgets/).

### overseer

The overseer is the built-in agent that judges whether the swarm is making progress and can add, redirect, or stop agents. This attribute chooses its model.

```hcl
overseer = { provider = provider.anthropic, model = "claude-sonnet-5" }
```

| Key | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `provider` | provider reference | yes | | A `provider.<name>` reference. |
| `model` | string | no | the provider's `model` | Model name, passed to the SDK unchanged. Required unless the provider sets `model`. |
| `can_spawn` | bool | no | `true` | Whether the overseer may call `create_agent`. |

`model` may be left out when the provider block sets its own [`model`](/docs/dsl/provider/#model); the overseer then runs on that one, as an agent with no `model` does:

```hcl
overseer = { provider = provider.llm }
```

With neither, the overseer has no model, which is [TS004](/docs/dsl/validation/#ts004).

Any other key is an error with a suggestion. When `overseer` is omitted, convoyd uses its `--overseer-provider` and `--overseer-model` flags. If neither is set, the server rejects the run with HTTP 422, code `overseer_missing`. This is not a `validate` error, because validation cannot know the server's flags. If the overseer comes from the flags and `max_cost_usd` is set, the server checks the overseer model's price and rejects the run with `unpriced_model` if it has none. See [The overseer](/docs/concepts/overseer/).

## Limits that are not in the language

There is no limit on the number of agents in the language. How many run at once is a property of the machine, set by `convoyd --max-processes`. Agents beyond it wait in Pending.

## What validate checks

| Code | Severity | Rule |
| --- | --- | --- |
| [TS001](/docs/dsl/validation/#ts001) | error | Exactly one `swarm` block |
| [TS002](/docs/dsl/validation/#ts002) | error | `goal`, `done_when`, or `max_runtime` is missing |
| [TS003](/docs/dsl/validation/#ts003) | error | An attribute has the wrong type, including a malformed duration |
| [TS004](/docs/dsl/validation/#ts004) | error | `overseer` sets no `model`, and neither does its provider |
| [TS010](/docs/dsl/validation/#ts010) | error | `overseer.provider` or a name in `done_when` does not exist |
| [TS021](/docs/dsl/validation/#ts021) | error | `done_when` reads a field of an agent with no `output` |
| [TS022](/docs/dsl/validation/#ts022) | error | `done_when` reads a field not in the agent's `output` |
| [TS023](/docs/dsl/validation/#ts023) | error | `done_when` references something other than `agent.*.output` |
| [TS060](/docs/dsl/validation/#ts060) | error | `max_cost_usd` is set and a model has no price |
| [TS061](/docs/dsl/validation/#ts061) | warning | `max_cost_usd` is not set |
| [TS062](/docs/dsl/validation/#ts062) | error | `max_cost_usd` is not a number greater than zero |

Unknown attributes, and unknown keys inside `overseer`, are also errors.
