Track LLM API costs across multiple providers. Token counting, cost calculation, budget alerts, and per-feature usage attribution.
## Task
Build an LLM API cost tracking and budget management system.
## Requirements
- Language: TypeScript
- Storage: PostgreSQL or SQLite
- Providers: OpenAI, Anthropic, Google, Cohere
## Specifications
```typescript
interface UsageEvent {
provider: "openai" | "anthropic" | "google" | "cohere";
model: string;
inputTokens: number;
outputTokens: number;
cachedTokens?: number;
feature: string; // e.g., "chat", "search", "summarize"
userId?: string;
latencyMs: number;
timestamp: Date;
}
// Pricing table (updated per-provider)
// OpenAI GPT-4o: $2.50/1M input, $10/1M output
// Claude Sonnet: $3/1M input, $15/1M output
// etc.
// Features:
// - Log every LLM call with token counts
// - Calculate cost per call, per feature, per user, per day
// - Budget alerts: warn at 80%, block at 100%
// - Dashboard API: /api/usage/summary?period=7d&groupBy=feature
```
## Implementation Notes
1. Use a middleware/wrapper that intercepts all LLM calls
2. Token counting: use tiktoken for OpenAI, approximate for others
3. Pricing table as config (easy to update when prices change)
4. Cache token counts — don't re-count for retries of same prompt
5. Include cached/discounted token tracking (Anthropic prompt caching)
6. Daily rollup job for aggregated statsNo gallery images yet.