- the master
- Posts
- AI Can't Calculate [Here is the Agent Fix]
AI Can't Calculate [Here is the Agent Fix]
Difference between a chatbot and an AI agent? One can think. The other can DO. Here's the framework that bridges the gap.
In today’s edition, I will break down how AI Agents use tools and show you why it matters, and give you a practical way to think about agent tool use.
Today’s edition is based on my session at AI Engineer HQ
In today’s edition:
AI Deep Dive— How AI Agents Use Tools [ReAct Framework Explained]
Build Together— Here’s How I Can Help You
AI Engineer Headquaters - Join the Next Live Bootcamp starting 15th October 2025.
[Sponsor Spotlight]
AI Engineering Bootcamp
Go from concepts to deployed applications in 100 days.
Detailed Roadmap is here: https://god-level-python.notion.site/AI-Engineer-HQ-b3c98407b4ab45819811db081ae9d102
Financial Aid / Scholarship
[AI Deep Dive]
How AI Agents Use Tools [ReAct Framework Explained]

Most people think AI models can do everything from solving math problems to browsing the web.
But that’s not true.
LLMs are great at understanding and generating text, but they can’t calculate, browse, or fetch live data on their own.
If you are starting your learning journey for AI Agents, start here:
So how do we make an AI agent use external tools like a calculator, database, or API?
The answer lies in a simple but powerful framework called ReAct, short for Reason + Act.
Let’s dive in.
What Problem Are We Solving?
LLMs alone can’t perform actions like:
doing precise math
fetching live stock prices
searching the web
accessing a company database
They can only simulate reasoning based on language.
To solve this, we connect an LLM with external tools that actually execute these tasks.
That’s where the ReAct (Reason + Act) framework comes in.
Anthropic has a collection of engineering AI Agent guides.

Understanding the ReAct Framework
ReAct is a loop where the agent:
reasons for what it needs to do
acts by using a tool
observes the result
Then repeats the loop until it reaches the final answer.

This loop helps the agent break a complex problem into smaller logical steps, just like how we humans think.
Let’s take an example.
You’re asked to calculate the total cost of 3 pizzas at $18.75 each, plus a 15% tip.
A normal LLM might try to guess the total.
A ReAct agent will think step-by-step:
Reason: I need to find the cost of 3 pizzas.
Act: Use the calculator tool to multiply 18.75 × 3.
Observe: The result is 56.25.
Reason: Now add a 15% tip.
Act: Use calculator tool again for 56.25 × 0.15 = 8.44.
Observe: Total = 64.69.
That’s the ReAct loop in action, a reasoning chain with tool use.
6 Steps to Build an Agent That Uses a Tool
Here’s how the process works in practice (using Python and LangChain with Gemini):
1) Set Up the Environment
Create a virtual environment:
python3 -m venv venv
source venv/bin/activate
Install dependencies:
pip install langchain langchain-google-genai python-dotenv
Load your Gemini API key from .env
:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")
This authenticates your code with Google Gemini, allowing your agent to use the LLM.
You can get the API key on the left side of your dashboard for free.

2) Define the Tool
A tool is any function that performs a specific external task.
For example, let’s create a Calculator Tool using Python’s eval()
function:
from langchain.tools import tool
@tool
def calculator(expression: str) -> str:
try:
return str(eval(expression))
except Exception as e:
return f"Error: {e}"
This simple tool takes a math expression and gives the result just like a real calculator.
3) Select the LLM (Planner)
The planner is your LLM, it reasons about what to do next.
Here, we use Google’s Gemini model:
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="gemini-1.5-flash",
temperature=0
)
A low temperature (0) ensures more precise and consistent answers.
4) Design the Prompt
The prompt gives the agent clear instructions on how to behave.
It defines how to think, act, and observe while solving a problem.
Here’s a simplified version of the ReAct prompt:
You are an intelligent agent.
You can use the following tools: {tools}.
Follow this format:
Question: the input question
Thought: think about what to do
Action: the action to take (tool name)
Action Input: input to the tool
Observation: result of the action
Repeat until you know the final answer.
Final Answer: the final answer to the original question.
This structure teaches the LLM to reason step-by-step.
5) Create the Agent
Combine everything using LangChain’s built-in ReAct agent utilities:
from langchain.agents import create_react_agent, AgentExecutor
tools = [calculator]
prompt = PromptTemplate(template=template, input_variables=["input", "tools"])
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
6) Run the Agent
Finally, test your agent with a question:
question = "If a pizza costs $18.75, what’s the total for 3 plus 15% tip?"
result = executor.invoke({"input": question})
print(result["output"])
The agent will print every thought, action, and observation step showing exactly how it reasons.
Output:
Thought: I need to calculate 3 × 18.75
Action: calculator
Action Input: 18.75 * 3
Observation: 56.25
Thought: Add 15% tip
Action: calculator
Action Input: 56.25 + (56.25 * 0.15)
Observation: 64.69
Final Answer: $64.69
Access the whole codebase here:
Why This Matters
Tool use turns a language model into a true agent.

It stops being just a chatbot and starts functioning like a worker that can:
calculate precisely
fetch real data
call APIs
execute commands
For AI engineers and developers, understanding this workflow is crucial to building reliable, multi-step agents that can handle real-world logic.
Key Takeaways
llms can’t calculate or browse on their own
tools extend an agent’s capability beyond text
react = Reason + Act + Observe loop
langchain simplifies creating tool-using agents
keep your agents simple, one task per agent is better than one doing everything
What makes this powerful is not the code it’s the mindset.
When you give an LLM a structure to reason and act, you turn it from a talker into a doer.
That’s the foundation of all modern AI agents.
Want to work together? Here’s How I Can Help You
AI Engineering & Consulting (B2B) at Dextar—[Request a Brainstorm]
You are a leader?—Join [The Elite]
Become an AI Engineer in 2025—[AI Engineer HQ]
AI Training for Enterprise Team—[MasterDexter]
Get in front of 5000+ AI leaders & professionals—[Sponsor this Newsletter]
I use BeeHiiv to send this newsletter.
How satisfied are you with today's Newsletter?This will help me serve you better |
Be part of 50,000+ like-minded AI professionals across the platform
PS: What topic would you like me to write about next?
Reply