Get Exact Per-Turn Cost in Claude Code¶
Claude Code writes no per-turn cost in its transcript. It reports one total for the whole session, and only when the session ends. Without help, OpenALR can give each turn only an allocated share of that total.
alr otel start fixes that. It starts a small receiver on your own machine and
points Claude Code at it. Claude Code then sends the cost of each model call
to the receiver, and OpenALR adds the calls up per turn.
alr otel start
This page applies to Claude Code only. Grok Build reports a cost for each turn
itself. Hermes passes one on when its model provider reports it; when it does
not, the turn reads ? and est. prices it. Neither needs a receiver.
Why a Turn Shows "in parent"¶
At the end of a turn, the record is written before Claude Code's cost for it
is known. The turn is on record with its model and tokens, and its cost column
reads in parent: in Claude Code, a turn's cost sits inside its session's.
Until the figure arrives, est. shows a price-book estimate marked
(for parent), and it is never added to anything.
Start the Receiver¶
alr otel start
The command does two things.
- It starts the receiver in the background. The receiver is
alr otel-sink. It runs as you, with no administrator rights, and listens on the loopback address127.0.0.1only, port 4318 by default. Use--portto change the port. Its output goes tosink.login theotelfolder of alr's state directory. - It sets four Claude Code settings. They go in the
envobject of your Claude Code user settings file, and nothing else in the file is changed.
| Setting | Value |
|---|---|
CLAUDE_CODE_ENABLE_TELEMETRY |
1 |
OTEL_LOGS_EXPORTER |
otlp |
OTEL_EXPORTER_OTLP_LOGS_PROTOCOL |
http/json |
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT |
The receiver's address, with a private token in its path |
The token is made once, kept in a private file beside the receiver's other files, and never printed. The receiver accepts only requests that carry it. It keeps the cost, the session, and the prompt id of each model call. It does not keep prompts or responses.
The settings file is $HOME\.claude\settings.json. alr's state folder
is $HOME\.local\state\openalr, and the receiver's files are in its
otel folder.
The settings file is ~/.claude/settings.json. alr's state folder is
~/.local/state/openalr (under $XDG_STATE_HOME/openalr when that is
set), and the receiver's files are in its otel folder.
If CLAUDE_CONFIG_DIR is set, the settings file is
$CLAUDE_CONFIG_DIR/settings.json instead. To use another home, pass
--home DIR. Setting ALR_EMIT_HOME moves the state folder to
<ALR_EMIT_HOME>/state.
The command prints what it did:
alr otel start: started the sink at http://127.0.0.1:4318/<token>/v1/logs (pid 156710; log <home>/state/otel/sink.log)
alr otel start: set CLAUDE_CODE_ENABLE_TELEMETRY, OTEL_LOGS_EXPORTER, OTEL_EXPORTER_OTLP_LOGS_PROTOCOL, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT in ~/.claude/settings.json
alr otel start: restart claude: a running claude keeps its old settings until it is restarted
Run it again and it changes nothing: it says the sink is already running and
the settings are already there. If one of the four settings already held a
value of your own, start replaces it, says so, and keeps your value so that
stop can put it back. If the settings file cannot be read as one JSON object,
start refuses and leaves it as it is. It also refuses when something else
holds the port.
Restart Claude Code¶
A running Claude Code keeps its old settings. Quit it and start it again after
alr otel start. A session that was already open when you started the receiver
keeps ? for its costs.
The receiver must be running whenever Claude Code runs. It is a background
process and does not come back after a reboot, so run alr otel start again
after one.
When the Figure Arrives¶
Claude Code's telemetry reaches the receiver a moment after the turn ends, so
the record written at the end of the turn does not have it. The next Stop or
SessionEnd hook finds it and writes the turn again with the measured figure
as cost_share_usd, metered, and a record that supersedes the first. The turn
then reads share in place of in parent.
In practice, the cost of a turn appears with the next turn, or when the
session ends with /exit. The record is never edited; see
Records and the Audit Trail.
Check That It Worked¶
alr otel status
alr otel status changes nothing. It shows the settings file, whether the
receiver is running, and one line for each of the four settings: set by alr,
set by alr, then changed, set by you (not by alr), or missing. It then
probes the receiver and reports whether it is reachable, whether the token is
accepted, and whether /v1/logs answers.
alr otel status: settings file ~/.claude/settings.json
CLAUDE_CODE_ENABLE_TELEMETRY: set by alr
OTEL_LOGS_EXPORTER: set by alr
OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: set by alr
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: set by alr
otel-sink: reachable at http://127.0.0.1:4318/<token>/v1/logs
otel-sink: token accepted
otel-sink: /v1/logs answers
Status also says when something overrides the settings:
- Managed settings. A file your organization manages sets the same variables and wins over alr's. On Linux and macOS, status names the file. On Windows, status says it did not check.
- Remote settings. A
remote-settings.jsonbeside your settings file means your organization may manage them. Check with/statusin Claude Code. - Your shell. A telemetry variable set in the shell where you run
alrdiffers from the file. Status names the variable, never its value.
Then do one turn and read it:
alr turns last
The turn first reads in parent. After the next turn, or after /exit, it
reads share with an m (metered). The record carries claude_code_cost,
which splits the figure by what the cost was for, such as the main thread, the
session title, and prompt suggestions. See the worked case in
WE 00.
What You See Without It¶
Without the receiver, a turn has no cost of its own. When the session ends,
alr shares the session total over the turns by price. It falls back to token
weight when a turn has no price. These shares are marked alloc. because they
are allocated, not measured. They add up to the session total, but a share is an
estimate of one turn's cost, not a measurement of it.
Set ALR_EMIT_COST_ALLOCATION to off and nothing is shared: a turn then has
no cost until a receiver supplies it.
Stop the Receiver¶
alr otel stop
This stops the receiver and removes the settings alr set. A setting you
changed after alr set it is left alone, and a value of your own that alr
replaced is put back. If start created the settings file and it is now empty,
stop removes the file. Records are kept. Restart Claude Code afterward for
the settings to take effect.
Next¶
- Tag Your Work, so each turn's cost lands on its task.
alr otel, withstart,status, andstop, in the CLI Reference.- How Costs Are Computed.