Skip to main content

Cost Alerts

Set per-service daily spend budgets and receive Slack notifications when they are exceeded.

ɳSelf can alert you when AI plugin spending exceeds a daily budget. Alerts are checked hourly and delivered to Slack.

Configuration

Add to .env.prod:

COST_ALERT_ENABLED=true

# Per-service daily budgets (USD)
COST_BUDGET_AI_DAILY_USD=5.00
COST_BUDGET_MEDIA_DAILY_USD=2.00
COST_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-alerts

Then redeploy: nself build && nself deploy.

How it works

  1. The AI plugin records cost_usd in np_ai_usage for every API call.
  2. The cost alerter runs hourly via the cron plugin.
  3. It sums cost_usd for the last 24 hours, per service.
  4. When any budget is exceeded, a Slack message is sent.

Example alert

[nSelf Alert] Daily AI cost exceeded budget
Service:  ai plugin
Budget:   $5.00/day
Actual:   $7.23 (as of 14:00 UTC)
Action:   Check /ai/usage or reduce sampling rate

Checking costs manually

# See today's AI usage breakdown
nself 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

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

COST_ALERT_ENABLED=false

Setting this to false disables all cost checks silently. No Slack messages, no DB queries.