Bird
Raised Fist0
LangChainframework~20 mins

ReAct agent implementation in LangChain - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
ReAct Agent Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this ReAct agent snippet?
Consider this simplified ReAct agent code using LangChain. What will be printed when the agent runs?
LangChain
from langchain.agents import ReActAgent
from langchain.schema import AgentAction, AgentFinish

class SimpleReActAgent(ReActAgent):
    def plan(self, intermediate_steps, **kwargs):
        if not intermediate_steps:
            return AgentAction(tool='search', tool_input='Python', log='Searching for Python')
        else:
            return AgentFinish(return_values={'output': 'Found info on Python'}, log='Done')

agent = SimpleReActAgent()
step1 = agent.plan([])
step2 = agent.plan([step1])
print(step1.log)
print(step2.log)
ADone\nSearching for Python
BSearching for Python\nDone
CSearching for Python\nSearching for Python
DDone\nDone
Attempts:
2 left
💡 Hint
Think about what the agent does when there are no intermediate steps versus when there are some.
state_output
intermediate
2:00remaining
What is the value of the agent's memory after these steps?
Given this ReAct agent code snippet, what will be the content of the agent's memory after executing the two steps?
LangChain
from langchain.agents import ReActAgent
from langchain.schema import AgentAction, AgentFinish

class MemoryReActAgent(ReActAgent):
    def __init__(self):
        super().__init__()
        self.memory = []
    def plan(self, intermediate_steps, **kwargs):
        if not intermediate_steps:
            self.memory.append('start')
            return AgentAction(tool='search', tool_input='LangChain', log='Starting search')
        else:
            self.memory.append('finish')
            return AgentFinish(return_values={'output': 'Result found'}, log='Finished')

agent = MemoryReActAgent()
agent.plan([])
agent.plan([AgentAction(tool='search', tool_input='LangChain', log='Starting search')])
memory_content = agent.memory
A[]
B['finish', 'start']
C['start']
D['start', 'finish']
Attempts:
2 left
💡 Hint
Look at when the memory list is appended to in the plan method.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this ReAct agent plan method?
Identify which plan method implementation will cause a syntax error when used in a ReAct agent subclass.
LangChain
def plan(self, intermediate_steps, **kwargs):
    # method body varies per option
A
if not intermediate_steps
    return AgentAction(tool='search', tool_input='Python', log='Searching')
else:
    return AgentFinish(return_values={'output': 'Done'}, log='Finished')
B
if not intermediate_steps:
    return AgentAction(tool='search', tool_input='Python', log='Searching')
return AgentFinish(return_values={'output': 'Done'}, log='Finished')
C
if not intermediate_steps:
    return AgentAction(tool='search', tool_input='Python', log='Searching')
else:
    return AgentFinish(return_values={'output': 'Done'}, log='Finished')
D
)'dehsiniF'=gol ,}'enoD' :'tuptuo'{=seulav_nruter(hsiniFtnegA nruter    
:esle
)'gnihcraeS'=gol ,'nohtyP'=tupni_loot ,'hcraes'=loot(noitcAtnegA nruter    
:spets_etaidemretni ton fi
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
🔧 Debug
advanced
2:00remaining
Why does this ReAct agent code raise a TypeError?
This ReAct agent subclass code raises a TypeError when calling plan. What is the cause?
LangChain
from langchain.agents import ReActAgent
from langchain.schema import AgentAction

class FaultyAgent(ReActAgent):
    def plan(self, intermediate_steps, **kwargs):
        return AgentAction(tool='search', tool_input=123, log='Searching')

agent = FaultyAgent()
agent.plan([])
AAgentAction requires log to be an int, but a string was given
BMissing return statement in plan method causes TypeError
CAgentAction expects tool_input to be a string, but 123 is an int causing TypeError
Dplan method must not accept **kwargs causing TypeError
Attempts:
2 left
💡 Hint
Check the types of arguments passed to AgentAction.
🧠 Conceptual
expert
2:00remaining
Which option best describes the ReAct agent's reasoning process?
In LangChain's ReAct agent implementation, what is the core idea behind the agent's reasoning and acting cycle?
AThe agent alternates between thinking (reasoning) and acting (tool use) based on observations to solve complex tasks
BThe agent only acts once after reasoning all steps internally without intermediate observations
CThe agent randomly chooses tools without reasoning to explore the environment
DThe agent uses a fixed sequence of tool calls without adapting to intermediate results
Attempts:
2 left
💡 Hint
Consider how ReAct combines reasoning and actions iteratively.

Practice

(1/5)
1. What is the main purpose of a ReAct agent in LangChain?
easy
A. To store data without processing or reasoning
B. To only perform simple, single-step actions without reasoning
C. To replace language models with rule-based systems
D. To combine reasoning and actions step-by-step to solve complex tasks

Solution

  1. Step 1: Understand the ReAct agent concept

    The ReAct agent is designed to think (reason) and act (perform tasks) in steps.
  2. Step 2: Identify its main use

    It helps solve problems that need multiple steps, like searching and calculating.
  3. Final Answer:

    To combine reasoning and actions step-by-step to solve complex tasks -> Option D
  4. Quick Check:

    ReAct agent = reasoning + actions [OK]
Hint: ReAct means think and act together step-by-step [OK]
Common Mistakes:
  • Thinking it only acts without reasoning
  • Confusing it with simple action-only agents
  • Assuming it replaces language models
2. Which of the following is the correct way to create a ReAct agent in LangChain?
easy
A. agent = ReActAgent(llm=llm, tools=tools)
B. agent = ReActAgent(tools=llm, llm=tools)
C. agent = ReActAgent()
D. agent = ReActAgent(llm)

Solution

  1. Step 1: Recall ReAct agent constructor parameters

    The ReAct agent requires a language model (llm) and a list of tools (tools) as named arguments.
  2. Step 2: Check each option for correct syntax

    agent = ReActAgent(llm=llm, tools=tools) correctly passes llm and tools by name. agent = ReActAgent(tools=llm, llm=tools) swaps them incorrectly. agent = ReActAgent() misses required arguments. agent = ReActAgent(llm) passes only llm without tools.
  3. Final Answer:

    agent = ReActAgent(llm=llm, tools=tools) -> Option A
  4. Quick Check:

    Correct parameters = llm and tools [OK]
Hint: Pass llm and tools as named parameters to ReActAgent [OK]
Common Mistakes:
  • Swapping llm and tools arguments
  • Omitting required parameters
  • Passing parameters positionally without names
3. Given this code snippet, what will be the output behavior of the ReAct agent?
from langchain.agents import ReActAgent
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
tools = [search_tool, calculator_tool]
agent = ReActAgent(llm=llm, tools=tools)

response = agent.run('What is the capital of France and what is 5 plus 3?')
medium
A. The agent will first search for the capital of France, then calculate 5 plus 3, returning both answers.
B. The agent will only perform the search and ignore the calculation.
C. The agent will return an error because multiple tools cannot be used.
D. The agent will calculate 5 plus 3 first, then search for the capital.

Solution

  1. Step 1: Understand ReAct agent multi-tool usage

    The ReAct agent can use multiple tools and decides which to use based on the question.
  2. Step 2: Analyze the question and agent behavior

    The question asks two things: capital of France (search) and 5 plus 3 (calculator). The agent will perform both actions step-by-step.
  3. Final Answer:

    The agent will first search for the capital of France, then calculate 5 plus 3, returning both answers. -> Option A
  4. Quick Check:

    Multi-tool agent answers multi-part questions [OK]
Hint: ReAct agents use all needed tools for multi-part questions [OK]
Common Mistakes:
  • Thinking agent uses only one tool per run
  • Assuming order is reversed without reason
  • Believing multiple tools cause errors
4. What is the likely cause of this error when running a ReAct agent?
TypeError: ReActAgent.__init__() missing 1 required positional argument: 'llm'
medium
A. The ReActAgent does not accept an llm argument.
B. The tools list was empty, causing the error.
C. The ReActAgent was created without passing the required language model (llm) argument.
D. The run method was called with an invalid input string.

Solution

  1. Step 1: Interpret the error message

    The error says the __init__ method is missing the required 'llm' argument.
  2. Step 2: Identify correct constructor usage

    ReActAgent requires an llm parameter when created. Missing it causes this TypeError.
  3. Final Answer:

    The ReActAgent was created without passing the required language model (llm) argument. -> Option C
  4. Quick Check:

    Missing llm argument = TypeError [OK]
Hint: Always pass llm when creating ReActAgent [OK]
Common Mistakes:
  • Forgetting to pass llm argument
  • Confusing tools argument with llm
  • Misreading error as related to run method
5. You want to build a ReAct agent that can handle a question requiring web search, math calculation, and database lookup. Which setup correctly supports this multi-step reasoning and acting?
hard
A. Create separate agents for each tool and run them independently without combining
B. Create a ReActAgent with llm and a tools list including search_tool, calculator_tool, and db_tool
C. Use only the calculator_tool since it can handle all tasks internally
D. Create a ReActAgent with llm but no tools, relying on the model alone

Solution

  1. Step 1: Identify the need for multiple tools

    The question requires web search, math calculation, and database lookup, so multiple tools are needed.
  2. Step 2: Choose the correct agent setup

    ReActAgent supports multiple tools passed as a list along with the llm to reason and act step-by-step.
  3. Step 3: Evaluate other options

    Separate agents won't combine reasoning easily; calculator_tool alone can't do all tasks; no tools means no external actions.
  4. Final Answer:

    Create a ReActAgent with llm and a tools list including search_tool, calculator_tool, and db_tool -> Option B
  5. Quick Check:

    Multi-tool ReActAgent = best for multi-step tasks [OK]
Hint: Pass all needed tools in one ReActAgent for multi-step tasks [OK]
Common Mistakes:
  • Trying to run separate agents instead of one combined
  • Assuming one tool can do all tasks
  • Not passing any tools to the agent