Problem: an assistant that reads invoices can extract fields well and still state a wrong total with full confidence. Result: a deterministic check that takes the fields the assistant extracted, compares them with trusted contract data and returns ok, mismatch or cannot_check, never a guess. Stack: Python 3.10+ standard library, Decimal for money (tested on 3.15.2). Boundaries: extraction from PDFs or photos is out of scope; the assistant does that part, this code only checks what it extracted
Why split extraction from checking
For me an AI assistant for business is an employee for one area with a clear input, a checkable output and rules in between. Document and number work fits that shape best. The weak spot is arithmetic: a language model produces fluent text, and fluent text is not proof that a sum is right
So the design splits the job. The model reads the document and fills in fields. Code does every comparison and calculation. The model can then explain the result to a person, but it never decides whether 1250 equals 1200
Input, processing, output
Input: a dict of fields the assistant extracted from an invoice, and a dict of trusted contract data
Processing: refuse to check if required fields are missing or the amount cannot be parsed; otherwise compare amount and bank account with the contract using Decimal
Output: a status plus either a list of issues or the reason the check could not run
The example
Contract data and invoices below are invented for illustration
from decimal import Decimal, InvalidOperation
def to_money(value):
try:
return Decimal(str(value).replace(" ", "").replace(",", "."))
except (InvalidOperation, AttributeError):
return None
def check_invoice(invoice: dict, contract: dict) -> dict:
"""invoice: fields extracted by the assistant; contract: trusted reference data"""
missing = [f for f in ("supplier", "amount", "account") if not invoice.get(f)]
if missing:
return {"status": "cannot_check", "why": "missing " + ", ".join(missing)}
amount = to_money(invoice["amount"])
if amount is None:
return {"status": "cannot_check", "why": f"unreadable amount {invoice['amount']!r}"}
issues = []
expected = Decimal(contract["amount"])
if amount != expected:
issues.append(f"amount {amount} != contract {expected} (diff {amount - expected})")
if invoice["account"] != contract["account"]:
issues.append("bank account differs from contract")
return {"status": "mismatch" if issues else "ok", "issues": issues}
if __name__ == "__main__":
contract = {"amount": "1200.00", "account": "ACC-001"}
cases = {
"matches": {"supplier": "Acme", "amount": "1 200,00", "account": "ACC-001"},
"wrong sum and account": {"supplier": "Acme", "amount": "1250", "account": "ACC-999"},
"no account": {"supplier": "Acme", "amount": "1200"},
"garbled amount": {"supplier": "Acme", "amount": "12OO", "account": "ACC-001"},
}
for name, inv in cases.items():
print(f"{name}: {check_invoice(inv, contract)}")
Output, exactly as printed when I ran it:
matches: {'status': 'ok', 'issues': []}
wrong sum and account: {'status': 'mismatch', 'issues': ['amount 1250 != contract 1200.00 (diff 50.00)', 'bank account differs from contract']}
no account: {'status': 'cannot_check', 'why': 'missing account'}
garbled amount: {'status': 'cannot_check', 'why': "unreadable amount '12OO'"}
The last case matters most. "12OO" with the letter O is a typical extraction error. A model asked to "check the invoice" might quietly read it as 1200 and report ok. The code refuses and says why
Design notes
Three statuses, not two. ok and mismatch are for documents that could be checked. cannot_check is for everything else, and it goes to a person with the original file attached. Collapsing cannot_check into mismatch hides extraction problems; collapsing it into ok is how wrong payments happen
Decimal, not float. Money compared as floats eventually produces a difference of 0.0000001 that someone has to explain. Decimal keeps the comparison exact and the diff readable
Normalise once, at the entrance. Spaces and decimal commas are handled in one function before any comparison. Every later rule works on clean values
The assistant explains, it does not decide. After the check, the model can turn the result into a short message: which invoice, what differs, what to do. Approving or paying stays with a person
Test the refusals, not only the matches. Most of the value of this check is in the cases where it says cannot_check. Keep broken examples in your test set permanently: a garbled amount, a missing account, an empty file. When you later change the extraction prompt or swap the model, those cases tell you immediately whether the assistant started guessing again
Log every decision with its input. A row that says mismatch is only useful if someone can see what the assistant extracted and what the contract said. Store both next to the status so a person can audit any line in seconds
Where this comes from
My own assistant Jes has been running on my server for 134 days. She runs 10 scheduled jobs on her own and hands tasks to 7 other bots. Four kinds of actions always wait for my explicit yes: deleting data, spending money, messaging clients and publishing. The same idea is behind this example: the assistant prepares, code verifies, a person decides where a mistake costs money
Errors and limits
The check is only as good as the contract data. If the reference amount is wrong, every invoice will be flagged or every wrong one will pass
It covers two fields. Real checks add due dates, VAT, line items and currency; each new rule should come with a test case, including a broken one
Extraction errors that produce a valid but wrong number, such as 1300 instead of 1200, will show up as mismatch, which is the right outcome: a person looks at the original
Reproduce it
- Save the code as
invoice.py - Run
python3 invoice.py - Compare with the four output lines above
- Change the contract amount to 1250 and run again: the second case should report only the account issue
The non-technical side of choosing a first role for an assistant is on my site: AI assistant for business: start with clear tasks
This article was created with the help of AI.



Top comments (0)