> ## Documentation Index
> Fetch the complete documentation index at: https://agentrail.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Record your CLI's calls

> agentrail-cli-sdk tells your own API which command an agent ran, in which run and session, so the agent's CLI calls join its journey.

`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`](https://agentrail.co/docs/guides/journeys-node) or [`agentrail-edge`](https://agentrail.co/docs/guides/journeys-edge) 0.4.0, [`agentrail-go`](https://agentrail.co/docs/guides/journeys-go) v0.4.0, or [`agentrail-python`](https://agentrail.co/docs/guides/journeys-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

```sh theme={null}
npm install agentrail-cli-sdk
```

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:

```ts commander theme={null}
import { Command } from 'commander';
import { instrumentCli } from 'agentrail-cli-sdk';

export const cli = instrumentCli({
  name: 'acme',
  version: '2.3.1',
  origins: ['https://api.example.com'],
});

const program = cli.commander(new Command('acme'));
// ... your commands
await program.parseAsync();
```

```ts yargs theme={null}
// yargs 17 and 18
await yargs(hideBin(process.argv))
  .middleware(cli.yargs)
  .command(/* ... */)
  .parseAsync();
```

```ts oclif theme={null}
// src/hooks/agentrail.ts, named in package.json under oclif.hooks.prerun
import { cli } from '../agentrail.js';

export default cli.oclif;
```

```ts Any other framework theme={null}
cli.command('deploy create');
```

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:

```ts theme={null}
import { instrumentCli } from 'agentrail-cli-sdk';

const cli = instrumentCli({
  name: 'acme',
  origins: ['https://api.example.com'],
  watchRequests: false,
});

await fetch(url, {
  headers: { ...cli.headers(url), ...yourHeaders },
  redirect: 'manual',
});
```

`cli.headers(url)` is empty when the SDK is off or `url` is not on one of your origins.

## What the header says

| Part | What it is |
| - | - |
| CLI and version | Your CLI's `name` and `version`, from your options |
| Run and sequence | A new id for each run of the CLI, and the request's place in it |
| Session | A one-way hash of the session, and where it came from (see [The session](#the-session)) |
| Mode | `agent`, `ci`, `interactive` (a terminal) or `non-interactive` (a script) |
| Agent | The agent running the CLI and its version, when one is detected |
| Evidence | The names of the environment variables that said so, never their values |
| Command | The command, as your code declared it, such as `deploy create`. None for the CLI's root command. |
| SDK version | The version of `agentrail-cli-sdk` |

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:

```ts theme={null}
if (cli.mode === 'agent') output.json = true;
```

`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

| Option | Default | What it does |
| - | - | - |
| `name` | Required | Your CLI's name, as people type it. |
| `version` | None | Your CLI's version. |
| `origins` | Required | The exact origins of your API, such as `https://api.example.com`. Never list an origin you do not run. |
| `enabled` | `true` | `false` turns everything off. |
| `sessionScope` | `name` | Tools of yours that share sessions give the same scope. |
| `watchRequests` | `true` | `false` adds the header to no request: use `headers(url)` instead. |
| `onError` | `console.error` | Told what could not be recorded. Your command carries on. |
| `env` | `process.env` | The environment to read. |

`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](https://agentrail.co/docs/guides/journeys-data.md#your-servers).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.