You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building an autonomous agent that makes multiple OpenAI calls per run. Sometimes a single run costs $0.50, sometimes it spirals to $15+ if the model gets stuck reasoning in circles.
OpenAI's dashboard spend limits are account-wide — they don't help when you need per-agent or per-run budgets. I wanted a way to say "this agent run gets $5 max, period."
Here's what I ended up with using AgentGuard:
fromagentguardimportTracer, BudgetGuard, patch_openai, JsonlFileSink# Set a $5 hard cap with a warning at 80%tracer=Tracer(
sink=JsonlFileSink("traces.jsonl"),
guards=[BudgetGuard(
max_cost_usd=5.00,
warn_at_pct=0.8,
on_warning=lambdamsg: print(f"WARNING: {msg}"),
)],
)
patch_openai(tracer) # auto-tracks cost per ChatCompletion call# Now use OpenAI normally — AgentGuard intercepts every callimportopenaiclient=openai.OpenAI()
foriinrange(100): # agent looptry:
resp=client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": f"Step {i}: analyze data"}],
)
exceptExceptionase:
if"BudgetExceeded"intype(e).__name__:
print(f"Agent stopped at budget limit: {e}")
breakraise
The key thing: patch_openai(tracer) monkey-patches the OpenAI client so every call gets cost-tracked automatically. When cumulative cost hits $4 (80%), the warning fires. At $5, BudgetExceeded is raised and the agent stops mid-run.
The cost estimates use published per-token pricing (GPT-4o, GPT-4, GPT-3.5, etc.), updated monthly.
Has anyone else solved per-agent spend limits differently? I'm curious about approaches that work with streaming responses or multi-agent systems where each agent needs its own budget.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm building an autonomous agent that makes multiple OpenAI calls per run. Sometimes a single run costs $0.50, sometimes it spirals to $15+ if the model gets stuck reasoning in circles.
OpenAI's dashboard spend limits are account-wide — they don't help when you need per-agent or per-run budgets. I wanted a way to say "this agent run gets $5 max, period."
Here's what I ended up with using AgentGuard:
The key thing:
patch_openai(tracer)monkey-patches the OpenAI client so every call gets cost-tracked automatically. When cumulative cost hits $4 (80%), the warning fires. At $5,BudgetExceededis raised and the agent stops mid-run.The cost estimates use published per-token pricing (GPT-4o, GPT-4, GPT-3.5, etc.), updated monthly.
Zero dependencies, MIT licensed: https://github.com/bmdhodl/agent47
Has anyone else solved per-agent spend limits differently? I'm curious about approaches that work with streaming responses or multi-agent systems where each agent needs its own budget.
Beta Was this translation helpful? Give feedback.
All reactions