The Fundamental Flaw in AI Math
Large language models predict the next most likely token. When given a restaurant bill image with five items, tax, and a service charge, a model can accurately extract line items like 'Grilled Salmon - $28.50'.
However, if you ask the model to calculate each person's share including a 15% tip and pro-rated tax, it will generate an answer that looks convincing but is mathematically inconsistent. Rounding cents across multiple participants is an exact constraint problem, not a next-token prediction task.
The Architecture: Probabilistic Parsing, Deterministic Calculation
In our Flutter app SettleTab, we engineered a clean separation of concerns:
1. Multimodal AI Extraction (Server-Side): An Edge Function receives the receipt image, calls the vision model, and returns an array of raw item names and prices conforming strictly to a Zod schema.
2. Human Review Screen (Client-Side): The user sees the extracted line items side-by-side with the receipt image and can edit prices, add missed items, or merge duplicates.
3. Deterministic Financial Engine (Client-Side Dart): All arithmetic—pro-rated tax distribution, tip percentages, item assignments, and penny-rounding reconciliation—is executed by deterministic application code.
// Edge Function: Strict Schema Validation
import { z } from "zod";
const ReceiptExtractionSchema = z.object({
merchantName: z.string().optional(),
date: z.string().optional(),
items: z.array(
z.object({
description: z.string().min(1),
priceInCents: z.number().int().nonnegative(),
})
),
detectedTaxInCents: z.number().int().optional(),
detectedTotalInCents: z.number().int().optional(),
});Fail-Safe Design: When AI Fails Completely
Receipts are often faded, crumpled, or poorly lit. If the model extraction times out or returns an unparseable response, your product must not break. In SettleTab, extraction failure cleanly falls back to manual entry with the photo pinned to the top of the screen.
The user is never blocked. AI accelerates the happy path by 80%, but deterministic fallback logic guarantees 100% completion even when the model fails.
Key Takeaway for Product Teams
AI should be used to turn unstructured, messy real-world inputs into structured data proposals. Application code must handle business rules, access control, and mathematical accuracy. When you separate intelligence from calculation, you get the best of both worlds.