How to Limit OpenAI API Spend Per Agent Run #200
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.
-
If you're running AI agents in production, you've probably wondered: how do I stop a single agent run from spending more than $X?
OpenAI's dashboard has organization-level spend limits, but those are blunt — they shut down everything, not just the one runaway agent. And they're reactive: by the time the limit kicks in, the money is gone.
Here's how to enforce a hard dollar cap per agent run using AgentGuard's
BudgetGuard.Quick setup (4 lines)
That's it.
patch_openaiwraps the OpenAI client so everychat.completions.create()call automatically:BudgetExceededif the limit is hitWarning before the hard stop
You probably want a chance to wrap up gracefully before the kill:
Checking spend mid-run
Also works with Anthropic
Install
Zero dependencies, MIT licensed, Python 3.9+.
GitHub: https://github.com/bmdhodl/agent47
What's your current approach to capping per-agent spend? Have you hit any edge cases with token estimation?
Beta Was this translation helpful? Give feedback.
All reactions