Replies: 1 comment
-
|
Nice approach with the monkey-patching pattern. We took a similar route on the Node.js side with burn0 -- it patches globalThis.fetch and node:http so every outbound API call gets cost-tracked automatically. No SDK-specific patching needed, which means it works across OpenAI, Anthropic, Gemini, and ~50 other services out of the box. For the multi-agent question you raised -- burn0 has a track() wrapper that lets you attribute costs to specific features or agent runs. Different ecosystem (JS vs Python) but same philosophy -- cost visibility shouldn't require a platform. Would be cool to compare notes on how you handle pricing updates for new models. |
Beta Was this translation helpful? Give feedback.
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