Inspiration
An AP clerk approves an invoice and the system records "approved". An auditor asks the only question that matters — why was this one approved? — and the answer is a row with a timestamp and a user id. That is not a control, it is a receipt. Automation moved the work; it did not move the evidence.
We wanted to know whether the evidence could be the product. So InvoiceFlow stores the reason behind every decision an agent makes: the natural-language reasoning, a confidence score, the exact rule that fired, the input and output of that step, and how long it took. Not a log line — a first-class, exportable artifact, one per decision, on every invoice.
What it does
Upload an invoice PDF or scan. Four agents run in a LangGraph pipeline:
- intake classifies the document ("is this actually an invoice?") and refuses packing slips, bank statements and remittance advice.
- extraction pulls vendor, invoice number, amount, currency, dates, PO number and line items, with per-field confidence.
- validation runs six deterministic checks in order — PO match, line-item compare, document identity, duplicate detection, amount and tax math, anomaly scan — and each one writes its own rule name, verdict and reasoning.
- approval routes by threshold: under $500 auto, $500–5,000 to a manager, $5,000–10,000 to an admin, above $10,000 dual approval; a new vendor always forces manual review even when the amount is small.
- the orchestrator finalizes the status and the payment is scheduled against a mock row.
Nothing is anonymous. Sign-in is required, and each of the three roles has real authority:
a manager settles up to $5,000, an admin up to $10,000, and anything above that needs two
distinct admins — the first signature leaves the request pending at 1 of 2. The required
authority is recomputed from the rules at the moment of decision, never read off the stored
reason string, and every human signature is written into the same decision trail as the agents
(agent_name: "human", the actor's id, name, role and signature index). A resolved request
cannot be settled twice and the same admin cannot sign twice. Visitors can sign in as a
read-only viewer and see everything without being able to move money.
Every step lands on a "confirmation slip" in the UI — a print-style record with the decision lines in order, the deciding rule, the confidence, the duration, and a deterministic seal derived from the rule and confidence. The record also serves the original document back (uploads are retained on the data volume, not deleted after extraction), lists the supplier with its derived history, and shows every decision ever taken on that invoice — agent or human.
The whole ledger is aggregated on a Transparency screen (decisions logged, rules fired, and one shared definition of automation: settled without a human signature), any record exports as JSON or PDF, and the entire register exports the same way. Vendors are derived from the invoices themselves — no extra table to drift — and a supplier with no settled history is marked as requiring verification, which is the same rule the approval agent uses to force manual review.
Try it: https://hermes-vm.tailfd1655.ts.net — sign in with one click as
[email protected] (admin), [email protected] (manager),
[email protected] (second admin) or [email protected] (read-only
viewer); password invoiceflow-demo-2026. Code: https://github.com/DawnofGenX/invoiceflow.
How we built it
Backend: FastAPI + LangGraph StateGraph, SQLAlchemy 2.x over SQLite, Tesseract for OCR,
sse-starlette for the live decision stream, reportlab for PDF export. LLM calls go through
one OpenAI-compatible gateway with per-agent model overrides, and a deterministic offline
stub (LLM_STUB=1) runs the entire pipeline with no network and no key — which is why the
test suite is reproducible by anyone who clones the repo.
Frontend: Next.js 16, React 19, Tailwind 4, hand-built primitives (no component library): the whole interface is a "security-print document control" world — safety-paper ground, guilloche rosette per decision that encodes the rule and confidence, ruled register, mono status tokens.
Auth: four roles over PBKDF2-SHA256 (600k iterations) password hashes, an HMAC-signed
stateless token delivered both as an httpOnly cookie and as a bearer header, and a role check
on every API route and every page — reads for any session, intake for manager and above,
seeding and maintenance for admins. No new dependency: hashlib and hmac from the standard
library.
Deployment: docker-compose (backend + frontend + nginx) on one VM, public ingress through a
Tailscale Funnel because the cloud firewall exposes only SSH. Production is same-origin — nginx
proxies /api — which is also why the session cookie works without a session table.
The part we are most deliberate about is the measurement. We built a 40-invoice corpus across eight defect classes (clean, duplicate, PO mismatch, line-total tamper, math error, unknown vendor, high value, non-invoice), five each, half rendered as scans through Tesseract and half as text-layer PDFs, with the expected outcome written down before the pipeline ever saw them. Then we ran it with a real LLM.
Challenges we ran into
- The corpus found real bugs, so we fixed them instead of reporting them. A line total that had been tampered with by 4.96% slipped through because the self-consistency check reused the 5% commercial tolerance meant for "a supplier may bill slightly over the PO". Those are different questions; they now have different tolerances. Two documents with no amount at all matched each other as duplicates. A document that labels itself "Invoice" but yields no payable total was being auto-rejected instead of held — rejecting is the wrong failure mode for a bad scan of a legitimate invoice. And documents that print tax but never add it to the total were being flagged as an unmatchable total instead of the specific defect they are.
- Honesty about a revised label. One scanned non-invoice at 200 dpi still has the word "Invoice" in its header, so no classifier can reject it. We originally pre-registered "reject" for it and changed the expectation to "hold" after the fact — and we report both numbers (40/40 as revised, 39/40 against the original expectation) rather than quietly keeping the flattering one.
- Explaining without inventing. Early versions of the UI generated a confidence for checks that produce no confidence, and a "pass rate" for rules the API only counts. Both were removed: where the data does not exist, the interface now says nothing.
- A cookie that quietly did not exist. Once every route was gated, the app worked in the
API but not in the browser: the frontend on
localhost:3031calling the API on127.0.0.1:8031is a cross-site request, soSameSite=Laxdropped the session cookie on every stream and export — and a Next.js trailing-slash redirect leaked it a second way by rewriting the request to an absolute backend URL. The fix was structural rather than a flag: a dev reverse proxy inside Next (/api/:path*→ backend) plus no-redirect route aliases on the server, so the browser only ever talks to one origin. Production had always been same-origin; the bug only existed because the local setup was not. - Dual approval is a state machine, not a checkbox. The first implementation let one admin settle a >$10k payment and minted a second payment row if the endpoint was called twice. It now recomputes the required authority per request, rejects a second decision with 409, rejects the same signer with 409, and reconstructs the signature list from the audit trail so the record cannot disagree with the log.
- Making the demo honest. The first recorded demo showed scenes where the interesting row — the duplicate rule, the matched line items — sat below the fold of the camera frame. We rewrote the recorder to frame by accessibility landmark, then had an independent agent re-verify every scene by OCR before shipping it.
Accomplishments that we're proud of
- 40/40 decisions (100%) and 232/240 extracted fields (96.7%) on the 40-document corpus with a real LLM — line items 45/45 rows, zero error statuses, 10.7 s per document.
- 20/20 decisions and 108/111 fields (97.3%) on a second corpus generated after the fixes and never used for tuning — the generalization check that most accuracy claims skip.
- A UI with zero critical or serious axe violations, zero horizontal overflow and zero console errors across seven routes — sign-in included — at 390/834/1440 px, verified by an independent agent against the public deployment, evidence committed in the repo.
- A 63-check end-to-end suite (signed in, over the real HTTP API) and 99 backend tests that assert the explainability contract itself — not just that an invoice was approved, but that the reason, rule, confidence and timing were written down for every step, and that the wrong role cannot settle it.
- The whole thing is MIT-licensed and runs offline with
LLM_STUB=1.
What we learned
- Explainability is a data-modelling problem before it is a UI problem. If the reasoning is not a stored column with a rule name beside it, no amount of interface polish recovers it.
- Tolerances are semantic. The same 5% means "commercially acceptable" on a PO and "the document contradicts itself" on its own totals; reusing one number for both is how a tampered invoice passes.
- A corpus you wrote yourself will find your bugs, and then it will stop finding them. The second, independent corpus is the only honest test of whether the first one taught you something general.
- Fail toward review. Auto-rejecting a document you cannot read is a silent data-loss bug wearing an accuracy metric.
What's next
- Close the remaining field gap: the six documents in the corpus that print no due date at all (truth expects the Net-30 convention) and the two scans where a printed tax line was not read.
- Calibrate the intake classifier on low-legibility scans instead of relying on the identity gate downstream.
- Real payment rails are deliberately out of scope for this build — money movement is
regulated and is not where the gap is. Likewise self-serve registration: the demo ships fixed
accounts (manager, two admins, viewer) and says so in
docs/AUTH.md, including the limits of a stateless session (no server-side revocation) that a real deployment would close.
Built with
python, fastapi, langgraph, next.js, react, typescript, tailwind css, sqlalchemy, sqlite, tesseract, docker, nginx, reportlab, llm, ai agents, accounts payable, explainable ai, ocr
Built With
- accounts-payable
- ai-agents
- docker
- explainable-ai
- fastapi
- langgraph
- llm
- next.js
- nginx
- ocr
- python
- react
- reportlab
- sqlalchemy
- sqlite
- tailwind-css
- tesseract
- typescript