// Real-time cost tracking system
export class AICostTracker {
private readonly pricing = {
'claude-opus-4-7' : { input: 0.015 , output: 0.075 },
'claude-sonnet-4-6' : { input: 0.003 , output: 0.015 },
'gpt-4' : { input: 0.03 , output: 0.06 },
'cursor-pro' : { monthly: 20 , included_requests: 500 }
async trackUsage ( event : AIUsageEvent ) : Promise < CostReport > {
const modelPricing = this .pricing[event.model];
const cost = this . calculateCost (event, modelPricing);
departmentId: event.departmentId,
tokensIn: event.tokensIn,
tokensOut: event.tokensOut,
timestamp: event.timestamp,
await this . checkBudgetAlerts (event.teamId, cost);
dailyTotal: await this . getDailyCost (event.userId),
monthlyTotal: await this . getMonthlyCost (event.teamId),
budgetRemaining: await this . getBudgetRemaining (event.teamId)
async generateCostReport ( period : Period ) : Promise < DetailedCostReport > {
const usage = await this .db. query ({ period });
totalCost: usage. reduce (( sum , u ) => sum + u.cost, 0 ),
byTeam: this . aggregateByTeam (usage),
byModel: this . aggregateByModel (usage),
byPurpose: this . aggregateByPurpose (usage),
topUsers: this . getTopUsers (usage),
trends: this . analyzeTrends (usage),
recommendations: this . generateRecommendations (usage)