Tracing

Set a project up for traces, start your app with an installed agent, and read, compare, export or gate stored traces in CI

Get traces flowing from your app with fagi init and fagi run, then read, watch, check, compare and export the traces the platform stored. To browse the same traces in the dashboard, see Explore dashboard.

Prerequisites
  • Signed in to the CLI (see Sign in)
  • An app to trace, or one that already sends traces, such as the one in the Quickstart

Set up a project

fagi init                        # asks for the project name, detects languages
fagi init --yes --project demo   # no prompts
fagi doctor --offline            # which runtimes have a tracing agent

fagi init writes futureagi.toml, a credential-free file that names the project, and says what this build can trace for each language it detects. Your key pair is never written to the file. Running init again leaves an existing file unchanged and exits 1 with futureagi.toml already exists and was not changed, so don’t re-run it as a CI step.

FlagWhat it does
--project NAMEThe project name traces go to. Defaults to the directory name
--yesAccepts the defaults without prompting. Required without a terminal or with --no-input, or init exits 2
--installTries to install agents for the detected languages. No zero-code agent is available through it yet; use fagi trace install --from instead

A project is created on the platform when your app exports its first trace; init only writes the local file.

Run a program with tracing

Zero-code tracing with fagi run is in preview and needs an agent you install first. fagi run uses the first one it finds:

  • An agent path in the environment, such as FI_NODE_AGENT=/path/to/agent (also FI_GO_AGENT and FI_RUBY_AGENT)
  • An agent you installed with fagi trace install node --from PATH (also go or ruby; see Install an agent)
  • An agent package your app already depends on, such as @traceai/auto installed in a Node project

A set FI_NODE_AGENT, FI_GO_AGENT or FI_RUBY_AGENT that points to a missing or unreadable file doesn’t fall back to an agent you installed with fagi trace install.

A Go binary built after fagi trace setup go (below) needs none of these.

fagi run can’t trace Python apps. Add tracing in code as the Observe quickstart shows, then start the app directly (python app.py). Zero-code tracing for Java and .NET isn’t available.

fagi run --dry-run -- node app.js
fagi run -- node app.js
fagi run --project support-bot --sample-rate 0.25 -- node server.js

Everything after -- is passed to your program unchanged, including --json and -o.

FlagWhat it does
--project NAMESets FI_PROJECT_NAME, the project the traces go to
--project-type TYPEobserve (the default) or experiment; sets FI_PROJECT_TYPE
--only NAMESInstrumentations to use, comma-separated; sets FI_INSTRUMENTATIONS
--disable NAMESInstrumentations to turn off, comma-separated; sets FI_DISABLED_INSTRUMENTATIONS
--sample-rate RATETrace sample rate from 0 to 1 (default 1.0); sets FI_SAMPLE_RATE
--debugPrints agent debug output; sets FI_AUTO_DEBUG
--go-injectExperimental Go injection; needs an installed Go agent payload
--dry-runPrints the environment variables fagi run would set (secrets masked) without starting the program. It still checks that the agent is installed

What happens at launch:

  • A recognized runtime (python, node, ruby, java, or go with --go-inject) without its agent, or any program when no runtime has an agent: fagi run starts nothing and exits 2 (details.code: agents_not_included with --json)
  • Node wrappers (npm, npx, pnpm, yarn) count as Node: with a Node agent they start after one fagi: warning that they may run untraced, and without one they exit 2. Launch node app.js directly. tsx and ts-node are checked like node
  • Other programs, such as make, sh -c or uv run, start after the same warning when some runtime has an agent
  • Otherwise fagi run exits with your program’s status (127 if it isn’t found, 126 if it isn’t executable). Its own errors use the shared exit codes

Warning

The program and its child processes receive your key pair (FI_API_KEY, FI_SECRET_KEY) and FI_BASE_URL, so run only programs you trust. Inputs and outputs are captured by default; set FI_HIDE_INPUTS=true or FI_HIDE_OUTPUTS=true to hide them where the agent supports it.

fagi trace env --format sh node
fagi trace env --format dockerfile node

fagi trace env [RUNTIME...] prints the environment variables that fagi run would set, without credentials, for a container image or a process manager that starts your app itself. --format takes sh (the default), fish, powershell, dockerfile or json. --format json is for inspection only; you can’t apply it as configuration. Run trace env where the app will run (or in the image build), because it prints local agent paths. Dockerfile ENV values expand at build time.

Install an agent

fagi trace install node --from /path/to/trusted-node-agent

fagi trace install RUNTIME --from PATH copies an agent you trust into the CLI’s private agent cache, for node, go or ruby. fagi doctor then lists it under Agents. No agent download is available yet; --from takes an agent directory you already have, such as a trusted build of @traceai/auto for Node.

Only --from with a local agent works today. --url and --sha256 are for Java and .NET agents, which aren’t available yet, and fagi trace install all isn’t supported.

Add the Go agent in code

fagi trace setup go          # the current directory
fagi trace setup go ./cmd/api

fagi trace setup go [DIR] adds the Go agent imports and module changes to your source, for you to review and commit. A binary built from it includes the agent, so fagi run starts it with no other setup. Pass the module’s real directory, not a symlinked path.

Read a stored trace

fagi trace view "$TRACE_ID"                         # project from futureagi.toml
fagi trace view "$TRACE_ID" --project support-bot --json
fagi trace view "$TRACE_ID" --project-id "$PROJECT_ID" -o jsonl

Get a trace ID from fagi trace watch --until-first --json (below), from your app’s OpenTelemetry logs, or from the trace page in the platform. The 32-hex ID an OpenTelemetry SDK prints works too. The table shows each span’s name, type, duration and error as an outline, then the trace’s summary fields:

TRACE       PROJECT
trace-demo  11111111-1111-4111-8111-111111111111
SPAN        TYPE       DURATION  ERROR
agent       agent      125 ms    -
  retrieve  retriever  -         -
  answer    llm        0 ms      returned error
SUMMARY     VALUE
span_count  3
Snapshot of stored spans; capture completeness is unknown.

A missing value shows as -, never zero. The outline stops at 200 rows and eight levels; --json keeps the complete response. A span error does not change the exit code: view is a reader, not a gate. Every read is a snapshot of the spans stored when the request ran, not proof that every span was captured.

Choose the project

trace view and trace watch pick the project the same way:

SelectionWhat it does
--project-id UUIDUsed as given, with no lookup
--project NAMEThe exact, case-sensitive project name, resolved with one lookup
NeitherThe project_name in futureagi.toml (or the file FI_CONFIG_FILE names), then FI_PROJECT_NAME

A name with no exact match exits 2 with project_not_found, and one that matches several projects exits 2 with project_ambiguous; pass --project-id instead. trace check, trace diff and trace export take only --project-id. To find a project’s UUID, run fagi api observability projects list --name support-bot --json.

Watch for new traces

fagi trace watch --until-first --timeout 90s
fagi trace watch --project support-bot --max-polls 30 -o jsonl
fagi trace watch --project-id "$PROJECT_ID" --trace-id "$TRACE_ID" --until-first --json

In a script, capture the ID of the first trace that arrives:

TRACE_ID=$(fagi trace watch --until-first --json \
  | jq -r '.events[] | select(.event == "trace_observed") | .trace.trace_id')

Run your app separately, then watch its project. trace watch polls the project’s stored traces and prints each trace once when it appears and again when it changes. It reads one page of 100 stored traces on each poll, in the server’s default order, so on a busy project it can miss a trace that arrives and drops out between polls. Pass --trace-id to wait for a specific one.

FlagWhat it does
--project, --project-idThe project, chosen as for trace view
--trace-id IDOnly rows for this trace ID
--until-firstStops after the first matching row
--poll-intervalWait between polls, at least 1s (default 2s)
--max-polls NMost requests, 1 to 300 (default 30)
--timeoutDeadline for the whole watch (default 90s). Here it replaces the global per-request --timeout; each request is still capped at 30 seconds

A matching row can predate the watch, even with --trace-id, so a match does not prove your latest run exported a trace. When --until-first sees no match within its budget, the command exits 6 with details.code: trace_not_observed; check the project and trace ID, or rerun with a larger --max-polls or --timeout. Network errors also exit 6, so tell them apart by details.code in --json output. Without --until-first, a watch that uses up its polls or time exits 0. A canceled watch exits 6 with watch_canceled. Other errors use the shared exit codes.

Gate a build on a trace

fagi trace check fails a build when a stored trace shows a problem a final-answer eval can’t see: a tool span that errored, a step that never ran, or a latency, token or cost budget that was exceeded.

mkdir -p reports &&
  fagi trace watch --project-id "$PROJECT_ID" --trace-id "$TRACE_ID" --until-first &&
  fagi trace check "$TRACE_ID" --project-id "$PROJECT_ID" \
    --max-error-spans 0 --require-span retrieve --require-span answer \
    --max-llm-tokens 20000 --max-llm-cost 0.05 --junit-file reports/trace.xml

Spans can be stored a little after your app returns, so wait for the trace with trace watch before you check it.

FlagWhat it does
--project-id UUIDThe project that owns the trace (required)
--max-error-spans NAt most N spans have an error status or error field
--require-span NAMEA span with exactly this name appears; repeatable
--max-root-latency-ms NThe top-level span’s latency_ms is at most N
--max-llm-tokens NTotal tokens over llm spans are at most N
--max-llm-cost DECIMALTotal cost over llm spans is at most this decimal, such as 0.05
--allow-unknownCounts a check that depends on missing values as passing
--junit-file PATHAlso writes one JUnit testcase per check. The parent directory must exist

Pass at least one check, or the command exits 2. Limits are inclusive. Token and cost sums cover llm spans only, because agent and chain spans can carry rolled-up totals. A missing metric makes a check unknown, and an unknown check fails unless you pass --allow-unknown.

When every check passes, the command exits 0. When one fails, it exits 5 and prints the report on stdout with nothing on stderr. The report names each failing check, the spans behind it, and what to try next:

TRACE       PROJECT
trace-demo  11111111-1111-4111-8111-111111111111
CHECK                   LIMIT  OBSERVED  RESULT
error spans             0      1         fail
required span retrieve  -      present   pass
required span answer    -      present   pass
llm tokens              20000  18342     pass
llm cost                0.05   unknown   fail
Trace check failed: 2 of 5 checks.
error spans: 1 span returned an error: web_search.
llm cost: 1 of 2 llm spans returned no usable cost: answer.
Snapshot of stored spans; capture completeness is unknown.

Next steps:
  1. see each span with fagi trace view TRACE_ID --project-id PROJECT_UUID
  2. record the missing values in your traces, or pass --allow-unknown to count unknown results as passing

Exit 5 fails a CI step.

Compare two runs

fagi trace diff "$BASE_TRACE_ID" "$CANDIDATE_TRACE_ID" --project-id "$PROJECT_ID"
fagi trace diff "$BASE_TRACE_ID" "$CANDIDATE_TRACE_ID" --project-id "$PROJECT_ID" \
  --candidate-project-id "$OTHER_PROJECT_ID" --json

After you change a prompt, model or tool, trace diff shows what changed in the agent’s run: steps added or removed, new errors, and latency, token and cost changes per step. --project-id is required; --candidate-project-id reads the second trace from another project. Spans are matched by their name path under their parents, and the summary counts these matched (aligned) spans.

SPAN            STATUS      LATENCY_MS    TOKENS        COST
  agent         OK          4210 -> 2980  -             -
    retrieve    OK          820 -> 790    -             -
-   web_search  ERROR -> -  1200 -> -     -             -
+   rerank      - -> OK     - -> 240      -             -
    answer      OK          3100 -> 1900  1840 -> 1210  0.0090 -> 0.0011
5 aligned spans: 1 added, 1 removed, 3 changed, 0 unchanged.
Error spans: 1 -> 0.
LLM totals (llm spans only): tokens 1840 -> 1210, cost 0.0090 -> 0.0011.
Spans align by name path; a renamed, moved or dynamically named span shows as removed plus added.
This compares two stored runs, not a verdict; capture completeness is unknown.

A renamed or moved span, or one whose name embeds an ID, shows as removed plus added. When they apply, the notes also list new errors and model or type changes.

trace diff is a reader: it exits 0 whenever both traces were read, even when the candidate has new errors or costs more. Use trace check to fail a build. Agents are often nondeterministic, so compare several pairs before you draw a conclusion.

Export traces as dataset rows

fagi trace export "$TRACE_ID_1" "$TRACE_ID_2" --project-id "$PROJECT_ID" -o jsonl > regressions.jsonl

trace export turns stored traces, such as failing production runs, into one flat row per trace with the input and output of one span. To keep the rows as eval cases, see Turn failing traces into an eval set.

FlagWhat it does
--project-id UUIDThe project that owns the traces (required)
--ids-file PATHTrace IDs, one per line or as JSONL objects with trace_id or id; - reads stdin
--max-traces NMost traces to read, 1 to 100 (default 25)
--span root|NAMEThe span that supplies each row: the top-level span (the default) or the one span with this name
--file PATHWrites the rows to a .jsonl file and prints a summary instead

Each row has trace_id, trace_name, span_name, input, output, status, latency_ms and model. To export the traces of an error cluster:

fagi api error-feed clusters traces list --cluster-id "$CLUSTER_ID" \
  --limit 25 --fields id -o jsonl |
  fagi trace export --ids-file - --project-id "$PROJECT_ID" --file evals/regressions.jsonl

Each cluster row’s id is the trace ID; --fields id keeps only that field, which --ids-file - reads.

Rows hold your application’s real inputs and outputs, which can include personal data, and the CLI does not mask them. Review the file before you share or upload it. If any trace fails, nothing is written and an existing --file stays unchanged.

Dive deeper

Was this page helpful?

Questions & Discussion