Cost Alerts
Cost Alerts
Section titled “Cost Alerts”ɳSelf can alert you when AI plugin spending exceeds a daily budget. Alerts are checked hourly and delivered to Slack.
Configuration
Section titled “Configuration”Add to .env.prod:
COST_ALERT_ENABLED=true
# Per-service daily budgets (USD)COST_BUDGET_AI_DAILY_USD=5.00COST_BUDGET_MEDIA_DAILY_USD=2.00COST_BUDGET_TOTAL_DAILY_USD=20.00
# Slack webhook (create at api.slack.com/apps)SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...SLACK_ALERT_CHANNEL=#nself-alertsThen redeploy: nself build && nself deploy.
How it works
Section titled “How it works”- The AI plugin records
cost_usdinnp_ai_usagefor every API call. - The cost alerter runs hourly via the cron plugin.
- It sums
cost_usdfor the last 24 hours, per service. - When any budget is exceeded, a Slack message is sent.
Example alert
Section titled “Example alert”[nSelf Alert] Daily AI cost exceeded budgetService: ai pluginBudget: $5.00/dayActual: $7.23 (as of 14:00 UTC)Action: Check /ai/usage or reduce sampling rateChecking costs manually
Section titled “Checking costs manually”# See today's AI usage breakdownnself db query --sql "SELECT source_account_id, SUM(cost_usd), COUNT(*) FROM np_ai_usage WHERE created_at >= now() - interval '24h' GROUP BY 1 ORDER BY 2 DESC"Grafana dashboard
Section titled “Grafana dashboard”Cost metrics are exposed at :8100/metrics as:
ai_cost_usd_total{model="gpt-4o"}ai_cost_usd_total{model="claude-3-5-sonnet"}Import the AI Cost dashboard (included in the ai plugin dashboards directory) for a visual breakdown.
Disabling cost checks
Section titled “Disabling cost checks”COST_ALERT_ENABLED=falseSetting this to false disables all cost checks silently. No Slack messages,
no DB queries.