AI Invoice Exception Desk
Backend, AI integration, and full-stack development · Personal engineering project
An invoice review workspace that uses Gemini to extract invoice data, backend rules to flag exceptions, and an AI investigator to gather evidence for human review.
Overview
An invoice review workspace where AI extracts and investigates, rules flag exceptions, and a person decides.
I built a workspace for reviewing invoices with missing fields, mismatched totals, unknown suppliers, or possible duplicates. Gemini extracts structured invoice data, backend rules flag exceptions, and an AI investigator gathers supporting evidence for the reviewer. An operator can correct fields, rerun validation, and decide whether to reject or post the invoice. The project uses synthetic invoices, and its public demo is read-only and fixture-backed.
Gemini extractionBackend exception rulesAI investigatorHuman review and posting
A personal engineering project, built by me. Who owned what
My part
Backend, AI integration, and full-stack development, across the whole workflow from intake to ledger export.
- The workflow. I built the workflow from invoice intake through exception review and CSV ledger export.
- Gemini extraction. I integrated Gemini behind an extraction adapter and validated its structured response with Pydantic.
- Financial checks. I implemented Decimal-based checks for totals, tax, line items, and currency precision, along with supplier and duplicate checks.
- The investigator. I built the LangGraph investigation workflow and its PostgreSQL-backed worker, with checkpoints and read-only evidence tools.
- Automation and demo. I wired Gmail intake and Slack notifications through n8n and built a reproducible demo with synthetic invoices.
Architecture
Two connected flows: invoice processing, then an AI investigation for the cases that need one.
Invoice processing, from intake to a review status. A failed extraction can be retried. Live Gemini extraction is opt-in; by default the extraction adapter is a mock.
AI investigation
The investigation runs outside the API request. Live Gemini and MCP investigation is opt-in, and the evidence tools are read-only.
Approval and posting stay in the application workflow. Posting revalidates the current data and records an immutable snapshot in the internal ledger.
Decisions
Six choices, and what each one costs.
Gemini extracts and synthesizes; backend rules check the money
- Decision
- Use Gemini for document extraction and evidence synthesis. Keep financial checks in explicit backend rules.
- Trade-off
- Every financial check has to be written and maintained as a rule, and the model's output must pass validation before the rules can use it. In return, totals, tax and currency checks behave the same way on every run.
Queue investigations in PostgreSQL
- Decision
- Queue investigations as durable PostgreSQL jobs so multiple model and tool calls can run outside the API request.
- Trade-off
- A separate worker has to run alongside the API, and the report arrives later rather than in the response. The queue uses the database the application already has.
The investigator reads; the application approves and posts
- Decision
- Give the investigator read-only tools. Keep approval and posting in the application workflow.
- Trade-off
- The investigator can only gather evidence and report; a person still makes every posting decision. That is slower than letting the model act, and keeps it out of the ledger.
Two kinds of duplicate
- Decision
- Detect identical uploads with a file hash and possible business duplicates with invoice fields.
- Trade-off
- A file hash only catches byte-identical files. Field-based matching catches the same invoice sent again in a different file, but can match legitimate invoices that look alike, so it flags a possible duplicate for review.
Revalidate before posting, and keep what was posted
- Decision
- Revalidate before posting and retain a snapshot of the posted data.
- Trade-off
- Posting does the validation work again and can be refused if the data changed after review. The snapshot keeps the ledger's record fixed even if the invoice is edited later.
Mock adapters by default
- Decision
- Default to mock adapters so the workflow can be demonstrated without live credentials.
- Trade-off
- The demo runs anywhere, but mock output does not show how the live model handles a given invoice. Live Gemini extraction and live Gemini and MCP investigation are opt-in.
Skills and evidence
The stack, grouped by what each part does.
Frontend
- Next.js
- TypeScript
- Tailwind CSS
Backend
- FastAPI
- Pydantic
- SQLAlchemy
- Alembic
Persistence and jobs
- PostgreSQL
AI
- Gemini
- LangGraph
- MCP
Automation
- n8n
- Gmail
- Slack
Development and testing
- Docker Compose
- pytest
- Playwright
Outcomes
What was hard, and how the design handles it.
- Challenge
- Gemini could return data that failed schema validation.
- Solution
- Strict response parsing surfaces the failure and allows extraction to be retried.
- Challenge
- A validation error can identify a mismatch while leaving its cause unclear.
- Solution
- The investigator checks supplier, document, and financial evidence to help the operator review the case.
- Challenge
- Multi-step investigations can fail partway through.
- Solution
- A separate worker processes durable jobs with checkpoints.
- Challenge
- A public portfolio demo needs to work without credentials.
- Solution
- Frozen fixtures support a read-only walkthrough.