{"slug": "claude-code-metrics-via-a-local-lgtm-stack", "title": "Claude code metrics via a local LGTM stack", "summary": "A developer documented how to export Claude Code telemetry to a locally running LGTM stack (Loki, Grafana, Tempo, Prometheus/Mimir) via OpenTelemetry. By setting environment variables such as CLAUDE_CODE_ENABLE_TELEMETRY, OTEL_METRICS_EXPORTER, and OTEL_EXPORTER_OTLP_ENDPOINT, Claude Code emits metrics (token usage, cost, session count, lines of code, active time), logs, and beta traces to an OTLP gRPC endpoint on port 4317. The writeup notes that containerized setups must swap localhost for host.containers.internal and points to Grafana dashboard ID 25255 for visualization.", "body_md": "This assumes that you have an LGTM stack running locally, for example [https://github.com/schroenser/lgtm](https://github.com/schroenser/lgtm).\nLGTM stands for\n\n- L — Loki (for logs)\n- G — Grafana (for visualization and correlation)\n- T — Tempo (for distributed traces)\n- M — Prometheus / Mimir (for metrics — with Prometheus handling collection/storage and Mimir often used for long-term scalable metrics)\n\nA set of environment variables must be set before running claude.\nThis can be done either using an `.envrc` file or claudes `settings.json`.\n\n💡 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.\n\n```\n{\n  \"env\": {\n    \"CLAUDE_CODE_ENABLE_TELEMETRY\": \"1\",\n    \"OTEL_METRICS_EXPORTER\": \"otlp\",\n    \"OTEL_METRIC_EXPORT_INTERVAL\": \"5000\",\n    \"OTEL_LOGS_EXPORTER\": \"otlp\",\n    \"OTEL_LOGS_EXPORT_INTERVAL\": \"5000\",\n    \"CLAUDE_CODE_ENHANCED_TELEMETRY_BETA\": \"1\",\n    \"OTEL_TRACES_EXPORTER\": \"otlp\",\n    \"OTEL_TRACES_EXPORT_INTERVAL\": \"5000\",\n    \"OTEL_EXPORTER_OTLP_PROTOCOL\": \"grpc\",\n    \"OTEL_EXPORTER_OTLP_ENDPOINT\": \"http://localhost:4317\",\n    \"OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE\": \"cumulative\"\n  }\n}\n```\n\ne.g. with [direnv](https://direnv.net/)\n\n```\n# Required\nexport CLAUDE_CODE_ENABLE_TELEMETRY=1\n\n# Metrics\nexport OTEL_METRICS_EXPORTER=otlp\nexport OTEL_METRIC_EXPORT_INTERVAL=5000\n\n# Logs / events\nexport OTEL_LOGS_EXPORTER=otlp\nexport OTEL_LOGS_EXPORT_INTERVAL=5000\n\n# Traces (beta — requires the flag below)\nexport CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1\nexport OTEL_TRACES_EXPORTER=otlp\nexport OTEL_TRACES_EXPORT_INTERVAL=5000\n\n# Transport\nexport OTEL_EXPORTER_OTLP_PROTOCOL=grpc\nexport OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317\nexport OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=cumulative\n\n# Optional: include sensitive content in logs\n# export OTEL_LOG_USER_PROMPTS=1\n# export OTEL_LOG_TOOL_DETAILS=1\n```\n\n| Signal | Key names | Default interval | \n|---|---|---|\n| 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 | \n| Logs | `claude_code.user_prompt` ,`claude_code.api_request` ,`claude_code.tool_result` ,`claude_code.tool_decision` | 5 s | \n| Traces (beta) | `claude_code.interaction` ,`claude_code.llm_request` ,`claude_code.tool` ,`claude_code.tool.execution` | 5 s | \n\nThe 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`.", "url": "https://wpnews.pro/news/claude-code-metrics-via-a-local-lgtm-stack", "canonical_source": "https://gist.github.com/schroenser/cf3535ee678600db430e3e417d6755bf", "published_at": "2026-11-02 23:02:28+00:00", "updated_at": "2026-11-05 06:22:01.393661+00:00", "lang": "en", "topics": ["ai-tools", "mlops", "developer-tools", "ai-agents"], "entities": ["Claude Code", "Grafana", "Loki", "Tempo", "Prometheus", "Mimir", "OpenTelemetry", "direnv"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/claude-code-metrics-via-a-local-lgtm-stack", "markdown": "https://wpnews.pro/news/claude-code-metrics-via-a-local-lgtm-stack.md", "text": "https://wpnews.pro/news/claude-code-metrics-via-a-local-lgtm-stack.txt", "jsonld": "https://wpnews.pro/news/claude-code-metrics-via-a-local-lgtm-stack.jsonld"}}