# Measure Pull Requests from Coding Agents

Set up Claude Code or Codex so every pull request it works on shows its token cost in LangWatch.

> Page: https://langwatch.ai/docs/coding-agents/measure-pull-requests
> Source: https://github.com/langwatch/langwatch/blob/main/docs/coding-agents/measure-pull-requests.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

Three things have to be in place for a pull request to show its cost: the agent is wired with `langwatch instrument` ([a wrapper run](#which-agents-report-their-pull-requests) can also do it), you start the agent inside the repository's folder, and your organization has GitHub connected. This page covers each one, what the command writes on your machine, and how to remove it again.

## Set it up

### 1. Install the command-line tool and sign in

```bash
npm install -g langwatch
langwatch login --device
```

Install it globally, not through `npx`. The Claude Code hooks that `langwatch instrument` writes call the `langwatch` command, and the Codex `notify` entry points at the copy that ran the setup, so a copy that npm deletes from its `npx` cache stops the reporting. The same applies to a wrapper started with `npx langwatch <tool>`.

### 2. Wire the agent

```bash
langwatch instrument claude
langwatch instrument codex
```

Run the line for each agent you use. The command prints where the telemetry goes, then exits. [The table below](#what-langwatch-instrument-does) lists every file it writes.

### 3. Connect GitHub

An organization admin connects GitHub once, under **Settings → Integrations**. LangWatch needs it to turn a branch into its pull requests. The GitHub App must have access to the repository: if it has access to selected repositories only and this one is not selected, the branch's row on **Pull requests** shows **Link this repo** beside the repository name. An organization admin can use that button to grant access.

### 4. Start the agent inside the repository

```bash
cd ~/code/your-repo
claude
```

Open a new terminal first if the agent was already running: it reads its configuration only at start.

> **Warning:** Start the agent from inside the repository folder. If you start the agent from your home directory, or any folder that is not a git checkout, the session reports no repository, so its cost shows in usage but on no pull request.

## What `langwatch instrument` does

The command takes one tool name: `claude`, `codex`, `gemini`, `opencode`, `copilot`, `code` (the GitHub Copilot extension in VS Code), `grok` (Grok CLI) or `pi`. It runs these steps in order:

1. Checks that your organization allows the tool to send telemetry directly. If it does not, the command stops before it writes anything and tells you to use `langwatch <tool>` instead.
2. Picks the destination. With no flag, that is the project this tool is already pinned to, if it has one. Otherwise it is your personal workspace, using the ingest key cached for this machine or a new one. `--project <id-or-slug>` mints a key for a team project, `--key <ingest-key>` uses a key you paste (or `LANGWATCH_INGEST_KEY`), and `--personal` clears a project pin. You can pass only one of the three.
3. Warns you if the endpoint is plain `http`, because the ingest key then crosses the network unencrypted. For a local instance it warns instead that the tool's global configuration file is about to be rewritten.
4. Writes the wiring for that tool, listed in the table below.
5. Prints `wired <tool>: plain <tool> runs now send telemetry to <destination> at <endpoint>`.

`--endpoint <url>` sets the LangWatch address for a pasted `--key`, for example a self-hosted instance. It is refused without a key: `--key`, or `LANGWATCH_INGEST_KEY` set in the shell when you pass no other flag.

| Tool       | What the command writes                                                                                                                                                                                                         |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `claude`   | The OpenTelemetry variables into the `env` block of `~/.claude/settings.json` (or `$CLAUDE_CONFIG_DIR/settings.json`), plus a `SessionStart` and a `Stop` hook in the same file. With the LangWatch Claude Code plugin installed, the plugin runs the hooks instead. |
| `codex`    | An `[otel]` block and a `notify` program in `$CODEX_HOME/config.toml`, plus an instruction block in `$CODEX_HOME/AGENTS.md` (`~/.codex` by default). Your own `notify` program keeps running after the LangWatch one. If your `notify` cannot be moved safely, the command prints a warning that the conversation will not be recorded and installs no `notify` program, so a plain `codex` session reports tokens but no conversation, and no repository unless `langwatch ingest context` runs and the declaration reaches LangWatch. To fix it, remove your `notify` setting and run the command again. |
| `gemini`   | A `gemini` shell function in `~/.zshrc`, `~/.bashrc` or `~/.config/fish/config.fish`.                                                                                                                                           |
| `opencode` | An `opencode` shell function in the same file, plus `experimental.openTelemetry: true` in `~/.config/opencode/opencode.jsonc` (or under `$XDG_CONFIG_HOME`). If you set that flag to `false` yourself, the command fails and asks you to remove the line.                                                                                                 |
| `copilot`  | A `copilot` shell function in the same file, plus a `sessionStart` and an `agentStop` hook in `~/.copilot/hooks/langwatch.json` (or `$COPILOT_HOME/hooks/langwatch.json`). If a file of yours is already at that path, the command leaves it alone and warns that Copilot sessions will not report their repository. |
| `code`     | A `code` shell function in the same file, the same Copilot hook file, plus a VS Code setting that clears the telemetry variables from its integrated terminals, so the key does not leak into every command you run there. |
| `grok`     | A `grok` shell function in the same file, plus a `SessionStart`, a `Stop` and a `SubagentStart` hook in `~/.grok/hooks/langwatch.json` (or `$GROK_HOME/hooks/langwatch.json`). If a file of yours is already at that path, the command leaves it alone and warns that Grok sessions will not report their repository. |
| `pi`       | A `pi` shell function in the same file, plus the LangWatch extension `~/.pi/agent/extensions/langwatch-pi-telemetry.ts` (or under `$PI_CODING_AGENT_DIR/extensions/`). The extension sends the telemetry and reports the repository. If a file of yours is already at that path, the command fails. |

The shell function sets the variables only for that one tool run, so other commands you start from the parent shell do not see the ingest key.

## Which agents report their pull requests

Claude Code, Codex, GitHub Copilot, GitHub Copilot in VS Code (`code`), Grok CLI and pi report the repository and branch after `langwatch instrument`, which is what joins a session to a pull request. Both Copilot tools read the same hook file. pi has no hooks of its own, so its LangWatch extension reports the repository each time the agent starts work.

A `langwatch <tool>` wrapper run can also set this up. For Codex, a run that sends telemetry directly installs the `notify` program. For Claude Code and both Copilot tools, a direct run writes the reporting hooks only when you accept its prompt to keep the plain command reporting, or when that wiring is already there. A gateway-route run writes none.

Grok CLI and pi only run directly. A `langwatch grok` run writes the Grok hook file every time, with no prompt, and a `langwatch pi` run writes or refreshes the pi extension every time.

For Claude Code and both Copilot tools, if a direct run leaves no hooks in the file [the table above](#what-langwatch-instrument-does) names, and no LangWatch plugin runs them, run `langwatch instrument <tool>`: with no hooks, a session does not report its repository by itself.

For Codex, look in `$CODEX_HOME/config.toml` for the `# >>> langwatch codex notify begin >>>` block. If it is missing, run `langwatch instrument codex`, which also turns conversation capture back on. If that command warns about your own `notify`, use the fix in the same table.

For Gemini CLI and opencode, these commands wire the telemetry only, so their sessions show cost and tokens on [the sessions list](https://langwatch.ai/docs/coding-agents/sessions) but are not joined to a pull request.

## Why the agent must start inside the repository

The repository comes from git, run in the session's working directory. At the start of each Claude Code session and after every turn, the `SessionStart` and `Stop` hooks run `langwatch ingest hook claude-code`, which reads:

- `git remote get-url origin` for the repository host, owner and name;
- `git branch --show-current` for the branch;
- `git rev-parse --git-dir` and `--git-common-dir` to tell a linked worktree from the main checkout.

LangWatch uses the working directory the agent reports, so a `cd` inside a shell command does not move it, but in Claude Code a checkout switch the agent makes does. Codex reads the values through its `notify` program after each turn, from the directory where the session started. [Repository and PR attribution](https://langwatch.ai/docs/coding-agents/repository-and-pr-attribution) explains both reporting channels and their limits.

A Claude Code session reports no repository in each of these cases:

- you started it outside a git checkout;
- the checkout has no remote named `origin`;
- the `origin` URL does not parse to a host, an owner and a name.

For Codex, when the working directory is not a git checkout with an `origin` remote, the repository URL and branch that Codex recorded for the session are used.

On a detached `HEAD` the session reports the repository but no branch, so it maps to no pull request. When an agent works in a different checkout through shell commands, have it run `langwatch ingest context` from inside that checkout. [Repository and PR attribution](https://langwatch.ai/docs/coding-agents/repository-and-pr-attribution) covers that command.

## What you see in LangWatch

Open **Pull requests** in the sidebar of the workspace you wired: your personal workspace, or the project you passed with `--project`. Each session sends one record with these attributes, and again whenever one of them changes:

| Attribute               | Value                                             |
| ----------------------- | ------------------------------------------------- |
| `vcs.repository.host`   | The host from the `origin` URL, such as `github.com` |
| `vcs.repository.owner`  | The owner or organization                         |
| `vcs.repository.name`   | The repository name, without `.git`               |
| `vcs.ref.head.name`     | The current branch                                |
| `vcs.worktree.name`     | The worktree folder name, for a linked worktree   |

For each branch a session reported, LangWatch asks GitHub for every pull request in that repository whose head is `<owner>:<branch>`, open, closed or merged. Pull requests opened from a fork against another repository do not show, because LangWatch looks up pull requests only in the repository your `origin` remote points to. The [pull requests page](https://langwatch.ai/docs/coding-agents/pull-requests) explains the columns and the per-contributor breakdown.

## What content is captured, and how to turn it off

By default the wiring captures the conversation as well as the token counts. To turn content off at the source, remove the content variables for your tool, then restart the agent. Cost and token counts keep flowing. The variable names are on each tool page, for example [Claude Code](https://langwatch.ai/docs/coding-agents/claude-code#content-and-privacy), and [Privacy](https://langwatch.ai/docs/coding-agents/privacy) explains the controls.

For Claude Code, also remove them from `.claude/settings.local.json` in any folder where you ran `langwatch claude`, because that file overrides your user settings.

For Codex, the conversation is read from the transcript by the `notify` program. The same program reports the repository and branch, so with the `langwatch instrument` wiring, removing it also stops the automatic pull request join: see [the Codex page](https://langwatch.ai/docs/coding-agents/openai-codex).

A removal on your machine may not last: `langwatch instrument`, a `langwatch <tool>` wrapper run that sends telemetry directly and `langwatch login` can write the variables back, and for Codex the `notify` program too. For content to stay off, drop it for the whole organization at ingestion: see [Privacy](https://langwatch.ai/docs/coding-agents/privacy).

## Undo it

```bash
langwatch logout
```

`langwatch logout` signs this machine out and removes what LangWatch wrote:

- for Claude Code: the telemetry variables and hooks, the LangWatch plugin and its marketplace entry, and the project pin in the current folder;
- for Codex: the `[otel]`, `notify` and gateway blocks, its profile file, the `AGENTS.md` instruction and its entries in `~/.codex/hooks.json`;
- the Copilot, Grok and opencode session hook files or plugins, and the pi extension;
- the capture agent of the GitHub Copilot app;
- the shell functions, and an older gateway block in your shell file;
- the VS Code setting that clears the telemetry variables.

In a file you also edit, it leaves the rest as it was.

It reads a Claude Code project pin (`.claude/settings.local.json`) only in the current folder, so run it from each folder where you ran `langwatch claude`, linked worktrees included.

- `--keep-credentials` removes the wiring and keeps you signed in.
- `-y` skips the confirmation prompt.

`experimental.openTelemetry` stays set in `opencode.jsonc`. Without the shell function it has no LangWatch endpoint to send to, and you can delete the line yourself.

**Also check:** [Pull request cost](https://langwatch.ai/docs/coding-agents/pull-requests) for reading the results, and [Repository and PR attribution](https://langwatch.ai/docs/coding-agents/repository-and-pr-attribution) for agents that move between checkouts.

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.
