System design answer
Invoice extraction for an accounts team
Document parsing, schema-based extraction, code validation of every field, and a review screen for failures.
The question“Design a system that reads supplier invoices (PDF, photos, email) and posts them into the accounting system.”
1Clarify0-5 min
Ask before you draw. These answers change the design.
- How many invoices a day and in what formats (digital PDF, scans, phone photos)?
- Which fields are required, and what accuracy is needed?
- What happens today when an invoice is wrong?
- Which accounting system?
2Requirements and numbers5-8 min
It must
- Accept invoices from email, WhatsApp and a folder
- Extract supplier, GSTIN, number, date, lines, taxes, total
- Validate and post; send failures to review
And it must be
- At least 98% field accuracy on critical fields
- Nothing posted that fails validation
- Original kept for audit
Numbers to say out loud
- Volume
- 3,000 invoices a day, 30% scans or photos
- Pages
- Average 2 pages
- Review share
- Target under 10%
3High-level design8-12 min
- Email, WhatsApp, folder → Store the original
- Store the original → Get text (text layer, OCR or vision model)
- Get text (text layer, OCR or vision model) → Extract with a schema
- Extract with a schema → Validate (formats, arithmetic, GSTIN, duplicates)
- Validate (formats, arithmetic, GSTIN, duplicates) → passes → Post to accounting
- Post to accounting → Audit trail
- Validate (formats, arithmetic, GSTIN, duplicates) → fails → Review screen (image beside fields)
- Review screen (image beside fields) → corrected → Post to accounting
- Review screen (image beside fields) → corrected → New test case
4Deep dives12-26 min
Validation
Code checks every field: GSTIN format and checksum, date in range, line totals adding up to the invoice total within ₹1, tax rates valid, and the invoice number not already posted for that supplier.
Review screen
Shows the page image beside the extracted fields with failed checks in red; corrections are saved as new test cases, so accuracy improves on the suppliers that fail most.
5Failures26-30 min
| What can go wrong | What stops it |
|---|---|
| Wrong total posted | Arithmetic validation blocks it |
| Duplicate invoice | Supplier + number uniqueness check |
| Unreadable photo | OCR confidence threshold sends it to review |
6Evals and monitoring30-33 min
- 300 labelled invoices across suppliers and formats; accuracy per field
- Share sent to review and time per review
- Weekly sample of posted invoices audited
7Trade-offs33-35 min
- A vision model handles messy photos but costs more than OCR; route by document type
- Strict validation means more reviews but almost no wrong postings
Follow-ups to expect
- How would you handle a new supplier layout?
- What if the accounting API is down?