Back to Blog

Why Your AI's Tax Total Doesn't Match the Invoices

Asked for sales tax and got $165.83. The invoices say $166.00. Each line's tax was rounded to the cent and then added. Rounding the grand total instead is a different number, and both queries succeed.

You ask: "What was sales tax in March?" The AI says $165.83. The invoices add up to $166.00. There are 200 lines of $10.05, taxed at 8.25%.

SELECT ROUND(SUM(line_amount * 0.0825), 2) AS tax
FROM invoice_lines

One line of tax is 10.05 * 0.0825 = 0.829125, which rounds to $0.83. The invoices charged $0.83 two hundred times: $166.00. The AI added the unrounded tax first. 200 * 0.829125 = 165.825, which rounds to $165.83.

The rounding has to happen where the money did

Both queries are legal, and they answer different procedures.

-- what the invoice did: round the line, then add
SELECT SUM(ROUND(line_amount * 0.0825, 2)) AS tax_on_the_invoices
FROM invoice_lines

-- what a summary does: add, then round
SELECT ROUND(SUM(line_amount * 0.0825), 2) AS tax_on_the_total
FROM invoice_lines

$166.00 and $165.83. The $0.17 is the leftover fractions. They do not cancel, because each line was pushed to the nearest cent on its own, and more of them were pushed up than down. Add more lines and the gap grows. It never throws an error, and it looks like a FLOAT total drifting by cents. The column can be exact numeric and this still happens. The ROUND is in the query.

The same split hits card fees. A fee of 2.9% plus $0.30 is computed per charge and rounded per charge. ROUND(SUM(...), 2) computes one fee for the whole month. The processor did not.

This is also not cents stored as integers and read as dollars. That one is off by 100×. This one is off by a fraction of a cent, times the number of lines.

How to catch it

Compute both, on the AI's filter.

SELECT SUM(ROUND(line_amount * 0.0825, 2)) AS per_line,
       ROUND(SUM(line_amount * 0.0825), 2) AS on_total
FROM invoice_lines
WHERE <the filter the AI used>

If per_line matches the invoices and on_total matches the AI, the AI rounded the total. Show one raw line next to ROUND of that line so the rate and the precision are visible. If a line lands on exactly half a cent, compare that single line to the application before you blame the total.

The prompt that heads this off

When I ask for tax, fees, or anything billed per line, round each line
to the cent and then sum. Do not round the grand total and present it
as the billed amount. Show me one raw line, the rounded line, and both
totals if they differ.

The short version

Rounding each line and then adding is what the invoices did. Adding and then rounding is a different number. The gap is fractions of a cent, accumulated, and it will not match the paperwork unless you round where the paperwork did.


Read-only MCP gateway: mcpserver.design.

Related reading: