Lesson 9 of 11
Chapter 18 . LangGraph . Lesson 9 of 11

The ReAct agent loop

The model reasons, calls a tool, reads the result, and repeats until it can answer.

01Real-world example

Illustration: A QA assistant answers "Is login_redirect flaky?" by checking the test history and the last error before it replies.
A QA assistant answers "Is login_redirect flaky?" by checking the test history and the last error before it replies.

02The graph

The ReAct agent loop as a graph
The agent asks for a tool. ToolNode runs it. tools_condition says: again, or done.

03How the code does it

  • @tool turns a Python function into a tool; the docstring is what the model reads to decide when to call it.
  • bind_tools(TOOLS) lets the model ask for tools; ToolNode(TOOLS) runs whatever it asked for.
  • tools_condition routes to tools when the last message has tool calls, otherwise to END: two nodes and one loop.
  • {"recursion_limit": 12} caps the loop because the default on 1.2.12 is 10007 steps.

Key points

  • This is what create_agent builds for you
  • Two nodes and one loop
  • Cap it with recursion_limit

04Run it

terminal
cd chapter_18_LangGraph/src/chapters
python 009_ReAct_Agent_Tools.py
output
[agent] calls get_test_history({'test_name': 'login_redirect'})
[tools] get_test_history -> passed, failed, passed, passed, failed, passed
[agent] calls get_test_history({'test_name': 'checkout_pay'})
[tools] get_test_history -> failed, failed, failed, failed, failed, failed

[agent] answer: ...
The tool calls are the lesson. The final answer's wording changes run to run.
Needs a key. Copy chapter_18_LangGraph/.env.sample to .env and add a free Groq key from console.groq.com.