1
00:00:03,000 --> 00:00:09,439
Welcome to this video on ReAct: Building Agents that Reason Before Acting.

2
00:00:09,439 --> 00:00:14,520
In this video, you'll explore how React enables step-by-step agent reasoning. You'll

3
00:00:14,520 --> 00:00:18,799
identify key components like thought, action, and observation.

4
00:00:18,799 --> 00:00:22,840
You'll construct a working React agent in LangGraph.

5
00:00:22,840 --> 00:00:27,399
And finally, you'll analyze how the agent reasons through tool calls to generate a final

6
00:00:27,399 --> 00:00:30,280
response.

7
00:00:30,280 --> 00:00:35,840
React agents are designed for complex tasks that require step-by-step reasoning. For example,

8
00:00:35,840 --> 00:00:38,360
you might begin with a system prompt like,

9
00:00:38,360 --> 00:00:43,360
You are a helpful AI assistant that thinks step-by-step and uses tools when needed.

10
00:00:43,360 --> 00:00:47,720
You can also prompt the LLM to respond in the following ways.

11
00:00:47,720 --> 00:00:51,919
Identify the information needed. Use tools when appropriate.

12
00:00:51,919 --> 00:00:57,919
Get clear and useful answers. And finally, walk through its reasoning process.

13
00:00:57,919 --> 00:01:02,880
The system message should instruct the LLM to format its output using a structured pattern.

14
00:01:02,880 --> 00:01:08,400
First, it starts with Thought, where the LLM reasons about what to do next.

15
00:01:08,400 --> 00:01:12,519
Then comes Action, the name of the tool to use.

16
00:01:12,519 --> 00:01:17,519
Next is Action Input, the input provided to the selected tool.

17
00:01:17,519 --> 00:01:21,839
After that is Observation, the result returned by the tool.

18
00:01:21,839 --> 00:01:28,239
And then Final Answer, the complete response to the user based on what the tool returned.

19
00:01:28,239 --> 00:01:32,419
Let's start with a simple example. You're a frequent traveler who owns multiple apartments

20
00:01:32,419 --> 00:01:37,120
in different cities and access to an LLM that can use two tools.

21
00:01:37,120 --> 00:01:39,800
A search tool that provides the current weather.

22
00:01:39,800 --> 00:01:45,059
And a clothing recommendation tool that suggests what to wear based on what's in your closet.

23
00:01:45,059 --> 00:01:48,339
In the block diagram, the LLM is shown in blue.

24
00:01:48,339 --> 00:01:50,940
The green box represents the environment.

25
00:01:50,940 --> 00:01:54,660
The LLM must interact with the environment through these tools.

26
00:01:54,660 --> 00:02:00,459
It reasons step-by-step, uses tool feedback, and iterates toward a final answer.

27
00:02:00,459 --> 00:02:02,980
In this case, what to wear.

28
00:02:02,980 --> 00:02:06,779
Consider the question, what's the weather in Tokyo and what should I wear?

29
00:02:06,779 --> 00:02:11,179
The LLM must use tools to obtain data from the environment, in this case, the current

30
00:02:11,179 --> 00:02:16,220
weather, and check what clothes are available using the clothing recommendation tool.

31
00:02:16,220 --> 00:02:20,539
The LLM outputs the thought, I need to look up the weather in Tokyo first.

32
00:02:20,539 --> 00:02:24,940
The LLM determines the action, in this case, to use the search tool.

33
00:02:24,940 --> 00:02:30,300
At the same time, the LLM generates the action input, Tokyo weather today.

34
00:02:30,300 --> 00:02:34,460
This serves as input to the tool, which in turn interacts with the environment to retrieve

35
00:02:34,460 --> 00:02:36,440
the relevant information.

36
00:02:36,440 --> 00:02:39,860
The tool returns an observation after accessing the environment.

37
00:02:39,860 --> 00:02:44,139
Tokyo is 22°C  with sunny skies.

38
00:02:44,139 --> 00:02:47,899
The observation and chat history are passed back into the LLM.

39
00:02:47,899 --> 00:02:51,979
The chat history is always included, even if not shown explicitly.

40
00:02:51,979 --> 00:02:55,220
The LLM takes the new input and processes a thought.

41
00:02:55,220 --> 00:02:59,059
Now I should recommend clothing for 22°C sunny weather.

42
00:02:59,059 --> 00:03:00,820
Next, the action.

43
00:03:00,820 --> 00:03:04,059
It selects the tool, recommend_clothing.

44
00:03:04,059 --> 00:03:08,779
The action input is produced, 22°C sunny weather.

45
00:03:08,779 --> 00:03:14,100
After the observation, light clothing recommended, t-shirt, shorts, sunglasses.

46
00:03:14,100 --> 00:03:17,419
Finally, the LLM reaches its last thought.

47
00:03:17,419 --> 00:03:20,059
It now has all the information it needs.

48
00:03:20,059 --> 00:03:21,940
It provides the final answer.

49
00:03:21,940 --> 00:03:25,339
Tokyo is 22°C and sunny today.

50
00:03:25,339 --> 00:03:30,059
I recommend wearing light clothing like a t-shirt, shorts, and sunglasses.

51
00:03:30,059 --> 00:03:33,779
Since there are no further tool calls, the process ends here.

52
00:03:33,779 --> 00:03:37,259
Now let's see how to implement this in LangGraph.

53
00:03:37,259 --> 00:03:41,979
The code example you are going to look at emphasizes simplicity by minimizing the schema,

54
00:03:41,979 --> 00:03:44,619
prompts, and auxiliary functions.

55
00:03:44,619 --> 00:03:49,699
It works well for basic use cases, but refer to the LangGraph documentation for more advanced

56
00:03:49,699 --> 00:03:52,179
implementations.

57
00:03:52,179 --> 00:03:57,699
Define a search tool by wrapping a TavilySearchResults object with the @ tool decorator.

58
00:03:57,699 --> 00:04:02,699
Create a recommend_clothing tool that analyzes keywords in the LLM's response,

59
00:04:02,699 --> 00:04:09,179
such as rain or wet, to suggest appropriate clothing, as described in the docstring.

60
00:04:09,179 --> 00:04:14,380
The schema is essentially a dictionary-like state where the key is messages and the value

61
00:04:14,380 --> 00:04:18,940
is a growing list of messages exchanged during the conversation.

62
00:04:18,940 --> 00:04:24,500
Sequence[BaseMessage] holds a list of any message type, HumanMessage, AIMessage, or

63
00:04:24,500 --> 00:04:26,339
ToolMessage.

64
00:04:26,339 --> 00:04:30,500
Add_messages appends new messages each time a node runs.

65
00:04:30,500 --> 00:04:35,179
Annotated is required to turn the messages field into the correct form so that LangGraph

66
00:04:35,179 --> 00:04:38,239
knows to apply add_messages.

67
00:04:38,239 --> 00:04:43,660
The agent state object behaves like a dictionary where messages is a key and its value is a

68
00:04:43,660 --> 00:04:47,260
list that stores all the messages exchanged in the conversation.

69
00:04:47,260 --> 00:04:52,500
Next, load the GPT model using Lengchain's OpenAI wrapper.

70
00:04:52,500 --> 00:04:59,059
Then create a list of tools and a dictionary mapping each tool's name to the tool itself.

71
00:04:59,059 --> 00:05:03,540
Define a system message that instructs the agent to respond step-by-step and use tools

72
00:05:03,540 --> 00:05:05,299
when necessary.

73
00:05:05,299 --> 00:05:10,820
The agent_scratchpad variable automatically stores the reasoning history in the format.

74
00:05:10,820 --> 00:05:18,279
Thought, to action, to action input, to observation, along with the user's input sequence.

75
00:05:18,279 --> 00:05:24,200
While many implementations store the user's input, or question, and tool messages separately,

76
00:05:24,200 --> 00:05:27,880
for brevity, store everything in the scratchpad.

77
00:05:27,880 --> 00:05:34,000
Next, chain the prompt to the model and bind the tools to form the agent.

78
00:05:34,000 --> 00:05:37,880
Define the first node in the graph, which takes the agent state.

79
00:05:37,880 --> 00:05:41,920
The state is passed to the model through scratch_pad.

80
00:05:41,920 --> 00:05:47,320
The model's response is returned and appended to the message history.

81
00:05:47,320 --> 00:05:49,959
Create a tool calling node.

82
00:05:49,959 --> 00:05:53,079
Create a new graph using agent state to track the data.

83
00:05:53,079 --> 00:05:57,399
Add a node named agent that runs the call_model function.

84
00:05:57,399 --> 00:06:02,559
Next, add a node named tools that runs the tool_node function.

85
00:06:02,559 --> 00:06:07,559
Finally, add an edge from agent to tools.

86
00:06:07,559 --> 00:06:11,880
Define a should_continue function to control the agent's flow.

87
00:06:11,880 --> 00:06:14,859
Pair it with a conditional edge that decides the next step.

88
00:06:14,859 --> 00:06:18,720
It takes the state from the agent and retrieves the last message.

89
00:06:18,720 --> 00:06:24,160
Based on that message, the graph routes to either the tools node or the end node.

90
00:06:24,160 --> 00:06:28,119
If the message has no tool calls, it returns end.

91
00:06:28,119 --> 00:06:30,600
This maps to the end node in the graph.

92
00:06:30,600 --> 00:06:36,279
Otherwise, it returns continue, and conditional edge maps continue to the tools node.

93
00:06:36,279 --> 00:06:39,600
Here, agent is set at the starting node of the graph.

94
00:06:39,600 --> 00:06:43,640
It compiles the workflow into an executable graph.

95
00:06:43,640 --> 00:06:47,239
This function will help print out results from the graph.

96
00:06:47,239 --> 00:06:50,200
Define the input message from the user to start the graph.

97
00:06:50,200 --> 00:06:51,799
What's the weather like in Zurich?

98
00:06:51,799 --> 00:06:53,880
And what should I wear based on the temperature?

99
00:06:53,880 --> 00:06:59,760
Next, run the graph and print each state update step-by-step using the print_stream

100
00:06:59,760 --> 00:07:01,519
function.

101
00:07:01,519 --> 00:07:06,920
The trace of the full reasoning process starting from the human message can be seen.

102
00:07:06,920 --> 00:07:11,440
The first AI message includes a tool call to the search tool with its parameters.

103
00:07:11,440 --> 00:07:13,200
The tool returns a result.

104
00:07:13,200 --> 00:07:16,679
The second AI message calls the clothing recommendation tool.

105
00:07:16,679 --> 00:07:19,279
The second tool result is added to the state.

106
00:07:19,279 --> 00:07:24,640
Finally, the LLM generates the last AI message with the complete answer.

107
00:07:24,640 --> 00:07:26,760
You can sketch what's happening in the graph.

108
00:07:26,760 --> 00:07:31,640
The initial human message is passed to the call_model via the state.

109
00:07:31,640 --> 00:07:39,320
The LLM responds, and an AI message is appended to the state messages variable.

110
00:07:39,320 --> 00:07:43,160
The state is then passed to the should_continue node.

111
00:07:43,160 --> 00:07:47,519
The last message, an AI message, is extracted from the state.

112
00:07:47,519 --> 00:07:51,760
If it includes a tool call, the conditional edge returns continue and routes to the tool

113
00:07:51,760 --> 00:07:53,239
node.

114
00:07:53,239 --> 00:07:57,480
Similarly, the tool node extracts the tool call from the state.

115
00:07:57,480 --> 00:07:59,119
The tool name is identified.

116
00:07:59,119 --> 00:08:02,519
The tool parameters are selected based on the chosen tool.

117
00:08:02,519 --> 00:08:06,519
The result is wrapped in a tool message and passed back to the model.

118
00:08:06,519 --> 00:08:09,640
This process repeats until no more tool calls are made.

119
00:08:09,640 --> 00:08:12,959
At that point, the final response is generated.

120
00:08:12,959 --> 00:08:15,600
The graph reaches the end node.

121
00:08:15,600 --> 00:08:18,000
In this video, you learned that

122
00:08:18,000 --> 00:08:23,079
React agents perform step-by-step reasoning and use tools to answer complex queries.

123
00:08:23,079 --> 00:08:25,440
Their output follows a structured format.

124
00:08:25,440 --> 00:08:31,119
Thought ➝ action ➝ action input ➝ observation ➝ final answer.

125
00:08:31,119 --> 00:08:36,200
Tool results (observations) feed back into the reasoning process to guide next steps.

126
00:08:36,200 --> 00:08:41,359
LangGraph is used to implement the React workflow connecting reasoning and tool nodes.

127
00:08:41,359 --> 00:08:46,280
The process continues until a final answer is generated with no further tool calls required.