Match supplier invoices to purchase orders.
Done by an agent. Checked twice.
Every invoice is cleared to pay or held with the exact reason, before any money moves.
Real files in
- invoices.csv
Ten supplier invoices, six with a problem planted in them. 11 lines, 609 bytes. - purchase_orders.csv
Seven purchase orders. 8 lines, 402 bytes.
Sample data written by Atris. Not a real company.
Three steps
- 1.Read the orders and the invoices
- 2.Decide each invoice
- 3.Write the result
Claude Sonnet 5.5, 34 seconds. No web. It never saw the check.
Checked twice
- Passed: 10 of 10 invoices appear once in matches.csv
- Passed: 10 of 10 statuses equal the status recomputed from the input files
- Passed: 10 of 10 amounts equal quantity times unit price
- Passed: summary.md states 4 invoices cleared to pay and their total, 7,435.50
- Passed: summary.md states 6 invoices held and their total, 10,048.60
- Passed: 6 of 6 held invoices are named in summary.md
- Passed: 12 of 12 figures in summary.md equal a figure of the invoice named on that line or a total recomputed from the input files
- Passed: 11 of 11 numbers quoted in the reasons are numbers of that invoice or its order
Second review by Claude Opus 5.5: pass.
Then we broke a copy of the output 6 ways on purpose. The check caught 6.
- Clear to pay: 4 invoices, total 7,435.50 - Held: 6 invoices, total 10,048.60 ## Held invoices - INV-7002 (hold_price, 1410.00): Invoiced unit price 235.00 differs from ordered unit price 215.00. - INV-7004 (hold_quantity, 1258.60): Invoiced quantity 14 differs from ordered quantity 12. - INV-7006 (hold_duplicate, 3600.00): PO-1043 was already billed by INV-7003. - INV-7008 (hold_no_po, 640.00): No po_number on the invoice. - INV-7009 (hold_supplier, 1290.00): Invoice supplier "Orchard IT Supplies Ltd" differs from order supplier "Orchard IT Supply". - INV-7010 (hold_no_po, 1850.00): PO-1099 is not in the purchase orders file.
How this was run
An AI agent on Claude Sonnet 5.5 was given a folder with the files below and a three-step flow, and nothing else. It had no web access and never saw the check. The check was written before the run and made stricter after it. It works the answer out again from the input files. Then Claude Opus 5.5 reviewed the result against the inputs. The input files are sample data written by Atris, with the problems a real week has planted in them.
Run 1 of 1 on 1 December 2026. The agent took 34 seconds.
What the second review verified
- Recomputed all 10 amounts from the invoices, e.g. INV-7004 14 × 89.90 = 1258.60 and INV-7007 18 × 24.75 = 445.50. All match matches.csv.
- Summary totals match the CSV: clear 1540.00 + 3600.00 + 1850.00 + 445.50 = 7,435.50 across 4 invoices, and held 1410.00 + 1258.60 + 3600.00 + 640.00 + 1290.00 + 1850.00 = 10,048.60 across 6 invoices.
- Statuses follow the required order (no PO, supplier, duplicate, quantity, price): INV-7002 unit price 235.00 vs 215.00 on the order; INV-7004 quantity 14 vs 12; INV-7006 is the second bill on PO-1043 after INV-7003 (2026-11-05); INV-7008 has a blank PO; INV-7010 uses PO-1099, which is not in the orders file.
- INV-7009 is held for supplier, which is correct: 'Orchard IT Supplies Ltd' on the invoice vs 'Orchard IT Supply' on PO-1047. All 10 invoices appear exactly once, and every held invoice has its own line in summary.md.
What this run does not show
- The ten invoices were written so each has one clear answer, while a real week has part deliveries, credit notes and prices agreed by phone.
- The check compares the numbers in each reason with the invoice and its order, so the wording is judged only by the second review.
- A wrong number that happens to equal another number of the same invoice would still pass.
Would the check catch a wrong answer?
A check that cannot fail proves nothing. After the run we took a copy of the output, broke it one small way at a time and ran the check again. It caught 6 of 6. The check was made stricter after the first try at this, and the same untouched output was graded again.
Open every file
flow.md · the three steps the agent followed
# Match supplier invoices to purchase orders function: finance for: the person who approves supplier payments goal: Every invoice is either cleared to pay or held with the exact reason, before any money moves. ## 1. Read the orders and the invoices Read input/purchase_orders.csv and input/invoices.csv. An invoice matches an order when the po_number exists, the supplier is the same, and quantity and unit price are both exactly equal to the order. ## 2. Decide each invoice Give every invoice exactly one status: - `clear` when it matches its order. - `hold_no_po` when the po_number is missing or not in the orders file. - `hold_duplicate` when an earlier invoice (by invoice_date, then invoice_id) already billed the same po_number. The earlier one is judged on its own. - `hold_quantity` when the quantity differs from the order. - `hold_price` when the unit price differs from the order. - `hold_supplier` when the supplier differs from the order. Check in this order: no_po, supplier, duplicate, quantity, price. Use the first that applies. ## 3. Write the result Write output/matches.csv with columns invoice_id,status,amount,reason. amount is quantity times unit price from the invoice, two decimals. reason is one plain sentence with the numbers that differ. Write output/summary.md for the approver: how many clear and the total to pay, how many held and the total held, then one line per held invoice. No invoice may be left out. ## Done looks like - Every invoice appears once in matches.csv. - The totals in summary.md equal the sums in matches.csv.
input/invoices.csv · 11 lines, 609 bytes
invoice_id,invoice_date,supplier,po_number,quantity,unit_price INV-7001,2026-11-03,Harlow Paper Co,PO-1041,40,38.50 INV-7002,2026-11-04,Brightline Freight,PO-1042,6,235.00 INV-7003,2026-11-05,Kestrel Software,PO-1043,25,144.00 INV-7004,2026-11-06,Harlow Paper Co,PO-1044,14,89.90 INV-7005,2026-11-07,Meridian Cleaning,PO-1045,1,1850.00 INV-7006,2026-11-10,Kestrel Software,PO-1043,25,144.00 INV-7007,2026-11-11,Dunmore Catering,PO-1046,18,24.75 INV-7008,2026-11-12,Northgate Signs,,1,640.00 INV-7009,2026-11-13,Orchard IT Supplies Ltd,PO-1047,10,129.00 INV-7010,2026-11-14,Meridian Cleaning,PO-1099,1,1850.00
input/purchase_orders.csv · 8 lines, 402 bytes
po_number,supplier,item,quantity,unit_price PO-1041,Harlow Paper Co,Copy paper cases,40,38.50 PO-1042,Brightline Freight,Pallet delivery,6,215.00 PO-1043,Kestrel Software,Annual licenses,25,144.00 PO-1044,Harlow Paper Co,Toner cartridges,12,89.90 PO-1045,Meridian Cleaning,Monthly office cleaning,1,1850.00 PO-1046,Dunmore Catering,Board lunch,18,24.75 PO-1047,Orchard IT Supply,Laptop docks,10,129.00
output/matches.csv · written by the agent, 11 lines
invoice_id,status,amount,reason INV-7001,clear,1540.00,"Matches PO-1041 on supplier, quantity 40 and unit price 38.50." INV-7002,hold_price,1410.00,Invoiced unit price 235.00 differs from ordered unit price 215.00. INV-7003,clear,3600.00,"Matches PO-1043 on supplier, quantity 25 and unit price 144.00." INV-7004,hold_quantity,1258.60,Invoiced quantity 14 differs from ordered quantity 12. INV-7005,clear,1850.00,"Matches PO-1045 on supplier, quantity 1 and unit price 1850.00." INV-7006,hold_duplicate,3600.00,PO-1043 was already billed by INV-7003. INV-7007,clear,445.50,"Matches PO-1046 on supplier, quantity 18 and unit price 24.75." INV-7008,hold_no_po,640.00,No po_number on the invoice. INV-7009,hold_supplier,1290.00,"Invoice supplier ""Orchard IT Supplies Ltd"" differs from order supplier ""Orchard IT Supply""." INV-7010,hold_no_po,1850.00,PO-1099 is not in the purchase orders file.
output/summary.md · written by the agent, 18 lines
# Invoice match summary - Clear to pay: 4 invoices, total 7,435.50 - Held: 6 invoices, total 10,048.60 ## Held invoices - INV-7002 (hold_price, 1410.00): Invoiced unit price 235.00 differs from ordered unit price 215.00. - INV-7004 (hold_quantity, 1258.60): Invoiced quantity 14 differs from ordered quantity 12. - INV-7006 (hold_duplicate, 3600.00): PO-1043 was already billed by INV-7003. - INV-7008 (hold_no_po, 640.00): No po_number on the invoice. - INV-7009 (hold_supplier, 1290.00): Invoice supplier "Orchard IT Supplies Ltd" differs from order supplier "Orchard IT Supply". - INV-7010 (hold_no_po, 1850.00): PO-1099 is not in the purchase orders file. ## Notes - Currency is not stated in the input files; amounts are plain numbers. - INV-7009 is held for supplier name mismatch only; its quantity and price match PO-1047.
check.py · the check, written before the run
"""Recomputes every status from the input files and compares it with what the agent wrote."""
import csv, re, sys
from decimal import Decimal
from pathlib import Path
root = Path(sys.argv[1]); ok = True
def say(passed, text):
global ok; ok = ok and passed; print(("PASS " if passed else "FAIL ") + text)
# Figures in the written files. A figure is a number that is not part of an id or a date.
FIG = re.compile(r"(?<![\w.])(?<!\w-)(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?(?!\w|-\w)")
DAY = re.compile(r"\d{4}-\d{2}-\d{2}")
def dec(x): return abs(Decimal(str(x).replace(",", "").replace("$", "").strip()))
def figs(line): return [dec(m.group(0)) for m in FIG.finditer(line)]
def together(text, *vals):
"""True when one line of the text carries every one of these figures."""
return any(all(dec(v) in figs(ln) for v in vals) for ln in text.splitlines())
def writeup(text, id_re, rec, glob):
"""Every figure of two or more characters and every date must belong to a record named on its line
or to the facts that hold for the whole file. Returns how many were seen and the ones that do not."""
seen, bad = 0, []
for ln in text.splitlines():
allow = set(glob).union(*(rec[i] for i in re.findall(id_re, ln) if i in rec))
for t in [m.group(0) for m in FIG.finditer(ln) if len(m.group(0)) > 1] + DAY.findall(ln):
seen += 1
if (t if DAY.fullmatch(t) else dec(t)) not in allow: bad.append(t)
return seen, bad
pos = {r["po_number"]: r for r in csv.DictReader((root/"input/purchase_orders.csv").open())}
invs = list(csv.DictReader((root/"input/invoices.csv").open()))
want, seen = {}, set()
for inv in sorted(invs, key=lambda r: (r["invoice_date"], r["invoice_id"])):
po = pos.get(inv["po_number"])
if not po: st = "hold_no_po"
elif inv["supplier"] != po["supplier"]: st = "hold_supplier"
elif inv["po_number"] in seen: st = "hold_duplicate"
elif float(inv["quantity"]) != float(po["quantity"]): st = "hold_quantity"
elif float(inv["unit_price"]) != float(po["unit_price"]): st = "hold_price"
else: st = "clear"
if po and inv["supplier"] == po["supplier"]: seen.add(inv["po_number"])
want[inv["invoice_id"]] = (st, round(float(inv["quantity"]) * float(inv["unit_price"]), 2))
path = root/"output/matches.csv"
if not path.exists():
say(False, "output/matches.csv was not written"); sys.exit(1)
rows = list(csv.DictReader(path.open()))
got = {r["invoice_id"]: r for r in rows}
say(set(got) == set(want) and len(rows) == len(want), f"{len(set(got) & set(want))} of {len(want)} invoices appear once in matches.csv")
right = sum(1 for k, (st, _) in want.items() if got.get(k, {}).get("status") == st)
say(right == len(want), f"{right} of {len(want)} statuses equal the status recomputed from the input files")
amt = sum(1 for k, (_, a) in want.items() if k in got and abs(float(got[k].get("amount") or 0) - a) < 0.005)
say(amt == len(want), f"{amt} of {len(want)} amounts equal quantity times unit price")
clear = round(sum(a for st, a in want.values() if st == "clear"), 2)
held = round(sum(a for st, a in want.values() if st != "clear"), 2)
summary = (root/"output/summary.md").read_text() if (root/"output/summary.md").exists() else ""
nums = {round(float(n.replace(",", "")), 2) for n in re.findall(r"\d[\d,]*\.\d{2}", summary)}
n_clear = sum(1 for st, _ in want.values() if st == "clear"); n_held = len(want) - n_clear
say(clear in nums and together(summary, clear, n_clear), f"summary.md states {n_clear} invoices cleared to pay and their total, {clear:,.2f}")
say(held in nums and together(summary, held, n_held), f"summary.md states {n_held} invoices held and their total, {held:,.2f}")
missing = [k for k, (st, _) in want.items() if st != "clear" and k not in summary]
say(not missing, f"{sum(1 for st, _ in want.values() if st != 'clear') - len(missing)} of {sum(1 for st, _ in want.values() if st != 'clear')} held invoices are named in summary.md")
# every number written beside an invoice must be a number of that invoice or of its order
rec = {}
for inv in invs:
po = pos.get(inv["po_number"], {})
rec[inv["invoice_id"]] = {dec(x) for x in (inv["quantity"], inv["unit_price"], want[inv["invoice_id"]][1], po.get("quantity"), po.get("unit_price")) if x not in (None, "")}
rec[inv["invoice_id"]] |= {inv["invoice_date"]}
if po: rec[inv["invoice_id"]].add(dec(round(float(po["quantity"]) * float(po["unit_price"]), 2)))
counts = {}
for st, _ in want.values(): counts[st] = counts.get(st, 0) + 1
facts = {dec(clear), dec(held), dec(round(clear + held, 2)), dec(len(want)), dec(len(pos))} | {dec(v) for v in counts.values()}
seen, bad = writeup(summary, r"INV-\d+", rec, facts)
say(seen > 0 and not bad, f"{seen - len(bad)} of {seen} figures in summary.md equal a figure of the invoice named on that line or a total recomputed from the input files")
seen = bad = 0
for k, r in got.items():
for t in [m.group(0) for m in FIG.finditer(r.get("reason") or "") if len(m.group(0)) > 1]:
seen += 1; bad += dec(t) not in rec.get(k, set())
say(seen > 0 and bad == 0, f"{seen - bad} of {seen} numbers quoted in the reasons are numbers of that invoice or its order")
sys.exit(0 if ok else 1)
invoices.csv: sample data written by Atris. Fingerprint b4c05f5bc9e9.
purchase_orders.csv: sample data written by Atris. Fingerprint 4e09c2a948c8.
matches.csv: written by the agent. Fingerprint 62e88da7d3dc.
summary.md: written by the agent. Fingerprint b9f95c2cdb3f.