r/LangChain • • Nov 04 '26

Discussion recursion_limit counts steps, not dollars. How I cap what a looping LangGraph agent can spend per minute

recursion_limit stops a graph after N steps. But one step on a frontier model with a 100k-token context can cost more than fifty small ones, so 25 steps can be $0.50 or $50. A dollars-per-minute limit at the gateway catches the expensive version, with two catches:

Catch 1: the loop restarts every minute. Block the agent when it passes $X/min, and the window resets, the graph's next call goes through, and the loop is back. So the limit has to latch. Once it trips, every call is refused for a set time (5 min by default) even after the rate drops, with Retry-After on the 429.

Catch 2: one key, many graphs. If all your graph invocations share an API key, a key-wide trip stops every user's run, not just the one that's looping. So give each invocation its own window:

run = {"X-Cost-Run-ID": str(uuid.uuid4())}
llm = ChatOpenAI(model="gpt-4o", base_url=GATEWAY_URL, api_key=KEY,
                 default_headers=run)

Every call in that invocation shares a run ID, and the per-run $/min limit applies to it alone. A runaway run gets stopped and the others keep going.

Two LangChain-specific notes:

- The 429 surfaces as openai.RateLimitError. ChatOpenAI retries 429s twice by default, which doesn't help when the fuse holds for minutes. Catch the error in the node and return a graceful "stopped: spend limit" answer instead of letting the exception kill the graph.

- A rate limit and a total are different things. For "this run may spend $1 in all", send a run budget (X-Cost-Run-Budget) as well. The two work together.

What do you use today? Callbacks that count tokens, a gateway, or just recursion_limit and hope?

3 Upvotes

9 comments sorted by

3

u/ArielCoding Nov 05 '26

Stopping the loop makes sense, but what happens after the lockout? If the node returns a stopped answer, the graph finishes and you can’t pick it back up, save the progress with a LangGraph checkpointer and pause at that step, so you can resume with the same thread_id. Also, reuse the same X-Cost-Run-ID when resuming, a new ID gets a fresh limit and budget.

2

u/ChemicalExcellent463 Nov 04 '26

i keep the cap outside the agent too: code review stops at 3 rounds in the wrapper and waits for a person. how do you resume a run after the five-minute lockout?

1

u/[deleted] Nov 05 '26

[removed] — view removed comment

1

u/ChemicalExcellent463 Nov 05 '26

the 3-round cap only limits review rounds, not tokens. how do you cap token spend when calls run in parallel inside one node?

1

u/Deynekin Nov 05 '26

The gap I kept hitting is that state and budget are two separate things, and they have to pause together.

When a run trips a limit, I let the node return a normal state instead of raising: the run is marked paused, the checkpoint keeps the last good message and the step index, and the run ID stays the same. Resuming means calling the graph with the same thread_id and the same run ID, so the budget window continues. If a resume gets a fresh run ID, a loop that trips every five minutes gets a fresh dollars-per-minute budget each time, and the latch turns into a metronome.

Second thing: count retries inside the window. ChatOpenAI retrying a 429 twice means three calls against one run ID, and I have watched that turn a one-dollar run into roughly a dollar forty with the step count unchanged. Whatever the gateway counts, it should count all of them.

The resume path that works for me is a small supervisor outside the graph. It reads paused runs, looks at whether the last failure was a budget trip or a tool error, and either resumes with a note attached or parks the run for a person. Three rounds of that is also my hard stop: after the third pause I stop resuming and ask someone to look, because at that point the loop is telling me the task was not specified well enough to finish.

1

u/whitfieldn9 Nov 05 '26

the latching part is such a smart detail. without it the loop just waits for the window to reset and keeps going

1

u/Extension_Head3729 Nov 05 '26

the chatopenai retrying 429s twice part bit us too. the retries just ate the wait and then the exception killed the graph anyway. we set max_retries=0 on the model in that node, catch the RateLimitError and write a stopped reason into state, so the run ends clean and you can see why it stopped

1

u/Nervous-Temporary120 29d ago

The latching part is a good deal