Abdul MoizFractional CTO · Senior EngineerBook a free call

AI models

RAG Does Not Do Math, and That's Okay

Retrieval gets your model the right numbers. It does not make the model good at arithmetic. Here is how to stop pretending otherwise.

Updated · 4 min read

Fractional CTO · Senior Engineer
Q1 3.1Q2 4.2+68%+35%

Key takeaways

  • RAG finds the right numbers, but the language model still has to do the arithmetic, and that is where it fails.
  • Send every calculation to code: a calculator tool, a small Python function or a SQL query.
  • The model then explains a result it did not compute. Check the trace to confirm the tool was called.

A support engineer on a fintech product I advised on once pasted me a transcript that looked, at first glance, completely fine. A user asked a RAG-powered assistant: "What was our revenue growth from Q1 to Q2?" The bot retrieved the right earnings snippet, quoted the right two numbers, and then confidently reported a growth percentage that was wrong by almost double.

Nobody had done anything unusual. The retrieval step worked exactly as designed. The failure was assuming that because the model found the right facts, it could also reason correctly about them.

Which part should do the work?
QuestionUse
What was Q2 revenue?Retrieval
How much did revenue grow?Retrieval, then code
Total spend across all invoicesSQL on structured data
Why did revenue grow?Retrieval, then the model

What RAG actually promises

Retrieval-augmented generation solves one problem well: getting relevant, current, and specific text in front of a language model before it answers. That's it. It is a search-and-stuff pipeline — embed the query, pull the nearest chunks from a vector store, drop them into the context window, and let the model generate a response grounded in that text.

What it does not do is turn the underlying model into a calculator. The model is still predicting the next token based on patterns in its training data. Arithmetic, multi-step percentage calculations, unit conversions, and anything involving carrying digits across several steps are exactly the class of task large language models are structurally bad at, retrieval or not.

Why LLM math errors surprise people

It's easy to conflate "has the right inputs" with "produces the right output," because for most RAG use cases — summarization, Q&A over docs, support ticket triage — having the right inputs is basically the whole job. The generation step is just rephrasing. So teams ship a RAG bot, watch it nail a dozen qualitative questions, and assume the quantitative ones will follow the same reliability curve. They don't. A model can retrieve "$4.2M" and "$3.1M" perfectly and still botch (4.2 - 3.1) / 3.1.

How to fix RAG math errors: use a tool, not a better retriever

I've watched teams respond to this by re-chunking documents, upgrading embedding models, or adding more context — none of which touch the actual bug, because the retrieval was never broken. The fix is to stop asking the language model to do the math at all.

python
# Instead of asking the LLM to compute this inline...
prompt = f"Revenue was {q1} in Q1 and {q2} in Q2. What was the growth rate?"

# ...extract the operands and hand the arithmetic to code.
def growth_rate(q1: float, q2: float) -> float:
    return (q2 - q1) / q1

answer = growth_rate(q1, q2)
response = f"Revenue grew {answer:.1%} quarter over quarter."

In practice this means treating numeric reasoning as a tool call, not a generation task. Give the model function-calling access to a calculator, a small Python sandbox, or — if the numbers live in a database — let it generate a SQL aggregation instead of eyeballing rows of retrieved text. (If the numbers live in documents, extract them into structured data first.) The retrieved chunks become the source of truth for values; a deterministic function becomes the source of truth for computation. The LLM's job shrinks to the part it's actually good at: figuring out which values are relevant and phrasing the final answer in plain language.

A rule of thumb for RAG and calculations

If a question can be answered by finding a sentence, RAG alone is fine. If a question requires combining two or more retrieved numbers with an operation — a ratio, a delta, a sum across rows, a currency conversion — assume the model will get it wrong some nonzero percentage of the time, and route that step to code. It costs one extra function definition and saves you from shipping a bot that sounds authoritative while being quietly, confidently incorrect.

RAG is a retrieval architecture, not a reasoning engine. Once a team internalizes that distinction, the fix is usually a day of work, not a redesign.

What the evidence says about code execution

A 2025 study of medical calculations tested this directly. Handing the arithmetic to Python cut arithmetic errors by roughly 83%. Retrieval helped with a different problem, finding the right formula and criteria. The best results came from using both, which matches the split above: retrieval for values, code for computation.

Making sure the tool is actually used

Adding a calculator is not enough, because models sometimes skip it and answer directly. Four checks keep them honest:

  1. Say it plainly in the instructions. Any number that is not copied from a source must come from the tool.
  2. Check the trace. If the answer contains a computed number and there was no tool call, reject it and retry.
  3. Return the working. Ask for the operands, their sources and the formula with the result, so a person can verify it.
  4. Keep numeric cases in your test set. Include negative growth, different units, missing quarters and division by zero.

That last one belongs in the checklist to run before trusting any AI system.

Common questions

Why do LLMs make arithmetic mistakes?

A language model predicts the next piece of text. It does not run a calculation unless it calls a tool that does. Retrieval improves the inputs, but it does not change how the answer is produced.

How do I stop a RAG chatbot from getting calculations wrong?

Give the model a calculator or code tool, tell it that calculations must use the tool, and check the trace to confirm the tool was called. Refuse or retry an answer that contains a computed number without a tool call.

Will a bigger model fix RAG math errors?

It may make fewer mistakes, but it will still make some, and they will look just as confident. Code gives the same correct result every time and costs very little to run.

Sources and further reading

Abdul MoizFractional CTOSenior Engineer

About the author

I build production-grade AI applications, AI agents and data systems, fix AI-built apps, and help teams as a fractional CTO. I've spent seven years building software people rely on, including a media platform that grew to 2M+ users.

ShareXLinkedIn

Keep reading

Working on something like this?

Tell me about it on a free 30-minute call.