Repository navigation
Recipe: a hard dollar limit per flow run for LLM tasks (flow_run.id + retry_condition_fn) #23301
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 flow that maps an LLM task over a batch (summarize 500 documents, enrich 2,000 rows, grade an eval set) can make a lot of paid calls, and task retries multiply them. Prefect bounds time and concurrency well, but there's no dollar ceiling for one flow run. This recipe adds one with an OpenAI client and
prefect.runtime.flow_run.id: every task in the run sends the run id and a budget to a local gateway, which refuses a call with HTTP 402 before it reaches OpenAI once the run's budget is used.Disclosure: I maintain the gateway (Inferrail, open source).
After a run,
inferrail work prefect-<flow run id>prints calls, tokens and dollars for it, andinferrail report --by work_idlists every run.What I checked (prefect 3.8.7, openai 3.24.0, inferrail 0.4.15, local ephemeral Prefect, 8 mapped tasks against a stand-in OpenAI endpoint with fixed token counts):
prefect-<flow run id>.@task(retries=2), each refused task was retried twice, so 12 refused requests for 4 tasks. Refusals cost nothing, but it's wasted work. Withretry_condition_fnas above: exactly 8 requests, and real transient errors still retry.Caveats: a dollar budget needs a price for the model (most OpenAI and Anthropic models are built in,
inferrail models; an unpriced one is refused, not guessed). The gateway stores usage and cost per call, never prompts or outputs. With workers on several machines, run one shared gateway and pointbase_urlat it so the run's budget is shared across them.If this is useful as a community recipe, I'm happy to host it as a standalone example.
All reactions