This function seems to replace `create_react_agent` and the mix of system prompt + tools is typical of ReAct agent. Yet one ingredient seems to be missing, from the quickstart doc I am reading: the agent loop. Is it built-in? Is the agent expected to run in loops and can I control this loop? I haven’t found the relevant APIs yet.
This doc seems to imply that there is a loop: Agents - Docs by LangChain
But I don’t see much control over the loop.
hi @eric-burel
Yes - create_agent constructs a LangGraph StateGraph that implements a ReAct-style loop (model → tools → model …) and runs until a stop condition is met. The loop is built-in.
How to control the loop:
- Limit iterations via the LangGraph executor’s
recursion_limitconfig when invoking/streaming the agent:
assert app.invoke(
{"messages": [HumanMessage(content="what is weather in sf")]},
{"recursion_limit": 2},
debug=True,
) == {
"messages": [
_AnyIdHumanMessage(content="what is weather in sf"),
_AnyIdAIMessage(content="Sorry, need more steps to process this request."),
]
}
- Exit early when a tool has
return_direct=Trueor when a structured output tool completes (see code excerpts above). - Skip the loop entirely by passing no tools; the graph becomes a single
modelnode (no tool-calling loop). - Programmatic control with middleware: set
state["jump_to"]to"model" | "tools" | "end"to steer or terminate the loop (see_resolve_jumpand the conditional edges around it in the same file). - Pause/resume specific nodes with
interrupt_before/interrupt_after(useful for human-in-the-loop UIs); these are exposed increate_agent(...).compile(...)arguments.
1 Like