# convoy goal

> Turn a written goal into a validated set of .cvy and prompt files by asking a model you name, retrying with diagnostics until the files pass. It does not start anything.

```text
convoy goal --provider <anthropic|openai> --model <id> [flags] "<text>"
```

`goal` is the only command that calls a model directly. It needs no running convoyd. The model you name here only writes the swarm. The swarm declares its own providers and models.

## What it does

1. Checks flags. A missing `--provider` or `--model`, or an unknown provider, is a usage error (exit 2) before any network call. There are no defaults.
2. Builds the system prompt (meta prompt 1) from the goal and three inserts: the providers, models, and tools a swarm may declare (all priced, so a generated `max_cost_usd` passes validation); the embedded DSL reference; and two embedded example swarms.
3. Calls the model with the goal as the user message. The model returns files as blocks:

   ```text
   <file path="main.cvy">
   ...
   </file>
   <file path="prompts/writer.md">
   ...
   </file>
   ```

   Paths must be relative, contain no `..`, and sit under `.` or `prompts/`.
4. Loads the files in memory and runs the same checks as [`convoy validate`](/docs/cli/validate/).
5. Writes the files if they pass, and prints a summary.

## The retry loop

If validation reports errors and attempts remain, the CLI appends the model's reply and a new user message to the conversation: `convoy validate reported these errors. Return the full corrected set of files.`, followed by every error in plain text. The model returns a complete corrected set and the loop repeats.

- Warnings are printed but never trigger a retry.
- `--attempts` bounds the loop (default 3).
- If errors remain after the last attempt, nothing is written. The diagnostics are printed, the last model output is saved to `.convoy/goal-failed.txt`, and the command exits 3.

## Flags

| Flag | Default | Meaning |
| --- | --- | --- |
| `--provider` | required | `anthropic` or `openai`. `openai` also covers OpenAI-compatible servers with `--base-url` |
| `--model` | required | Model id, passed through unchanged. Not checked against any price catalog |
| `--base-url` | provider default | Override the inference URL for a proxy, gateway, or self-hosted server |
| `--attempts` | `3` | Total model calls before giving up |
| `--max-retries` | `3` | Retries of a failed model request (network or API errors) |
| `--overwrite` | off | Replace `main.cvy` and any other returned file that already exists |
| `--dry-run` | off | Print the files to stdout instead of writing |

Global flags such as `--dir` apply. The key comes from `ANTHROPIC_API_KEY` or `OPENAI_API_KEY` in your shell.

## Example

```console
$ convoy goal --provider anthropic --model claude-opus-5 \
    "Write a sourced 10-page market report on edge inference chips"
  wrote ./main.cvy
  wrote ./prompts/research.md
  wrote ./prompts/writer.md
  wrote ./prompts/critic.md
  validate: ok

  agents   researcher[nvidia, groq, cerebras, tenstorrent] -> writer <-> critic
  edges    researcher -> writer
  topics   findings, drafts, review, human

  review the files, then: convoy run
```

The summary collapses each `count` or `for_each` agent into one line, with its index range (`worker[0..3]`) or its keys, lists dependency edges as `a -> b`, and ends by pointing at `convoy run`.

When a first attempt fails and the second passes, the extra diagnostics go to stderr:

```console
$ convoy goal --provider openai --model gpt-6.1-sol "Fix the failing tests in ./repo"
attempt 1: 2 errors, retrying
  main.cvy:14: error: agent "fixer" references undeclared topic "results"
  main.cvy:22: error: swarm "fix_tests" has no done_when
  wrote ./main.cvy
  wrote ./prompts/fixer.md
  validate: ok
```

## Existing files

After validation, if `main.cvy` or any returned path exists and `--overwrite` is not set, the command exits 1 with `main.cvy exists; pass --overwrite`. Nothing is written.

## Exit codes

| Code | Cause |
| --- | --- |
| `0` | Files written (or printed with `--dry-run`) |
| `1` | A file exists and `--overwrite` is not set, or the model request failed |
| `2` | Missing `--provider` or `--model`, or an unknown provider |
| `3` | Errors remain after the last attempt |

## API endpoint

None. `goal` talks to the model provider, not to convoyd.

## Related

- [convoy validate](/docs/cli/validate/) runs the same checks on the result.
- [convoy run](/docs/cli/run/) is the next step.
- [Your first swarm](/docs/first-swarm/) shows editing a generated file.
- [Validation rules](/docs/dsl/validation/)
