How to Limit OpenAI API Spend Per Agent Run #203
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.
-
How to Limit OpenAI API Spend Per Agent Run
Category: Show and tell
Labels: openai, budget, cost-control
If you're running autonomous agents with OpenAI, you've probably wondered: how do I set a hard dollar limit per agent run?
OpenAI has account-level spend limits, but those are a kill switch for your entire organization. They don't help when one agent run out of 50 goes haywire.
Here's how to set a per-run budget that kills the agent the moment it exceeds your limit.
Setup
patch_openaiintercepts everyChatCompletionresponse, extracts token counts, estimates cost using published pricing, and feeds it into BudgetGuard. When the cumulative cost exceeds your limit,BudgetExceededis raised.Warning before the hard stop
The
warn_at_pct=0.8parameter fires a callback at 80% of the budget. You can use this for graceful shutdown:Zero dependencies
AgentGuard is pure Python stdlib. No extra packages to audit.
Repo: github.com/bmdhodl/agent47
What's your current approach to limiting per-agent spend? Curious if anyone has built custom solutions.
Beta Was this translation helpful? Give feedback.
All reactions