Soft alerts are comforting.
They are also how you wake up to a surprise OpenAI bill from a retry loop at 3am.
If you run multi-step agents (tools + LLM calls), you need two systems:
- Observability — what happened (traces, tokens, errors)
- Enforcement — what is allowed to happen next (budget, tools, memory scope)
Most stacks are strong at (1) and weak at (2).
Soft alerts vs hard budgets
| Approach | What it does | Failure mode |
|---|---|---|
| Alert at 80% of monthly spend | Email / Slack | Agent keeps running |
| Dashboard “cost this week” | Human looks later | Overnight burn |
| Hard budget on the run | Reject next spend when ceiling hits | Run stops; you debug |
Hard budgets only work if LLM/tool cost is recorded on the hot path (or immediately after each call). A run with only “start/finish” spans cannot enforce real dollars.
Minimal mental model
Agent run starts with budget_usd = 0.50
→ tool call
→ LLM call (+ tokens → $)
→ if spent >= budget → fail closed (raise / stop)
→ else continue
Enforcement should not be “we'll audit later.”
If the gate is only after the wire transfer (or the refund email), it's already too late.
Python sketch (pattern)
You can implement this in app code. The important parts:
- Attach a budget to the run (not only org monthly total)
- Record every LLM call’s estimated cost
- On exceed: raise (or return a controlled error) — don’t log-and-continue
python
class BudgetExceeded(Exception):
pass
class RunBudget:
def __init__(self, limit_usd: float):
self.limit = limit_usd
self.spent = 0.0
def charge(self, usd: float) -> None:
if self.spent + usd > self.limit:
raise BudgetExceeded(
f"spent={self.spent:.4f} + {usd:.4f} > limit={self.limit}"
)
self.spent += usd
Wire that into your OpenAI wrapper after each completion (use usage tokens × your price table).
Top comments (0)