Skip to content
Back to Advanced agents

Advanced agents · Chapter 6 of 10

Loops that stop

A loop needs stops that hold whatever the model does: a recursion limit, a budget of calls, a detector for repeated calls, and a person who can say no.

20 minutes0 of 4 goals done

Goals

  • Run an agent that repeats one tool call until GraphRecursionError stops it.To do
  • Arm a call budget and a repeat detector, and see which fires first.To do
  • Pause before a risky tool and decline the call.To do
  • Pass the check.To do

Step 1 of 4: The read

A loop needs stops the model does not choose

An agent loop ends when the model answers without asking for a tool. A turn is one model call plus the tools it asks for. If the model keeps asking, for example to check a status that never changes, every turn is another paid model call and nothing new is learned. So you add stops that hold whatever the model does.

LangGraph's cap: recursionLimit

In a loop like this one, each node run is one superstep. recursionLimit in the run's config caps the supersteps, 25 by default. Past it, LangGraph.js throws GraphRecursionError, with lc_error_code GRAPH_RECURSION_LIMIT. It is a backstop: it counts supersteps, not turns, it stops late, and without a checkpointer the run's state is lost with the error.

Stops as middleware

Middleware are hooks that LangChain 1.6's createAgent runs around the model call; beforeModel and afterModel hooks each become a node of the graph. modelCallLimitMiddleware is a budget in model calls: runLimit counts one run, threadLimit a whole thread, and exitBehavior 'end' ends the run with a message. toolCallLimitMiddleware counts calls to one tool or to all. A budget in tokens or money works the same way: count, compare, stop. This site re-creates both, as modelCallLimit and toolCallLimit; the lab uses modelCallLimit.

A repeat detector catches a stuck loop early: the same tool with the same arguments, threshold times in the last few calls. The repeatDetector here is this site's own; LangChain has no built-in for it. With exitBehavior 'end' it ends the run; with 'continue' it refuses the call and tells the model to try something else. Here each hook also writes a trace event, so you can see which stop fired.

A person as the stop

Some calls should wait for a yes, such as sending money. interruptBefore pauses the graph before a node, and a checkpointer keeps the paused state. The breakpoint stops before every run of that node, so your code picks the calls that need a person and shows them. To decline, updateState answers the call with a ToolMessage as if the tools node had run; invoking again with a null input resumes the run. In Node, humanInTheLoopMiddleware does this with interrupt(), which needs AsyncLocalStorage; browsers lack it, so this page uses the breakpoint.

Step 2 of 4: Try it: Three ways to stop a loop

Three ways to stop a loop

A scripted agent checks a refund that stays pending. Stop it with recursionLimit, then with a budget and a repeat detector, then with a person before the risky tool.

Mode:RealRe-createdSimulatedReal: LangGraph.js 1.5.1 runs in this page, with its ToolNode, recursionLimit and GraphRecursionError, the checkpointer and the breakpoint. Re-created: LangChain 1.6 createAgent middleware rebuilt on LangGraph.js 1.5.1, with LangChain's middleware names; repeatDetector is this site's own. Simulated: the model's decisions are scripted, and the refund is fictional.

Step 3 of 4: Check

Check

Code
const agent = createAgent({
  model,
  tools: [checkRefund],
  middleware: [
    modelCallLimit({
      runLimit: 3,
      exitBehavior: 'end',
    }),
    repeatDetector({
      window: 6,
      threshold: 4,
      exitBehavior: 'end',
    }),
  ],
});
// recursionLimit: 25 (the default)
await agent.graph.invoke({
  messages: [task],
});
The model asks for check_refund({ order: '4471' }) on every turn and never answers. Which stop ends this run?

Step 4 of 4: What you can do now

What you can do now

  • Set recursionLimit as a backstop and catch GraphRecursionError.
  • Add a call budget and a repeat detector as middleware, and read which one fired from its trace event.
  • Pause before a risky tool with interruptBefore, and decline the call with updateState.