Hands‑On ReAct with LangGraph: Dissecting the AI Reason‑Act‑Observe Loop

This tutorial explains the ReAct (Reason‑Act‑Observe) loop in LangGraph, shows how to control execution branches with conditional edges, provides a full hand‑written agent example, demonstrates the convenience of the prebuilt create_react_agent, and covers multi‑turn dialogue, streaming output, and loop‑count limits.

Tech Ocean
Tech Ocean
Tech Ocean
Hands‑On ReAct with LangGraph: Dissecting the AI Reason‑Act‑Observe Loop

ReAct combines reasoning and acting, forming a loop of Reason → Act → Observe → Reason …. A helper function should_continue examines the last message for tool calls and returns "action" to continue or "end" to stop.

Conditional edges

The add_conditional_edges API selects the next node based on the return value of a decision function, mapping values such as "node_a" or "node_b" to target paths.

Hand‑written ReAct agent

A complete example defines an AgentState extending MessagesState, implements should_continue, a reasoning node that would call an LLM, and an action node that performs tool calls. The graph is built with StateGraph, nodes are added, edges connect START to the reasoning node, and conditional edges route from reasoning to either the action node or END. Finally the graph is compiled.

Using create_react_agent

For a quicker setup, the prebuilt create_react_agent is used. A simple multiply tool is defined, a Claude‑Sonnet model is instantiated, and an in‑memory checkpoint saver is created. The agent is invoked with a message such as "3乘4等于多少?" and the result is printed.

Multi‑turn conversation

Successive invoke calls with the same thread_id preserve context, allowing follow‑up queries like asking about the weather in Shanghai after Beijing.

Streaming output

By calling agent.stream with stream_mode="values" or "messages", each step or message increment is printed in real time, exposing the agent’s internal reasoning process.

Controlling loop iterations

An iteration field in AgentState and a modified should_continue function limit the loop to a maximum of five cycles, preventing infinite execution.

Key concepts

ReAct : Reasoning + Acting, the think‑act loop.

should_continue : Determines whether the loop should keep running.

add_conditional_edges : Branches execution based on a function’s return value.

create_react_agent : Prebuilt agent creation in a single line.

thread_id : Isolates conversation state across different dialogues.

References

Official docs: https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/

create_react_agent reference: https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.create_react_agent

ReAct paper: https://arxiv.org/abs/2210.03629

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonReActLangChainAgentLangGraph
Tech Ocean
Written by

Tech Ocean

Focused on AI programming, sharing ready-to-use development efficiency solutions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.