# Claude code metrics via a local LGTM stack

> Source: <https://gist.github.com/schroenser/cf3535ee678600db430e3e417d6755bf>
> Published: 2026-11-02 23:02:28+00:00

This assumes that you have an LGTM stack running locally, for example [https://github.com/schroenser/lgtm](https://github.com/schroenser/lgtm).
LGTM stands for

- L — Loki (for logs)
- G — Grafana (for visualization and correlation)
- T — Tempo (for distributed traces)
- M — Prometheus / Mimir (for metrics — with Prometheus handling collection/storage and Mimir often used for long-term scalable metrics)

A set of environment variables must be set before running claude.
This can be done either using an `.envrc` file or claudes `settings.json`.

💡 Note that if you run claude in a (dev)container itself, you need to exchange `localhost` for `host.containers.internal` so it can reach the gRPC port which the LGTM stack exposes on the host machine.

```
{
  "env": {
    "CLAUDE_CODE_ENABLE_TELEMETRY": "1",
    "OTEL_METRICS_EXPORTER": "otlp",
    "OTEL_METRIC_EXPORT_INTERVAL": "5000",
    "OTEL_LOGS_EXPORTER": "otlp",
    "OTEL_LOGS_EXPORT_INTERVAL": "5000",
    "CLAUDE_CODE_ENHANCED_TELEMETRY_BETA": "1",
    "OTEL_TRACES_EXPORTER": "otlp",
    "OTEL_TRACES_EXPORT_INTERVAL": "5000",
    "OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",
    "OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4317",
    "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE": "cumulative"
  }
}
```

e.g. with [direnv](https://direnv.net/)

```
# Required
export CLAUDE_CODE_ENABLE_TELEMETRY=1

# Metrics
export OTEL_METRICS_EXPORTER=otlp
export OTEL_METRIC_EXPORT_INTERVAL=5000

# Logs / events
export OTEL_LOGS_EXPORTER=otlp
export OTEL_LOGS_EXPORT_INTERVAL=5000

# Traces (beta — requires the flag below)
export CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1
export OTEL_TRACES_EXPORTER=otlp
export OTEL_TRACES_EXPORT_INTERVAL=5000

# Transport
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=cumulative

# Optional: include sensitive content in logs
# export OTEL_LOG_USER_PROMPTS=1
# export OTEL_LOG_TOOL_DETAILS=1
```

| Signal | Key names | Default interval | 
|---|---|---|
| Metrics | `claude_code.token.usage` ,`claude_code.cost.usage` ,`claude_code.session.count` ,`claude_code.lines_of_code.count` ,`claude_code.active_time.total` | 60 s | 
| Logs | `claude_code.user_prompt` ,`claude_code.api_request` ,`claude_code.tool_result` ,`claude_code.tool_decision` | 5 s | 
| Traces (beta) | `claude_code.interaction` ,`claude_code.llm_request` ,`claude_code.tool` ,`claude_code.tool.execution` | 5 s | 

The most straight forward dashboard to view those metrics is the [Claude Code Metrics (Prometheus)](https://grafana.com/grafana/dashboards/25255) dashboard, which can be imported into grafana using the "Grafana ID" `25255`.
