Add /usage endpoint with token tracking, live balances, and cost calculations
This commit is contained in:
@@ -59,7 +59,7 @@ cd ein-ai-proxy
|
||||
cp config.example.json config.json
|
||||
```
|
||||
|
||||
Edit `config.json` with your API keys:
|
||||
Edit `config.json` with your API keys and pricing:
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -69,10 +69,26 @@ Edit `config.json` with your API keys:
|
||||
"anthropic": { "apiKey": "sk-ant-..." },
|
||||
"zai": { "apiKey": "your-zai-key" },
|
||||
"deepseek": { "apiKey": "your-deepseek-key" }
|
||||
},
|
||||
"pricing": {
|
||||
"anthropic": {
|
||||
"inputPerMTok": 15,
|
||||
"outputPerMTok": 75
|
||||
},
|
||||
"deepseek": {
|
||||
"inputPerMTok": 0.27,
|
||||
"outputPerMTok": 1.10
|
||||
},
|
||||
"zai": {
|
||||
"type": "flat",
|
||||
"monthlyCostUsd": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Pricing notes**: Rates are per million tokens (input/output). Update these values based on current provider pricing. Z.ai uses a flat-rate model ($10/mo) regardless of token usage.
|
||||
|
||||
### Run
|
||||
|
||||
```bash
|
||||
@@ -185,6 +201,61 @@ curl https://ai.ein-softworks.com/health
|
||||
|
||||
Returns the proxy status and current routing table. No authentication required.
|
||||
|
||||
### Usage Statistics
|
||||
|
||||
```bash
|
||||
curl https://ai.ein-softworks.com/usage
|
||||
```
|
||||
|
||||
Returns comprehensive usage data grouped by provider:
|
||||
|
||||
- **Per-model breakdown**: Requested model name, actual provider model, total + monthly token counts and request counts
|
||||
- **Cost calculations**: Dollar amounts for each model (based on pricing in `config.json`)
|
||||
- **Live balances**: Account balances from providers that support it (DeepSeek)
|
||||
- **Totals**: Aggregated request counts and costs across all models
|
||||
|
||||
Example response:
|
||||
|
||||
```json
|
||||
{
|
||||
"service": "ein-ai-proxy",
|
||||
"generatedAt": "2026-03-27T00:38:00.000Z",
|
||||
"currentPeriod": "2026-03",
|
||||
"providers": {
|
||||
"anthropic": {
|
||||
"balance": null,
|
||||
"pricingNote": "No balance API available — check console.anthropic.com",
|
||||
"models": {
|
||||
"claude-opus-4-6": {
|
||||
"routesTo": "claude-opus-4-6",
|
||||
"total": { "inputTokens": 150000, "outputTokens": 50000, "requests": 42, "costUsd": 6.00 },
|
||||
"monthly": { "inputTokens": 30000, "outputTokens": 10000, "requests": 8, "costUsd": 1.20 }
|
||||
}
|
||||
}
|
||||
},
|
||||
"deepseek": {
|
||||
"balance": { "totalBalance": "8.52", "currency": "USD" },
|
||||
"models": { ... }
|
||||
},
|
||||
"zai": {
|
||||
"balance": null,
|
||||
"pricing": "flat_rate",
|
||||
"monthlyCostUsd": 10,
|
||||
"models": { ... }
|
||||
}
|
||||
},
|
||||
"totals": {
|
||||
"totalRequests": 252,
|
||||
"monthlyRequests": 53,
|
||||
"totalCostUsd": 6.14,
|
||||
"monthlyCostUsd": 1.24,
|
||||
"note": "Cost excludes flat-rate models. Z.ai costs $10/mo regardless of usage."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: Monthly totals reset on the 1st of each month. Usage data is persisted to `usage-data.json` in the proxy directory.
|
||||
|
||||
### Messages Endpoint
|
||||
|
||||
```bash
|
||||
|
||||
Reference in New Issue
Block a user