A background agent spends tokens the same way an interactive one does, and needs the same accounting. This page covers machines with no interactive login: a VPS full of agents, a CI job, a base image, a managed fleet.
One machine, no login
--key takes an ingest key (ik-lw-...). It needs no login and contacts no server:
langwatch instrument claude --key ik-lw-...langwatch instrument codex --key ik-lw-...langwatch instrument pi --key ik-lw-...langwatch instrument grok --key ik-lw-...GitHub Copilot CLI is different: langwatch instrument copilot writes a shell function to your shell's startup file, which a non-interactive bash or zsh shell, such as a CI step or a service, does not load. On those machines, export the Copilot settings in the job or the image instead.
langwatch ingest install copilot_cli --env-only prints those settings, but it needs a signed-in device, so run it on a machine where you are signed in and copy the lines, as Bake it into an image shows. With an ingest key alone, write the same lines by hand from Manual OpenTelemetry setup.
pi and Grok work the same way: langwatch instrument pi and langwatch instrument grok each write a shell function, because neither tool reads its export settings from a file. In a CI step or a service, set the variables from pi manual setup or Grok manual setup in the job instead.
pi also needs its telemetry extension on the machine, so run langwatch instrument pi --key ik-lw-... once in the job first, or langwatch instrument pi with LANGWATCH_INGEST_KEY set: it writes the extension, and pi sends no telemetry without it.
The command reads the same value from the environment, which is how you keep the key out of shell history and provisioning scripts:
export LANGWATCH_INGEST_KEY=ik-lw-...langwatch instrument claudeFor a self-hosted instance, add its address:
langwatch instrument claude --key ik-lw-... --endpoint https://langwatch.your-company.internalAn ingest key can create traces and no other requests, so a leaked key from a build log cannot read or change project data. Get one from the dashboard: the ingestion template install drawer mints one for the tool, scoped to the project it should report to.
CI
The same setup, inside the job. For example, a GitHub Actions job running Claude Code headless:
jobs: nightly-maintenance: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm install -g langwatch @anthropic-ai/claude-code - run: langwatch instrument claude env: LANGWATCH_INGEST_KEY: ${{ secrets.LANGWATCH_INGEST_KEY }} - run: claude -p "Update the dependency table in README.md" env: ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}A job running pi headless writes the extension first, then sets the variables on the step that runs pi:
- run: npm install -g langwatch @earendil-works/pi-coding-agent - run: langwatch instrument pi env: LANGWATCH_INGEST_KEY: ${{ secrets.LANGWATCH_INGEST_KEY }} - run: pi -p "Update the dependency table in README.md" env: OTEL_TRACES_EXPORTER: otlp OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: "true" OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf OTEL_EXPORTER_OTLP_ENDPOINT: https://app.langwatch.ai/api/otel OTEL_EXPORTER_OTLP_HEADERS: Authorization=Bearer ${{ secrets.LANGWATCH_INGEST_KEY }} OTEL_RESOURCE_ATTRIBUTES: service.name=pi ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}The session appears on the project's sessions list like any other, and its branch work attributes to pull requests the same way.
For CI jobs that use the LangWatch SDK or CLI itself rather than a coding agent, langwatch login --api-key <sk-lw-...> writes a project API key to .env non-interactively, and langwatch login --token <token> installs a pre-minted device token. Both fail fast on a machine without a terminal instead of hanging on a device-code prompt.
Bake it into an image
langwatch ingest install <tool> --env-only prints the export lines without writing any config file, which is the form you want in a Dockerfile or a provisioning script. Supported tools: claude_code, codex, gemini, opencode, copilot_cli, grok_cli, pi_cli. Add --json for machine-readable output. The command needs a signed-in device, unlike --key.
--env-only also skips the hooks that report each session's repository and branch. For copilot_cli, that is the file ~/.copilot/hooks/langwatch.json, and without it Copilot sessions reach no pull request. Run the command without --env-only to write it. See GitHub Copilot.
For grok_cli, the skipped hook file is ~/.grok/hooks/langwatch.json, so Grok sessions report no repository or branch. For pi_cli, the skipped file is pi's telemetry extension, and without it pi sends no telemetry at all.
Write pi's extension when the container starts, not while the image builds: run langwatch instrument pi with LANGWATCH_INGEST_KEY supplied as a runtime secret. Both commands that write the extension also save the ingest key to the CLI's config file, so running either in a build step leaves the key in an image layer.
Roll out to a managed fleet
For Claude Code, an administrator can apply the telemetry configuration to every machine through the managed settings file, distributed with your device management system:
- macOS:
/Library/Application Support/ClaudeCode/managed-settings.json - Linux and Windows Subsystem for Linux:
/etc/claude-code/managed-settings.json - Windows:
C:\ProgramData\ClaudeCode\managed-settings.json
Put the same env block in it that Manual OpenTelemetry setup shows for ~/.claude/settings.json. Variables in the managed settings file take precedence, and a user cannot override them.
Name the sessions of a background agent
The Sessions page names a session by the title the agent generated or by the first line of its first prompt. A background agent often starts every session with the same scripted text, so its sessions all get the same name, or none.
To name them yourself, add langwatch.session.name to the resource attributes the agent exports with. Keep the ones already in OTEL_RESOURCE_ATTRIBUTES, such as service.name=claude-code, and append the name after a comma:
export OTEL_RESOURCE_ATTRIBUTES="${OTEL_RESOURCE_ATTRIBUTES:+$OTEL_RESOURCE_ATTRIBUTES,}langwatch.session.name=nightly-maintenance"Every session the agent starts is then listed under that name. When the LangWatch hooks report a name the harness holds, such as one from Claude Code's --name or /rename, that name replaces it.
Keep a long-lived agent on one session
Each Claude Code session gets its own row on the Sessions page. An agent that restarts, or starts a new session for each scheduled run, ends up with one row per run.
To list all of them as one session, add langwatch.session.id to the same resource attributes, with an id that stays the same for the agent:
export OTEL_RESOURCE_ATTRIBUTES="${OTEL_RESOURCE_ATTRIBUTES:+$OTEL_RESOURCE_ATTRIBUTES,}langwatch.session.id=nightly-maintenance,langwatch.session.name=nightly-maintenance"Every run recorded from then on adds its traces, tokens and cost to that one session. Runs recorded before keep their own rows. Link to the session with /<project>/sessions?session=nightly-maintenance.
If the managed settings file already sets OTEL_RESOURCE_ATTRIBUTES, it wins over the shell export, so add langwatch.session.id to the value in the managed settings file instead.
See your machines
Every CLI sign-in, CI runners included, appears under Devices on /coding-agents/configure, where you revoke one machine or all of them. An organization can cap how long a device session lives before it must sign in again; see the session policy on the governance dashboard.
Also check: Team projects for pinning interactive machines to a shared project with a login.