API Cost Tracker 💰
Comprehensive AI API cost tracking across multiple providers. Monitor spending, set budgets, get alerts, and optimize your AI costs.
Features
- - ✅ Multi-Provider Support - OpenAI, Anthropic, Google AI
- ✅ Real-Time Tracking - Monitor costs as they happen
- ✅ Budget Alerts - Get notified when approaching limits
- ✅ Usage Analytics - Detailed insights into API usage
- ✅ Cost Optimization - Tips to reduce spending
- ✅ Export Reports - JSON, CSV, Markdown formats
- ✅ Historical Data - Track costs over time
- ✅ Model Comparison - Compare costs across models
Installation
CODEBLOCK0
Quick Start
CODEBLOCK1
Configuration
Edit config.json:
CODEBLOCK2
API Reference
track(options)
Track API usage and costs.
Options:
- -
provider (string): Specific provider or 'all' - INLINECODE3 (string): 'today', 'week', 'month', 'all'
Returns:
CODEBLOCK3
analytics(period)
Get detailed analytics.
Period: 'day', 'week', 'month', 'year'
Returns:
- - Cost trends
- Usage patterns
- Model efficiency
- Optimization suggestions
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
CODEBLOCK4
Output:
CODEBLOCK5
Set Budget Alert
CODEBLOCK6
Export Monthly Report
CODEBLOCK7
Compare Models
CODEBLOCK8
Pricing Reference
OpenAI (per 1K tokens)
- - GPT-4: $0.03 (input) / $0.06 (output)
- GPT-4 Turbo: $0.01 / $0.03
- GPT-3.5 Turbo: $0.0005 / $0.0015
Anthropic (per 1K tokens)
- - Claude-3 Opus: $0.015 / $0.075
- Claude-3 Sonnet: $0.003 / $0.015
- Claude-3 Haiku: $0.00025 / $0.00125
Google AI (per 1K tokens)
- - Gemini Pro: $0.00025 / $0.0005
- Gemini Ultra: $0.0025 / $0.0075
Integration with OpenClaw
Add to your HEARTBEAT.md for automated tracking:
CODEBLOCK9
Automated Budget Monitoring
CODEBLOCK10
Advanced Features
Cost Optimization Tips
Run optimization analysis:
CODEBLOCK11
Get suggestions like:
- - Switch to GPT-3.5 for simple tasks
- Use Claude-3 Haiku for fast responses
- Batch requests to reduce API calls
- Cache common responses
Webhook Integration
Configure alerts to send to webhooks:
CODEBLOCK12
Historical Analysis
CODEBLOCK13
Data Storage
Cost data is stored locally:
CODEBLOCK14
Troubleshooting
API Key Issues
CODEBLOCK15
Missing Data
CODEBLOCK16
Testing
CODEBLOCK17
License
MIT - Free for personal and commercial use.
Support
- - GitHub Issues: [your-repo]
- OpenClaw Discord: https://discord.gg/clawd
Roadmap
- - [ ] More providers (Cohere, AI21, etc.)
- [ ] Real-time dashboard
- [ ] Team cost sharing
- [ ] Custom pricing rules
- [ ] Predictive analytics
API 成本追踪器 💰
跨多提供商的全方位AI API成本追踪。监控支出、设定预算、获取警报,优化您的AI成本。
功能特性
- - ✅ 多提供商支持 - OpenAI、Anthropic、Google AI
- ✅ 实时追踪 - 实时监控成本发生情况
- ✅ 预算警报 - 接近限额时自动通知
- ✅ 使用分析 - API使用的详细洞察
- ✅ 成本优化 - 降低支出的实用建议
- ✅ 导出报告 - 支持JSON、CSV、Markdown格式
- ✅ 历史数据 - 追踪长期成本变化
- ✅ 模型对比 - 跨模型成本比较
安装
bash
cd api-cost-tracker
npm install
快速开始
bash
追踪所有提供商
node scripts/main.mjs track
追踪特定提供商
node scripts/main.mjs track --provider openai
查看分析
node scripts/main.mjs analytics
设置预算
node scripts/main.mjs budget set 100 --monthly
导出报告
node scripts/main.mjs export --format markdown --output report.md
配置
编辑 config.json:
json
{
providers: {
openai: {
enabled: true,
apiKey: ${OPENAIAPIKEY}
},
anthropic: {
enabled: true,
apiKey: ${ANTHROPICAPIKEY}
},
google: {
enabled: true,
apiKey: ${GOOGLEAIKEY}
}
},
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 参考
track(options)
追踪API使用和成本。
选项:
- - provider (字符串):指定提供商或all
- period (字符串):today、week、month、all
返回:
json
{
total: 45.67,
providers: {
openai: 32.10,
anthropic: 10.50,
google: 3.07
},
models: {
gpt-4: 28.50,
claude-3: 10.50
}
}
analytics(period)
获取详细分析。
周期: day、week、month、year
返回:
budget.set(amount, period)
设置预算限额。
budget.check()
检查当前预算状态。
export(format, options)
导出成本报告。
格式: json、csv、markdown
使用示例
追踪每日成本
bash
node scripts/main.mjs track --period today
输出:
💰 API 成本 - 今日
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
OpenAI
GPT-4:$12.50(125K tokens)
GPT-3.5:$2.30(230K tokens)
小计:$14.80
Anthropic
Claude-3:$8.20(82K tokens)
小计:$8.20
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
总计:$23.00
预算:$200/月(已使用11.5%)
设置预算警报
bash
node scripts/main.mjs budget set 100 --monthly
node scripts/main.mjs alerts enable
导出月度报告
bash
node scripts/main.mjs export --format markdown --period month --output monthly-report.md
比较模型
bash
node scripts/main.mjs compare --models gpt-4,claude-3,gemini-pro
定价参考
OpenAI(每1K tokens)
- - GPT-4:$0.03(输入)/ $0.06(输出)
- GPT-4 Turbo:$0.01 / $0.03
- GPT-3.5 Turbo:$0.0005 / $0.0015
Anthropic(每1K tokens)
- - Claude-3 Opus:$0.015 / $0.075
- Claude-3 Sonnet:$0.003 / $0.015
- Claude-3 Haiku:$0.00025 / $0.00125
Google AI(每1K tokens)
- - Gemini Pro:$0.00025 / $0.0005
- Gemini Ultra:$0.0025 / $0.0075
与OpenClaw集成
添加到您的HEARTBEAT.md以实现自动追踪:
markdown
每6小时:
- - 运行:node /path/to/api-cost-tracker/scripts/main.mjs track
- 如果预算 > 75%则发出警报
自动预算监控
bash
添加到crontab
0
/6 cd /path/to/api-cost-tracker && node scripts/main.mjs check-budget
高级功能
成本优化建议
运行优化分析:
bash
node scripts/main.mjs optimize
获取建议如:
- - 简单任务切换到GPT-3.5
- 快速响应使用Claude-3 Haiku
- 批量请求减少API调用
- 缓存常见响应
Webhook集成
配置警报发送到webhooks:
json
{
webhooks: [
{
url: https://your-slack-webhook.com,
events: [budgetexceeded, highusage]
}
]
}
历史分析
bash
查看最近30天
node scripts/main.mjs history --days 30
比较月份
node scripts/main.mjs compare --period month --previous
数据存储
成本数据本地存储:
data/
├── costs/
│ ├── 2026-03-01.json
│ ├── 2026-03-02.json
│ └── ...
├── budgets.json
└── alerts.log
故障排除
API密钥问题
bash
测试API密钥
node scripts/main.mjs test-keys
数据缺失
bash
重建数据库
node scripts/main.mjs rebuild
测试
bash
npm test
许可证
MIT - 免费用于个人和商业用途。
支持
- - GitHub Issues:[your-repo]
- OpenClaw Discord:https://discord.gg/clawd
路线图
- - [ ] 更多提供商(Cohere、AI21等)
- [ ] 实时仪表板
- [ ] 团队成本分摊
- [ ] 自定义定价规则
- [ ] 预测分析