How to build accruals using Claude

- You need
- Invoice history by vendor, as one CSV file
- Tested with
- Claude Sonnet 5.5, made-up data, 3 Nov 2026
- Result
- 11 of 11 accrue calls and 7 of 7 amounts matched our reference answers
Before you start
At month end the invoice from the electricity company has not arrived, and the books close tonight. Someone looks at what was paid in the last few months and books a guess.
Turning that routine into a program used to be a job for the ERP team. On 3 Nov 2026 we wrote the six rules in plain English and Claude wrote the program.
Six rules sound small.
The file has 48 invoices from 11 vendors, and between them the vendors cover every rule: one stopped billing in July, one is a sponsorship that will never repeat, one has an invoice that swings between 7,100 and 18,900, and three have an October invoice that arrived after the close.
Add entities, currencies, purchase orders already received but not invoiced, and accruals that someone books by hand, and the corners multiply.
The rules also leave gaps.
The policy does not say what happens when a vendor's October invoice arrives on time but for the wrong amount.
Mid size and large companies are better off not maintaining this.
Accruals run on exceptions, and the exceptions decide how the application changes: the vendor that bills quarterly, the contract with a fixed fee plus usage, the invoice that covers two months. Each is a rule somebody has to write and defend to the auditor.
Building it took one session. Keeping it right is the longer job.
The script we got has the close date, the look-back window and the reversal date written at the top of the file, and the entry IDs and memo text also name October, so next month somebody edits all of those and runs the checks again.
We did not test the upkeep. For the build or buy trade-off, read Build vs Buy: Finance AI Agents in 2026.
What you need
An ERP connection
You need the invoice history by vendor: which service month each invoice belongs to, the amount, and the day it was booked. Those are two different dates, and the policy depends on both.
Then you need to post a journal. Many ERPs let you mark a journal to reverse itself in the next period, so check whether yours does before you build the reversal yourself.
If your ERP has a mature, documented REST API for AP invoices and journals you can build the read and the posting. Check its 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 and writes back only drafts a person approves through the official API. Our script used a CSV file and posts nothing.
The data points
| Column | Used for |
|---|---|
vendor_id, vendor | One decision per vendor |
expense_account | The debit line |
one_time | Y means never accrue |
invoice_month | The service month. Drives the look-back window and the average |
amount | The average and the 1.5 times test |
posted_date | Whether an October invoice arrived before the close |
A plan, broken into steps
Attach invoice_history.csv and paste this.
I work in accounting. I want a small tool that works out our month-end accrual for recurring vendors whose October invoice has not arrived. I have one file, invoice_history.csv (vendor_id, vendor, expense_account, one_time, invoice_month, amount, posted_date). It lists the vendor invoices we have received, May to October 2026. invoice_month is the month the service belongs to. posted_date is when we booked the invoice. Today is 31 Oct 2026 and the books close for October at the end of today. Our policy: 1. Accrue for October a vendor that has no October invoice posted on or before 31 Oct 2026. An October invoice posted later does not count. 2. Only accrue for a vendor that sent an invoice in at least 3 of the last 4 months before October (June to September). 3. Never accrue for a vendor marked one_time = Y. 4. The accrual is the average of the vendor's last three invoices on file before October, rounded to the cent. 5. If the largest of those three invoices is more than 1.5 times the smallest, still accrue the average but flag the vendor for review. 6. The journal entry debits each vendor's expense account and credits account 2100 Accrued expenses, dated 31 Oct 2026. It reverses on 1 Nov 2026. Do not write code yet. Break the job into numbered steps. For each step say what could go wrong. Then list anything in the policy that is unclear.
Claude gave eight steps and said what could go wrong in each. It noticed two traps in the policy.
"Which month" must come from invoice_month, while "was it booked in time" must come from posted_date. And a vendor's last three invoices could include one that was only booked after close.
It then listed eight places where the policy is silent. Three changed a number:
- Our file has posted dates of 3 and 4 Nov, yet today is 31 Oct. Claude treated anything posted after the close as not on file, and left it out of the history test and the average.
- Three vendors have an October invoice already in the file. The policy still says to accrue the average. Claude followed the policy.
- Should the average use the invoice month or the posting date? Claude chose invoice month.
Settle before relying on it. We did not answer them. We told Claude to use its plan and it applied its own reading of each.
The first point comes from our test file: it has Nov posting dates although the scenario is 31 Oct, which cannot happen. We planted them to test rule 1, and Claude pointed out the contradiction. Your controller settles all three.
Ask Claude to build it
Good. Use that plan. Write one Python script, standard library only, that reads the file and writes two files. accruals.csv has one row per vendor: vendor_id, decision (accrue, no accrual or skip), accrual_amount, flag, reason. journal.csv has the October entry and its reversal: entry, date, account, vendor_id, debit, credit, memo. Use exact decimal arithmetic for money. Never change my file. Run it, then show me the total accrual, the vendors flagged for review, and check that the entry and its reversal both balance.
Claude Sonnet 5.5 (claude-sonnet-5-5) ran both prompts in Claude Code on 3 Nov 2026, in one session that held both messages and played the two turns in order. We did not run them in the chat app.
The script is 322 lines of Python, standard library only, with money in Decimal. It accrued 7 vendors, made no accrual for 1 and skipped 3, for a total of 49,645.95.
It flagged one vendor for review, and the October entry and its reversal on 1 Nov both balanced at 49,645.95, with seven credit lines to account 2100, one per vendor. The input file was unchanged, which the script proved with a checksum before and after.
| Vendor | Name | Decision | Why, in our words |
|---|---|---|---|
| V01 | Metro Power and Water | Accrue 4,625.95 | No October invoice. Average of July, August and September. |
| V02 | Lumen Telecom | Accrue 1,290.00 | October invoice posted 4 Nov, after close, so it does not count. |
| V03 | Alder Cloud | Accrue 4,200.00 | No October invoice. |
| V04 | Shine Cleaning | Skip | Invoices in only 2 of the last 4 months (June and September). |
| V05 | Calloway Accounting | Skip | Invoices in only 2 of the last 4 months. Nothing since July. |
| V06 | Cascade Freight | Accrue 11,600.00, review | Largest invoice 18,900.00 is 2.66 times the smallest, 7,100.00. |
| V07 | Expo Chicago Sponsor | Skip | Marked one_time = Y. |
| V08 | Harbor Property Mgmt | Accrue 24,000.00 | October invoice posted 4 Nov, after close. |
| V09 | Quill Tools | Accrue 1,180.00 | October invoice posted 3 Nov, after close. |
| V10 | Brightline Data | Accrue 2,750.00 | No July invoice, but 3 of 4 months is enough. Average of June, August and September. |
| V11 | Northgate Security | No accrual | October invoice already posted on 28 Oct. |
Claude added a warning that we had not asked for. The three vendors with a late October invoice, V02, V08 and V09, make up 26,470.00 of the total. If any of those invoices was in fact posted by 31 Oct, that vendor drops out.
Where to run it
Accruals are a once a month run, right at close, when nobody wants to debug a script.
It needs to run on a schedule or on a button, read the invoice history from the ERP, show the accountant the flagged vendors before anything posts, and keep a copy of what was booked. A hosting service such as Vercel is one option. Nothing here was hosted.
If you would rather buy it
ChatFin's build or buy page says maintenance of connectors, models and controls is the vendor's job, not your backlog. We did not run this policy through ChatFin. ChatFin also has a page titled Accrual Reversals and Cutoff: Avoiding Period-End Errors.
Test it before you trust it
1. Plant a case for every rule and keep the answer key
We built the 11 vendors so that each rule gets tested by at least one: two that fail the three of four months test, one one-time vendor, three with a late October invoice, one on time, one with a missing month that still passes, and one that swings enough to be flagged.
Claude was not given the key. All 11 accrue or no-accrue calls, all 7 amounts and the one flag matched it. Two vendors got "skip" where our key says "no accrual", because the prompt never defined the labels.
Made-up data. The data is made up and we chose the cases. A match shows that the script follows the written rules on these vendors. It says nothing about your vendors.
2. Try the edges of each rule
We ran 14 single-vendor files, some of them variations of one case. The table groups them into eleven rows.
| What we sent | What the script did |
|---|---|
| Largest invoice exactly 1.5 times the smallest | Accrued, not flagged |
| Largest invoice 1.5 times the smallest plus 1 cent | Accrued and flagged. The reason reads "Largest 1500.01 is 1.50 times the smallest 1000.00; limit is 1.5", which looks like a tie |
| Three invoices of 100.00, 100.00 and 100.01 | Average 100.00, a third of a cent rounded away |
| Invoices in exactly 3 of the last 4 months | Accrued |
| Invoices in June and September, plus May | Skipped. May is outside the window |
| October invoice posted on 31 Oct, and on 1 Nov | No accrual for the first, accrual for the second |
| Two invoices in August, and one each in June and September | Accrued from the last three invoices (August, August, September), flagged at 2.0 times. The June invoice is left out of the average |
| September invoice posted 2 Nov, after close | Left out of every test. The average moves to June, July and August |
| One-time vendor with a full history | Skipped |
| Credit note, or an invoice of 0.00, among the last three | Stopped the whole run, "amount must be a positive number", no output |
Amount typed 1,000.00, or a blank posted_date | Stopped, names the line, no output |
The script did not crash on any of these. It stopped on bad data and named the line, which is what its plan said it would do. The two judgement calls are the ones to take to your controller.
A second invoice in the same month changes which invoices are the "last three". A late-posted invoice from September changes the average.
3. Compare it with a second implementation
We wrote our own version of the six rules and made 399 random vendors with random histories, one-time flags and October invoices posted on or before 31 Oct. Claude's script made the same decision, the same amount and the same flag on all 399, 144 of which were accruals.
None of the random vendors had an October invoice posted after the close, so that rule rests on the 11 vendors and two edge rows. The script and seed are in the edge folder.
4. Take the gaps to your controller
The policy says nothing about credit notes, a second invoice in one month, a vendor who bills quarterly, or foreign currency.
One credit note or zero amount anywhere in the file, for any vendor, stops the whole run, which makes the script unusable on real AP history as written. It guesses on the rest. Add what you want to the prompt and run the same files again.
Where ChatFin fits
The script shows how far six rules and one file get you in a session.
What is left is everything around it: the connection that reads the invoice history and posts the draft, a screen where the controller clears the flagged vendors, a record of who approved the entry, and a person who changes the rules when a vendor starts billing differently.
ChatFin's own build or buy page lists the governed connection, the approval workflow and an audit trail as what you get when you buy (Build vs Buy: Finance AI Agents in 2026). ChatFin also has a page titled Best AI Agents for Accruals and Provisions in 2026.