# Claude Code OpenTelemetry: Metrics, Events and Cost

> Six variables give you per-user cost, token and tool telemetry. The interesting part is which attributes the cost metric carries, and what content logging asks of you.

URL: https://www.matthewswong.com/en/blog/claude-code-analytics-otel-monitoring/

- Author: Matthews Wong (https://www.matthewswong.com/en/about/)
- Published: 2026-08-27
- Updated: 2026-08-27
- Category: DevOps

> **Key takeaway:** Claude Code exports OpenTelemetry metrics and events once you set CLAUDE_CODE_ENABLE_TELEMETRY to 1 along with an exporter and endpoint. Eight metrics cover sessions, code, cost, tokens, tool decisions and active time, and the cost metric is attributed by model, skill, plugin, agent and MCP tool — not just by user.

The question that made me set this up was not what are we spending. It was which of these things is spending it. A monthly figure tells you a team is using Claude Code; it does not tell you that one MCP server accounts for a third of the tokens, and it certainly does not tell you which skill was running when it happened.

The telemetry answers that, and the attribute list on the cost metric is why. This post covers the minimal setup, the eight metrics and which two actually matter, the event stream and its correlation identifiers, the content-logging options that are a policy decision rather than a configuration one, and the cardinality controls that decide whether your collector survives a large team.

## Six variables and you are exporting

There is no agent to install and nothing to run alongside Claude Code. It speaks OTLP directly, so the entire configuration is environment variables pointed at a collector you already have. Administrators put the same block in the environment section of managed settings, which overrides developer settings and removes conflicting per-signal variables so the destination is locked.

> Every metric carries a standard attribute set including session id, application version and entrypoint, organisation id, several user identifiers including email, and terminal type. That means user attribution is free and automatic — and also that you are shipping developer emails to your collector by default. Decide whether that is acceptable under your own policies before you roll it out, not after someone notices.

## Eight metrics, and the two that earn their keep

The full set covers activity well, but most of it is context rather than signal. Session counts and lines-of-code totals make good dashboard furniture and poor alerts. The two worth building on are cost and token usage, because of what they are attributed by.

That attribute list is the whole reason to do this properly. Cost broken down by MCP server and MCP tool tells you whether a connector is worth its keep. Broken down by skill and plugin, it tells you which of your team's customisations are expensive. Broken down by speed and effort, it tells you whether someone left a session at a higher effort level for a week. None of that is visible in a billing total.

## The event stream, and how to join it together

Events carry what metrics cannot: individual requests, tool results, errors and refusals. Three correlation attributes make the stream joinable, and knowing them turns a pile of events into a trace you can actually follow:

- A prompt identifier that links every event generated by one user prompt — the join key for the question how much did that one request cost.
- A message identifier matching the session transcript, so an event can be tied back to what the user actually saw.
- A client request identifier, also sent as a request header, which is what you quote when asking Anthropic about a specific request.
- The API request event carries input, output, cache-read and cache-creation tokens separately, which is the cleanest way to watch cache health across a team rather than one statusline at a time.

## Content logging is a policy decision

Five separate variables control whether the actual content of a session leaves the machine, and they are deliberately separate because they carry different risk. Logging tool details is a reasonable audit control. Logging user prompts and assistant responses means your collector now holds the conversation, and your collector's retention policy is now your AI data-retention policy.

> Raw API bodies can be written inline or, with the file form, to a directory on disk. That is genuinely useful for debugging a gateway and genuinely dangerous as a standing configuration, because it puts full request bodies on developer machines with no lifecycle attached. Turn it on to diagnose something specific, and turn it off in the same change that closes the ticket.

## What to actually alert on

Dashboards are easy and alerts are the hard part, because most of these metrics are activity rather than health. These four have a defensible threshold behind them.

| Signal | What it catches | Why it beats the obvious alternative |
| --- | --- | --- |
| Cache-creation tokens staying high | Something invalidating the prefix every turn | Visible in a session; a cost alert takes a billing cycle |
| Cost by MCP server | A connector nobody uses that everybody pays for | A total tells you the number, not the cause |
| API error and refusal events | Provider trouble, or a repo tripping a classifier | Users report these as the tool being slow |
| Tool decision events with a denied outcome | Permission rules that are fighting the work | Nobody files a ticket about a prompt they clicked through |

## Cardinality, before your collector notices

Session id and account identifier are included in metrics by default; version and entrypoint are not. That default is right for a small team and wrong for a large one — session id is unbounded, so every session creates a new time series, and a few hundred developers running several sessions a day will make that felt.

Four toggles control it, and the honest approach is to decide what question you are answering. If you need per-session attribution, keep it and size the collector for it. If you only need per-user totals, turn session id off and keep the account identifier; you lose the ability to ask about one session and you stop paying for a series per session forever.

> There is a distributed-tracing beta behind a separate variable that produces a real span hierarchy — an interaction span with model requests, hooks and tool spans beneath it, including a span for the time a tool spent blocked on the user. That last one is the most interesting number in the whole system, because it measures how much of a session is waiting for a human rather than waiting for a model.

## Verifying it, in the order that saves time

Three checks, in this order, because each rules out a different layer:

1. Look for the session-count metric in your backend. If it arrives, the exporter, endpoint and credentials are all correct.
2. If you configured logs only, submit a prompt and look for the user-prompt event instead — metrics and logs have separate exporter variables and one can be misconfigured alone.
3. If nothing arrives, run with the debug flag, which surfaces OTel export errors that are otherwise silent.

Set this up before you need it, because the questions it answers are all retrospective. The configuration is six variables and an afternoon; the value shows up the first time someone asks why last month cost what it did, and the answer is a breakdown by MCP tool rather than a shrug. Just make the content-logging decision deliberately, and write down what your collector's retention policy now means — because once prompts are flowing into it, that policy is part of your AI data handling whether anyone wrote it down or not.

## Sources & further reading

- Claude Code docs — Monitor usage with OpenTelemetry
- Claude Code docs — Analytics
- Claude Code docs — Manage costs
- Claude Code docs — Managed settings
- Claude Code docs — Environment variables
- Claude Code docs — Data usage and telemetry services
- Claude Code docs — Statusline configuration
- Claude Code docs — How Claude Code uses prompt caching

## Source links

- https://code.claude.com/docs/en/monitoring-usage
- https://code.claude.com/docs/en/analytics
- https://code.claude.com/docs/en/costs
- https://code.claude.com/docs/en/managed-settings
- https://code.claude.com/docs/en/env-vars
- https://code.claude.com/docs/en/data-usage
- https://code.claude.com/docs/en/statusline
- https://code.claude.com/docs/en/prompt-caching

## FAQ

### How do I enable OpenTelemetry in Claude Code?

Set CLAUDE_CODE_ENABLE_TELEMETRY to 1, then choose exporters and an endpoint — typically OTEL_METRICS_EXPORTER and OTEL_LOGS_EXPORTER set to otlp, OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_HEADERS. There is no agent to install. Administrators put the same block in the env section of managed settings to lock the destination.

### Which Claude Code metrics are most useful?

The cost and token usage metrics, because of what they are attributed by. Cost carries model, query source, speed, effort, agent name, skill name, plugin name, marketplace name, MCP server name and MCP tool name — so you can attribute spend to a specific connector or skill rather than just to a user or a month.

### Does Claude Code telemetry log my prompts?

Not by default. Content logging is opt-in through separate variables for user prompts, assistant responses, tool details, tool content and raw API bodies. Enabling prompt and response logging means your collector holds the conversation, so its retention policy becomes part of your AI data-retention posture.

### How do I control telemetry cardinality for a large team?

Session id and account UUID are included in metrics by default, while version and entrypoint are not. Session id is unbounded, so every session creates a new time series. Set OTEL_METRICS_INCLUDE_SESSION_ID to false if per-user totals are enough; keep it and size the collector accordingly if you need per-session attribution.

### Can I see prompt cache performance across a team?

Yes. The API request event carries input, output, cache-read and cache-creation tokens as separate attributes, so you can watch the read-to-creation ratio per user and per session. Cache creation staying high turn after turn means something is invalidating the prefix, which shows up in a session rather than a billing cycle.

---

- HTML version: https://www.matthewswong.com/en/blog/claude-code-analytics-otel-monitoring/
- Bahasa Indonesia: https://www.matthewswong.com/id/blog/claude-code-analytics-otel-monitoring/
- Site index for agents: https://www.matthewswong.com/llms.txt
- Sitemap: https://www.matthewswong.com/sitemap.xml
- API description (OpenAPI): https://www.matthewswong.com/openapi.json
