Skip to content

How it's builtAI systems

Expense Auditor

A month of expense claims at a made-up design studio, run through all four kinds of AI in your browser: Tesseract's neural network reads each receipt, a naive Bayes model sorts it and robust statistics spot the odd ones, the studio's policy routes it for approval, and a small language model writes the month up for the owner. On the twelve sample photos it reads 60 of 60 fields right, sorts all twelve into the right category and catches all 6 planted problems.

The problem

At the end of each month someone at a small firm sits down with a pile of receipts: they type each one into a spreadsheet, decide what it was for, check it against the policy, chase the approvals and write the owner a note on where the money went. It takes days, and the mistakes that cost money are the easy ones to miss: the same dinner claimed twice, a bill ten times the usual, a weekend trip nobody signed off.

The client in this demo is Kovai Design Studio, a made-up 14-person studio in Coimbatore. The owner wants every receipt read, sorted into a category and checked against the policy, the odd ones sent to the right person, and a short summary of the month.

The data

Twelve receipts I made for the demo: a taxi, a fuel slip, a bus e-ticket, restaurant bills, stationery, a software renewal, a workshop fee and a courier docket, each in its own layout and typeface. I wrote them as HTML, rendered them with Playwright and photographed them on a desk in software, with soft shadows and uneven light. The fuel slip is crumpled and turned 3°, and one dinner bill is photographed twice, as if two people claimed it. The right answers are stored beside each one.

Six hundred past claims, October 2024 to September 2026, generated from a fixed seed: who claimed, the merchant, what was bought, the purpose, the category and the amount. Each person has their own habits, and restaurants and their dishes turn up under both meals and client entertainment, so the model has to read the purpose. They train the category model, set the usual amount for each category and each person, and give last month's totals.

The planted problems: a crew lunch over the meal cap, a software renewal over the finance limit, fuel with no project code, a bus ticket for a Saturday, a ₹9,800 paper bill where office supplies usually come to about ₹800, and the repeated dinner. The studio, its people, the shops and every figure are invented.

The model

Deep learning reads. Tesseract's LSTM recogniser, a recurrent neural network, reads each photo as WebAssembly in a Web Worker: the same self-hosted engine as my Invoice Reader, which prepares the photo first by scaling it, evening out the light and straightening it. Rules then find the fields: the merchant is the tallest text at the top, the total sits beside a label such as “Grand Total” or “Amount paid”, tax lines are added up, and the payment comes from a line such as “Paid by UPI”. The ₹ sign is not in the OCR's alphabet and often comes back as a 3; when the sub-total and the tax only add up to the total without it, the code takes it off.

Machine learning sorts and spots. Multinomial naive Bayes over the merchant, the items and the purpose picks one of seven categories. It trains in your browser on 480 past claims in a few milliseconds; its probabilities are softened with one temperature chosen on held-out claims, as naive Bayes is famously over-sure. Each amount gets a robust z-score: how far its logarithm sits from the median of past claims in the same category, and of the same person's, in median absolute deviations, so one huge bill in the past cannot hide the next. A near-duplicate is the same merchant (a Jaro-Winkler similarity of 0.9 or more, so an OCR slip does not hide it), the same amount and dates within three days.

Automation routes. The policy is seven rules: meals over ₹1,500 a day and weekend claims go to the manager, travel needs a project code, anything over ₹10,000 goes to the finance head, unusual amounts go to the manager, duplicates are rejected, and a receipt whose total or date could not be read goes to a person. You can change all but the last in the demo. Every rule that applies is listed with its reason, and the strictest decides. The approvers act on the board, the audit trail records every step and change, and the month downloads as a CSV that opens safely in a spreadsheet.

Generative AI writes. SmolLM2 360M Instruct gets the month's totals as a short list of facts and writes a paragraph for the owner. A model this small sometimes gets a number wrong, so every amount, count and percentage it writes is checked against the facts with my Copy Writer's fact check, and anything not in them is marked. Until you load it, a template writes the paragraph from the same facts, so the system is complete without the download.

Each stage hands plain data to the next: the OCR's fields go to the model, the category and the flags go to the rules, and the routes and totals go to the writer. The system map at the top of the demo shows what went in and out of each, how long it took and why.

The pipeline

  1. 01
    Read

    Tesseract's neural network reads each photo; rules find the merchant, date, total, tax and payment.

  2. 02
    Sort

    Naive Bayes picks the category from the merchant, the items and the purpose.

  3. 03
    Check

    Robust z-scores against the category and the person; Jaro-Winkler for repeats.

  4. 04
    Route

    The policy sends each claim to auto-approval, the manager, the finance head or rejection.

  5. 05
    Approve

    The approvers act on the board, the audit trail records it, and the month exports as a CSV.

  6. 06
    Summarise

    SmolLM2 writes the month up from the totals, and every number is checked.

Results

Fields read right
60 of 60
merchant, date, total, tax and payment on the 12 photos; 93% mean word confidence
Category model
94%
on 120 held-out claims; always guessing meals gets 24%
Receipts sorted right
12 of 12
from what the OCR read
Planted problems caught
6 of 6
with no false alarms on the other 6
Reading time
1.3 s
a receipt on average, measured in Edge

Measured when this page was built, with the same code the demo runs, from what Tesseract read from the twelve photos in Edge. On the design software receipt the OCR read the ₹ sign as a digit; the sums put it right. Last month's figures come from the generated history.

For a client

For a real firm I would take receipts where they already arrive: a WhatsApp number or an email inbox, straight from the phone. The claim would bring the employee and the project from the expense app, so only the receipt needs reading.

The category model would learn from the firm's own past claims, with their categories and ledger codes, and relearn each month from the approvers' corrections. The usual amounts would come from the same history, by person, category and season.

The policy would live where the finance team can change it without me, approvals would go out as messages with Approve and Reject buttons, and the month would land in their accounting software rather than a CSV. A larger language model could write the summary on a server, but the check on its numbers stays.

Stack

PartWhat it does hereLicence
Tesseract OCR and Tesseract.jsReads the receipts: the LSTM recogniser and its English model, self-hosted, in a Web WorkerApache 2.0
SmolLM2 360M InstructWrites the month's summary from the totalsApache 2.0
Transformers.jsRuns SmolLM2 in the browser, on the graphics chip where it canApache 2.0
My machine-learning libraryNaive Bayes, the robust statistics, Jaro-Winkler and the generated history, in TypeScript with unit testsMine
My Invoice Reader and Copy WriterThe image preparation and OCR plumbing; the fact check on the summary's numbersMine
PlaywrightRendered the sample receipts from HTMLApache 2.0
Preact and AstroThe interactive demo and the static page around itMIT

Privacy

Everything happens in your browser. The receipts, including any photo you add, are read in this tab and never uploaded: there is no server behind this page.

The OCR engine (about 7 MB) comes from this site and SmolLM2 from Hugging Face's public host; your browser keeps both for next time.

Limits

  • The receipts are made up and rendered cleanly. Real photos are blurrier, folded and shot at angles, and the OCR gets more wrong; every field stays editable, and the system re-checks and re-routes after each edit.
  • Merchants are matched by the name as printed. Matching them to the firm's list of suppliers would make the duplicate check stronger.
  • The category model has only seen this studio's invented history. On real claims it would need the firm's own data and a few weeks of corrections.
  • A small language model can still get a fact wrong. The check catches amounts, counts and percentages, not a wrong word around a right number, so the summary is a draft for a person to read.