How to Implement AI Cost Alerts and Budgets
Set up automatic alerts when AI spending approaches budget limits.
Jay Banlasan
The AI Systems Guy
Setting up ai api cost alerts with budget limits is the difference between a controlled system and a surprise invoice. I learned this the hard way when a batch job ran unchecked overnight and burned through $200 in tokens.
Every production AI system needs a spending cap. Here is how to build one that actually works.
What You Need Before Starting
- Python 3.8+ with
requestsinstalled - A SQLite database for tracking spend
- Slack or email for alert delivery
- Your provider pricing per 1K tokens
Step 1: Build the Usage Tracker
Every API call gets logged with its token count and calculated cost:
import sqlite3
from datetime import datetime
PRICING = {
"claude-sonnet-4-20250514": {"input": 0.003, "output": 0.015},
"claude-3-5-haiku-20241022": {"input": 0.001, "output": 0.005},
"gpt-4o": {"input": 0.005, "output": 0.015},
"gpt-4o-mini": {"input": 0.00015, "output": 0.0006},
}
def log_usage(db_path, model, input_tokens, output_tokens, project="default"):
pricing = PRICING.get(model, {"input": 0.01, "output": 0.03})
cost = (input_tokens / 1000 * pricing["input"]) + (output_tokens / 1000 * pricing["output"])
conn = sqlite3.connect(db_path)
conn.execute("""CREATE TABLE IF NOT EXISTS usage_log (
id INTEGER PRIMARY KEY, model TEXT, input_tokens INTEGER, output_tokens INTEGER,
cost REAL, project TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)""")
conn.execute("INSERT INTO usage_log (model, input_tokens, output_tokens, cost, project) VALUES (?,?,?,?,?)",
(model, input_tokens, output_tokens, cost, project))
conn.commit()
conn.close()
return cost
Step 2: Set Budget Thresholds
BUDGETS = {
"daily": 25.00,
"weekly": 100.00,
"monthly": 350.00,
"per_project": {
"client_reports": 50.00,
"ad_copy": 30.00,
"default": 100.00,
}
}
def get_spend(db_path, period="daily", project=None):
conn = sqlite3.connect(db_path)
if period == "daily":
where = "DATE(timestamp) = DATE('now')"
elif period == "weekly":
where = "timestamp >= DATE('now', '-7 days')"
elif period == "monthly":
where = "timestamp >= DATE('now', 'start of month')"
query = f"SELECT COALESCE(SUM(cost), 0) FROM usage_log WHERE {where}"
if project:
query += f" AND project = '{project}'"
spend = conn.execute(query).fetchone()[0]
conn.close()
return spend
Step 3: Build the Alert System
import requests
import os
def send_alert(message, level="warning"):
slack_token = os.getenv("SLACK_BOT_TOKEN")
channel = os.getenv("SLACK_ALERTS_CHANNEL", "C0AL72BP4RW")
emoji = "🔴" if level == "critical" else "🟡"
requests.post("https://slack.api/api/chat.postMessage",
headers={"Authorization": f"Bearer {slack_token}"},
json={"channel": channel, "text": f"{emoji} AI Cost Alert: {message}"}
)
def check_budgets(db_path):
alerts = []
for period in ["daily", "weekly", "monthly"]:
spend = get_spend(db_path, period)
budget = BUDGETS[period]
pct = (spend / budget) * 100
if pct >= 100:
alerts.append({"level": "critical", "msg": f"{period.upper()} budget EXCEEDED: ${spend:.2f} / ${budget:.2f}"})
elif pct >= 80:
alerts.append({"level": "warning", "msg": f"{period.upper()} budget at {pct:.0f}%: ${spend:.2f} / ${budget:.2f}"})
for alert in alerts:
send_alert(alert["msg"], alert["level"])
return alerts
Step 4: Add a Circuit Breaker
When budget is blown, stop making calls:
def can_spend(db_path, estimated_cost, project="default"):
daily_spend = get_spend(db_path, "daily")
if daily_spend + estimated_cost > BUDGETS["daily"]:
send_alert(f"BLOCKED: Request would exceed daily budget. Current: ${daily_spend:.2f}", "critical")
return False
project_budget = BUDGETS["per_project"].get(project, BUDGETS["per_project"]["default"])
project_spend = get_spend(db_path, "monthly", project)
if project_spend + estimated_cost > project_budget:
send_alert(f"BLOCKED: Project '{project}' at monthly limit. Current: ${project_spend:.2f}", "critical")
return False
return True
Step 5: Schedule the Budget Check
Run this as a cron job every hour:
crontab -e
# Add:
0 * * * * python3 /path/to/check_budgets.py
The script calls check_budgets() and exits. If anything is over 80%, you get a Slack ping before it becomes a problem.
What to Build Next
Connect the circuit breaker directly to your API wrapper so every call goes through can_spend() before hitting the provider. Add a weekly summary email that shows spend by project and model.
Related Reading
- Why Monitoring Is Not Optional - the case for monitoring every AI system you run
- The Real Cost of Manual Operations - understanding where money actually goes
- The Priority Matrix for AI Projects - how to prioritize which systems to build first
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment