Behind the Scene — ReAct Agents in Langchain

Prabha Arivalagan
10 min readJan 31, 2024

The ReAct (Reason & Action) framework was introduced in the paper Yao et al., 2022. It is one of the widely used prompting strategies in Generative AI applications. For more details on the ReAct framework, please refer here.

In this blog, we will delve into the implementation of the ReAct framework within Langchain and provide a detailed, step-by-step guide on the functioning of a simple agent. Additionally, there will be a bonus section detailing how to utilize LangSmith for a thorough step-by-step analysis of an agent’s execution and how to use Langchain to align agents behavior.

The “Magic Sauce” in the ReAct framework is the prompt that encourages the LLM model to use the below thought process: Question, Action, Action Input, and Observation.


Answer the following questions as best you can. You have access
to the following tools:

{tools}

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: {input}
Thought:{agent_scratchpad}…

--

--