# A Budget Guard for LLM Calls: Cap Daily Spend in a Small-Business Automation (Python)

> Source: <https://dev.to/ujjwal_dubey_9/a-budget-guard-for-llm-calls-cap-daily-spend-in-a-small-business-automation-python-3m8g>
> Published: 2026-11-13 14:36:19+00:00

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.
