Query and monitor the quota/balance/consumption of various API providers and subscription services.
Quick Start
# 查询所有已配置的供应商
python3 scripts/query_quota.py
# 查询特定供应商
python3 scripts/query_quota.py xai
python3 scripts/query_quota.py openrouter
python3 scripts/query_quota.py brave
# 只查官方供应商
python3 scripts/query_quota.py --type official
Query Subscription Services Only
python3 scripts/query_quota.py --type service
JSON Outputpython3 scripts/query_quota.py --format json
Discover Configured Providerspython3 scripts/query_quota.py --discover
## 工作流
- Discover locally configured APIs/providers (from environment variables, auth-session-state.json) ↓
- Query by type: API direct query > browser scraping ↓
- Generate report: Provider | Consumption | Total Quota | Status
## 供应商分类
### 官方供应商 (Official)
| 供应商 | 查询方式 | 环境变量 | 状态 |
|--------|----------|----------|------|
| ZAI (智谱) | API 直查 | `ZAI_API_KEY` | ✅ 支持 |
| Minimax | API 直查 | `MINIMAX_API_KEY` + `MINIMAX_GROUP_ID` | ✅ 支持 |
| OpenRouter | API 直查 | `OPENROUTER_API_KEY` | ✅ 支持 |
| xAI (Grok) | 浏览器 | `XAI_API_KEY` | 🔍 需浏览器 |
| Gemini | 浏览器 | `GOOGLE_API_KEY` | 🔍 需浏览器 |
| Anthropic | 浏览器 | `ANTHROPIC_API_KEY` | 🔍 需浏览器 |
### 中转站 (Reseller)
| 平台 | 查询方式 | 状态来源 |
|------|----------|----------|
| AIXN | 浏览器抓 ai.9w7.cn | auth-session-state.json |
| Provider-A | 浏览器抓 your-provider.example.com | auth-session-state.json |
中转站查询流程:
1. 检查 `~/.openclaw/auth-session-state.json` 获取登录状态
2. 使用 `browser(action='navigate', profile='openclaw')` 访问控制台
3. 从 snapshot 提取余额/消耗信息
### 订阅服务 (Services)
| 服务 | 查询方式 | 环境变量 |
|------|----------|----------|
| Brave Search API | API 直查 | `BRAVE_API_KEY` |
| Tavily API | API 直查 | `TAVILY_API_KEY` |
| Serper API | API 直查 | `SERPER_API_KEY` |
## 浏览器查询 (需浏览器抓取的供应商)
当供应商没有公开的 usage API 时,使用 browser 工具:
1. Navigate to the console page
browser(action='navigate', profile='openclaw', targetUrl='<console_url>')
2. Wait for loading, then take a snapshotbrowser(action='snapshot', profile='openclaw', compact=true, maxChars=2000)
3. Extract balance/consumption information from the snapshot Search keywords: balance, balance, usage, consumption, tokens, quota
**各供应商控制台 URL:**
- xAI: https://console.x.ai/usage
- Gemini: https://aistudio.google.com/apikey
- Anthropic: https://console.anthropic.com/settings/billing
- AIXN: https://ai.9w7.cn/console
- Provider-A: https://your-provider.example.com/dashboard
## 报告格式
📊 API Quota Report ⏰ 2026-03-15 20:30
━━ Official Providers ━━ ✅ ZAI (Zhipu): ¥50.00 ✅ Minimax: ¥10.00 CNY ✅ OpenRouter: $45.00 remaining 🔍 xAI (Grok): browser query required
━━ Subscription Services ━━ ✅ Brave Search: 1500/2000 requests ✅ Tavily: 800 remaining
━━ Relay Stations ━━ ✅ AIXN: ¥238.81 ✅ Provider-A: ¥9.35
## 扩展新供应商
在 `scripts/query_quota.py` 中添加新供应商配置:
```python
OFFICIAL_PROVIDERS = {
"new_provider": {
"name": "New Provider",
"env_key": "NEW_PROVIDER_API_KEY",
"api_url": "https://api.newprovider.com/v1/usage",
"method": "api", # 或 "browser"
"extract": lambda data: {
"balance": data.get("balance", 0),
}
},
}
Caching Strategy
- API query results can be cached for 1-6 hours (to avoid frequent requests)
- Cache file:
~/.openclaw/quota-cache.json - Format:
{
"openrouter": {
"queried_at": "2026-03-15T20:00:00",
"balance": 45.0,
"cached": true
}
}
Scheduled Monitoring
A cron job can be configured to check periodically and push reports:
# 在 OpenClaw cron 中添加
# 每 6 小时检查一次 API 供应商额度
0 */6 * * * python3 ~/clawd/skills/api-quota-monitor/scripts/query_quota.py --report | newsbot_send
Reference Documentation
For detailed API endpoints and response formats, see references/providers.md