# Code team

> A planner splits a small project, two coders build their halves at the same time, and a reviewer runs the tests until the whole suite passes.

A planner splits a small project in two and sends each coder its half. The coders build at the same time in one shared folder. A reviewer runs the whole test suite, tries the program, and sends each failure back to the coder who owns the file. The swarm is done when everything passes. The folder is `examples/code_team`.

```text
planner ──work──> coder[0], coder[1] ──done──> reviewer
   │                  ^──────── review ───────────┘
   └───────────────── inputs ─────────────────────┘
```

The result is a working, tested Python package in `./workspace`. Measured runs took one to two minutes and cost about $0.17 on `claude-sonnet-5-5` and $0.14 on `gpt-6.1-sol`, for a package with fifteen to thirty passing tests.

The coders and the reviewer have the `bash` tool, with your user's permissions and no sandbox. They are told to stay inside `./workspace`. Read the prompts before you point this at anything you care about.

## Run it

With [`convoyd` running](/docs/quickstart/):

```sh
cd examples/code_team
convoy run
convoy watch code_team          # or: convoy ui code_team
```

When the run ends, do not take the reviewer's word for it. Run the tests and the program yourself:

```sh
cd workspace
uv run --no-project --with pytest python -m pytest -q
python -m wordfreq ../README.md
```

## The swarm file

```hcl title="main.cvy"
# A small engineering team. A planner splits the job, two coders build their
# halves at the same time, and a reviewer runs the tests and sends each
# failure back to the coder who owns it. Done when the whole suite passes.
#
#   planner ──work──> coder[0], coder[1] ──done──> reviewer
#      │                  ^──────── review ───────────┘
#      └───────────────── inputs ─────────────────────┘
#
# The code is written to ./workspace in the folder you run convoy from.

swarm "code_team" {
  goal         = "Build wordfreq, a Python command-line tool with pytest tests. `python -m wordfreq FILE` prints the file's word count, its reading time at 200 words a minute, and its ten most frequent words."
  done_when    = agent.reviewer.output.approved
  max_runtime  = "30m"
  max_cost_usd = 5
  overseer     = { provider = provider.llm, can_spawn = false }
}

# One API key is enough. With an OpenAI key, use the values in the comments.
provider "llm" {
  source = "claude_agent_sdk"  # "openai_agents"
  model  = "claude-sonnet-5-5" # "gpt-6.1-sol"
}

topic "work" { description = "Planner to one coder: the files that coder owns, what they must do, and what the other coder provides" }
topic "done" { description = "Coders: my part is ready. Files, number of tests, last line of the test run" }
topic "review" { description = "Reviewer to one coder: the failing tests in that coder's files, with the first error of each" }

# Designs the split, sends each coder its task, and finishes with the
# ownership map the reviewer routes failures by.
agent "planner" {
  provider  = provider.llm
  prompt    = file("prompts/planner.md")
  writes    = [topic.work]
  output    = { owners = map(string), run = string, notes = string }
  max_turns = 3
}

# Two coders from one block. They start at once, wait for a task addressed to
# them, and are woken again by anything the reviewer sends back.
agent "coder" {
  count    = 2
  provider = provider.llm
  prompt   = file("prompts/coder.md")
  vars = {
    dir  = "${env("PWD")}/workspace"
    test = "uv run --no-project --with pytest python -m pytest -q"
  }
  tools     = ["bash", "read_file", "write_file", "edit_file"]
  writes    = [topic.done]
  watches   = [topic.work, topic.review]
  max_turns = 8
}

# Starts when the planner finishes, then loops with the coders through the
# board. Its approval ends the swarm.
agent "reviewer" {
  provider = provider.llm
  prompt   = file("prompts/reviewer.md")
  vars = {
    dir  = "${env("PWD")}/workspace"
    test = "uv run --no-project --with pytest python -m pytest -q"
  }
  tools     = ["bash", "read_file"]
  inputs    = { plan = agent.planner.output }
  writes    = [topic.review]
  watches   = [topic.done]
  output    = { approved = bool, passed = number, total = number, notes = string }
  max_turns = 10
}
```

## What it adds

- **`count` makes a pool.** `count = 2` creates `coder[0]` and `coder[1]` from one block.
- **Messages can be addressed.** The planner publishes one task per coder with `to` set, and the reviewer sends each failure to the coder whose file is at fault. Everyone can read the board, but an addressed message wakes only its recipient. See [the message board](/docs/concepts/board/).
- **`done_when` rests on something real.** The reviewer approves only after it has run the suite and the program in that turn, so `agent.reviewer.output.approved` means the tests passed.
- **One agent can be in the graph and in a loop.** The reviewer starts when the planner finishes (`inputs`), then goes round with the coders through the `done` and `review` topics.

## The reviewer's prompt

```md title="prompts/reviewer.md"
Runs the whole test suite when coders report done, sends failures to their owners, and approves when everything passes.

Your input `plan` is the planner's output. `plan.owners` maps every file to
the coder that owns it, and `plan.run` is the command that runs the finished
program. The shared project folder is {{dir}}. You read and run code
there. You never edit it.

EACH TURN
Read what is new on "done" first.

1. If a coder named in `plan.owners` has not posted to "done" yet, end your
   turn. The next post wakes you.
2. Run the whole suite:

       cd {{dir}} && {{test}}

3. Try the program the way a user would: write a short text file, run
   `plan.run` on it, and check the output against the swarm goal.
4. For each failing test or wrong output, publish one message to "review"
   with `to` set to the owner of the file at fault, from `plan.owners`: the
   failing test ids and the first error line of each, or the command you ran
   and what it printed. Then end your turn and wait for the fix.
5. When every test passes and the program does what the goal says, call
   finish with approved = true, passed and total from the test output, and
   one sentence of notes.

Never approve with zero tests, and never approve a suite you did not run in
this turn.
```

`{{dir}}` and `{{test}}` are replaced from the agent's `vars`.

## Use an OpenAI key instead

Change the two lines of the `provider` block to the values in their comments:

```hcl
provider "llm" {
  source = "openai_agents"
  model  = "gpt-6.1-sol"
}
```

`bash`, `read_file`, `write_file`, and `edit_file` exist on both providers, so nothing else changes. `convoyd` needs `OPENAI_API_KEY` in its environment instead of `ANTHROPIC_API_KEY`.

## Change it

- **Build something else.** Change the `goal`. The planner designs the split from it, so any small Python package works: a Markdown table formatter, a unit converter, a log summarizer.
- **Steer it.** `convoy say code_team review "Add a --top N flag" --to "coder[1]"`.
- **Put the planner on a bigger model.** Add `model = "claude-opus-5-5"` to the `planner` block.

For larger swarms shaped after long-horizon benchmarks, see [the benchmark swarms](/docs/examples/#benchmark-swarms).
