Coding agents

Drive fagi from a coding agent: machine-readable output, offline command discovery, previews before charged writes, and safe handling of errors and retries

Run fagi from a coding agent or script with JSON output, no prompts, and exit codes it can branch on.

Prerequisites

Machine mode

Put every command in machine mode at once:

export FI_OUTPUT=json FI_NO_INPUT=1

Stdout then carries one JSON document per command, and a command that would prompt fails instead. Stderr carries the JSON error, and can also carry target, pagination and warning records, so read it line by line (see What goes to stderr). Check the exit code rather than parsing text:

status=0
fagi trace check TRACE_ID --project-id PROJECT_ID --max-error-spans 0 > report.json || status=$?
[ "$status" -eq 5 ] && echo "gate failed"

Find commands

fagi schema describes every command as JSON, offline and without credentials. Search by words, then read one command’s entry, and check its access and open_world before you run it, to avoid charged or destructive commands while exploring (Find commands explains the fields):

fagi schema --search 'push dataset'
fagi schema --command 'datasets push'

Preview and confirm

Preview with --dry-run where a command offers it, and pass --yes only deliberately, once you have read the preview. Global flags lists which commands ask for confirmation. fagi epoch run (CLI 0.3.0 or later) never asks, so always preview it (see Start an Epoch).

Handle errors

Branch on the exit code first, then on details.code in the JSON error. Exit codes defines each code; this is what an agent does next:

ExitWhat to do
0Done
1Read the error; for HTTP 409, read the resource again before you change it
2Fix the input or flags; details.code and details.hint say what was wrong
3Check the key pair, its access and the plan
4Check the ID or name
5A gate failed: read the report on stdout
6Network or server error, or a fagi sim or fagi epoch verdict wait ran out. Retry only when details.retryable is true
8fagi sim work ended without a usable result: read it before you rerun anything (fagi sim ships in a later CLI release)

fagi sim retries a read (never a write) up to twice after a network error or HTTP 429, 502, 503 or 504, waiting 0.25 s, then 1 s, or the server’s Retry-After when that is longer; a Retry-After over 30 s ends the read without a retry, and an error after retries says how many times the request was sent. Otherwise the CLI never retries for you. Before you repeat a write that exited 6 or carries write_state: "unknown", check whether it landed, or rerun with the same --idempotency-key where the command takes one.

Run an environment and fix failures

fagi sim ships in a later CLI release. Follow the Simulate quickstart. In machine mode, save the --json output of fagi sim env create --wait or fagi sim env status --wait to a file; after exit 8, test it with jq -e '.question' to tell a question from the builder (answer it, see Follow the build) from a failed or canceled build (read the error).

Dive deeper

Was this page helpful?

Questions & Discussion