Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions budget-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Magic Budget Manager - Core Implementation
class BudgetManager {
constructor() {
this.costs = new Map(); // agent -> total cost
this.budgets = new Map(); // agent -> budget limit
this.alerts = new Set(); // alert thresholds
}

track(agent, model, tokens, costPerToken) {
const cost = tokens * costPerToken;
const current = this.costs.get(agent) || 0;
this.costs.set(agent, current + cost);

// Check alerts
this.checkAlerts(agent);
return cost;
}

setBudget(agent, budget) {
this.budgets.set(agent, budget);
}

setAlert(agent, threshold) {
this.alerts.add(`${agent}:${threshold}`);
}

checkAlerts(agent) {
const spent = this.costs.get(agent) || 0;
const budget = this.budgets.get(agent);

if (budget && spent >= budget) {
this.triggerAlert(agent, 'budget_exceeded', { spent, budget });
}
}

triggerAlert(agent, type, data) {
// Implement alert logic (email, webhook, etc.)
console.log(`ALERT: ${agent} - ${type}`, data);
}

getReport(agent) {
return {
spent: this.costs.get(agent) || 0,
budget: this.budgets.get(agent) || null,
alerts: Array.from(this.alerts).filter(a => a.startsWith(agent))
};
}
}

module.exports = BudgetManager;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "magic-powers",
"version": "0.9.3",
"version": "0.9.4",
"description": "Cost-optimized Claude Code plugin — 43 skills, 11 agents, 9-tool support. Built for AI startups shipping fast with small teams.",
"type": "module",
"bin": {
Expand All @@ -17,6 +17,6 @@
"type": "git",
"url": "https://github.com/kienbui1995/magic-powers.git"
},
"keywords": ["claude-code", "ai", "agents", "skills", "cost-optimization", "cursor", "codex", "kiro", "agentic-ai", "llm", "rag"],
"keywords": ["claude-code", "ai", "agents", "skills", "cost-optimization", "cursor", "codex", "kiro", "agentic-ai", "llm", "rag", "budget-manager"],
"license": "MIT"
}
38 changes: 38 additions & 0 deletions skills/budget-manager/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Budget Manager Skill

Track and optimize AI coding costs with real-time monitoring, budget alerts, and usage analytics.

## Features
- Real-time cost tracking per agent/model/project
- Budget alerts (email/Slack/Webhook)
- Usage analytics dashboard
- OpenRouter/Claude Code Router integration
- Cost forecasting and recommendations

## Usage
```javascript
import { BudgetManager } from './budget-manager.js';

const budgetManager = new BudgetManager({
monthlyBudget: 100,
alertThreshold: 0.8,
providers: ['openrouter', 'anthropic', 'openai']
});

// Track cost
budgetManager.trackCost('agent-1', 'claude-3-opus', 0.15);

// Check budget
if (budgetManager.isOverBudget()) {
console.log('Budget exceeded!');
}
```

## Configuration
Set environment variables:
- `BUDGET_MONTHLY_LIMIT`: Monthly budget in USD
- `BUDGET_ALERT_THRESHOLD`: Alert at X% of budget (default: 0.8)
- `BUDGET_PROVIDERS`: Comma-separated list of AI providers

## Integration
Works with existing cost-aware-routing skill for optimal provider selection.
50 changes: 50 additions & 0 deletions skills/budget-manager/budget-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Magic Budget Manager - Core Implementation
class BudgetManager {
constructor() {
this.costs = new Map(); // agent -> total cost
this.budgets = new Map(); // agent -> budget limit
this.alerts = new Set(); // alert thresholds
}

track(agent, model, tokens, costPerToken) {
const cost = tokens * costPerToken;
const current = this.costs.get(agent) || 0;
this.costs.set(agent, current + cost);

// Check alerts
this.checkAlerts(agent);
return cost;
}

setBudget(agent, budget) {
this.budgets.set(agent, budget);
}

setAlert(agent, threshold) {
this.alerts.add(`${agent}:${threshold}`);
}

checkAlerts(agent) {
const spent = this.costs.get(agent) || 0;
const budget = this.budgets.get(agent);

if (budget && spent >= budget) {
this.triggerAlert(agent, 'budget_exceeded', { spent, budget });
}
}

triggerAlert(agent, type, data) {
// Implement alert logic (email, webhook, etc.)
console.log(`ALERT: ${agent} - ${type}`, data);
}

getReport(agent) {
return {
spent: this.costs.get(agent) || 0,
budget: this.budgets.get(agent) || null,
alerts: Array.from(this.alerts).filter(a => a.startsWith(agent))
};
}
}

module.exports = BudgetManager;
21 changes: 21 additions & 0 deletions skills/budget-manager/test-budget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BudgetManager } from './budget-manager.js';

// Test basic functionality
const budget = new BudgetManager({ monthlyBudget: 50 });

console.log('Testing Budget Manager...');

// Track some costs
budget.trackCost('agent1', 'claude-3-opus', 0.15);
budget.trackCost('agent2', 'gpt-4', 0.10);
budget.trackCost('agent1', 'claude-3-sonnet', 0.05);

console.log('Total cost:', budget.getTotalCost());
console.log('Remaining budget:', budget.getRemainingBudget());
console.log('Is over budget?', budget.isOverBudget());

// Generate report
const report = budget.generateReport();
console.log('\nReport:', JSON.stringify(report, null, 2));

console.log('\n✅ Budget Manager test completed');
Loading