Budget Enforcement Patterns for OpenAI API Calls #108
bmdhodl
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
One of the most common questions I see in agent repos: "How do I stop my agent from spending more than $X?"
OpenAI's API doesn't have a per-request budget cap. You can set spend limits at the account level, but that's a blunt instrument — it kills everything, not just the runaway agent. And by the time the limit kicks in, you've already spent the money.
Here are three patterns for enforcing budgets at the agent level.
Pattern 1: Hard dollar cap per run
Stop execution the moment estimated cost exceeds a threshold:
This is the simplest pattern. Set a dollar amount, consume after each call, catch the exception.
Pattern 2: Warning before the limit
Get a heads-up at 80% of the budget so you can gracefully wrap up:
The
on_warningcallback fires at 80%.BudgetExceededis the hard stop. This gives you a two-phase shutdown.Pattern 3: Auto-tracking with OpenAI patching
Skip manual
consume()calls — let AgentGuard estimate cost automatically:The cost estimates use published token pricing. You can override prices for custom or fine-tuned models:
Combining with loop detection
Budget overruns and loops often go together. An agent stuck in a loop burns through your budget fast. Layer both guards:
Whichever guard triggers first stops the agent. The loop guard catches pathological repeats; the budget guard catches legitimate-but-expensive runs.
Install
Zero dependencies, MIT licensed, Python 3.9+.
Repo: https://github.com/bmdhodl/agent47
What's your current approach to budget enforcement? Curious if anyone's built custom solutions or run into edge cases.
Beta Was this translation helpful? Give feedback.
All reactions