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.

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

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.

{
  "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

SettingEffect
--jsonJSON on stdout for any command with a JSON form
ANYR_OUTPUT=jsonSame as --json on every command
--quiet or ANYR_QUIET=1No update notices or background update check
NO_COLOR=1No 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:

{"error":{"code":"auth","exit":4,"message":"HTTP 401: invalid key","hint":"sign in again: anyr login"}}
ExitcodeMeaning
0okSuccess
1errorThe request or command failed
2usageBad flag, argument or subcommand. Nothing was sent
3not_foundThe model, key, id or resource does not exist
4authNo key, a rejected key, or a key without the needed scope
5rate_limitedToo many requests. Retry later

Ask a model

anyr chat "Reply with ok" --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:
git diff | anyr chat "Review this diff" - --json

Authenticate without a login

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

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.

AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer