Back to Documentation
Cost Management
Track, optimize, and control your AI spending. Budget management, cost estimation, analytics, and automated optimization.
npm install @rana/core
70%
Avg. Savings
9+
Providers
Yes
Real-time
Built-in
Alerts
Cost Tracking
Real-time tracking of all AI API costs
import { CostTracker } from '@rana/core';
const tracker = new CostTracker({
storage: 'postgresql',
currency: 'USD'
});
// Automatic tracking on all requests
const result = await tracker.wrap(
() => chat('Hello, world!'),
{ userId: 'user-123', feature: 'chat' }
);
console.log(result.cost); // $0.0023
console.log(result.inputTokens); // 15
console.log(result.outputTokens); // 42
// Get cost summary
const daily = await tracker.getDailyCosts();
const byUser = await tracker.getCostsByUser('user-123');
const byFeature = await tracker.getCostsByMetadata('feature');
// Export for billing
const invoice = await tracker.generateInvoice({
userId: 'user-123',
period: 'last-month'
});Budget Management
Set and enforce spending limits
import { BudgetManager } from '@rana/core';
const budget = new BudgetManager({
storage: 'redis',
alertThresholds: [0.5, 0.8, 0.9, 1.0]
});
// Set global budget
await budget.setGlobalBudget({
daily: 100, // $100/day
monthly: 2000 // $2000/month
});
// Set per-user budgets
await budget.setUserBudget('user-123', {
daily: 5,
monthly: 50
});
// Check budget before request
const allowed = await budget.checkBudget('user-123', estimatedCost);
if (!allowed.success) {
console.log(`Budget exceeded: ${allowed.reason}`);
console.log(`Resets at: ${allowed.resetsAt}`);
throw new BudgetExceededError(allowed);
}
// Listen for alerts
budget.on('threshold', (userId, percentage, budget) => {
if (percentage >= 0.8) {
notifyUser(userId, `You've used ${percentage * 100}% of your budget`);
}
});Cost Optimization
Automatic cost reduction strategies
import { CostOptimizer } from '@rana/core';
const optimizer = new CostOptimizer({
strategies: ['caching', 'model-routing', 'prompt-compression'],
targetSavings: 0.5 // Target 50% cost reduction
});
// Automatic optimization
const result = await optimizer.optimize(
() => chat(longPrompt),
{ quality: 'balanced' } // 'fast', 'balanced', 'quality'
);
console.log(result.originalCost); // $0.05
console.log(result.optimizedCost); // $0.02
console.log(result.savings); // 60%
console.log(result.strategies); // ['cached', 'compressed']
// Get optimization recommendations
const recommendations = await optimizer.analyze();
// [
// { type: 'caching', potentialSavings: '$500/mo', effort: 'low' },
// { type: 'model-routing', potentialSavings: '$300/mo', effort: 'medium' },
// ]Cost Estimation
Predict costs before making requests
import { CostEstimator } from '@rana/core';
const estimator = new CostEstimator();
// Estimate single request
const estimate = estimator.estimate({
model: 'claude-sonnet-4-20250514',
inputText: userPrompt,
expectedOutputTokens: 500
});
console.log(estimate.inputCost); // $0.003
console.log(estimate.outputCost); // $0.015
console.log(estimate.totalCost); // $0.018
console.log(estimate.confidence); // 0.95
// Estimate batch processing
const batchEstimate = estimator.estimateBatch({
model: 'claude-sonnet-4-20250514',
documents: documents,
operation: 'summarize'
});
console.log(batchEstimate.totalCost); // $45.00
console.log(batchEstimate.timeEstimate); // '2 hours'
// Compare model costs
const comparison = estimator.compareModels(userPrompt, [
'claude-sonnet-4-20250514',
'gpt-4o',
'gemini-pro'
]);Usage Analytics
Detailed analytics and reporting
import { UsageAnalytics } from '@rana/core';
const analytics = new UsageAnalytics({
storage: 'postgresql',
retention: '1 year'
});
// Get usage breakdown
const usage = await analytics.getUsage({
period: 'last-30-days',
groupBy: ['model', 'feature', 'user']
});
// Cost trends
const trends = await analytics.getTrends({
metric: 'cost',
period: 'last-6-months',
resolution: 'weekly'
});
// Top consumers
const topUsers = await analytics.getTopConsumers({
metric: 'cost',
limit: 10
});
// Export reports
const report = await analytics.exportReport({
format: 'pdf',
period: 'last-quarter',
sections: ['summary', 'breakdown', 'trends', 'recommendations']
});
// Dashboard data
const dashboard = await analytics.getDashboard();
console.log(dashboard.totalCost); // $1,234.56
console.log(dashboard.costChange); // -15% (vs last period)
console.log(dashboard.topModels); // [{ model: 'gpt-4', cost: $800 }]Cost Alerts
Proactive alerting for cost anomalies
import { CostAlerts } from '@rana/core';
const alerts = new CostAlerts({
channels: [
{ type: 'email', recipients: ['admin@company.com'] },
{ type: 'slack', webhook: process.env.SLACK_WEBHOOK },
{ type: 'pagerduty', routingKey: process.env.PD_KEY }
]
});
// Budget alerts
alerts.addRule({
name: 'daily-budget-warning',
condition: 'daily_cost > daily_budget * 0.8',
severity: 'warning',
channels: ['email', 'slack']
});
// Anomaly detection
alerts.addRule({
name: 'cost-spike',
condition: 'hourly_cost > avg_hourly_cost * 3',
severity: 'critical',
channels: ['slack', 'pagerduty']
});
// Per-user alerts
alerts.addRule({
name: 'user-overspend',
condition: 'user_daily_cost > user_daily_limit',
severity: 'high',
action: async (context) => {
await disableUser(context.userId);
await notifyAdmin(context);
}
});
// Start monitoring
alerts.start();Model Pricing Reference
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| Claude 3.5 Sonnet | $3.00 | $15.00 |
| Claude 3 Opus | $15.00 | $75.00 |
| GPT-4o | $5.00 | $15.00 |
| GPT-4 Turbo | $10.00 | $30.00 |
| Gemini Pro | $0.50 | $1.50 |
| Llama 3 70B | $0.70 | $0.90 |
* Prices are approximate and may vary. RANA automatically tracks actual costs from each provider.