Claude Code Metrics Setup: Prometheus + Grafana dashboards for tracking AI coding productivity Technical guide for setting up OpenTelemetry (OTEL) telemetry from Claude Code to Prometheus and Grafana to track AI coding productivity metrics. It details the configuration steps, including enabling telemetry via environment variables, installing and configuring Prometheus with OTLP receiver support, and setting up Grafana dashboards for visualizing metrics like token usage, cost, lines of code, and productivity ratios. The guide also includes PromQL query examples for calculating key performance indicators such as productivity ratio, lines per dollar, and cache efficiency. Claude Code Metrics Setup Guide This guide walks through setting up OTEL telemetry from Claude Code to Prometheus + Grafana. Architecture Claude Code -- OTLP/HTTP -- Prometheus -- Grafana metrics protobuf storage viz 1. Environment Variables Add to your shell profile ~/.bashrc for Linux, ~/.zshrc for macOS : bash Enable Claude Code telemetry export CLAUDE CODE ENABLE TELEMETRY=1 OTEL export configuration export OTEL METRICS EXPORTER=otlp export OTEL EXPORTER OTLP PROTOCOL=http/protobuf export OTEL EXPORTER OTLP ENDPOINT=http://localhost:9090/api/v1/otlp export OTEL EXPORTER OTLP METRICS TEMPORALITY PREFERENCE=delta Reload your shell: source ~/.bashrc 2. Prometheus Setup Install AL2023 / Amazon Linux bash Download latest Prometheus cd /tmp curl -LO https://github.com/prometheus/prometheus/releases/download/v3.0.0/prometheus-3.0.0.linux-amd64.tar.gz tar xzf prometheus-3.0.0.linux-amd64.tar.gz sudo mv prometheus-3.0.0.linux-amd64 /opt/prometheus Create data directory mkdir -p ~/.prometheus/data Install macOS with Homebrew bash brew install prometheus Configuration Create ~/.prometheus/prometheus.yml : yaml global: scrape interval: 15s scrape configs: - job name: "prometheus" static configs: - targets: "localhost:9090" Run Prometheus with OTLP Receiver Critical flags: - --web.enable-otlp-receiver - Enables the OTLP ingestion endpoint - --enable-feature=otlp-deltatocumulative - Converts delta metrics like lines of code to cumulative bash Linux /opt/prometheus/prometheus \ --config.file=~/.prometheus/prometheus.yml \ --storage.tsdb.path=~/.prometheus/data \ --web.listen-address=127.0.0.1:9090 \ --web.enable-otlp-receiver \ --enable-feature=otlp-deltatocumulative macOS if running manually prometheus \ --config.file=/opt/homebrew/etc/prometheus.yml \ --storage.tsdb.path=/opt/homebrew/var/prometheus \ --web.listen-address=127.0.0.1:9090 \ --web.enable-otlp-receiver \ --enable-feature=otlp-deltatocumulative You can run this in a tmux session or background it. 3. Grafana Setup Install AL2023 / Amazon Linux bash Add Grafana repo sudo tee /etc/yum.repos.d/grafana.repo << 'EOF' grafana name=grafana baseurl=https://rpm.grafana.com repo gpgcheck=1 enabled=1 gpgcheck=1 gpgkey=https://rpm.grafana.com/gpg.key sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt EOF sudo dnf install grafana -y sudo systemctl start grafana-server Install macOS bash brew install grafana brew services start grafana Access - URL: http://localhost:3000 - Default login: admin / admin Add Prometheus Data Source 1. Go to Connections → Data Sources → Add data source 2. Select Prometheus 3. URL: http://localhost:9090 4. Save & Test Import Dashboards 1. Go to Dashboards → Import 2. Paste the JSON from the dashboard files in this directory 3. Select your Prometheus data source 4. Import Dashboard files included: - dashboard-metrics.json - Claude Code Metrics tokens, cost, productivity ratio - dashboard-summary.json - Daily/Weekly Summary aggregated stats - dashboard-economics.json - Engineering Economics ROI, team equivalence 4. Verify Metrics Are Flowing After using Claude Code with telemetry enabled: bash Check available metrics curl -s 'http://localhost:9090/api/v1/label/ name /values' | jq '.data ' | grep claude You should see: - claude code token usage tokens total - claude code cost usage USD total - claude code active time seconds total - claude code lines of code count total - claude code session count total 5. Available Metrics | Metric | Labels | Description | |--------|--------|-------------| | claude code token usage tokens total | type input/output/cacheRead/cacheCreation , model | Token consumption | | claude code cost usage USD total | model | Estimated API cost in USD | | claude code active time seconds total | type cli/user | Time tracking | | claude code lines of code count total | type added/removed | Code output delta metric | | claude code session count total | | Session count | | claude code event count total | event | Events commits, PRs, etc. | 6. Key Queries Productivity Ratio CLI time / User time : promql sum claude code active time seconds total{type="cli"} / sum claude code active time seconds total{type="user"} Lines per Dollar : promql sum sum over time claude code lines of code count total $ range / sum increase claude code cost usage USD total $ range Cache Efficiency : promql sum claude code token usage tokens total{type="cacheRead"} / sum claude code token usage tokens total{type="cacheRead"} + sum claude code token usage tokens total{type="input"} 100 Important: The lines of code metric is a delta metric , not cumulative. Use sum over time instead of increase : promql Correct sum sum over time claude code lines of code count total $ range Wrong - will give weird extrapolated values sum increase claude code lines of code count total $ range 7. Troubleshooting No metrics showing up 1. Check env vars are set: env | grep OTEL 2. Verify Prometheus is running with OTLP flags: ps aux | grep prometheus 3. Check Prometheus logs for OTLP errors 4. Make sure you've used Claude Code after setting env vars restart terminal first Grafana can't connect to Prometheus 1. Verify Prometheus is running: curl http://localhost:9090/-/healthy 2. Check Grafana data source URL matches Prometheus listen address Dashboard shows no data 1. Check time range try "Last 24 hours" 2. Verify metrics exist: curl 'http://localhost:9090/api/v1/query?query=claude code token usage tokens total' 3. Make sure Prometheus data source is selected in dashboard 8. For Datadog Alternative If pushing to Datadog instead of local Prometheus: bash export OTEL METRICS EXPORTER=otlp export OTEL EXPORTER OTLP PROTOCOL=http/protobuf export OTEL EXPORTER OTLP ENDPOINT=https://otel.datadoghq.com export OTEL EXPORTER OTLP HEADERS="DD-API-KEY=your api key" The same metrics will flow to Datadog, and you can build similar dashboards there.