# Use anyr from scripts and AI agents

> Drive the AnyRouter CLI from CI, scripts and coding agents with JSON output, JSON errors and fixed exit codes.


# Use `anyr` from scripts and AI agents

Run any `anyr` command without a terminal: add `--json` for parseable output, and errors come back as JSON with a fixed exit code. `anyr help agents` prints this guide in the terminal.

## Discover commands

```bash
anyr --help             # everyday commands
anyr help --all         # every command, grouped
anyr commands --json    # full command tree as JSON
```

`anyr commands --json` (schema `anyr.commands/v1`) lists each command with its subcommands, flags, aliases, group and whether it has JSON output, plus the exit codes. New fields may be added; existing ones keep their meaning.

```json
{
  "schema": "anyr.commands/v1",
  "commands": [
    {
      "name": "chat",
      "summary": "Ask a model: prompt, stdin or a REPL; --json for scripts",
      "group": "chat",
      "aliases": [],
      "subcommands": [],
      "flags": ["base-url", "config", "key", "model", "no-stream", "profile", "system"],
      "json": true
    }
  ],
  "exit_codes": [{ "code": 0, "name": "ok", "meaning": "success" }]
}
```

## Output settings

| Setting | Effect |
|---|---|
| `--json` | JSON on stdout for any command with a JSON form |
| `ANYR_OUTPUT=json` | Same as `--json` on every command |
| `--quiet` or `ANYR_QUIET=1` | No update notices or background update check |
| `NO_COLOR=1` | No colour (also off whenever stdout is not a terminal) |

Without a terminal there are no prompts, spinners or update notices. A command that would ask first, such as revoking a key, fails with a usage error until you pass `--yes`.

## Errors and exit codes

With `--json`, an error is one JSON object on stderr and stdout stays empty:

```json
{"error":{"code":"auth","exit":4,"message":"HTTP 401: invalid key","hint":"sign in again: anyr login"}}
```

| Exit | `code` | Meaning |
|---|---|---|
| 0 | `ok` | Success |
| 1 | `error` | The request or command failed |
| 2 | `usage` | Bad flag, argument or subcommand. Nothing was sent |
| 3 | `not_found` | The model, key, id or resource does not exist |
| 4 | `auth` | No key, a rejected key, or a key without the needed scope |
| 5 | `rate_limited` | Too many requests. Retry later |

## Ask a model

```bash
anyr chat "Reply with ok" --json
```

```json
{"id":"req_…","model":"z-ai/glm-4.7-flash","content":"ok","finish_reason":"stop","usage":{"prompt_tokens":12,"completion_tokens":1}}
```

- `--json` waits for the full answer. `id` is what `anyr generation <id>` takes, to look up cost and latency.
- `--no-stream` waits for the full answer and prints only the text.
- Piped input becomes the prompt. A lone `-` places it after your own words:

```bash
git diff | anyr chat "Review this diff" - --json
```

## Authenticate without a login

```bash
export ANYROUTER_API_KEY=sk-ar-v1-…        # inference key
export ANYROUTER_MANAGEMENT_KEY=ak_…       # presets, provider keys, device removal
anyr whoami --json
```

## Commands with JSON output

```bash
anyr models --json --free
anyr usage --json
anyr logs --json --status error
anyr keys list --json
anyr embed "text" --json
anyr artifact publish page.html --json
anyr api credits            # any dashboard resource; prints JSON when piped
```

Launchers such as `anyr claude` are interactive and have no JSON form, but their errors follow the same rules. Use `--dry-run` to see the command and environment a launch would use.


## Related

- [Usage, logs & transactions](/docs/cli/usage.md)
- [Configuration](/docs/cli/configuration.md)
- [Delegate tasks between coding agents](/docs/cli/delegate.md)
