Prometheus exporter for Claude API usage and cost metrics
Project description
Claude Prometheus Exporter
A Prometheus exporter for Claude API usage and cost metrics.
Features
- Token Metrics: Track input, output, cache read, and cache creation tokens
- Request Metrics: Monitor API request counts
- Cost Metrics: Track API costs in USD
- Automatic Collection: Continuous metric collection with configurable intervals
- Simple: Single-file implementation following the same pattern as updownio-exporter
Prerequisites
- Python 3.13+
- Poetry (for dependency management)
- Claude Admin API key (starts with
sk-ant-admin...)
Installation
poetry install
Configuration
Configuration via environment variables or config files:
Environment Variables
export APIKEY="sk-ant-admin-..."
export PROMETHEUS_PORT=9001
export LOOP_INTERVAL=30
export LOGGING_LEVEL=INFO
Config Files
Place a JSON config file at:
/etc/claude-prometheus/claude-prometheus.json~/.config/claude-prometheus.json
Example config:
{
"apikey": "sk-ant-admin-...",
"prometheus": {
"port": 9001,
"namespace": "claude"
},
"loop": {
"interval": 30
},
"logging": {
"level": "INFO"
}
}
Configuration Options
| Option | Description | Default |
|---|---|---|
apikey |
Claude Admin API key | Required |
prometheus.port |
Port to expose metrics on | 9001 |
prometheus.namespace |
Prometheus namespace | claude |
loop.interval |
Seconds between collections | 30 |
logging.level |
Log level (DEBUG, INFO, WARNING, ERROR) | INFO |
Note: The exporter always fetches data from midnight (UTC) to now on each collection. It uses delta calculation to avoid double-counting, so counters only increment with new usage since the last collection.
Usage
poetry run python claude_prometheus.py
Or with custom config:
export APIKEY="sk-ant-admin-..."
export LOOP_INTERVAL=60
poetry run python claude_prometheus.py
Metrics Endpoint
Once running, metrics are available at:
http://localhost:9001/metrics
Exported Metrics
All metrics use the claude_ namespace prefix (configurable).
Token Metrics
claude_tokens_total(Counter): Total tokens consumed by type- Labels:
type(input, output, cache_read, cache_creation),model,api_key_id,api_key_name,owner_email,owner_name
- Labels:
claude_tokens_current(Gauge): Tokens in current period- Labels:
type,model,api_key_id,api_key_name,owner_email,owner_name
- Labels:
Request Metrics
claude_requests_total(Counter): Total API requests- Labels:
model,api_key_id,api_key_name,owner_email,owner_name
- Labels:
claude_requests_current(Gauge): Requests in current period- Labels:
model,api_key_id,api_key_name,owner_email,owner_name
- Labels:
Cost Metrics
claude_cost_total_usd(Counter): Total cumulative cost in USD- Labels:
workspace_id
- Labels:
claude_cost_current_usd(Gauge): Cost in current period in USD- Labels:
workspace_id
- Labels:
Daemon Metrics
Provided by daemon-metrics:
claude_prometheus_loop_duration_seconds: Duration of each collection loopclaude_prometheus_loop_success: Whether the last loop succeededclaude_prometheus_item_success: Per-API-key success metrics
Getting an Admin API Key
- Log in to the Claude Console
- Navigate to the Admin settings (requires admin role)
- Generate an Admin API key
Note: Standard API keys (starting with sk-ant-api) will NOT work.
Example Prometheus Configuration
Add this to your prometheus.yml:
scrape_configs:
- job_name: 'claude-exporter'
scrape_interval: 60s
static_configs:
- targets: ['localhost:9001']
Example Queries
Total tokens by model
sum by (model) (rate(claude_tokens_total[5m]))
Total tokens by API key owner
sum by (owner_name, owner_email) (rate(claude_tokens_total[5m]))
Total tokens by API key
sum by (api_key_id, api_key_name) (rate(claude_tokens_total[5m]))
Request rate by owner
sum by (owner_name) (rate(claude_requests_total[5m]))
Top users by token usage
topk(10, sum by (owner_name, owner_email) (claude_tokens_current))
Top API keys by usage
topk(10, sum by (api_key_name, owner_name) (claude_tokens_current))
Current cost by workspace
sum by (workspace_id) (claude_cost_current_usd)
Development
Code Quality Tools
# Format code
poetry run black claude_prometheus.py
# Type checking
poetry run mypy claude_prometheus.py
# Linting
poetry run pylint claude_prometheus.py
poetry run flake8 claude_prometheus.py
poetry run pycodestyle claude_prometheus.py
Architecture
Following the same pattern as updownio-exporter, this is a single-file implementation with:
- Configuration management via
the-conf - Daemon metrics via
daemon-metrics - Clean collection loop with proper timing
- Prometheus metrics via
prometheus_client
Troubleshooting
Authentication Errors
Ensure you're using an Admin API key (starts with sk-ant-admin).
Permission Errors
Your API key must have admin permissions to access usage/cost data.
No Data in Metrics
- Check that your organization has recent API usage
- Adjust
loop.lookbackto a longer period - Check logs for API errors
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claude_prometheus_exporter-0.1.0.tar.gz.
File metadata
- Download URL: claude_prometheus_exporter-0.1.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.3 Linux/6.14.0-27-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2196de59fa7fb6d1c380daf4f897aac4f984cb3768114ed73fcc07a6d5263d00
|
|
| MD5 |
894f008b8793cc0b71b331838ba96f8b
|
|
| BLAKE2b-256 |
74f76ae6630e64884d3e2511b9ff527fb427f145b215cf50ee3c5156aa850866
|
File details
Details for the file claude_prometheus_exporter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: claude_prometheus_exporter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.3 Linux/6.14.0-27-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
feedec7de4a352e4dc3ec45478d6f504643f7b0f2731441dab0e56a454c017c8
|
|
| MD5 |
65aa72b84e321ddd904b8eb1193b7628
|
|
| BLAKE2b-256 |
d89ed8d7a831caa1ecee438b1545bd3ee9b1752048d54edd037052081ea62c8c
|