Claude Code supports OpenTelemetry, a standard for collecting and exporting telemetry, or usage data. It can report sessions (groups of agent execution), cost, and tokens (units of model input and output). It also reports the results of tools, the operations an agent invokes. You enable export with environment variables and aggregate the data in your receiver.
Export uses the OpenTelemetry Protocol (OTLP). Choose a collector, a service that gathers telemetry, or a monitoring backend that accepts the protocol you configure. This lets you view Claude Code usage alongside your existing monitoring data.
Traces, records of how an operation proceeds, are in beta and need an extra variable. The text of a user instruction, or prompt, isn't exported by default. Project settings cannot supply variables for the exporter, the component that sends telemetry. Hooks, commands triggered at specific events, and other subprocesses don't inherit the OTEL_* variables. Event names for individual occurrences also differ between the documentation and attribute values.
This guide uses the Harnessforce telemetry specification as a receiver design example. It assembles logs (records of events) and metrics (aggregate measurements) into a per-session run. The design works without traces, and the workspace decides whether content that members' machines send is stored. We distinguish the documented sender behavior from choices made for this receiver.
Below, we'll cover the 3 Claude Code telemetry signals, the 4 capabilities you need to export them well, and how to operate the setup once it's running.
In this article
Start with the available signals, then configure export and check the operating constraints. The role-based section gives you a starting point for your responsibilities.
- The 3 Claude Code telemetry signals
- 4 capabilities for exporting telemetry
- Operating and improving Claude Code telemetry
- Getting started by role
The 3 Claude Code telemetry signals
Claude Code telemetry comes in 3 types of data, called signals: metrics, logs (events), and traces. Metrics and events turn on with CLAUDE_CODE_ENABLE_TELEMETRY=1 plus an exporter for each signal. Traces are in beta and need a separate variable (monitoring documentation).
| Signal | Variables that enable it | What it contains |
|---|---|---|
| Metrics | CLAUDE_CODE_ENABLE_TELEMETRY=1 and OTEL_METRICS_EXPORTER | Counters such as sessions, tokens, cost, and active time |
| Logs (events) | CLAUDE_CODE_ENABLE_TELEMETRY=1 and OTEL_LOGS_EXPORTER | Things that happened, such as prompts, API requests, and tool results |
| Traces (beta) | The above, plus CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1 and OTEL_TRACES_EXPORTER | How each prompt was processed, step by step |
Metrics
Claude Code exports 8 metrics. Cost is reported in US dollars (USD) and active time in seconds. CLI in the table means the command-line interface used to run Claude Code.
| Metric | What it measures | Unit |
|---|---|---|
claude_code.session.count | CLI sessions started | None |
claude_code.lines_of_code.count | Lines of code modified | None |
claude_code.pull_request.count | Pull requests created | None |
claude_code.commit.count | Git commits created | None |
claude_code.cost.usage | Cost of the session | USD |
claude_code.token.usage | Tokens used | Tokens |
claude_code.code_edit_tool.decision | Permission decisions for code editing tools | None |
claude_code.active_time.total | Total active time | Seconds |
Events and shared attributes
Events record what happened inside Claude Code: a prompt was submitted (user_prompt), an API request was made (api_request) or failed (api_error), a tool finished (tool_result), or a tool permission was decided (tool_decision), among others. Standard metric and event attributes include session.id, organization.id, user.account_uuid, user.id, user.email, and terminal.type.
Their availability depends on authentication and settings: metric session IDs and account UUIDs can be disabled with their include variables, while organization IDs and email addresses are only attached when available. Events also carry prompt.id, which ties together the events from one prompt, and event.sequence, which orders them.
4 capabilities for exporting telemetry
Exporting Claude Code telemetry takes 4 capabilities: turning export on, choosing a destination and protocol, enabling traces when you need them, and deciding how to handle content and identifiers. The first 2 configure metric and event export. The last 2 decide how much detail you collect and what leaves the machine.
1. Turn on export and choose exporters
Set CLAUDE_CODE_ENABLE_TELEMETRY=1 and pick an exporter for each signal. OTEL_METRICS_EXPORTER accepts console, otlp, prometheus, or none. OTEL_LOGS_EXPORTER and OTEL_TRACES_EXPORTER accept console, otlp, or none. To try it yourself, export the variables in your shell or add them to the env block of ~/.claude/settings.json.
export CLAUDE_CODE_ENABLE_TELEMETRY=1
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otel.example.com
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer <token>"By default, metrics export every 60 seconds (OTEL_METRIC_EXPORT_INTERVAL=60000), and logs every 5 seconds (OTEL_LOGS_EXPORT_INTERVAL=5000). The documentation recommends shortening these intervals while checking delivery, then restoring them when you finish.
2. Choose a destination and protocol
Point the exporter at a base URL and pick a protocol your receiver accepts. OTEL_EXPORTER_OTLP_PROTOCOL accepts grpc, http/json, or http/protobuf. The OpenTelemetry exporter specification says the default, when nothing is set, should be http/protobuf.
Claude Code itself has no default protocol, so set OTEL_EXPORTER_OTLP_PROTOCOL or the signal-specific protocol explicitly.
Over OTLP/HTTP, OTEL_EXPORTER_OTLP_ENDPOINT is treated as a base URL. Unless you set per-signal endpoints, the exporter appends v1/traces, v1/metrics, or v1/logs to it.
For example, setting https://otel.example.com makes https://otel.example.com/v1/logs the destination for logs. Headers carry information such as credentials with each request. Put auth headers in OTEL_EXPORTER_OTLP_HEADERS as key1=value1,key2=value2.
If your authentication tokens, the credentials used to identify a sender, expire, point otelHeadersHelper at a script that generates the headers. The script prints a JSON object of header names and values, and Claude Code runs it at startup and every 29 minutes by default. CLAUDE_CODE_OTEL_HEADERS_HELPER_DEBOUNCE_MS changes the interval. Dynamic headers only apply to http/protobuf and http/json; with grpc, Claude Code uses only the static header variables.
The Harnessforce receiver specification accepts OTLP/HTTP only, with protobuf or JSON bodies and either no compression or gzip. It doesn't accept gRPC. On the sending side, the specification fixes the protocol to http/protobuf, because dynamic headers only work over HTTP.
3. Turn on traces (beta)
For traces, add CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1 and choose a destination with OTEL_TRACES_EXPORTER. Traces are off by default and need CLAUDE_CODE_ENABLE_TELEMETRY=1 as well. A span records one operation from start to finish.
Once traces are on, each user prompt starts a claude_code.interaction root span, the top-level span for that prompt. API requests (claude_code.llm_request) and tool calls (claude_code.tool) become its children. Hook executions (claude_code.hook) appear only with detailed beta tracing.
If you want Claude Code's spans to join an existing trace, note that only sessions using the Agent SDK (a library for building agents) and non-interactive sessions started with -p read TRACEPARENT and TRACESTATE. Interactive sessions ignore TRACEPARENT, so they don't inherit a stray value from an automated build or container environment.
Because traces are still in beta, it's safer to build a receiver that doesn't depend on them. The Harnessforce design assembles each session's run from log events (prompt.id and event.sequence) and metrics alone, and adds spans as detail when they arrive. Logs or metrics carrying a session identifier can form a run even when traces are absent.
4. Decide how to handle content and identifiers
Content is off by default, but several variables turn it on, so decide your policy before anyone flips one. By default, the user_prompt event carries only the prompt's length (prompt_length), not its text. Spans also redact prompt text, tool input details, and tool output. These variables turn content and detail on by kind. OTEL_LOG_ASSISTANT_RESPONSES is the exception: when it is unset, it takes the value of OTEL_LOG_USER_PROMPTS, so turning prompt text on also sends response text; write 0 explicitly for every category you don't send.
| Variable | What it adds |
|---|---|
OTEL_LOG_USER_PROMPTS | Prompt text |
OTEL_LOG_ASSISTANT_RESPONSES | Response text on assistant_response events |
OTEL_LOG_TOOL_DETAILS | Tool arguments, such as Bash commands, MCP server and tool names, and skill names |
OTEL_LOG_TOOL_CONTENT | Tool output in the tool.output span event |
OTEL_LOG_RAW_API_BODIES | Full API request and response JSON |
Attributes that carry content are capped at 61,440 UTF-16 code units (units of text encoding) by default (CLAUDE_CODE_OTEL_CONTENT_MAX_LENGTH). An attribute cut at the cap comes with matching _truncated and _original_length attributes.
Identifiers deserve the same care. user.email is included whenever it's available, so your backend receives each user's email address. To roll usage up by department or team, add key-value pairs to OTEL_RESOURCE_ATTRIBUTES, such as department=platform,team.id=web. Values can't contain spaces, and characters outside printable US-ASCII, along with quotes, commas, and similar characters, must be percent-encoded.
In Harnessforce, machines send content by default, and each member can stop it on their own machine. The Workspace, the organizational unit in Harnessforce, decides whether arriving content is stored, who can read it, and for how many days. A new Workspace stores it, lets every member read it, and keeps it for 30 days or the plan's telemetry retention period, whichever is shorter (14 days on Free). Before content is sent, Harnessforce shows what is stored, who can read it, the retention period, and that redaction may not detect everything.
Redaction replaces sensitive patterns with placeholder text. Harnessforce scans eligible remaining string fields for patterns that look like API keys, tokens, private keys, connection strings, email addresses, and phone numbers, and replaces each match with [REDACTED:<kind>], such as [REDACTED:api_key], before storing anything.
The top-level user.email, user.id, and user.account_uuid attributes are exempt from redaction when their values are strings, because Harnessforce uses them to identify users. After identifying the user, Harnessforce removes them from the telemetry rows it stores for search and display. OTLP identifiers also have exclusions. Redaction does not anonymize all personal information.
Operating and improving Claude Code telemetry
In day-to-day operation, 2 things matter: where the settings live, and what you can assume when you join the data. Miss either one, and you end up with telemetry that arrives but can't be counted.
- Pick where settings live: For a whole organization, use managed settings. For individual use, use your shell or
~/.claude/settings.json. Claude Code ignores exporter variables in a repository's.claude/settings.jsonand.claude/settings.local.json. When managed settings setOTEL_EXPORTER_OTLP_ENDPOINT, Claude Code removes any per-signal endpoints an engineer set, at startup. - Remember subprocesses don't inherit: Claude Code doesn't pass
OTEL_*variables to the subprocesses it spawns, including the Bash tool, hooks, and MCP servers. If an application you run through the Bash tool exports its own telemetry, set the variables directly in that command. - Handle both forms of event names: The documentation lists event names with a prefix, such as
claude_code.user_prompt, but lists theevent.nameattribute value without it, as inuser_prompt. It doesn't say where the prefixed name lands in the OTLP log record. The Harnessforce receiver recognizes Claude Code events by either form. - Know how ordering and joins work:
event.sequencecounts per Claude Code process, not per session. The hooks documentation states that a hook'sprompt_idmatches the telemetry'sprompt.id, but it doesn't say that a hook'ssession_idmatchessession.id. Harnessforce treats a hook record and a telemetry session as the same session when either the session ID or the first prompt ID matches. - Treat cost as an estimate: The documentation describes the
cost_usdattribute onapi_requestas an estimated cost. Use it to see trends, not to reconcile against invoices.
We have a guide to measuring AI agent work per issue that goes deeper into using this telemetry per issue.
How Harnessforce can help
Receiving and storing Claude Code telemetry doesn't tell you which piece of work it went into. Harnessforce makes that telemetry readable issue by issue.
Harnessforce can help you:
- Turn AI work into data: Harnessforce turns AI work into data your team can analyze.
- Follow every issue: For each issue, see how many times AI ran, what it did, what it cost, and how continuous integration (CI) and review went, then drill into traces, logs, and metrics.
- Show gaps as gaps: Anything that can't be collected is marked as not measured, never shown as if it had been collected.
- Say what happens to content: Prompt and response content arrives only from machines that send it. Machines send it by default, and each member can stop it on their own machine. The workspace decides whether it is stored, who can read it, and for how long.
- Work alongside your tools: Harnessforce doesn't replace the tools you already use; it runs alongside them.
Read the Harnessforce installation guide, or talk to us.
Getting started by role
Start by confirming metrics and logs arrive if you are setting up your own machine. Platform engineers set the destination and content policy; analysts begin with cost and token trends.
Individual developers
Turn on metrics and logs in your shell or ~/.claude/settings.json, shorten the export intervals, and confirm data reaches your collector. Add traces once metrics and logs are arriving.
Platform engineers
Ship your organization's endpoint and auth headers in the managed env block so engineers can't redirect them. Settle whether any content variable may be turned on before you roll out, not after.
Analysts
Start with claude_code.cost.usage and claude_code.token.usage to see overall trends. When you need detail, read the api_request and tool_result events grouped by session.id and prompt.id. Keep in mind that cost values are estimates.
Glossary
A signal is a type of data, an exporter sends it, and OTLP defines how it travels. When reading the result, distinguish spans that describe operations from events that record individual occurrences.
Signal
A kind of telemetry data in OpenTelemetry. Claude Code exports 3: metrics, logs (events), and traces.
Exporter
The component that sends telemetry out. In Claude Code, you choose one per signal, such as console or otlp.
OTLP
The protocol for sending OpenTelemetry data. It comes in 3 variants: grpc, http/json, and http/protobuf.
Span
A unit of a trace that covers one operation from start to finish. In Claude Code, the per-prompt claude_code.interaction span is the root.
Event
A record of something that happened at a point in time. Claude Code sends events through the logs signal, and event.name identifies the kind.
Resource attribute
An attribute that describes the sender as a whole. In Claude Code, you set it in OTEL_RESOURCE_ATTRIBUTES as comma-separated key-value pairs.
This guide was written with AI assistance and reviewed by the author.