Repository navigation
Putting a dollar cap on one Stagehand session #3097
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.
Uh oh!
There was an error while loading. Please reload this page.
A Stagehand script can make a lot of model calls per session (every
act,extractandobserve, plus retries when a page doesn't behave), and nothing bounds what one session spends. Here's one way to give each session a hard dollar budget without changing your script, using thellmClientoption Stagehand already has.Point an AI SDK OpenAI-compatible provider at a small gateway running next to your app, and send the session's id and budget as headers:
Each call reserves its worst-case cost before it's sent, and a call that doesn't fit is refused with a 402 before it reaches OpenAI (the AI SDK throws an
APICallErrorwithstatusCode === 402, so theactorextractthat hit the limit fails). Afterwardsinferrail work session-001shows what the session cost.Caveats: Chat Completions only, so this covers
act/extract/observewith a chat model, not computer-use agent models that need OpenAI's Responses API; screenshots are fine (they're counted with a fixed per-image estimate, inferrail 0.4.12 or later). The model needs a price in Inferrail (inferrail modelslists them;gpt-4.1-mini,gpt-4.1,gpt-4o-miniare built in). I haven't run a long Browserbase session through it, so reports are welcome.Setup details: guide. I maintain Inferrail (open source, Apache-2.0), so take the suggestion with that in mind.
All reactions