LangChain ReAct Loop
Agents feel magical until you see the pattern under the hood. Most agents follow a loop called ReAct, short for Reason and Act. The agent thinks about the goal, picks an action, observes the result, and thinks again. The loop repeats until the agent has enough information to answer. Knowing this loop helps you debug, tune, and trust your agents.
Detective Analogy
A detective never solves a case in one step. The detective forms an idea, checks a clue, learns something new, and adjusts the plan. ReAct agents work the same way: reason, act, observe, repeat.
The Loop in a Diagram
+------------------------------+
| GOAL / QUESTION |
+--------------+---------------+
|
v
+-----------------+
+---→ | REASON | "What do I need next?"
| +--------+--------+
| |
| v
| +-----------------+
| | ACT | call a tool
| +--------+--------+
| |
| v
| +-----------------+
+-----+ OBSERVE | read the tool result
+--------+--------+
|
enough information?
|
v
FINAL ANSWER
A Worked Example
Take the question: "How many days until the next public holiday, and what is it called?" The agent owns two tools: one returns today's date, and one returns a holiday list.
Reason: I need today's date first. Act: get_today() Observe: 2026-10-28 Reason: Now I need upcoming holidays. Act: get_holidays(after="2026-10-28") Observe: Diwali, 2026-11-08 Reason: I can calculate the gap: 11 days. Act: none Answer: The next holiday is Diwali, in 11 days.
Notice the agent chose the order of steps itself. No developer wrote that sequence.
Building a Simple Agent
from langchain.agents import create_agent
from langchain_core.tools import tool
@tool
def get_today() -> str:
"""Return today's date in YYYY-MM-DD format."""
from datetime import date
return str(date.today())
@tool
def get_holidays(after: str) -> str:
"""Return the next public holiday after a date (YYYY-MM-DD)."""
return "Diwali, 2026-11-08"
agent = create_agent(
model="openai:gpt-4o-mini",
tools=[get_today, get_holidays],
system_prompt="You are a careful assistant. Use tools when facts are needed.",
)Watching the Loop Run
Streaming the agent reveals every step.
inputs = {"messages": [{"role": "user", "content": "How many days until the next holiday?"}]}
for step in agent.stream(inputs, stream_mode="values"):
step["messages"][-1].pretty_print()The printout shows each AI message with tool requests, each tool result, and the final answer. Reading these traces teaches more than any description.
Why the Loop Sometimes Struggles
Vague Tool Descriptions
The agent picks the wrong tool when two descriptions sound alike. Rewrite them to show clear differences.
Too Many Tools
A long tool list confuses the model. Give each agent five to eight focused tools.
Missing Instructions
A system prompt that explains the agent's role, limits, and style improves decisions.
Endless Looping
An agent that never finds the answer keeps trying. Set a step limit so the loop ends.
Improving Reasoning Quality
- Ask the agent in the system prompt to check facts with tools before answering.
- Tell the agent to say "I do not know" when the tools return nothing useful.
- Return clear error messages from tools, so the agent can adjust the next action.
- Test with tricky questions that need two or three steps.
ReAct vs a Fixed Chain
A chain follows steps written in advance. A ReAct agent chooses steps while it works. Pick a chain when the path never changes. Pick an agent when the path depends on what the tools return.
Real-World Scenario
A personal finance helper answers the question: "Did I spend more on food this month than last month?" The agent calls a tool for this month's food total and a tool for last month's total. After reading both numbers, the agent compares them and replies with the difference. The developer never wrote the order of these calls. Streaming the run showed each step clearly, and a short system prompt kept the agent from guessing numbers it had not fetched.
Practice Tasks
- Build an agent with a calculator tool and a date tool, then stream a two-step question.
- Read each message in the stream and label it as reason, act, or observe.
- Remove one tool and watch how the agent explains the missing ability.
- Set a recursion limit of 3 and observe what happens on a hard question.
Key Points
- ReAct stands for Reason and Act.
- The agent loops through reason, act, and observe until it can answer.
- Streaming exposes each step for debugging.
- Clear tools and instructions improve decisions.
- Step limits stop endless loops.
