Repository navigation
A hard dollar limit per background job with RubyLLM (openai_api_base + with_headers) #1034
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 Rails job that summarizes a ticket, drafts a reply and runs a tool or two can make several model calls, and a retry or a long thread can quietly multiply that. Token counts get recorded afterwards, but nothing stops one job from overspending. Here's one way to give each job a hard dollar budget using two things RubyLLM already has:
openai_api_baseandwith_headers.Run a small OpenAI-compatible gateway next to the app (it uses your provider key):
Every call in the job sends the same id, so they share one budget. Each call reserves its worst-case cost before it's sent, and a call that doesn't fit is refused with HTTP 402 before it reaches OpenAI, which surfaces as an error in the job.
inferrail work draft-reply-<job id>shows what the job cost. Usingjob_idmeans an ActiveJob retry of the same job draws from the same budget instead of getting a fresh one.Caveats: chat completions only (embeddings should stay on their normal endpoint), the budget store is a SQLite file on the gateway host, so run one gateway for all workers, and the model needs a price in Inferrail (
gpt-4o-mini,gpt-4.1-mini,gpt-4.1are built in). I've tested the gateway with the Python and TypeScript OpenAI clients, not RubyLLM end to end, 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