ChatFin/Allocate Shared Costs to the Cent With Claude

Allocate Shared Costs to the Cent With Claude

By Ashok ManthenaPublished 5 Nov 20267 min read
You need
Pool balances, drivers and departments, as CSV
Tested with
Claude Sonnet 5.5, made-up data, 5 Nov 2026
Result
102 of 102 department lines match the key, both runs

Thirty pools, sixteen with odd cents

Split 100.00 three ways and every department gets 33.33, which adds up to 99.99, so one department has to take the missing 0.01 for the entry to balance.

On 5 Nov 2026 we gave Claude a five-rule prompt for that and thirty pools across 8 departments, sixteen of them built to leave odd cents, and checked every amount on every line.

Rules 2 to 4 of the five-rule prompt

Round each share to the cent. If the rounded shares do not add up to the pool amount, put the whole difference on the department with the largest driver value in that pool. If two departments tie for the largest, use the one whose name comes first alphabetically. A department whose rounded share is 0.00 gets no journal line.

Twenty-two of the 30 pools have at least two departments tied for the largest driver. Three have a department whose share rounds to 0.00, two use decimal drivers such as 2.5, and three have shares that land on exactly half a cent.

Every pool is at least 100.00, because the rules here say nothing about credits. The data is made up.

Input and output files

The script Claude wrote reads pool_costs.csv, drivers.csv and departments.csv and writes journal_lines.csv. It posts nothing.

Columns in pool_costs.csv, drivers.csv and departments.csv
ColumnUsed for
pool, pool_name, pool_account, amountThe cost to share and the account that holds it
pool, department, driver_valueEach department's weight in each pool. Decimals are allowed
department, expense_accountThe expense account each department's share is debited to

Ask Claude for a plan first

Attach pool_costs.csv, drivers.csv and departments.csv and paste this. We do not publish the data, so your numbers will differ from ours.

Prompt 1: plan
I work in the accounting team. At month end I split shared cost pools across departments, and the entries have to balance to the cent. I want a small tool for the arithmetic.

I have three files:
- pool_costs.csv: one row per pool (pool, pool_name, pool_account, amount). The amount is what sits in the pool account.
- drivers.csv: one row per pool and department (pool, department, driver_value). The driver can be headcount, floor area or anything else, and it can have decimals.
- departments.csv: department, expense_account.

My rules:
1. Each department's share of a pool is the pool amount times its driver value divided by the pool's total of driver values.
2. Round each share to the cent.
3. If the rounded shares do not add up to the pool amount, put the whole difference on the department with the largest driver value in that pool. If two departments tie for the largest, use the one whose name comes first alphabetically.
4. A department whose rounded share is 0.00 gets no journal line.
5. Each pool gets one balanced entry dated 31 Oct 2026: debit each department's expense account for its share, and credit the pool account for the pool amount.

Do not write code yet. Break the job into numbered steps. For each step say what could go wrong. Then list anything in my rules that is unclear.

Run 1 gave ten steps and run 2 gave eight, and both opened their list of unclear points with the same item, one our rules leave out: how a half cent rounds.

Run 1 pointed at the three pools whose shares land on exactly half a cent, POOL-20, POOL-21 and POOL-22, and worked one of them through: with halves up, each department in POOL-22 gets 50.03 except Billing at 50.00, and with halves to even, Billing gets 50.05 and the rest 50.02.

Run 2 named the same three pools. The lists also raised:

  • Whether a department's share is dropped at 0.00 before or after the adjustment is applied. Run 1 asked, and said it would apply the adjustment first.
  • Whether one department may take more than one cent. POOL-07 leaves three, and Run 1 asked whether to cap it.
  • Whether a department that would round to nothing should get a minimum of one cent instead. Run 2 asked.
  • Whether "first alphabetically" ignores case, and how spaces sort ("Field Ops" against "Finance").
  • The journal layout your ERP expects. Both asked.

We left them all open and told Claude to use its plan.

Both runs chose halves up and wrote 132 lines each

Prompt 2: build and run
Good. Use that plan. Write one Python script, standard library only, that reads the three files and writes journal_lines.csv with one row per journal line: entry (ALLOC- followed by the pool), date, account, department, debit, credit. Use exact decimal arithmetic for money and for driver values. Never change my files. Run it, then show me for every pool that needed an adjustment the department that took it and how much, and check that every entry balances.

Both prompts went to Claude Sonnet 5.5 (claude-sonnet-5-5) on 5 Nov 2026, as two turns of one Claude Code session with the CSV files in a working folder, and then again in a new session.

Both scripts are 166 lines of Python, standard library only, with money and drivers as Decimal.

Both chose halves up and said so in their reply, and both read the written journal file back to check every entry balances. Each wrote 132 lines: 102 debits and 30 credits, and the credits add up to 21,159.84, the total of the 30 pools.

Six of the 16 adjusted pools, and one that dropped a line
PoolAmountWhat happened
POOL-01100.00Three equal drivers give 33.33 each and leave 0.01. Field Ops comes first alphabetically in that pool and takes it: 33.34
POOL-02100.01Three equal drivers round up to 33.34 each, 0.01 too much. Billing gives one back: 33.33
POOL-071,200.05Seven equal drivers round up to 171.44 each, 0.03 too much. Billing gives all three back and ends at 171.41
POOL-15150.00A driver of 1 against 40,000. Billing's share is 0.0037, rounds to 0.00 and gets no line
POOL-20100.05Each share is exactly 50.025. Both round up to 50.03, 0.01 too much. Billing gives it back: 50.02
POOL-21200.25Each share is exactly 100.125. Data gives the cent back: 100.12 and 100.13
POOL-22300.15Six shares of exactly 50.025 round up to 50.03 each, 0.03 too much. Billing gives it all back: 50.00

Checks: a key, 24 edge files, 3,000 random pools

1. A key kept out of the working folder

Our own code worked out each department's amount for all 30 pools, and kept the key out of the folder we told Claude to work in.

Both journal files have the same amounts and differ only in the order of the rows. We also made a second key with halves to even. Against that key each run is off on 10 lines in the three half-cent pools.

2. Edge cases

We ran 24 small files through both scripts, 14 on the rounding rules and 10 on input the rules do not cover.

Edge case results
What we sentWhat happened
100.00, 100.01 and 100.02 over three equal driversFirst department takes the cent in the first, gives one back in the second, and all three get 33.34 in the third
200.02 over three equal drivers, rows listed Charlie, Bravo, AlphaAlpha gets the cent: 66.68
Drivers 1, 2 and 2 on 200.01The two largest tie. Bravo, first alphabetically, gets the cent: 80.01 and 80.00
Exact half cents: 100.05 and 200.25 over two equal drivers50.02 and 50.03, then 100.12 and 100.13
0.02 over three equal drivers; 0.01 over twoTwo lines of 0.01; then one line of 0.01. The department that takes the adjustment ends at 0.00 and gets no line
A share under 0.004 beside a driver 40,000 times largerOne line for the whole 150.00
A driver of 0 beside two others; one department onlyThe zero gets no line; the single department takes the whole pool
A pool amount of -100.00Run 1 stops: "must be positive". Run 2 writes negative numbers in the debit and credit columns
A pool amount of 0.00Run 1 stops. Run 2 writes a credit of 0.00 and no department lines
A negative driver; a pool with no drivers; drivers that are all 0; amount 10.005; amount typed 1,000.00; a department that is not in the file; a department twice in one poolBoth runs stopped each time, with a message naming the pool
A driver typed 1e3Neither run stopped. Both read it as a number and split the pool 99.90 and 0.10

3. 3,000 random pools against our own function

We wrote our own allocation function and made 3,000 random pools with one to eight departments, equal and unequal drivers, decimal drivers, and drivers of 1 against 50,000. 1,300 of them needed a cent adjustment.

Both scripts gave our amount on every department line of every pool. We wrote our function from the same five rules, so this test cannot catch a mistake in the rules.