Inspiration
A supermarket receipt is the one bill nobody reads line by line. Two identical lines from a double scan, a "total" that does not equal the sum of its parts, a promo that never came off — all of it is visible on the paper, and almost nobody checks. The arithmetic is trivial; the friction is that you have to sit down and do it.
So the useful version of this app is not "an AI that reads receipts". It is "an audit you would actually run in the ten metres between the till and the door". That reframing decided everything that follows: it has to be fast, it has to work without a signal in a basement supermarket, and the photo — which contains where you shop, when, and what you buy — should not be uploaded to anyone's server.
What it does
Take a photo of a receipt. In about thirteen seconds you get the item lines as structured data, plus an audit: identical line items grouped and flagged as probable double scans, the item sum compared against the printed total, and the amount you were plausibly overcharged.
The report is deliberately narrow. It only does arithmetic on what is printed — it never guesses what a product "should" have cost. That is a design decision, not a missing feature: a market-price guess is exactly the kind of output that looks clever and is wrong, and it would turn a defensible audit into an argument with the cashier.
The scan is the free part. Unlocking the full report — the line-by-line breakdown with the overcharge figure — costs ¥0.5 through a real Alipay trade, which is the app's actual business model: the check has to be cheaper than the overcharge it finds, or nobody runs it at the door.
How we built it
The AI is Firebase AI Logic with the hybrid inference API, pinned to on-device execution:
app/hybrid-ai.js
-> import { getAI, getGenerativeModel, InferenceMode, GoogleAIBackend }
-> ai.getGenerativeModel(instance, {
mode: InferenceMode.ONLY_ON_DEVICE,
onDeviceParams: { createOptions: {
initialPrompts: [{ role: 'system', content: SYSTEM_PROMPT }],
expectedInputs: [{ type: 'text', languages: ['en'] }, { type: 'image' }],
expectedOutputs: [{ type: 'text', languages: ['en'] }]
}}
})
-> model.generateContent([{ text: USER_PROMPT }, { inlineData: { mimeType, data } }])
-> result.inferenceSource // proves which engine actually answered
That call runs against Chrome's built-in Gemini Nano through the Prompt API. No bundler, no build step: the app is plain ES modules served by a small Node/Express server, and the SDK arrives via a dynamic import from gstatic.
Three things sit around the model call:
A prompt contract, not a chat. The system instruction pins the output to one JSON shape and tells the model to emit null rather than invent a value it cannot read. Parsing is strict; a response that does not parse is treated as a failure, not coaxed into shape.
One audit implementation, shared. auditReceipt() lives in app/audit.js and is imported by both the browser bundle and the Node server. The duplicate-line grouping and the total reconciliation are the product; having two drifting copies of them — one in the page, one on the server — is how this class of app usually breaks.
An honest fallback. If the browser does not expose an on-device model (Safari, older Chrome, a locked-down corporate machine), the same photo goes to POST /api/scan on the server. The UI badge always states which path ran — "⚡ On-device · Gemini Nano" or "☁️ Server-side model" — and that badge is driven by result.inferenceSource, not by a guess.
App Check is registered with a Google Cloud Fraud Defense key, and the Firebase project stays on the free Spark plan.
Challenges
The on-device path is the only path we could ship, so we made that the design. The project has no billing account: the cloud Gemini endpoint returns "the free tier is not available for this project", and the card we have cannot be attached to Google Payments. Rather than fake a cloud leg, we pinned ONLY_ON_DEVICE and moved the fallback to our own server proxy. The hybrid API is what made that a one-line decision — PREFER_ON_DEVICE, ONLY_ON_DEVICE, PREFER_IN_CLOUD and ONLY_IN_CLOUD are enum values on the same getGenerativeModel call, so the deployment target is a configuration choice rather than a rewrite. We would rather submit a real on-device app with an honest fallback than a hybrid claim we could not run.
Getting App Check to stop blocking us, and then finding out it did not matter. Firebase has required App Check for AI Logic since 2026-11-02, and reCAPTCHA Classic keys silently fail to register now — the Fraud Defense key was the only thing that worked. The token is also bound to the exact origin string, so 127.0.0.1 and localhost behave differently. Then, to size the risk for the deployed domain, we deliberately set an invalid site key: on-device inference still returned a correct report. The token exchange only gates the cloud path. That is worth knowing before you spend an evening on a domain allowlist.
Reading a receipt is harder than OCR. Thermal receipts are low-contrast, wraparound item names, and a total that may or may not include tax. The prompt had to forbid the model from "fixing" a total that looks wrong — the mismatch is the finding, not a bug to smooth over.
Accomplishments that we're proud of
A measured, end-to-end on-device run on the deployed site, not a local demo:
- a 5-line receipt read in ~13 s, all 5 items correct, printed total ¥128.50 matched
- the duplicated "Eggs 30-pack" line flagged automatically, overcharge computed as −¥18.50
- DevTools resource log during that scan: zero requests to generativelanguage.googleapis.com and one App Check token exchange — the evidence that the photo really stayed local
- the audit module is genuinely shared: the same auditReceipt() produces the browser report and the server response, so the two paths cannot disagree
- a real ¥0.5 Alipay trade closes the loop, with RSA2 signature verification on the async notify
What we learned
On-device inference changes what you are willing to ask a user to hand over. Once the photo does not leave the device, "upload your receipt" stops being a privacy trade and becomes a normal thing to do at a till — and that, not the model quality, is what makes this app plausible.
We also learned to treat the fallback as a product surface. Users forgive a slower path; they do not forgive a badge that lies about which path ran.
What's next
- Android with Gemini Nano 4 through the same hybrid SDK — a till-side check belongs on the phone that took the photo, and a single hybrid call site is the reason this is a port rather than a rewrite
- a household view: the same audit across a month of receipts, still local, to catch a shop that double-scans habitually rather than once
- restore PREFER_ON_DEVICE with a cloud handoff for the genuinely hard cases (crumpled, faded, or photographed at an angle), which needs a billing account and nothing else in the code
Built With
- alipay
- chrome-prompt-api
- css
- express.js
- firebase
- firebase-ai-logic
- firebase-app-check
- gemini-nano
- html
- javascript
- node.js