In previous lessons, we learned how agents can reflect and use tools to interact with the outside world. But what about planning? How can agents determine the sequence of steps needed to achieve a larger goal?
This is where the Planning Pattern comes into play. It enables an LLM to break down a complex task into smaller, manageable subgoals without losing track of the overall objective.
The ReAct technique - short for Reason + Act - is a paradigmatic example of this pattern. In this lesson, you’ll learn how this technique works and implement a ReAct Agent from scratch using Python and Groq LLMs.
Ready? Let’s go! 👇
This is the third lesson of the Agentic Patterns from Scratch course. This lesson builds on the theory and code covered in the previous ones, so be sure to check them out if you haven’t already!
The ReAct System Prompt
Just like with the Tool Pattern, the ReAct technique also requires a System Prompt. This prompt is quite similar, but it also describes the ReAct loop to ensure the LLM understands the three operations it’s allowed to perform:
Thought - The LLM will think about which action to take
Action - The LLM will use a Tool to interact with the environment and take action
Observation - The LLM will observe the Tool’s output and reflect on the next step to take
Another key difference from the Tool Pattern System Prompt is that we’ll enclose all messages within tags, like this: < >.
While it’s possible to implement the ReAct logic without these tags, I’ve found that using them makes it easier for the LLM to follow the instructions.
Enough theory for now! Here’s the prompt 👇
If you take a look at the prompt, you’ll notice that we need a set of tools, which are enclosed within the <tools></tools> tags. The example we’ll build involves using three tools, as shown in the code snippet below.
Remember that the tool decorator - implemented in the previous lesson - allows us to automatically convert any Python function into a Tool.
Next, we simply concatenate the tool signatures and add them to the System Prompt.
With the System Prompt complete, it’s time to begin the first iteration of the ReAct loop!
ReAct Loop - Choosing the First Tool
The question we are going to answer is the following:
I want to calculate the sum of 1234 and 5678 and multiply the result by 5. Then, I want to take the logarithm of this result
As you can see, the question is easily solved using the provided tools in the correct order but, can the agent do the same? Let’s generate the first completion and find out.
The output generated is a <thought> and a <tool_call>, that tells the Agent which tool to use.
ReAct Loop - Running the First Tool
Since we have the <tool_call>, it’s very easy to evaluate the Tool to get the result - this is basically the action the agent is going to take.
The Tool result is just the sum of 1234 and 5678, that is 6912.
ReAct Loop - Choosing the Second Tool
If you look closely at the content we were adding to the chat_history, you’ll notice that we’re including the Tool result (6912) as an <observation>. This allows the LLM to think, once again, about the next Tool to use.
Running a completion as we did before, we should get something like this, where the LLM correctly selects the multiplication tool to multiply the previous result by 5.
ReAct Loop - Running the Second Tool
We proceed exactly the same way as before. The tool result in this case is 34560.
ReAct Loop - Choosing the Third Tool
After running the next completion, we’ll get the following output.
After having computed the sum and the multiplication, the only operation left is the logarithm, implemented in the compute_log tool.
ReAct Loop - Running the Third Tool and Final Output
After running the logarithm tool and adding the result to the chat_history, it’s time to run the last completion. The generated output will contain a <response> tag, which marks the end of the loop and contains the final answer.
Our LLM says 10.45. And that’s … correct! - if you don’t believe me, go ahead and check with your calculator 😂
The ReAct Agent
As we did in previous posts, you can achieve the same results using my agentic_patterns library, which implements the code above “the good way”.
I’ve created a ReactAgent class that encapsulates the ReAct loop, accepting a list of available tools. Check it out here!
If you prefer video lectures, I also have a YouTube video covering the Planning Pattern! 👇
That’s all for today! Next week, we’ll talk about the MultiAgent Pattern.
Happy planning! 👋
Miguel
Awesome article Miguel! Keep em' coming ;)