Repository navigation
Putting a dollar cap on one Strands agent run #4842
domondi1
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.
A Strands agent decides for itself how many model calls a task takes, so a tool that keeps failing can turn one request into dozens of calls. Here's one way to put a hard dollar ceiling on a single run without a plugin or a change to the agent.
OpenAIModelpassesclient_argsto the OpenAI client, so the run's id and budget go indefault_headers, pointed at a small gateway running in the same process:Each call reserves its worst-case cost before it goes out, so it's checked before the money is spent rather than after. In a test with a tool that always answered "unavailable, try again", a $0.002 budget stopped the loop after 17 calls: the next call got a 402 before the provider and the event loop ended with
EventLoopException.inferrail work <run id>shows what the run cost.Caveats: Chat Completions only, set
max_tokens, and only calls through that model count. Tested withstrands-agents1.57.1 and 1.57.2.Full setup: guide. I maintain Inferrail (open source, Apache-2.0), so take the recommendation with that in mind. Curious how others bound the cost of a single run here.
All reactions