I run a swarm of AI agents that call LLM APIs all day. One night a retry loop went haywire and burned through 3 days of budget in two hours. I didn't find out until the invoice email the next morning.
That's the problem with API spend: there's no dashboard that pings you the moment something goes wrong. You have to go look for it.
So I wrote a small stdlib-only Python script that watches a CSV of your daily spend and fires a Slack or Discord webhook the moment you cross a threshold. No dependencies, no cloud service, no API keys beyond the webhook URL.
The core idea
Most cost trackers already dump a CSV with a cost_usd column (OpenAI, Anthropic, your own logging — doesn't matter which). You just need something that reads today's total and screams if it's too high.
import csv
import json
import sys
import urllib.request
from datetime import date
def today_spend(csv_path, date_col="date", cost_col="cost_usd"):
total = 0.0
today = date.today().isoformat()
with open(csv_path, newline="") as f:
for row in csv.DictReader(f):
if row[date_col] == today:
total += float(row[cost_col])
return total
def send_webhook(url, message):
payload = json.dumps({"content": message}).encode()
req = urllib.request.Request(url, data=payload, headers={"Content-Type": "application/json"})
urllib.request.urlopen(req, timeout=10)
def check(csv_path, threshold, webhook_url, dry_run=False):
spend = today_spend(csv_path)
if spend >= threshold:
msg = f"⚠️ Spend alert: ${spend:.2f} today (threshold ${threshold:.2f})"
if dry_run:
print("[DRY RUN]", msg)
else:
send_webhook(webhook_url, msg)
return 2 # exit code your cron/scheduler can check for
return 0
if __name__ == "__main__":
code = check(sys.argv[1], float(sys.argv[2]), sys.argv[3], dry_run="--dry-run" in sys.argv)
sys.exit(code)
Run it from cron (or your agent scheduler) every 30 minutes:
python spend_alert.py usage.csv 10.00 https://discord.com/api/webhooks/XXXX --dry-run
Drop --dry-run once you trust it. The nonzero exit code is the useful part — you can chain it into a kill-switch that pauses your agents the second spend crosses the line, instead of finding out from a billing email.
That's it. No dashboard login, no third-party SaaS reading your API keys, just a text file and a webhook.
I packaged the full version (plus a sample CSV and README) as a free/pay-what-you-want download here: https://renevibe76.gumroad.com/l/djlwjm
How do you currently find out your API spend jumped — do you check manually, or has something ever caught you by surprise before the invoice did?
Top comments (0)