SETTINGS · cost-engineering

Claude Code OTel cost telemetry kit

Managed settings that force cost telemetry on for a fleet, an OTel collector config that receives it, and a starter Grafana dashboard for spend by user, model, and token type.

✓ verified 2026-07-07 · stack: claude-code · source

Three files that turn Claude Code cost telemetry from a volunteer activity into a fleet property: a managed settings file users cannot override, a collector configuration that receives metrics and events, and a starter dashboard. Background and the full attribute reference: instrumenting Claude Code.

The managed settings file lives at /Library/Application Support/ClaudeCode/managed-settings.json on macOS or /etc/claude-code/managed-settings.json on Linux and WSL (on Windows, C:\Program Files\ClaudeCode\: the old C:\ProgramData\ path stopped working in v2.1.75), distributed by MDM or configuration management:

managed-settings.json
{
"env": {
"CLAUDE_CODE_ENABLE_TELEMETRY": "1",
"OTEL_METRICS_EXPORTER": "otlp",
"OTEL_LOGS_EXPORTER": "otlp",
"OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",
"OTEL_EXPORTER_OTLP_ENDPOINT": "https://otel-collector.internal.example.com:4317",
"OTEL_METRICS_INCLUDE_SESSION_ID": "false",
"OTEL_METRICS_INCLUDE_ACCOUNT_UUID": "true"
}
}

Team- and cost-centre-level dimensions belong in each team’s shell profile, not the fleet file:

per-team shell profile (sourced from ~/.zshrc or ~/.bashrc)
export OTEL_RESOURCE_ATTRIBUTES="department=engineering,team.id=${TEAM},cost_center=${COSTCENTER}"

The collector receives OTLP from every laptop, batches, and fans out: metrics to a Prometheus scrape endpoint, events to your log backend.

otel-collector.yaml
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
processors:
batch: {}
exporters:
prometheus:
endpoint: 0.0.0.0:9464
otlphttp/logs:
endpoint: https://logs.internal.example.com/otlp
service:
pipelines:
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheus]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlphttp/logs]

The starter dashboard covers the four week-one questions: spend by user, spend by model, token mix by type, and the cache-read share.

claude-code-cost-dashboard.json (Grafana)
{
"title": "Claude Code — cost",
"schemaVersion": 39,
"time": { "from": "now-7d", "to": "now" },
"panels": [
{
"type": "timeseries",
"title": "Daily spend by user (USD, client-side estimate)",
"gridPos": { "x": 0, "y": 0, "w": 12, "h": 8 },
"targets": [
{ "expr": "sum by (user_email) (increase(claude_code_cost_usage_USD_total[1d]))" }
]
},
{
"type": "timeseries",
"title": "Daily spend by model (USD, client-side estimate)",
"gridPos": { "x": 12, "y": 0, "w": 12, "h": 8 },
"targets": [
{ "expr": "sum by (model) (increase(claude_code_cost_usage_USD_total[1d]))" }
]
},
{
"type": "timeseries",
"title": "Token mix by type",
"gridPos": { "x": 0, "y": 8, "w": 12, "h": 8 },
"targets": [
{ "expr": "sum by (type) (increase(claude_code_token_usage_tokens_total[1d]))" }
]
},
{
"type": "stat",
"title": "Cache-read share (7d)",
"gridPos": { "x": 12, "y": 8, "w": 12, "h": 8 },
"targets": [
{
"expr": "sum(increase(claude_code_token_usage_tokens_total{type=\"cacheRead\"}[7d])) / sum(increase(claude_code_token_usage_tokens_total[7d]))"
}
]
}
]
}

What it does

  • The managed settings sit at the top of Claude Code’s precedence order (above user, project, and command-line settings), so telemetry is on for every session on the machine and no local setting can switch it off.
  • Metrics arrive with user.email, user.account_uuid, and organization.id when the developer is authenticated: per-developer spend attribution with no per-repo work.
  • OTEL_METRICS_INCLUDE_SESSION_ID=false keeps fleet metric cardinality bounded; per-session detail still flows on the events side, where session.id and prompt.id are fields rather than time series.
  • All content-carrying gates (OTEL_LOG_USER_PROMPTS, OTEL_LOG_ASSISTANT_RESPONSES, OTEL_LOG_TOOL_DETAILS) are deliberately absent, so they stay at their off defaults: cost analysis needs none of them.
  • To verify emission on one machine before the rollout, run with OTEL_METRICS_EXPORTER=console and OTEL_METRIC_EXPORT_INTERVAL=1000: metrics print to the terminal every second.

Failure modes

  • The dollars are estimates. claude_code.cost.usage is computed client-side; the docs say plainly to use your provider’s billing data for anything official. Label dashboards accordingly (the panel titles here do). The first time this number is quoted at finance as a bill, the measurement programme loses its credibility.
  • Prometheus metric names need verifying once. The PromQL above uses the common normalisation (claude_code_cost_usage_USD_total); exact names depend on the collector version’s translation settings. Check curl localhost:9464/metrics on the collector once and adjust the three expressions if yours differ.
  • OTEL_* variables do not reach subprocesses. Claude Code deliberately keeps them out of Bash tool calls, hooks, and MCP servers, so anything a session runs is not accidentally instrumented, and your own scripts cannot rely on inheriting the endpoint.
  • The events stream carries identity. user.email on every event is the point, and it is PII. Treat the logs backend with the same access controls as any HR-adjacent system, and leave the content gates off.
  • Unmanaged laptops leak. Managed settings only bind machines that have the file; contractors and personal machines silently fall outside the data set. Track coverage (sessions by known user.email versus headcount) rather than assuming it.
  • Subscription sessions report value, not spend. On Pro and Max plans the figures are API-equivalent value of quota consumed, not incremental dollars. Fine for comparing teams and models; wrong for invoice forecasting.

Verified against the Claude Code monitoring, status line, and settings documentation (metric and event names, attribute lists, managed-settings paths and precedence, cardinality defaults), 2026-07-07.