Why Your AI Agent Needs a Calculator
LLMs are bad at math. Fix that by giving them a deterministic calculator tool.
Dwizi Team
Editorial
Why Your AI Agent Needs a Calculator
Here is a fact that surprises many people: ChatGPT cannot do math.
It can pretend to do math. If you ask it "What is 2 + 2?", it will say "4". But it doesn't calculate "4"; it predicts that "4" is the word that usually follows "2 + 2 =".
This distinction doesn't matter for simple arithmetic, because the model has seen "2 + 2 = 4" billions of times in its training data. But what happens when you ask it something it hasn't seen?
"Calculate the monthly mortgage payment on a $543,200 loan at 6.13% interest for 27 years."
The model will confidently spit out a number. It will look like a number. It might even be close to the right number. But often, it is completely wrong. The model is hallucinating—guessing the digits based on probability, not calculation.
The Problem: The Poet Trying to be an Accountant
LLMs are like incredibly well-read poets. They are masters of language, nuance, and creativity. But you wouldn't ask a poet to do your taxes.
When you need precision—in finance, engineering, or science—probability is your enemy. You don't want the "most likely" answer; you want the correct answer. You need determinism.
The Solution: Offloading the Math
The solution is simple: Let the poet be the poet, and let a calculator be the calculator.
We can build a Dwizi tool called calculate_mortgage. When the AI encounters a math problem, it stops guessing. It extracts the numbers from the user's question, sends them to our tool, and waits for the precise result.
The Implementation
This tool uses zero external APIs. It has no secrets. It is pure, raw logic running in a secure sandbox.
/**
* Calculates the mortgage payment for a given loan.
*
* Description for LLM: "Use this whenever you need to calculate monthly payments. Do not estimate."
* We explicitly tell the LLM: "Do not estimate." This instruction is powerful.
* It overrides the model's tendency to just guess.
*/
type Input = {
principal: number;
rate: number; // Annual interest rate in percent (e.g. 5.5)
years: number;
};
export default async function calculateMortgage({ principal, rate, years }: Input) {
// 1. The Math
// This code runs on a CPU, not a Neural Network.
// It uses standard floating-point arithmetic.
// It is deterministic: the same input ALWAYS yields the same output.
const r = rate / 100 / 12;
const n = years * 12;
const payment = (principal * r * Math.pow(1 + r, n)) / (Math.pow(1 + r, n) - 1);
// 2. The Structured Return
// We don't just return the number. We return a breakdown.
// This allows the AI to give a much richer answer to the user.
// It can say "Your monthly payment is X, and over 30 years you'll pay Y in interest."
return {
monthlyPayment: Math.round(payment * 100) / 100,
totalPayment: Math.round(payment * n * 100) / 100,
totalInterest: Math.round((payment * n - principal) * 100) / 100,
};
}
Why Determinism Matters
In software, "determinism" means that if you run a function with the same inputs, you get the same outputs. Every single time.
LLMs are inherently non-deterministic. If you ask the same question twice, you might get different answers. This is great for creative writing (it's not boring!), but it's terrifying for finance.
By wrapping this logic in a Dwizi tool, you introduce a "island of determinism" into the sea of AI probability. You anchor the AI's response to reality.
The Execution Story
User: "Can I afford a $500k house? I make $5k a month and interest rates are like 6.5%."
Without the tool: The AI might say, "A $500k house at 6.5% is roughly $3,000 a month." (Is it? Maybe. It's guessing.)
With the tool:
- The AI analyzes the request. It extracts:
principal: 500000,rate: 6.5. It assumesyears: 30(standard). - It calls
calculate_mortgage. - The tool runs the formula and returns:
{ "monthlyPayment": 3160.34 } - The AI receives this exact number.
- Agent: "At 6.5% interest, your payment would be $3,160.34 per month. Since you make $5,000/mo, this would be over 60% of your income. That is likely unaffordable."
The advice is now grounded in fact. The poet used a calculator, and the result is wisdom.
Subscribe to Dwizi Blog
Get stories on the future of work, autonomous agents, and the infrastructure that powers them. No fluff.
We respect your inbox. Unsubscribe at any time.
Read Next
Currency Conversion (Determinism)
A simple tool that proves a big point: Why we need 'Islands of Truth' in a sea of hallucination.
Automating Client Emails with AI
Draft and send personalized emails directly from your AI agent using Resend.
Building a Slack Bot without a Server
How to let your AI agents post to Slack using a secure, serverless Dwizi tool.