0
0
LangChainframework~10 mins

ReAct agent implementation in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ReAct agent implementation
Start: Receive user query
Agent decides: Think or Act?
Think: Reason
Update agent memory with observation
Check if answer ready
Return answer
End
The ReAct agent loops between thinking (reasoning) and acting (calling tools), updating memory until it produces a final answer.
Execution Sample
LangChain
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI

# Define tools and initialize agent
agent = initialize_agent(tools, llm, agent='zero-shot-react-description')
This code sets up a ReAct agent with tools and a language model to handle user queries by reasoning and acting.
Execution Table
StepAgent InputAgent ActionTool CalledObservationAgent Memory UpdateAnswer Ready?
1User query: 'What is the capital of France?'Think: Plans to find capitalNoneNoneStores plan to use knowledge toolNo
2Plan: Use knowledge toolAct: Calls knowledge toolKnowledge ToolParisStores observation 'Paris'No
3Observation: 'Paris'Think: Confirms answerNoneNoneUpdates memory with confirmationYes
4Final answer readyReturn answer: 'The capital of France is Paris.'NoneNoneEnds processYes
💡 Agent returns final answer after confirming observation matches query.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
agent_memory{}{plan: 'use knowledge tool'}{plan: 'use knowledge tool', observation: 'Paris'}{plan: 'use knowledge tool', observation: 'Paris', confirmation: true}{final_answer: 'The capital of France is Paris.'}
Key Moments - 2 Insights
Why does the agent alternate between thinking and acting?
The agent first thinks to decide what to do, then acts by calling a tool. This cycle repeats until it has enough info to answer, as shown in steps 1-3 in the execution table.
What happens if the tool returns no useful observation?
The agent updates memory with the observation (even if empty) and thinks again to decide next steps, continuing the loop until answer is ready.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the agent do at step 2?
ACalls a tool to get information
BReturns the final answer
CThinks about the next step without calling a tool
DEnds the process without answer
💡 Hint
Check the 'Agent Action' and 'Tool Called' columns at step 2.
At which step does the agent confirm it has enough information to answer?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Answer Ready?' column to see when it changes to Yes.
If the tool returned no observation at step 2, how would the agent proceed?
AReturn an empty answer immediately
BUpdate memory with empty observation and think again
CStop and report an error
DCall a different tool without thinking
💡 Hint
Refer to the key moment about handling empty observations.
Concept Snapshot
ReAct agent loops: Think (reason) -> Act (call tool) -> Observe -> Update memory.
Repeat until answer is ready.
Use tools to get info, then reason to decide next step.
Final output returned when confident.
This cycle enables dynamic problem solving.
Full Transcript
The ReAct agent starts by receiving a user query. It thinks about how to answer, then acts by calling a tool if needed. The tool returns an observation, which the agent stores in memory. The agent thinks again to decide if it has enough info. This loop continues until the agent is confident to return a final answer. For example, when asked 'What is the capital of France?', the agent plans to use a knowledge tool, calls it, gets 'Paris', confirms the answer, and returns it. This process shows how the agent dynamically reasons and acts to solve problems.