Sign inSign up

antlac1/claude-api-exporter

By antlac1

Updated 27 days ago

Prometheus exporter for Anthropic Claude API usage and cost per key, model, and monthly rollups.

Image
API management
Developer tools
Monitoring & observability
0

3.9K

antlac1/claude-api-exporter repository overview

Anthropic API Usage & Cost Exporter

Prometheus exporter for Anthropic Claude API spend. Polls the Admin Usage API and Cost API on a configurable interval and exposes metrics on :9402/metrics — broken down by API key, model, token type, geographic region, and date, with dedicated monthly rollup metrics for at-a-glance cost tracking.

A pre-built Grafana dashboard is included in the GitHub repo.


Quick Start

docker run -d \
  --name anthropic-exporter \
  --restart unless-stopped \
  -e ANTHROPIC_ADMIN_KEY=sk-ant-admin01-... \
  -p 9402:9402 \
  antlac1/claude-api-exporter:latest

Then verify:

curl http://localhost:9402/metrics

Docker Compose

services:
  anthropic-exporter:
    image: antlac1/claude-api-exporter:latest
    container_name: anthropic-exporter
    restart: unless-stopped
    environment:
      ANTHROPIC_ADMIN_KEY: sk-ant-admin01-...
    ports:
      - "9402:9402"

Metrics

MetricLabelsDescription
anthropic_usage_tokensapi_key_id, api_key_name, model, token_type, inference_geo, dateToken counts per daily bucket. token_type: input / output / cache_write / cache_read
anthropic_estimated_cost_usdapi_key_id, api_key_name, model, inference_geo, dateEstimated USD cost from token counts × pricing table
anthropic_billed_cost_usdworkspace_id, description, dateActual billed cost from the Cost API (workspace level)
anthropic_monthly_estimated_cost_usdapi_key_id, api_key_nameEstimated cost for the current calendar month, per API key
anthropic_monthly_billed_cost_usdworkspace_idBilled cost for the current calendar month, per workspace
anthropic_exporter_up1 if the last poll succeeded, 0 if it failed
anthropic_exporter_last_success_timestamp_secondsUnix timestamp of the last successful poll
anthropic_exporter_poll_errors_totalCumulative count of failed poll cycles

Configuration

All configuration is via environment variables.

VariableDefaultDescription
ANTHROPIC_ADMIN_KEYRequired. Org admin key (sk-ant-admin01-...) — create one in the Claude Console
EXPORTER_PORT9402Port the /metrics endpoint listens on
POLL_INTERVAL_SECONDS300Seconds between poll cycles. Keep ≥ 60
LOOKBACK_DAYS2Days of history re-fetched each cycle (covers late/revised data)
COST_IN_CENTStrueSet to false if your Cost API returns fractional dollars instead of cents
PRICING_FILE/app/pricing.jsonPath to the pricing table. Mount a custom file to override rates without rebuilding
LOG_LEVELINFOSet to DEBUG to log unmapped token fields

Pricing

Model pricing is fetched automatically from the Anthropic documentation at image build time. If the fetch fails, the image falls back to a bundled pricing.json. You can override rates at any time by mounting your own file:

docker run -d \
  -e ANTHROPIC_ADMIN_KEY=sk-ant-admin01-... \
  -v ./pricing.json:/app/pricing.json:ro \
  -p 9402:9402 \
  antlac1/claude-api-exporter:latest

Prometheus Scrape Config

scrape_configs:
  - job_name: anthropic
    scrape_interval: 300s
    static_configs:
      - targets: ["anthropic-exporter:9402"]

Grafana Dashboard

A pre-built dashboard is available in the GitHub repo at grafana/dashboard.json. Import it via Dashboards → Import → Upload JSON file and select your Prometheus data source.

Dashboard sections:

  • Health — Exporter status, time since last poll, lookback-window cost totals
  • Monthly Summary — Current-month estimated and billed cost totals, per-key and per-workspace breakdowns
  • Cost Breakdown — Estimated cost by API key and by model
  • Token Usage — Tokens by type, by API key, and cache hit ratio
  • Reconciliation — Estimated vs. billed side-by-side, billed cost by description
  • Detail — Filterable table with token counts and cost per key / model / date

Source

github.com/alacava/claude-api-exporter

Tag summary

Content type

Image

Digest

sha256:a6c6e8e7e

Size

42.7 MB

Last updated

27 days ago

docker pull antlac1/claude-api-exporter