Agents and Tools
ReAct Agent
A ReAct agent is a specialized framework that combines reasoning traces and task-specific actions to solve complex, multi-step problems. By forcing the language model to articulate its thought process before executing a tool, it significantly reduces hallucination and increases reliability in dynamic environments. You reach for a ReAct agent when your application requires autonomous decision-making over external data sources rather than simple request-response flows.
The Core Philosophy of ReAct
The ReAct paradigm, which stands for 'Reasoning and Acting', is built on the observation that language models often struggle to perform multi-step tasks when asked to provide an immediate answer. By interleaving reasoning steps with action steps, the model gains a 'scratchpad' to track its progress. In the reasoning phase, the model generates a thought about what it needs to do next. In the action phase, it selects a tool and provides the necessary arguments. Crucially, the model then pauses to observe the tool's output, integrating this new information into its subsequent thought. This feedback loop is essential because it anchors the model's imagination in reality, ensuring that every step taken is based on verified input from the external world. Without this structured approach, an agent might attempt to hallucinate answers instead of querying the tools it has been provided.
# Simple structure of a ReAct thought loop
# Thought: I need to check the stock price to answer the user.
# Action: StockLookupTool
# Action Input: {'ticker': 'AAPL'}
# Observation: 175.50
# Thought: The price is 175.50, I can now respond to the user.Defining Tools for the Agent
Tools are the hands of the agent; they allow it to interact with systems outside its own training data. A tool in this context is essentially a function wrapped in metadata that describes its purpose to the language model. When designing these tools, it is vital to provide clear, descriptive docstrings or function descriptions. The agent uses these descriptions to decide which tool to call and what arguments to supply. If the description is vague, the agent will frequently misinterpret the tool's capabilities or fail to format its arguments correctly. You must treat these tool definitions as part of your prompt engineering strategy. By constraining the inputs and clearly defining the output behavior, you provide a stable API that the language model can reliably navigate, which is the foundational requirement for building a predictable, robust agentic system that does not drift off-task.
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Returns the current weather for a specific city name."""
# Imagine a real API call here
return f"The weather in {city} is sunny and 75 degrees."The Agent Executor Orchestrator
The Agent Executor acts as the runtime environment that facilitates the interaction between the model, the tools, and the observed output. It serves as the loop controller that manages the sequence of events. When you invoke the executor, it sends the user's initial prompt along with the available tool definitions to the language model. If the model determines it needs to call a tool, the executor parses that request, runs the actual tool function, and then injects the resulting string observation back into the model's context window. This creates a stateful conversation history. The executor is responsible for managing the stop conditions—such as the maximum number of iterations or an explicit finish signal—preventing the agent from falling into infinite loops when it encounters complex or unsolvable queries in your production environment.
from langchain.agents import create_react_agent, AgentExecutor
# Assuming llm and tools are defined
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
executor.invoke({'input': 'What is the weather in Paris?'})Prompt Engineering the ReAct Loop
The intelligence of a ReAct agent is heavily dependent on the quality of the system prompt. The prompt must explicitly instruct the model to follow the 'Thought, Action, Action Input, Observation' format. If you skip this instruction, the model might attempt to combine reasoning and acting into a single step, which breaks the feedback loop and prevents the model from properly processing the observation before continuing. Your prompt should act as a set of rules for the model's 'internal monologue'. By providing a few-shot example in the prompt, you further anchor the model to the expected output schema. This is especially important when you are working with smaller models that may not have robust inherent knowledge of the ReAct pattern. A well-constructed prompt ensures that the model treats tools as distinct entities rather than part of its own narrative text.
from langchain_core.prompts import PromptTemplate
template = """Answer the user question using these steps: Thought, Action, Action Input, Observation.
Question: {input}
{agent_scratchpad}"""
prompt = PromptTemplate.from_template(template)Handling Errors and Unexpected Tool Results
In a real-world scenario, tools fail. An API might time out, a database query might return a syntax error, or the model might generate an invalid action input format. A well-designed ReAct agent must be resilient to these failures. The Agent Executor allows you to handle these situations by catching exceptions during the execution phase and reporting the error as an 'Observation' back to the model. This allows the agent to 'realize' that its previous approach failed and adjust its strategy accordingly. For example, if a search tool returns no results, the agent can use that negative information to rethink its approach, perhaps by querying a different source. This self-correction capability is the primary reason why ReAct agents are so much more powerful than simple chained prompts; they can learn from their own failures in real-time.
try:
# Tool execution within the loop
result = tool.run(action_input)
except Exception as e:
# Feed the error back to the model
result = f"Error occurred: {str(e)}. Try a different strategy."Key points
- ReAct agents derive their power from the iterative loop of reasoning and acting.
- The reasoning phase provides the agent with a scratchpad to plan and document its thought process.
- Observation steps are crucial because they ground the agent's logic in real-world data.
- Tools must have precise and descriptive docstrings to ensure the agent selects them correctly.
- The Agent Executor functions as the runtime controller that manages the loop and error handling.
- Prompt structure is vital for enforcing the strict sequence of thought-action-observation cycles.
- Self-correction occurs when the model receives failure feedback as an observation to adjust its next move.
- ReAct agents are ideal for complex, multi-step tasks requiring external data interaction.
Common mistakes
- Mistake: Providing insufficient tool descriptions. Why it's wrong: The ReAct agent relies on LLM reasoning to map a user request to a tool; if descriptions are vague, the agent cannot determine which tool to invoke. Fix: Use descriptive docstrings or Pydantic field descriptions for every tool.
- Mistake: Creating an infinite tool-use loop. Why it's wrong: If the model is not given a clear exit condition, it may repeatedly call the same tool with the same arguments. Fix: Ensure the ReAct prompt explicitly instructs the agent to return a final answer once the objective is met.
- Mistake: Overloading the agent with too many tools. Why it's wrong: LLMs have a limited context window and performance degrades as the number of available tools increases. Fix: Limit the toolset to only those strictly necessary for the specific task at hand.
- Mistake: Ignoring output formatting requirements. Why it's wrong: The agent's ability to parse tool results depends on the format. Fix: Utilize structured output parsers and enforce a clear Thought-Action-Observation loop structure.
- Mistake: Failing to provide error-handling feedback. Why it's wrong: When a tool fails, the agent needs to know why so it can retry or pivot. Fix: Ensure the tool execution environment catches exceptions and returns them as a string observation for the agent to process.
Interview questions
What is a ReAct agent in the context of LangChain, and what does the name stand for?
A ReAct agent in LangChain is an architecture that enables an LLM to reason through a problem and take actions to reach a solution. The name stands for 'Reasoning and Acting.' It combines a model's ability to generate reasoning traces—where the model explicitly thinks about the steps it needs to take—with its ability to perform actions, such as calling tools or searching the web, to achieve a specific goal.
How does a ReAct agent decide which tool to use when presented with a user query?
In LangChain, a ReAct agent decides which tool to use by following a structured prompt template. When the agent receives a query, it generates a 'Thought' block explaining its reasoning. It then generates an 'Action' block specifying which tool to invoke and the 'Action Input' for that tool. After the tool executes and returns an observation, the model incorporates that result back into its context to determine the next step.
What is the role of the 'Thought' step in the ReAct prompting cycle?
The 'Thought' step is crucial because it forces the LangChain agent to decompose a complex objective into manageable sub-tasks. By externalizing its reasoning, the model reduces hallucinations and improves transparency. If a model tries to perform an action without a clear thought process, it is more likely to lose track of the intermediate goals required to answer the user's final request accurately and logically.
Compare a standard chain and a ReAct agent in LangChain. When should you prefer one over the other?
A standard LangChain chain follows a fixed, linear sequence of operations, which is efficient for predictable workflows like simple summarization or data extraction. In contrast, a ReAct agent is dynamic; it iteratively decides which tools to call based on the output of previous steps. You should prefer a standard chain for speed and reliability in repetitive tasks, but choose a ReAct agent for complex, open-ended tasks requiring external information or multi-step reasoning.
How do you handle agent loops that never terminate or exceed the maximum allowed iterations in LangChain?
To prevent infinite loops, LangChain provides a 'max_iterations' parameter in the agent executor. If an agent hits this limit, it halts and raises an exception. To manage this effectively, you should implement robust stop sequences and provide clear instructions in the prompt. Furthermore, you can use the 'max_execution_time' parameter to force termination, ensuring the agent does not consume excessive compute resources if it gets stuck in a recursive reasoning cycle.
How would you implement a custom tool for a ReAct agent and ensure the agent correctly interprets the tool's return value?
You implement a custom tool by using the `@tool` decorator or extending the `BaseTool` class in LangChain. The most critical part is providing a descriptive 'docstring' for the tool; the agent uses this description to decide when to call the function. To ensure the agent correctly interprets the output, the tool must return a string or a structured format that the model can parse. For example: `@tool def get_weather(city: str): 'Returns weather for a city'. return 'Sunny'.`
Check yourself
1. What is the primary role of the 'Thought' component in the ReAct pattern?
- A.To execute a function call directly.
- B.To record the agent's internal reasoning process for selecting the next step.
- C.To summarize the final output for the user.
- D.To manage the memory state of the chat history.
Show answer
B. To record the agent's internal reasoning process for selecting the next step.
The 'Thought' component allows the agent to reason about the state of the task, ensuring it chooses tools logically. Option 0 is wrong because that is the 'Action' step; Option 2 is the 'Final Answer'; Option 3 is handled by memory buffers, not the ReAct logic.
2. In a LangChain ReAct agent, how does the agent 'learn' from a tool invocation?
- A.By fine-tuning the underlying model weights.
- B.By observing the output string appended to the prompt as an 'Observation'.
- C.By automatically generating new system instructions.
- D.By updating the local vector database index.
Show answer
B. By observing the output string appended to the prompt as an 'Observation'.
ReAct agents learn through context in the prompt; the Observation is injected back into the context window for the LLM to process. Option 0 is too slow for runtime agent behavior; Options 2 and 3 do not occur automatically during a standard ReAct cycle.
3. What happens if a ReAct agent is asked a question that none of its available tools can answer?
- A.The agent will automatically hallucinate a tool response.
- B.The agent will crash and raise a Python Exception.
- C.The agent relies on its internal training data to provide a 'Final Answer'.
- D.The agent will enter an infinite loop of calling the first tool in the list.
Show answer
C. The agent relies on its internal training data to provide a 'Final Answer'.
If the agent exhausts its reasoning and finds no tool applicable, it can provide a direct answer based on its model training. Option 0 is a failure state, not intended behavior; Option 1 is incorrect as agents are designed to handle 'don't know' scenarios; Option 3 is avoided by proper prompt engineering.
4. Why is it important to provide clear 'input' descriptions for tools in a ReAct agent?
- A.To help the LLM generate the correct arguments for the tool call.
- B.To speed up the network latency of the tool execution.
- C.To improve the readability of the terminal logs.
- D.To force the agent to use tools in a specific sequence.
Show answer
A. To help the LLM generate the correct arguments for the tool call.
The LLM needs to know exactly what format or data type the tool expects to generate valid calls. Option 1 relates to infrastructure; Option 2 relates to UI; Option 3 is false because the agent follows a logic-driven sequence, not a static hard-coded sequence.
5. Which of the following best describes the 'ReAct' acronym in the context of LangChain?
- A.Return, Execute, Act, Confirm, Task
- B.Reasoning, Action, Trace
- C.Reasoning + Acting
- D.Request, Analyze, Communicate, Transmit
Show answer
C. Reasoning + Acting
ReAct stands for Reasoning and Acting. It signifies the synergy where the agent uses reasoning to determine an action and acts to gather information. The other options are incorrect interpretations of the established pattern.