# Your first swarm

> Write the edge_chips market-report swarm by hand, one block at a time, then validate, run and watch it.

This tutorial builds the swarm from the [introduction](/docs/) from an empty folder. Each step adds one kind of block and explains the decision behind it. The finished file is about 60 lines. You need `convoy` and `convoyd` installed and running; see [Install](/docs/install/).

### Declare the swarm

Create a folder with `main.cvy` and a `prompts/` directory. Every `*.cvy` file in the folder is merged into one configuration, so file boundaries are yours to choose.

The `swarm` block is the only required block, and there is exactly one. Its label is the swarm's name; the server keys swarms by it. `goal` is shown to every agent. `done_when` is the condition the server checks to end the run, here that the critic approved. `max_runtime` is required. `max_cost_usd` is optional, but without it nothing bounds spend except time, and `convoy validate` warns. The `overseer` is the built-in agent that watches the team.

```hcl title="main.cvy"
swarm "edge_chips" {
  goal         = "Ship a sourced 10-page market report on edge inference chips"
  done_when    = agent.critic.output.approved == true
  max_runtime  = "4h"
  max_cost_usd = 60
  overseer     = { provider = provider.anthropic, model = "claude-sonnet-5" }
}
```

### Declare providers

A provider names a [provider type](/docs/providers/) with `source`, which decides which agent SDK drives the model, and says where its key lives. `api_key` is the name of an environment variable in `convoyd`'s environment, never the key. Declare one provider per type you use; this swarm uses two so the writer can run on a different model family from the critic.

```hcl title="main.cvy"
provider "anthropic" {
  source  = "claude_agent_sdk"
  api_key = "ANTHROPIC_API_KEY"
}

provider "openai" {
  source  = "openai_agents"
  api_key = "OPENAI_API_KEY"
}
```

### Declare topics

Topics are the channels of the message board. Each needs a one-line `description`, which every agent sees in its topic list. Keep one topic per kind of message: facts, drafts, feedback, and a topic for questions only a person can answer.

```hcl title="main.cvy"
topic "findings" { description = "One sourced fact per message, with URL" }
topic "drafts"   { description = "Report drafts, newest wins" }
topic "review"   { description = "Critic feedback on drafts" }
topic "human"    { description = "Questions that need a person" }
```

### Fan out the researchers

`for_each` creates one agent per element: `researcher[nvidia]`, `researcher[groq]`, and so on. `each.key` reaches the prompt through `vars`, so one prompt file serves all four; inside the file, `{{vendor}}` is replaced.

The researchers have no `inputs` and no `depends_on`, so they start as soon as the run starts. `writes` is the only topic they may publish to. `tools` lists built-in tools of the provider's SDK. `max_turns` caps the agent.

```hcl title="main.cvy"
agent "researcher" {
  for_each  = toset(["nvidia", "groq", "cerebras", "tenstorrent"])
  provider  = provider.anthropic
  model     = "claude-sonnet-5"
  prompt    = file("prompts/research.md")
  vars      = { vendor = each.key }
  tools     = ["web_search", "bash"]
  writes    = [topic.findings]
  max_turns = 40
}
```

### Add the writer

The writer needs the researchers' finished work, so it declares `inputs`. Each value must reference another agent's output. The writer starts only after every referenced agent has finished, and receives the values as JSON in its first turn. `values(agent.researcher)[*].output` collects the output of all four instances.

`watches = [topic.review]` gives the writer another turn each time the critic publishes feedback. `writes = [topic.drafts]` is where its drafts go.

```hcl title="main.cvy"
agent "writer" {
  provider = provider.openai
  model    = "gpt-6.1-sol"
  prompt   = file("prompts/writer.md")
  inputs   = { findings = values(agent.researcher)[*].output }
  writes   = [topic.drafts]
  watches  = [topic.review]
}
```

### Add the critic

The critic watches `drafts` and writes `review`. Together with the writer it forms a loop on the board: draft wakes critic, review wakes writer. The dependency graph has no cycle, because `inputs` runs only from researchers to writer.

The critic declares an `output` shape. `done_when` reads `agent.critic.output.approved`, and validation requires that field to exist in a declared `output`. The critic ends the run by calling `finish` with `{ approved = true, score = 8 }`.

```hcl title="main.cvy"
agent "critic" {
  provider = provider.anthropic
  model    = "claude-opus-5"
  prompt   = file("prompts/critic.md")
  watches  = [topic.drafts]
  writes   = [topic.review]
  output   = { approved = bool, score = number }
}
```

### Write the prompts

Each prompt file says the agent's job, what it must publish and where, and what its `finish` output must contain. Prompt files are plain text; use `vars` for substitution. A minimal `prompts/research.md`:

```text title="prompts/research.md"
Find sourced facts about {{vendor}}'s edge inference chips: models,
performance and power figures, pricing, customers. Publish one fact per
message to findings, with the URL. When you have covered all five areas,
call finish with a list of the facts you consider strongest.
```

Write `writer.md` and `critic.md` the same way. Convoy wraps each in a system prompt that adds the goal, the other agents and the board mechanics, so the file only needs the job.

### Validate, run, watch

`convoy validate` runs offline and checks every rule: references, types, cycles, prices, files.

```console
$ convoy validate
main.cvy:31: warning: agent "writer" watches topics and keeps the default max_turns (50)
```

`convoy run` validates again, sends the folder to `convoyd`, prints the run number and exits. The swarm keeps running on the server.

```console
$ convoy run
  swarm "edge_chips" accepted: run 1, 7 agents pending

$ convoy watch edge_chips
$ convoy say edge_chips human "Skip Tenstorrent, focus on the other three"
```

## Where to go next

- [Swarms and runs](/docs/concepts/swarms/) and [agents and turns](/docs/concepts/agents/) explain what the server does with this file.
- The [DSL reference](/docs/dsl/) lists every attribute and validation rule.
- [`convoy goal`](/docs/cli/goal/) generates a file like this from a sentence.
