Autonomous agents can run up unexpected API bills. Budget Controller puts a hard ceiling on what they can spend.
The Problem
OWASP LLM10 — Unbounded Consumption — is a real risk in production agent deployments. An agent stuck in a loop, or handed an expensive task, can exhaust an API budget before anyone notices. There’s no standard lightweight solution for runtime cost enforcement.
The Solution
Agent Budget Controller intercepts agent calls and enforces configurable spending limits. Multiple limit types, 4 enforcement actions, human-in-the-loop approval for pause events, and accurate cost estimation for 10+ models.
Limit types:
- Per-call cost/tokens
- Per-task cost/tokens
- Hourly / daily / monthly totals
- Session-level caps
4 enforcement actions:
STOP— raiseBudgetExceededErrorPAUSE— callapproval_fn(human-in-the-loop)WARN— notify and continueNOTIFY— log only
2026 cost model (10+ models): Claude Opus ($15/$75), Sonnet ($3/$15), Haiku ($0.80/$4), GPT-4o ($2.50/$10), GPT-4o-mini ($0.15/$0.60), o3 ($10/$40), Gemini 2.0 Flash ($0.075/$0.30), and more.
Usage
from budget_controller import BudgetController, Budget
ctrl = BudgetController(
budget=Budget(
daily_cost_usd=5.0,
per_task_cost_usd=0.50,
action=LimitAction.PAUSE, # Ask human before proceeding
),
approval_fn=lambda ctx: input(f"Approve ${ctx.task_cost:.4f} spend? [y/n] ") == "y",
)
@ctrl.guard
def call_llm(model: str, prompt: str) -> str:
return api.complete(model=model, prompt=prompt)
# Or as a context manager
with ctrl.task(name="research task"):
result = call_llm("claude-sonnet-4-5", "Summarize this 10,000 page document...")
# Check remaining budget
remaining = ctrl.remaining()
print(f"Daily: ${remaining.daily_cost:.2f} left")
# Status report
status = ctrl.status()
print(f"Utilization: {status.daily_pct:.1f}% of daily budget")
Status
✅ Complete — 43/43 tests passing
| Feature | Status |
|---|---|
| Per-call / per-task / time-window limits | ✅ |
| STOP / PAUSE / WARN / NOTIFY actions | ✅ |
| Human-in-the-loop approval | ✅ |
| 10+ model cost estimation | ✅ |
| 4 presets (strict/standard/generous/free) | ✅ |
| Decorator + context manager API | ✅ |
Addresses OWASP LLM10: Unbounded Consumption. No external dependencies. Python 3.9+. MIT License.