A Budget Guard for LLM Calls: Cap Daily Spend in a Small-Business Automation (Python) A developer published a Python budget guard that wraps every LLM call, tracks a running daily spend in a JSON state file, and raises a BudgetExceeded exception once a configurable cap is reached, routing the item to a human queue instead. The guard estimates input tokens from prompt length and projects cost using per-1K input and output rates pulled from environment variables, so caps and pricing are never hard-coded. The approach is framed as a way for small businesses to degrade to manual processing rather than drop enquiries when a runaway loop would otherwise burn through spend overnight. Estimating model spend before launch is useful. Stopping a runaway loop at 2 a.m. is more useful. For small businesses, keeping small-business automation spend predictable https://www.nxflowai.com/blog/how-to-audit-business-workflow-before-ai matters more than squeezing the last fraction out of each call. This guard wraps every model call, keeps a running daily total and refuses calls once a cap is reached, routing the item to a person instead. python import json, os, time, datetime STATE = "budget state.json" DAILY CAP = float os.environ.get "DAILY CAP", "0" set in your own currency units RATE IN = float os.environ.get "RATE PER 1K IN", "0" from your provider's pricing page RATE OUT = float os.environ.get "RATE PER 1K OUT", "0" class BudgetExceeded Exception : pass def load : today = datetime.date.today .isoformat if os.path.exists STATE : s = json.load open STATE if s.get "day" == today: return s return {"day": today, "spent": 0.0, "calls": 0} def save s : json.dump s, open STATE, "w" def guarded call call model, prompt, est out tokens=300 : s = load est in = len prompt / 4 rough token estimate projected = s "spent" + est in / 1000 RATE IN + est out tokens / 1000 RATE OUT if DAILY CAP and projected DAILY CAP: raise BudgetExceeded f"cap reached: {s 'spent' :.2f} spent today" result, used in, used out = call model prompt s "spent" += used in / 1000 RATE IN + used out / 1000 RATE OUT s "calls" += 1 save s return result Use it like this: try: reply = guarded call my model fn, prompt except BudgetExceeded: send to human queue item, reason="daily model budget reached" The business still needs the work done. A capped day should degrade to the manual process, not drop enquiries. The rates and the cap are yours to set from your provider's pricing page; never hard-code them. More build notes are on our engineering blog index.