API Cost Tracker
by @k5rs4n
Track AI API costs across OpenAI, Anthropic, Google AI with budget alerts, analytics, and optimization tips
clawhub install api-cost-trackerπ About This Skill
name: api-cost-tracker description: "Track AI API costs across OpenAI, Anthropic, Google AI with budget alerts, analytics, and optimization tips" metadata: clawdbot: emoji: π° requires: env: - OPENAI_API_KEY (optional) - ANTHROPIC_API_KEY (optional) - GOOGLE_AI_KEY (optional)
API Cost Tracker π°
Comprehensive AI API cost tracking across multiple providers. Monitor spending, set budgets, get alerts, and optimize your AI costs.
Features
Installation
cd api-cost-tracker
npm install
Quick Start
# Track all providers
node scripts/main.mjs trackTrack specific provider
node scripts/main.mjs track --provider openaiView analytics
node scripts/main.mjs analyticsSet budget
node scripts/main.mjs budget set 100 --monthlyExport report
node scripts/main.mjs export --format markdown --output report.md
Configuration
Edit config.json:
{
"providers": {
"openai": {
"enabled": true,
"apiKey": "${OPENAI_API_KEY}"
},
"anthropic": {
"enabled": true,
"apiKey": "${ANTHROPIC_API_KEY}"
},
"google": {
"enabled": true,
"apiKey": "${GOOGLE_AI_KEY}"
}
},
"budgets": {
"daily": 10,
"weekly": 50,
"monthly": 200
},
"alerts": {
"enabled": true,
"thresholds": [50, 75, 90, 100],
"webhook": "https://your-webhook.com/alert"
},
"tracking": {
"autoTrack": true,
"interval": 300000
}
}
API Reference
track(options)
Track API usage and costs.Options:
provider (string): Specific provider or 'all'period (string): 'today', 'week', 'month', 'all'Returns:
{
"total": 45.67,
"providers": {
"openai": 32.10,
"anthropic": 10.50,
"google": 3.07
},
"models": {
"gpt-4": 28.50,
"claude-3": 10.50
}
}
analytics(period)
Get detailed analytics.Period: 'day', 'week', 'month', 'year'
Returns:
budget.set(amount, period)
Set budget limit.budget.check()
Check current budget status.export(format, options)
Export cost report.Formats: 'json', 'csv', 'markdown'
Usage Examples
Track Daily Costs
node scripts/main.mjs track --period today
Output:
π° API Costs - Today
ββββββββββββββββββββββββββββ
OpenAI
GPT-4: $12.50 (125K tokens)
GPT-3.5: $2.30 (230K tokens)
Subtotal: $14.80Anthropic
Claude-3: $8.20 (82K tokens)
Subtotal: $8.20
ββββββββββββββββββββββββββββ
Total: $23.00
Budget: $200/month (11.5% used)
Set Budget Alert
node scripts/main.mjs budget set 100 --monthly
node scripts/main.mjs alerts enable
Export Monthly Report
node scripts/main.mjs export --format markdown --period month --output monthly-report.md
Compare Models
node scripts/main.mjs compare --models gpt-4,claude-3,gemini-pro
Pricing Reference
OpenAI (per 1K tokens)
Anthropic (per 1K tokens)
Google AI (per 1K tokens)
Integration with OpenClaw
Add to your HEARTBEAT.md for automated tracking:
Every 6 hours:
Run: node /path/to/api-cost-tracker/scripts/main.mjs track
Alert if budget > 75%
Automated Budget Monitoring
# Add to crontab
0 */6 * * * cd /path/to/api-cost-tracker && node scripts/main.mjs check-budget
Advanced Features
Cost Optimization Tips
Run optimization analysis:
node scripts/main.mjs optimize
Get suggestions like:
Webhook Integration
Configure alerts to send to webhooks:
{
"webhooks": [
{
"url": "https://your-slack-webhook.com",
"events": ["budget_exceeded", "high_usage"]
}
]
}
Historical Analysis
# View last 30 days
node scripts/main.mjs history --days 30Compare months
node scripts/main.mjs compare --period month --previous
Data Storage
Cost data is stored locally:
data/
βββ costs/
β βββ 2026-03-01.json
β βββ 2026-03-02.json
β βββ ...
βββ budgets.json
βββ alerts.log
Troubleshooting
API Key Issues
# Test API keys
node scripts/main.mjs test-keys
Missing Data
# Rebuild database
node scripts/main.mjs rebuild
Testing
npm test
License
MIT - Free for personal and commercial use.
Support
Roadmap
π‘ Examples
# Track all providers
node scripts/main.mjs trackTrack specific provider
node scripts/main.mjs track --provider openaiView analytics
node scripts/main.mjs analyticsSet budget
node scripts/main.mjs budget set 100 --monthlyExport report
node scripts/main.mjs export --format markdown --output report.md
βοΈ Configuration
Edit config.json:
{
"providers": {
"openai": {
"enabled": true,
"apiKey": "${OPENAI_API_KEY}"
},
"anthropic": {
"enabled": true,
"apiKey": "${ANTHROPIC_API_KEY}"
},
"google": {
"enabled": true,
"apiKey": "${GOOGLE_AI_KEY}"
}
},
"budgets": {
"daily": 10,
"weekly": 50,
"monthly": 200
},
"alerts": {
"enabled": true,
"thresholds": [50, 75, 90, 100],
"webhook": "https://your-webhook.com/alert"
},
"tracking": {
"autoTrack": true,
"interval": 300000
}
}
π Tips & Best Practices
API Key Issues
# Test API keys
node scripts/main.mjs test-keys
Missing Data
# Rebuild database
node scripts/main.mjs rebuild