# agent

> An agent block declares one agent, or several with count or for_each, including its model, prompt, tools, dependencies, topics, output shape, and limits.

An `agent` block declares one agent, several identical ones when `count` is set, or one per element of a set or map when `for_each` is set. An agent runs in turns, and one turn is one call into its SDK. It starts when its `inputs` and `depends_on` are satisfied, takes more turns when a topic it watches gets a message or a message is addressed to it, and ends when it calls `finish`. See [Agents and turns](/docs/concepts/agents/) for the runtime behaviour. This page is the language reference.

## Minimal example

```hcl title="main.cvy"
agent "poet" {
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/poet.md")
  output   = { done = bool }
}
```

`provider` and `prompt` are always required. `model` is required unless the provider block sets [`model`](/docs/dsl/provider/#model), which is then the default for every agent on it.

## Full example

```hcl title="main.cvy"
agent "implementer" {
  count    = 4
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/implementer.md")
  vars = {
    me      = "implementer[${count.index}]"
    repo    = env("COMMIT0_REPO_DIR")
    library = "simpy"
  }
  tools      = ["bash", "read_file", "write_file", "edit_file"]
  inputs     = { assignments = agent.lead.output }
  depends_on = [agent.setup]
  writes     = [topic.builds, topic.human]
  watches    = [topic.tasks, topic.failures]
  output     = { files = list(string), passed = bool }
  max_turns  = 60
  can_spawn  = true
}
```

## Attributes

| Attribute | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `provider` | provider reference | yes | | The `provider` block to run under, as `provider.<name>`. |
| `model` | string | no | the provider's `model` | Model name, passed to the provider unchanged. Required unless the provider sets `model`. |
| `prompt` | string | yes | | The agent's instructions. Almost always `file("prompts/<name>.md")`. |
| `vars` | map of string | no | none | Values substituted for `{{name}}` in the prompt text. |
| `tools` | list of string | no | `[]` | Built-in tools of the provider's type this agent may use. |
| `settings` | object | no | `{}` | Settings of the provider's type for this agent, such as `{ effort = "high" }`. |
| `inputs` | map of expression | no | none | Named values from other agents' outputs. Agent starts after they finish. |
| `depends_on` | list of agent references | no | `[]` | Agents that must finish first. Passes nothing. |
| `writes` | list of topic references | no | `[]` | Topics this agent may publish to. |
| `watches` | list of topic references | no | `[]` | Topics whose new messages give this agent a turn. |
| `output` | object type | no | none | Shape of the `finish` payload. |
| `max_turns` | number | no | `50` | The agent is stopped after this many turns. |
| `can_spawn` | bool | no | `false` | Whether the agent may call `create_agent`. |
| `count` | whole number | no | none | Creates that many identical agents. |
| `for_each` | set of string or map | no | none | Creates one agent per element. |

### provider and model

`provider = provider.anthropic` names the [provider](/docs/dsl/provider/) block. It is a reference, not a string: `provider = "anthropic"` is an error (`Expected a reference like provider.<name>`). `model` is passed to the provider unchanged, for example `claude-opus-5-5` or `gpt-6.1-sol`.

An agent that sets no `model` runs on its provider's [`model`](/docs/dsl/provider/#model), the default for every agent on that provider. An agent that sets one uses its own. One of the two must be set, or the agent is [TS004](/docs/dsl/validation/#ts004):

```hcl
provider "llm" {
  source = "claude_agent_sdk"
  model  = "claude-sonnet-5-5"
}

agent "writer" {
  provider = provider.llm              # claude-sonnet-5-5, from the provider
  prompt   = file("prompts/writer.md")
}

agent "critic" {
  provider = provider.llm
  model    = "claude-opus-5-5"         # its own
  prompt   = file("prompts/critic.md")
}
```

Every instance of a `count` or `for_each` agent gets the same model, whichever way it is set. When the swarm sets `max_cost_usd`, the model must have a price ([TS060](/docs/dsl/validation/#ts060)): its provider's `price`, or the price in its type's catalog. A type with a closed catalog warns about a model it does not list ([TS019](/docs/dsl/validation/#ts019)); the built-in types accept any name.

### prompt

The agent's instructions. Convoy places them in the YOUR JOB section of the system prompt it builds for every agent, below the swarm goal, the agent's name, its topics, and how to use the swarm tools. Use `file()`, so the prompt lives in `prompts/`:

```hcl
prompt = file("prompts/critic.md")
```

A path that does not exist is [TS040](/docs/dsl/validation/#ts040). The file is read when the swarm is loaded and sent to the server in the bundle. Later edits to the file do not change a run that is already started. The expression must evaluate to a string and may use `count.index`, `each.key`, and `each.value`, but the file text itself is not interpolated.

### vars

`vars` substitutes values into the prompt text. `{{vendor}}` in the prompt file is replaced with `vars.vendor`.

```hcl title="main.cvy"
agent "researcher" {
  for_each = toset(["nvidia", "groq"])
  prompt   = file("prompts/research.md")
  vars     = { vendor = each.key }
  # ...
}
```

```text title="prompts/research.md"
You research one vendor: {{vendor}}.
Publish one sourced fact per message to the findings topic.
```

Rules:

- `vars` is a map of strings. The samples quote numbers (`max_rounds = "6"`).
- Every `{{name}}` in the prompt whose `name` is a key of `vars` is replaced.
- A `{{name}}` with no matching var is left unchanged, so placeholders meant for other layers survive.
- A var that the prompt never uses is a warning ([TS054](/docs/dsl/validation/#ts054)).
- `vars` may use `count.index`, `each.key`, `each.value`, `env()`, and string interpolation.
- This is the only substitution. `${...}` inside a prompt file is not interpolated.

Use `env()` for paths and settings, never for secrets. The values travel in the bundle and are stored with the run.

### tools

The built-in tools of the provider's type that this agent may use. Each name must be one the type offers. The built-in types offer:

| Type | Offers |
| --- | --- |
| `claude_agent_sdk` | `web_search`, `bash`, `read_file`, `write_file`, `edit_file` |
| `openai_agents` | `web_search`, `bash`, `read_file`, `write_file`, `edit_file`, `code_interpreter`, `file_search` |
| `openai_compatible` | `bash`, `read_file`, `write_file`, `edit_file` |

`bash`, `read_file`, `write_file` and `edit_file` run on the agent's machine and mean the same thing on all three types, so an agent that lists them can move between providers by changing its `provider` and `model`. `web_search` on `openai_agents`, `code_interpreter` and `file_search` are hosted: they run on OpenAI's platform. For any other type, `convoy provider show <source>` lists its tools. A name the type does not offer is [TS013](/docs/dsl/validation/#ts013), and the message lists the offered tools and suggests the nearest name. Swarm tools (`list_topics`, `read_topic`, `publish`, `watch_topic`, `create_topic`, `list_agents`, `create_agent`, `stop_agent`, `report_progress`, `finish`) are always available and are never written in `tools`. `create_agent` is offered only when `can_spawn` is true. An empty `tools` list is valid and gives an agent that can only use the swarm tools.

### settings

Settings of the provider's type that tune this agent's model calls, such as reasoning effort or temperature. Each key must be a setting the type defines:

| Type | Settings |
| --- | --- |
| `claude_agent_sdk` | `effort` (`low`, `medium`, `high`, `xhigh`, `max`), `max_thinking_tokens` (0 or more) |
| `openai_agents`, `openai_compatible` | `temperature` (0 to 2), `max_tokens` (1 or more) |

```hcl
agent "planner" {
  provider = provider.anthropic
  model    = "claude-opus-5"
  prompt   = file("prompts/planner.md")
  settings = { effort = "max" }
}
```

Each setting resolves key by key: the agent's `settings`, then its [provider's `settings`](/docs/dsl/provider/#settings), then the type's default. A setting none of them sets is not passed, so the SDK's own default applies. An unknown key or a value outside a setting's range is [TS018](/docs/dsl/validation/#ts018). Agents created at run time with `create_agent` may set `settings` too, and are checked the same way.

### inputs, depends_on, and watches

These three attributes control when an agent runs. Only the first two order agents.

| Attribute | Effect | Graph edge |
| --- | --- | --- |
| `inputs` | The agent starts after every referenced agent has finished, and receives the values as JSON in its first turn. | yes |
| `depends_on` | The agent starts after every referenced agent has finished. Nothing is passed. | yes |
| `watches` | A message on one of these topics gives the agent another turn. | no |

An agent with no `inputs` and no `depends_on` starts when the swarm starts.

**inputs** is a map of names to expressions. Each expression must read another agent's `output`:

```hcl
inputs = {
  brief  = agent.triager.output
  claude = agent.proposer_claude.output
}
```

The agent starts once `triager` and `proposer_claude` are both Done and gets `{"brief": {...}, "claude": {...}}` in its first turn. The expressions may reach into a field (`agent.coach.output.version`), or collect every instance's outputs, `agent.worker[*].output` for a `count` agent and `values(agent.builder)[*].output` for a `for_each` agent:

```hcl
inputs = { components = values(agent.builder)[*].output }
```

Anything with a root other than `agent` is an error, `inputs may only reference agent outputs`. Reading a field requires the referenced agent to declare it in `output` ([TS021](/docs/dsl/validation/#ts021), [TS022](/docs/dsl/validation/#ts022)). Each `inputs` reference is an edge, and the graph must have no cycle ([TS020](/docs/dsl/validation/#ts020)).

**depends_on** is a list of agent references. Use it to order agents when you do not need the data:

```hcl
depends_on = [agent.manager]            # all instances of manager
depends_on = [agent.runner["shard_a"]]  # one instance
```

**watches** is a list of topics. After its first turn, an agent gets another turn in exactly two cases: a message is published to a topic in `watches`, or a message is sent with `to` set to the agent's name. An agent with an empty `watches` list works until it calls `finish`: whenever it ends a turn without finishing, it gets a continue turn, until it finishes or reaches `max_turns`.

The writer and critic in `edge_chips` loop through the board (`writes` and `watches`) while the graph has no cycle, because `watches` and `writes` are not edges.

### writes

The topics the agent may publish to. A `publish` call to any other topic is rejected and the model sees the error. Each entry is a `topic.<name>` reference.

### output

The shape of the object the agent passes to `finish`. After `finish`, the status is Done and `agent.<name>.output` resolves to the payload. The server checks the payload against the shape. A mismatch is returned to the model as a tool error with the path, for example `output.score: number required`, and the model can try again.

`output` is required whenever `done_when` or another agent's `inputs` reads a field of this agent's output.

Two forms are accepted. They mean the same:

```hcl
output = { approved = bool, score = number }
output = object({ approved = bool, score = number })
```

The shorthand is used in every sample. The type must be an object. Field types are limited to:

| Type | Example |
| --- | --- |
| `string` | `path = string` |
| `number` | `score = number` |
| `bool` | `approved = bool` |
| `list(T)` | `files = list(string)` |
| `map(T)` | `scores = map(number)` |
| nested object | `meta = { id = string, tags = list(string) }` |

`any` is rejected, so the server can always check a payload. A primitive or collection at the top level (`output = string`) is an error, `output must be an object type`.

An agent with no `output` can still call `finish`; the payload is accepted as free-form JSON. Nothing may read named fields of it, though.

### max_turns

The agent is stopped after this many turns. Default `50`. A turn is one SDK call, so a long-lived agent that wakes for every message on a busy topic can use turns fast; set the limit deliberately. For long jobs, set it high: the Vending-Bench operator uses `max_turns = 1500`. See [Budgets and limits](/docs/concepts/budgets/).

### can_spawn

When `true`, the agent is given the `create_agent` tool and can add agents to the running swarm. Whether a team can grow is declared in the file, not left to a prompt. A child created this way may set `can_spawn = true` only if its creator could spawn, and is bounded by the creator's tools, providers, and models. The server also rejects `create_agent` from any other caller. See [Spawning agents](/docs/concepts/spawning/).

### count

`count` is allowed on `agent` blocks only. The value is a whole number, and that many identical agents are created. Use it for a pool of workers that differ only in their index; use `for_each` when each instance gets its own input.

```hcl
agent "implementer" {
  count = 4
  vars  = { me = "implementer[${count.index}]" }
  # ...
}
```

- **Names.** Each instance is named `<name>[<index>]`, from `implementer[0]` to `implementer[3]`.
- **count.index.** Inside the block, `count.index` is the instance's index, a number. It is available in `prompt` and `vars` only.
- **Values.** The value must be known when the files load and be a whole number from 0 to 1000 ([TS032](/docs/dsl/validation/#ts032)). `0` produces zero instances and a warning.
- **References.** `agent.implementer` is a list, as in Terraform. `agent.implementer[0].output` is one instance, `agent.implementer[*].output` is the list of all outputs in index order, and `length(agent.implementer)` is the count. See [References and functions](/docs/dsl/expressions/#count-and-for_each-agents).
- **`count.index` without `count`** is [TS033](/docs/dsl/validation/#ts033). An agent with both `count` and `for_each` is [TS034](/docs/dsl/validation/#ts034).

### for_each

`for_each` is allowed on `agent` blocks only. The value is a set of strings or a map, and one agent is created per element.

```hcl
agent "researcher" {
  for_each = toset(["nvidia", "groq", "cerebras", "tenstorrent"])
  vars     = { vendor = each.key }
  # ...
}

agent "runner" {
  for_each = {
    shard_a = "0,1,3,4,5,7,9,10"
    shard_b = "11,12,14,15,17,20,21,23"
  }
  vars = { shard = each.key, task_ids = each.value }
  # ...
}
```

- **Names.** Each instance is named `<name>[<key>]`, for example `researcher[nvidia]`. Instances are processed in sorted key order.
- **each.** Inside the block, `each.key` is the set element or map key, and `each.value` is the element (for a set, the same as the key) or the map value. `each` is available in `prompt` and `vars`. It is not available in `provider`, `model`, `tools`, or other static attributes.
- **A list is rejected.** `for_each = ["a", "b"]` fails: `for_each requires a set or map; wrap the value in toset()` ([TS030](/docs/dsl/validation/#ts030)). A set must contain strings.
- **An empty set** produces zero instances and a warning.
- **References.** `agent.researcher` is an object keyed by instance key. `agent.researcher["nvidia"].output` is one instance, and `values(agent.researcher)[*].output` is the list of all outputs. See [References and functions](/docs/dsl/expressions/#count-and-for_each-agents).
- **Limits.** The language has no cap on the number of `for_each` instances. `convoyd --max-processes` caps concurrent runners; extra agents wait in Pending.
- **`each` without `for_each`** is [TS031](/docs/dsl/validation/#ts031). `for_each` on a block other than `agent` is an error.

## How agents start, turn, and finish

| Phase | Rule |
| --- | --- |
| Start | No `inputs` and no `depends_on`: starts with the swarm. Otherwise starts after every referenced agent has finished. |
| More turns | A message on a watched topic, or a message addressed to the agent. With empty `watches`, a continue turn after every turn that does not finish. |
| Finish | `finish(output)` ends the agent as Done. `output` is checked against the declared shape. |
| End of swarm | Running agents are stopped when `done_when` is true, `max_runtime` passes, or the budget is spent. |

Agents that watch topics usually never call `finish`. The server stops them when the swarm ends.

## What validate checks

| Code | Severity | Rule |
| --- | --- | --- |
| [TS002](/docs/dsl/validation/#ts002) | error | `provider` or `prompt` is missing |
| [TS003](/docs/dsl/validation/#ts003) | error | An attribute has the wrong type |
| [TS004](/docs/dsl/validation/#ts004) | error | No `model` on the agent, and none on its provider |
| [TS010](/docs/dsl/validation/#ts010) | error | `provider`, `writes`, `watches`, `depends_on`, or `inputs` names a block that does not exist |
| [TS013](/docs/dsl/validation/#ts013) | error | A `tools` entry the provider's type does not offer |
| [TS018](/docs/dsl/validation/#ts018) | error | `settings` does not match the type's settings |
| [TS019](/docs/dsl/validation/#ts019) | warning | `model` is not in a closed catalog |
| [TS020](/docs/dsl/validation/#ts020) | error | A dependency cycle through `inputs` and `depends_on` |
| [TS021](/docs/dsl/validation/#ts021) | error | An `inputs` value reads a field of an agent with no `output` |
| [TS022](/docs/dsl/validation/#ts022) | error | An `inputs` value reads a field not in the agent's `output` |
| [TS030](/docs/dsl/validation/#ts030) | error | `for_each` is a list or a non-string set |
| [TS031](/docs/dsl/validation/#ts031) | error | `each` used without `for_each` |
| [TS032](/docs/dsl/validation/#ts032) | error | `count` is not a whole number from 0 to 1000 |
| [TS033](/docs/dsl/validation/#ts033) | error | `count.index` used without `count` |
| [TS034](/docs/dsl/validation/#ts034) | error | both `count` and `for_each` are set |
| [TS040](/docs/dsl/validation/#ts040) | error | A `file()` path does not exist |
| [TS052](/docs/dsl/validation/#ts052) | warning | Non-empty `watches` and no explicit `max_turns` |
| [TS053](/docs/dsl/validation/#ts053) | warning | No `watches`, no `inputs`, and no `writes` |
| [TS054](/docs/dsl/validation/#ts054) | warning | A var in `vars` the prompt never uses |
| [TS055](/docs/dsl/validation/#ts055) | warning | No `watches`, no `output`, and no explicit `max_turns` |
| [TS060](/docs/dsl/validation/#ts060) | error | `max_cost_usd` is set and the model has no price |
| [TS070](/docs/dsl/validation/#ts070) | warning | The model, or a setting, is deprecated by its type |
