Skip to main content
agentrail-cli-sdk is for a CLI that calls your own API. When an agent such as Claude Code or Codex runs your CLI, the CLI’s calls look like any other client’s. The SDK puts one header, x-agentrail-cli, on those calls, saying which command ran, in which run, in which of the agent’s sessions and under which agent. The server SDK or edge in front of your API records it, so the agent’s CLI calls join the rest of its journey. It needs Node 22 or later. The header goes only to the origins you list, never to Agentrail, and never carries what people type. The SDK makes no network call of its own and writes at most one small file.

Before you start

Your API must be recorded by a server SDK or the edge that reads the header: agentrail-node or agentrail-edge 0.4.0, agentrail-go v0.4.0, or agentrail-python 0.3.0, or later. On an older one, the header reaches your API and changes nothing. There is nothing to configure on that side.

Install it

It is an ES module. From CommonJS, require it on Node 22.12 or later.

Start it

Call instrumentCli first thing, before your CLI parses its arguments or makes a request, then hand it your framework, so each request names the command being run:
The command is named by the words your code declared, such as deploy create, even when someone typed an alias. Arguments and flag values are never read.

Which requests carry it

In Node, requests made with fetch, undici, http or https, or a client built on them, such as axios, got, node-fetch and most generated SDKs, get the header on their own. It goes only to the exact origins in origins, checked for each request as it is made: when your API redirects elsewhere, such as to a download host, the header comes off. Bun and Deno do not let the SDK see requests. There, set watchRequests: false, add the header to each request yourself, and follow redirects yourself:
cli.headers(url) is empty when the SDK is off or url is not on one of your origins.

What the header says

It never carries arguments, flag values, paths, environment values or the agent’s own session id. Your server sends the session to Agentrail only as a keyed hash, made with your workspace’s hash key.

The session

One agent session is one journey, however many times the agent runs your CLI. The session comes from, in order:
  1. The agent’s own session, where the agent gives one to the commands it runs, as Claude Code, Codex, Copilot CLI, OpenCode and Goose do.
  2. The CI run, on GitHub Actions, GitLab, CircleCI, Buildkite, Azure Pipelines, Bitbucket, Jenkins, CodeBuild and Travis.
  3. A local session, kept in a small file in the user’s state folder. It ends after 30 minutes without a request and lasts a day at most, and a person’s runs and each agent’s are kept apart.
  4. The run alone, when no file can be written, such as in a sandbox.
The value sent is a one-way hash of that source and your sessionScope, which is your CLI’s name unless you set one. So the agent’s session id never leaves the machine, and two companies’ CLIs in one agent session send values that cannot be matched. Give your other tools, such as a second CLI or a local MCP server, the same sessionScope so they share one session.

Agents, CI and people

cli.mode and cli.agent say who is running the CLI, even when the SDK is off, so you can answer an agent as it reads best:
cli.agent is the detected agent’s id, such as claude-code or codex, and its version, or null. A bare CI=1 is not read as CI, because some agents set it in their shells.

Turn it off

  • DO_NOT_TRACK=1 turns everything off.
  • enabled: false turns it off: wire your CLI’s own telemetry setting here.
  • cli.close() turns it off for the rest of the run.
  • AGENTRAIL_CLI_DEBUG=1 prints each header, and each request left without one, to standard error.
cli.enabled is false when it is off.

Options

name, version and sessionScope are 1 to 64 printable ASCII characters, with no space at either end. origins holds exact http or https origins, without wildcards. Any other value throws a ConfigurationError when instrumentCli runs.

What you see

In a journey, a request your CLI made shows as its command, such as acme deploy create, instead of its method and path. The Map tab shows the CLI as a place of its own. For what each source sends, see What Agentrail records.