How to build three way matching using Claude

- You need
- Purchase orders, goods receipts and vendor invoices, as three CSV files
- Tested with
- Claude Sonnet 5.5, made-up data, 3 Nov 2026
- Result
- 32 of 32 invoices matched the answer key
Before you start
A few years ago, a tool that matched purchase orders, receipts and invoices meant a developer, a written spec and a project plan.
Now you describe the policy in plain English and get working code back in one session. We did that with Claude on 3 Nov 2026 and tested what came back.
You compare a purchase order, a goods receipt and an invoice. Our policy had seven rules, and the script ended up with more checks than that, because it also tests the vendor, the item and whether the numbers can be read. Size adds to it.
Every extra entity brings its own vendors, currencies, tax codes and approval limits, and every new kind of invoice (a credit note, a split delivery, freight billed on the invoice but missing from the PO) needs a rule that is written, tested and made consistent with the others.
Our 32 invoices cannot show how fast that grows. They show where it starts.
The script below passed our test on made-up data. At a mid size or large company we would not build and own this ourselves. Accounts payable runs on exceptions, and the exceptions decide how the application has to change.
A vendor who bills before the goods arrive is a case our policy never mentioned, and every case like it is a rule somebody has to write, test and keep working after the next ERP upgrade.
Building it took one session. We expect keeping it right to take longer: the ERP changes a field, a vendor bills in a new way, the policy is revised, and each change needs a new run against the answer key.
We did not test that part. For the build or buy trade-off, read Build vs Buy: Finance AI Agents in 2026.
What you need
An ERP connection
The script needs three lists from your ERP: purchase order lines, goods receipts and vendor invoices. They join on the PO number and the PO line.
In our test they were CSV files, so we did not test a live connection. From a live ERP you pull them through its API. If your ERP has a mature, documented REST API, pulling three lists is a normal integration job.
Some ERPs limit it. Kai Waehner reported on 2 May 2026 that SAP's API policy restricts using SAP APIs with AI systems that choose their own calls (his article; we did not read SAP's own text). Read your ERP vendor's API terms before you connect an AI tool to it.
ChatFin's build or buy page says ChatFin runs on the ERP and version you already have, with no database credentials, reads through governed feeds and writes back only drafts that a person approves through the official API. That moves the upkeep of the connection to the vendor.
The data points
| File | Columns |
|---|---|
| Purchase orders | po_no, po_line, vendor_id, item, uom, po_qty, po_price |
| Goods receipts | gr_no, po_no, po_line, item, received_qty, gr_date |
| Vendor invoices | invoice_no, vendor_id, po_no, po_line, item, inv_qty, inv_price, uom, inv_date |
Claude noticed that our goods receipt file has no unit of measure column. A unit mismatch can be caught between the PO and the invoice, but not on the receipt. The script never read the PO quantity, the receipt number, the receipt item or either date.
A plan, broken into steps
Before any code, ask for the steps. Attach the three files and paste this.
I work in accounts payable. I want a small tool that does three way matching before invoices are approved for payment. Files: - purchase_orders.csv (po_no, po_line, vendor_id, vendor, item, description, uom, po_qty, po_price) - goods_receipts.csv (gr_no, po_no, po_line, item, received_qty, gr_date) - vendor_invoices.csv (invoice_no, vendor_id, po_no, po_line, item, inv_qty, inv_price, uom, inv_date) Our policy: 1. Approve an invoice only when the PO, a goods receipt and the invoice agree. 2. Price: the invoice price may be up to 2 percent above the PO price. More than that, hold it. 3. Quantity: we pay for what was received, not what was ordered. If the invoice quantity is more than the total received so far, hold it. 4. The unit of measure on the invoice must equal the PO's. 5. Hold any invoice with no goods receipt. 6. The same invoice number from the same vendor twice is a duplicate: approve the first, hold the second. 7. The total invoiced quantity on a PO line must never be more than what was received. Do not write code yet. Break the job into numbered steps and say, for each, what it checks and what happens to an invoice that fails. Then list anything in the policy that is unclear.
Claude read the files and answered with eight steps: read and tidy the files, find the PO line, check for a duplicate invoice, check a receipt exists, check quantity, check the unit of measure, check price, then decide and write the result.
For each step it said what happens to an invoice that fails.
It also asked ten questions about our policy. Three of them could change a decision or its reason:
- An invoice dated 15 October has a goods receipt dated 18 October. Should it be held?
- Two invoices with different numbers share a vendor, PO line, quantity and price. Policy 6 only catches the same number. Should the script flag this?
- If 200 are invoiced and 100 received, hold the whole invoice or approve 100?
Policy questions. We did not answer them. We told Claude to use its plan, so it picked an answer to most of the questions and listed them. You would answer them with your controller.
Ask Claude to build it
Good. Use that plan. Write one Python script, standard library only, that reads the three files and writes decisions.csv with: invoice_no, po_no, decision (approve or hold), reason. The reason is one short sentence that names the rule and the numbers. Never change my files. Run it, then show me how many invoices were approved and held, with every hold and its reason.
We ran both prompts in Claude Code with Sonnet 5.5 (claude-sonnet-5-5) on 3 Nov 2026, as two turns of one conversation. Claude Code runs the script it writes. We did not test the chat app.
The result was a 155 line Python script that uses only the standard library and keeps money in Decimal, not floating point. It read 32 invoices and approved 20. It held 12, each with a reason that names the rule and the values:
| Invoice | PO | Reason the script gave |
|---|---|---|
| VI-9016 | PO-5016 | Policy 2: invoice price 9.96 is 5.96 percent above PO price 9.4, limit is 2 percent. |
| VI-9017 | PO-5017 | Policy 2: invoice price 7.16 is 6.07 percent above PO price 6.75, limit is 2 percent. |
| VI-9018 | PO-5018 | Policy 2: invoice price 1.96 is 5.95 percent above PO price 1.85, limit is 2 percent. |
| VI-9019 | PO-5019 | Policy 3 and 7: invoice qty 200 plus 0 already approved is more than 100 received. |
| VI-9020 | PO-5020 | Policy 3 and 7: invoice qty 400 plus 0 already approved is more than 300 received. |
| VI-9021 | PO-5021 | Policy 3 and 7: invoice qty 200 plus 0 already approved is more than 100 received. |
| VI-9022 | PO-5022 | Policy 5: no goods receipt for PO-5022 line 1. |
| VI-9023 | PO-5023 | Policy 5: no goods receipt for PO-5023 line 1. |
| VI-9026 | PO-5026 | Policy 4: invoice unit CT differs from PO unit RL. |
| VI-9027 | PO-5027 | Policy 4: invoice unit BX differs from PO unit CT. |
| VI-9777 | PO-5028 | Policy 6: invoice VI-9777 from V104 was already submitted earlier; Policy 3 and 7: invoice qty 250 plus 250 already approved is more than 250 received. |
| VI-9802 | PO-5030 | Policy 3 and 7: invoice qty 250 plus 250 already approved is more than 250 received. |
Two decisions came with a warning from Claude. It approved VI-9029, which is dated before its receipt, because the policy is silent.
It held VI-9802 only because of the quantity rule. VI-9801 and VI-9802 look like the same invoice under two numbers, and the script does not say so.
Where to run it
A script on a laptop helps one person. For AP to use it every day it needs somewhere to run, a login, a schedule, and a place to store each decision and who released a hold. A hosting service such as Vercel is one option for the web part. Nothing here was hosted.
If you would rather buy it
ChatFin's build or buy page says ChatFin runs on the ERP and version you already have, reads through governed feeds and writes back only drafts a person approves through the official API. We did not run this policy through ChatFin.
Test it before you trust it
Do not check a script like this by reading its output and nodding. Give it a test with a known answer.
1. Plant the exceptions and keep the answer key
We built the test file with 12 known exceptions: 3 over the price tolerance, 3 where the invoice quantity was above what was received, 2 with no goods receipt, 2 with the wrong unit of measure, 1 repeated invoice and 1 second invoice on a line already invoiced.
The answer key stayed out of the chat. In this run, Claude's decisions matched the key on all 32 rows.
We then ran both prompts again from scratch in a clean folder, this time in one session that held both messages. The second script was longer (266 lines), worded its reasons differently and made the same 20 approvals and 12 holds.
Made-up data. The 32 invoices are made up, and we planted the exceptions ourselves. 12 held out of 32 is not a rate to expect. A match with the key shows that the script follows the policy on these cases. It says nothing about your data.
2. Test the edges of each rule
We added 13 invoices on the boundaries of the policy and ran the same script. The first eleven rows cover all 13, because two rows cover a pair of invoices each. The last row is a separate file with a blank price.
| Case | What the policy says | What the script did |
|---|---|---|
| Price exactly 2.00% above the PO | Allowed (up to 2 percent) | Approved |
| Price 2.01% above the PO | Hold | Held |
| Price 10% below the PO | Says nothing | Approved |
| Negative quantity (a credit note) | Says nothing | Approved like a normal invoice |
| Invoice names a PO line that is not in the PO file | Not stated. Claude assumed hold | Held |
| Invoice vendor differs from the PO vendor | Not stated. Claude assumed hold | Held |
| Two receipts for one PO line that add up to the invoice quantity | Says nothing | Approved |
| Same invoice number from two different vendors | Not a duplicate | Both approved |
| Same invoice number typed in lower case with a trailing space | Repeated invoice number, hold. Case and spaces not stated | Held |
| Invoice item differs from the PO item | Not stated. Claude assumed hold | Held |
| Price typed as 3,00 instead of 3.00 | Says nothing | Held as unreadable data |
| A separate file: one purchase order with a blank price, and an invoice against it | Says nothing | The first script stopped with an error and wrote no decisions. The second script held that invoice as a data problem |
3. Bring the gaps to your controller
Neither prompt asked for protection against that crash, so the two scripts handle bad data differently. The policy never mentioned credit notes, prices below the PO, or an invoice dated before its receipt.
The script made a choice on each, and we never reviewed those choices. A controller decides what the choice should be. Then you add the rule and run the same test file again. Keep the answer key and rerun it after every change.
Where ChatFin fits
The script above shows how far one short session and three files get you. The rest is around it: an ERP connection that keeps working, a place where AP approves or releases a hold, a record of who approved what, and someone to change the rules when a vendor bills in a new way.
ChatFin's own build or buy page lists the parts you get when you buy: the governed connection, the approval workflow and an audit trail from prompt to prepared payload (Build vs Buy: Finance AI Agents in 2026).
It also says the maintenance of connectors, models and controls is the vendor's job. ChatFin also has a page titled How AI Agents Resolve Three-Way Match Mismatches.