cd /news/ai-tools/a-budget-guard-for-llm-calls-cap-dai… · home › topics › ai-tools › article
[ARTICLE · art-169033] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=↑ positive

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.

by read1 min views3 publishedNov 13, 2026

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 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.

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.

── more in #ai-tools 4 stories · sorted by recency
── more on @nxflowai.com 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/a-budget-guard-for-l…] indexed:0 read:1min 2026-11-13 · —