# Writer and critic

> The smallest useful swarm. Two agents and one loop, in about thirty lines, done when the critic approves.

This is the place to start. A writer drafts what the goal asks for. A critic scores each draft and sends numbered notes back. The swarm is done when the critic approves. The folder is `examples/writer_critic`.

```text
writer ──drafts──> critic
   ^──── review ─────┘
```

It takes about 40 seconds. Measured runs cost $0.10 on `claude-sonnet-5-5` and $0.03 on `gpt-6.1-sol`.

## Run it

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

```sh
cd examples/writer_critic
convoy run
convoy watch writer_critic
```

[`convoy watch`](/docs/cli/watch/) shows the two agents and the board as drafts and notes go back and forth. When the critic approves, the run ends and prints the result:

```console
run 1 ended: DONE

result  critic
  approved  true
  score     9
  final     A race condition happens when a program's result depends on ...
```

[`convoy ui writer_critic`](/docs/cli/ui/) opens the same run in the browser, with each agent's full transcript.

## The swarm file

```hcl title="main.cvy"
# The smallest useful swarm: two agents and one loop.
#
#   writer ──drafts──> critic
#      ^──── review ─────┘
#
# The writer drafts. The critic scores each draft and sends notes back. The
# swarm is done when the critic approves. Change the goal and run it again.

swarm "writer_critic" {
  goal         = "Explain what a race condition is to a new programmer, in under 150 words, with one concrete example"
  done_when    = agent.critic.output.approved
  max_runtime  = "10m"
  max_cost_usd = 1
  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 "drafts" { description = "The writer's drafts, in full. The newest one counts" }
topic "review" { description = "The critic's score and numbered notes on the newest draft" }

agent "writer" {
  provider  = provider.llm
  prompt    = file("prompts/writer.md")
  writes    = [topic.drafts]
  watches   = [topic.review]
  max_turns = 6
}

agent "critic" {
  provider  = provider.llm
  prompt    = file("prompts/critic.md")
  watches   = [topic.drafts]
  writes    = [topic.review]
  output    = { approved = bool, score = number, final = string }
  max_turns = 6
}
```

| Block | What it says |
|---|---|
| `swarm` | The goal, when the swarm is done (`done_when`), and the limits: ten minutes and one dollar |
| `provider` | Which SDK runs the agents, and the model they use |
| `topic` | Two channels on the message board, `drafts` and `review` |
| `agent "writer"` | Writes to `drafts`. A message on `review` gives it a turn |
| `agent "critic"` | A message on `drafts` gives it a turn. It writes to `review` and finishes with a typed `output` |

## Why it loops

The loop is not written anywhere as a loop. It comes from who `watches` what: the writer's draft wakes the critic, and the critic's notes wake the writer. See [the message board](/docs/concepts/board/).

`done_when = agent.critic.output.approved` reads the critic's output. The critic calling `finish` with `approved = true` is what ends the run. If that never happens, `max_runtime` or `max_cost_usd` ends it instead. See [budgets](/docs/concepts/budgets/).

## The prompts

Prompts are plain Markdown. The first line is the one-line role other agents see; the rest is the agent's job.

```md title="prompts/critic.md"
Scores each draft against the swarm goal and approves the first one that is good enough.

You review drafts of the piece the swarm goal describes. If your turn starts
and "drafts" has nothing new, end the turn. A new draft wakes you.

For the newest draft on "drafts":

1. Score it from 0 to 10 on three things: it meets every constraint in the
   goal (length, audience, format), it is accurate, and it is clear.
2. Below 8: publish the score and at most three numbered, specific fixes to
   "review", then end your turn.
3. 8 or above: call finish with approved = true, the score, and the approved
   draft, word for word, as final.

A first draft can always be tightened, so send one round of notes before you
approve anything. On your third review, approve if the draft meets the
goal's constraints.
```

The prompts take their task from the swarm's `goal`, so changing the goal line is enough to give the pair a different job.

## 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"
}
```

Nothing else changes. `convoyd` needs `OPENAI_API_KEY` in its environment instead of `ANTHROPIC_API_KEY`.

## Change it

- **A different job.** Change the `goal` and run again.
- **Talk to it while it runs.** `convoy say writer_critic review "Use a bank account in the example" --to writer` posts to the board as a person and wakes the writer. See [`convoy say`](/docs/cli/say/).
- **A cheaper critic.** Add `model = "claude-haiku-5-5"` to the `critic` block. An agent's `model` wins over its provider's.
- **A stricter bar.** Add `&& agent.critic.output.score >= 9` to `done_when`.

Next: [the research team](/docs/examples/research-team/) adds parallel agents, a dependency graph, and tools.
