6 6
7> Learn how to enable and configure OpenTelemetry for Claude Code.7> Learn how to enable and configure OpenTelemetry for Claude Code.
8 8
9Claude Code supports OpenTelemetry (OTel) metrics and events for monitoring and observability.9Track Claude Code usage, costs, and tool activity across your organization by exporting telemetry data through OpenTelemetry (OTel). Claude Code exports metrics as time series data via the standard metrics protocol, and events via the logs/events protocol. Configure your metrics and logs backends to match your monitoring requirements.
10
11All metrics are time series data exported via OpenTelemetry's standard metrics protocol, and events are exported via OpenTelemetry's logs/events protocol. It is the user's responsibility to ensure their metrics and logs backends are properly configured and that the aggregation granularity meets their monitoring requirements.
12 10
13## Quick start11## Quick start
14 12
56 "OTEL_METRICS_EXPORTER": "otlp",54 "OTEL_METRICS_EXPORTER": "otlp",
57 "OTEL_LOGS_EXPORTER": "otlp",55 "OTEL_LOGS_EXPORTER": "otlp",
58 "OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",56 "OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",
59 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://collector.company.com:4317",57 "OTEL_EXPORTER_OTLP_ENDPOINT": "http://collector.example.com:4317",
60 "OTEL_EXPORTER_OTLP_HEADERS": "Authorization=Bearer company-token"58 "OTEL_EXPORTER_OTLP_HEADERS": "Authorization=Bearer example-token"
61 }59 }
62}60}
63```61```
71### Common configuration variables69### Common configuration variables
72 70
73| Environment Variable | Description | Example Values |71| Environment Variable | Description | Example Values |
74| ----------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------ |72| --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
75| `CLAUDE_CODE_ENABLE_TELEMETRY` | Enables telemetry collection (required) | `1` |73| `CLAUDE_CODE_ENABLE_TELEMETRY` | Enables telemetry collection (required) | `1` |
76| `OTEL_METRICS_EXPORTER` | Metrics exporter type(s) (comma-separated) | `console`, `otlp`, `prometheus` |74| `OTEL_METRICS_EXPORTER` | Metrics exporter types, comma-separated | `console`, `otlp`, `prometheus` |
77| `OTEL_LOGS_EXPORTER` | Logs/events exporter type(s) (comma-separated) | `console`, `otlp` |75| `OTEL_LOGS_EXPORTER` | Logs/events exporter types, comma-separated | `console`, `otlp` |
78| `OTEL_EXPORTER_OTLP_PROTOCOL` | Protocol for OTLP exporter (all signals) | `grpc`, `http/json`, `http/protobuf` |76| `OTEL_EXPORTER_OTLP_PROTOCOL` | Protocol for OTLP exporter, applies to all signals | `grpc`, `http/json`, `http/protobuf` |
79| `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP collector endpoint (all signals) | `http://localhost:4317` |77| `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP collector endpoint for all signals | `http://localhost:4317` |
80| `OTEL_EXPORTER_OTLP_METRICS_PROTOCOL` | Protocol for metrics (overrides general) | `grpc`, `http/json`, `http/protobuf` |78| `OTEL_EXPORTER_OTLP_METRICS_PROTOCOL` | Protocol for metrics, overrides general setting | `grpc`, `http/json`, `http/protobuf` |
81| `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT` | OTLP metrics endpoint (overrides general) | `http://localhost:4318/v1/metrics` |79| `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT` | OTLP metrics endpoint, overrides general setting | `http://localhost:4318/v1/metrics` |
82| `OTEL_EXPORTER_OTLP_LOGS_PROTOCOL` | Protocol for logs (overrides general) | `grpc`, `http/json`, `http/protobuf` |80| `OTEL_EXPORTER_OTLP_LOGS_PROTOCOL` | Protocol for logs, overrides general setting | `grpc`, `http/json`, `http/protobuf` |
83| `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` | OTLP logs endpoint (overrides general) | `http://localhost:4318/v1/logs` |81| `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` | OTLP logs endpoint, overrides general setting | `http://localhost:4318/v1/logs` |
84| `OTEL_EXPORTER_OTLP_HEADERS` | Authentication headers for OTLP | `Authorization=Bearer token` |82| `OTEL_EXPORTER_OTLP_HEADERS` | Authentication headers for OTLP | `Authorization=Bearer token` |
85| `OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY` | Client key for mTLS authentication | Path to client key file |83| `OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY` | Client key for mTLS authentication | Path to client key file |
86| `OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE` | Client certificate for mTLS authentication | Path to client cert file |84| `OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE` | Client certificate for mTLS authentication | Path to client cert file |
88| `OTEL_LOGS_EXPORT_INTERVAL` | Logs export interval in milliseconds (default: 5000) | `1000`, `10000` |86| `OTEL_LOGS_EXPORT_INTERVAL` | Logs export interval in milliseconds (default: 5000) | `1000`, `10000` |
89| `OTEL_LOG_USER_PROMPTS` | Enable logging of user prompt content (default: disabled) | `1` to enable |87| `OTEL_LOG_USER_PROMPTS` | Enable logging of user prompt content (default: disabled) | `1` to enable |
90| `OTEL_LOG_TOOL_DETAILS` | Enable logging of MCP server/tool names and skill names in tool events (default: disabled) | `1` to enable |88| `OTEL_LOG_TOOL_DETAILS` | Enable logging of MCP server/tool names and skill names in tool events (default: disabled) | `1` to enable |
89| `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` | Metrics temporality preference (default: `delta`). Set to `cumulative` if your backend expects cumulative temporality | `delta`, `cumulative` |
91| `CLAUDE_CODE_OTEL_HEADERS_HELPER_DEBOUNCE_MS` | Interval for refreshing dynamic headers (default: 1740000ms / 29 minutes) | `900000` |90| `CLAUDE_CODE_OTEL_HEADERS_HELPER_DEBOUNCE_MS` | Interval for refreshing dynamic headers (default: 1740000ms / 29 minutes) | `900000` |
92 91
93### Metrics cardinality control92### Metrics cardinality control
149<Warning>148<Warning>
150 **Important formatting requirements for OTEL\_RESOURCE\_ATTRIBUTES:**149 **Important formatting requirements for OTEL\_RESOURCE\_ATTRIBUTES:**
151 150
152 The `OTEL_RESOURCE_ATTRIBUTES` environment variable follows the [W3C Baggage specification](https://www.w3.org/TR/baggage/), which has strict formatting requirements:151 The `OTEL_RESOURCE_ATTRIBUTES` environment variable uses comma-separated key=value pairs with strict formatting requirements:
153 152
154 * **No spaces allowed**: Values cannot contain spaces. For example, `user.organizationName=My Company` is invalid153 * **No spaces allowed**: Values cannot contain spaces. For example, `user.organizationName=My Company` is invalid
155 * **Format**: Must be comma-separated key=value pairs: `key1=value1,key2=value2`154 * **Format**: Must be comma-separated key=value pairs: `key1=value1,key2=value2`
175 174
176### Example configurations175### Example configurations
177 176
177Set these environment variables before running `claude`. Each block shows a complete configuration for a different exporter or deployment scenario:
178
178```bash theme={null}179```bash theme={null}
179# Console debugging (1-second intervals)180# Console debugging (1-second intervals)
180export CLAUDE_CODE_ENABLE_TELEMETRY=1181export CLAUDE_CODE_ENABLE_TELEMETRY=1
201export OTEL_METRICS_EXPORTER=otlp202export OTEL_METRICS_EXPORTER=otlp
202export OTEL_LOGS_EXPORTER=otlp203export OTEL_LOGS_EXPORTER=otlp
203export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=http/protobuf204export OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=http/protobuf
204export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://metrics.company.com:4318205export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://metrics.example.com:4318
205export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc206export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc
206export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://logs.company.com:4317207export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://logs.example.com:4317
207 208
208# Metrics only (no events/logs)209# Metrics only (no events/logs)
209export CLAUDE_CODE_ENABLE_TELEMETRY=1210export CLAUDE_CODE_ENABLE_TELEMETRY=1
225All metrics and events share these standard attributes:226All metrics and events share these standard attributes:
226 227
227| Attribute | Description | Controlled By |228| Attribute | Description | Controlled By |
228| ------------------- | -------------------------------------------------------------------- | --------------------------------------------------- |229| ------------------- | -------------------------------------------------------------------------------- | --------------------------------------------------- |
229| `session.id` | Unique session identifier | `OTEL_METRICS_INCLUDE_SESSION_ID` (default: true) |230| `session.id` | Unique session identifier | `OTEL_METRICS_INCLUDE_SESSION_ID` (default: true) |
230| `app.version` | Current Claude Code version | `OTEL_METRICS_INCLUDE_VERSION` (default: false) |231| `app.version` | Current Claude Code version | `OTEL_METRICS_INCLUDE_VERSION` (default: false) |
231| `organization.id` | Organization UUID (when authenticated) | Always included when available |232| `organization.id` | Organization UUID (when authenticated) | Always included when available |
232| `user.account_uuid` | Account UUID (when authenticated) | `OTEL_METRICS_INCLUDE_ACCOUNT_UUID` (default: true) |233| `user.account_uuid` | Account UUID (when authenticated) | `OTEL_METRICS_INCLUDE_ACCOUNT_UUID` (default: true) |
233| `terminal.type` | Terminal type (for example, `iTerm.app`, `vscode`, `cursor`, `tmux`) | Always included when detected |234| `user.id` | Anonymous device/installation identifier, generated per Claude Code installation | Always included |
235| `user.email` | User email address (when authenticated via OAuth) | Always included when available |
236| `terminal.type` | Terminal type, such as `iTerm.app`, `vscode`, `cursor`, or `tmux` | Always included when detected |
234 237
235### Metrics238### Metrics
236 239
249 252
250### Metric details253### Metric details
251 254
255Each metric includes the standard attributes listed above. Metrics with additional context-specific attributes are noted below.
256
252#### Session counter257#### Session counter
253 258
254Incremented at the start of each session.259Incremented at the start of each session.
308**Attributes**:313**Attributes**:
309 314
310* All [standard attributes](#standard-attributes)315* All [standard attributes](#standard-attributes)
311* `tool`: Tool name (`"Edit"`, `"Write"`, `"NotebookEdit"`)316* `tool_name`: Tool name (`"Edit"`, `"Write"`, `"NotebookEdit"`)
312* `decision`: User decision (`"accept"`, `"reject"`)317* `decision`: User decision (`"accept"`, `"reject"`)
313* `language`: Programming language of the edited file (for example, `"TypeScript"`, `"Python"`, `"JavaScript"`, `"Markdown"`). Returns `"unknown"` for unrecognized file extensions.318* `source`: Decision source - `"config"`, `"hook"`, `"user_permanent"`, `"user_temporary"`, `"user_abort"`, or `"user_reject"`
319* `language`: Programming language of the edited file, such as `"TypeScript"`, `"Python"`, `"JavaScript"`, or `"Markdown"`. Returns `"unknown"` for unrecognized file extensions.
314 320
315#### Active time counter321#### Active time counter
316 322
317Tracks actual time spent actively using Claude Code (not idle time). This metric is incremented during user interactions such as typing prompts or receiving responses.323Tracks actual time spent actively using Claude Code, excluding idle time. This metric is incremented during user interactions (typing, reading responses) and during CLI processing (tool execution, AI response generation).
318 324
319**Attributes**:325**Attributes**:
320 326
321* All [standard attributes](#standard-attributes)327* All [standard attributes](#standard-attributes)
328* `type`: `"user"` for keyboard interactions, `"cli"` for tool execution and AI responses
322 329
323### Events330### Events
324 331
325Claude Code exports the following events via OpenTelemetry logs/events (when `OTEL_LOGS_EXPORTER` is configured):332Claude Code exports the following events via OpenTelemetry logs/events (when `OTEL_LOGS_EXPORTER` is configured):
326 333
334#### Event correlation attributes
335
336When a user submits a prompt, Claude Code may make multiple API calls and run several tools. The `prompt.id` attribute lets you tie all of those events back to the single prompt that triggered them.
337
338| Attribute | Description |
339| ----------- | ------------------------------------------------------------------------------------ |
340| `prompt.id` | UUID v4 identifier linking all events produced while processing a single user prompt |
341
342To trace all activity triggered by a single prompt, filter your events by a specific `prompt.id` value. This returns the user\_prompt event, any api\_request events, and any tool\_result events that occurred while processing that prompt.
343
344<Note>
345 `prompt.id` is intentionally excluded from metrics because each prompt generates a unique ID, which would create an ever-growing number of time series. Use it for event-level analysis and audit trails only.
346</Note>
347
327#### User prompt event348#### User prompt event
328 349
329Logged when a user submits a prompt.350Logged when a user submits a prompt.
355* `success`: `"true"` or `"false"`376* `success`: `"true"` or `"false"`
356* `duration_ms`: Execution time in milliseconds377* `duration_ms`: Execution time in milliseconds
357* `error`: Error message (if failed)378* `error`: Error message (if failed)
358* `decision`: Either `"accept"` or `"reject"`379* `decision_type`: Either `"accept"` or `"reject"`
359* `source`: Decision source - `"config"`, `"user_permanent"`, `"user_temporary"`, `"user_abort"`, or `"user_reject"`380* `decision_source`: Decision source - `"config"`, `"hook"`, `"user_permanent"`, `"user_temporary"`, `"user_abort"`, or `"user_reject"`
381* `tool_result_size_bytes`: Size of the tool result in bytes
382* `mcp_server_scope`: MCP server scope identifier (for MCP tools)
360* `tool_parameters`: JSON string containing tool-specific parameters (when available)383* `tool_parameters`: JSON string containing tool-specific parameters (when available)
361 * For Bash tool: includes `bash_command`, `full_command`, `timeout`, `description`, `sandbox`384 * For Bash tool: includes `bash_command`, `full_command`, `timeout`, `description`, `dangerouslyDisableSandbox`, and `git_commit_id` (the commit SHA, when a `git commit` command succeeds)
362 * For MCP tools (when `OTEL_LOG_TOOL_DETAILS=1`): includes `mcp_server_name`, `mcp_tool_name`385 * For MCP tools (when `OTEL_LOG_TOOL_DETAILS=1`): includes `mcp_server_name`, `mcp_tool_name`
363 * For Skill tool (when `OTEL_LOG_TOOL_DETAILS=1`): includes `skill_name`386 * For Skill tool (when `OTEL_LOG_TOOL_DETAILS=1`): includes `skill_name`
364 387
381* `output_tokens`: Number of output tokens404* `output_tokens`: Number of output tokens
382* `cache_read_tokens`: Number of tokens read from cache405* `cache_read_tokens`: Number of tokens read from cache
383* `cache_creation_tokens`: Number of tokens used for cache creation406* `cache_creation_tokens`: Number of tokens used for cache creation
407* `speed`: `"fast"` or `"normal"`, indicating whether fast mode was active
384 408
385#### API error event409#### API error event
386 410
396* `event.sequence`: monotonically increasing counter for ordering events within a session420* `event.sequence`: monotonically increasing counter for ordering events within a session
397* `model`: Model used (for example, "claude-sonnet-4-6")421* `model`: Model used (for example, "claude-sonnet-4-6")
398* `error`: Error message422* `error`: Error message
399* `status_code`: HTTP status code (if applicable)423* `status_code`: HTTP status code as a string, or `"undefined"` for non-HTTP errors
400* `duration_ms`: Request duration in milliseconds424* `duration_ms`: Request duration in milliseconds
401* `attempt`: Attempt number (for retried requests)425* `attempt`: Attempt number (for retried requests)
426* `speed`: `"fast"` or `"normal"`, indicating whether fast mode was active
402 427
403#### Tool decision event428#### Tool decision event
404 429
414* `event.sequence`: monotonically increasing counter for ordering events within a session439* `event.sequence`: monotonically increasing counter for ordering events within a session
415* `tool_name`: Name of the tool (for example, "Read", "Edit", "Write", "NotebookEdit")440* `tool_name`: Name of the tool (for example, "Read", "Edit", "Write", "NotebookEdit")
416* `decision`: Either `"accept"` or `"reject"`441* `decision`: Either `"accept"` or `"reject"`
417* `source`: Decision source - `"config"`, `"user_permanent"`, `"user_temporary"`, `"user_abort"`, or `"user_reject"`442* `source`: Decision source - `"config"`, `"hook"`, `"user_permanent"`, `"user_temporary"`, `"user_abort"`, or `"user_reject"`
418 443
419## Interpreting metrics and events data444## Interpret metrics and events data
420 445
421The metrics exported by Claude Code provide valuable insights into usage patterns and productivity. Here are some common visualizations and analyses you can create:446The exported metrics and events support a range of analyses:
422 447
423### Usage monitoring448### Usage monitoring
424 449
497 522
498For a comprehensive guide on measuring return on investment for Claude Code, including telemetry setup, cost analysis, productivity metrics, and automated reporting, see the [Claude Code ROI Measurement Guide](https://github.com/anthropics/claude-code-monitoring-guide). This repository provides ready-to-use Docker Compose configurations, Prometheus and OpenTelemetry setups, and templates for generating productivity reports integrated with tools like Linear.523For a comprehensive guide on measuring return on investment for Claude Code, including telemetry setup, cost analysis, productivity metrics, and automated reporting, see the [Claude Code ROI Measurement Guide](https://github.com/anthropics/claude-code-monitoring-guide). This repository provides ready-to-use Docker Compose configurations, Prometheus and OpenTelemetry setups, and templates for generating productivity reports integrated with tools like Linear.
499 524
500## Security/privacy considerations525## Security and privacy
501 526
502* Telemetry is opt-in and requires explicit configuration527* Telemetry is opt-in and requires explicit configuration
503* Sensitive information like API keys or file contents are never included in metrics or events528* Raw file contents and code snippets are not included in metrics or events. Tool execution events include bash commands and file paths in the `tool_parameters` field, which may contain sensitive values. If your commands may include secrets, configure your telemetry backend to filter or redact `tool_parameters`
504* User prompt content is redacted by default, only prompt length is recorded. To enable user prompt logging, set `OTEL_LOG_USER_PROMPTS=1`529* When authenticated via OAuth, `user.email` is included in telemetry attributes. If this is a concern for your organization, work with your telemetry backend to filter or redact this field
505* MCP server/tool names and skill names are not logged by default because they can reveal user-specific configurations. To enable, set `OTEL_LOG_TOOL_DETAILS=1`530* User prompt content is not collected by default. Only prompt length is recorded. To include prompt content, set `OTEL_LOG_USER_PROMPTS=1`
531* MCP server/tool names and skill names are not logged by default because they can reveal user-specific configurations. To include them, set `OTEL_LOG_TOOL_DETAILS=1`
506 532
507## Monitoring Claude Code on Amazon Bedrock533## Monitor Claude Code on Amazon Bedrock
508 534
509For detailed Claude Code usage monitoring guidance for Amazon Bedrock, see [Claude Code Monitoring Implementation (Bedrock)](https://github.com/aws-solutions-library-samples/guidance-for-claude-code-with-amazon-bedrock/blob/main/assets/docs/MONITORING.md).535For detailed Claude Code usage monitoring guidance for Amazon Bedrock, see [Claude Code Monitoring Implementation (Bedrock)](https://github.com/aws-solutions-library-samples/guidance-for-claude-code-with-amazon-bedrock/blob/main/assets/docs/MONITORING.md).