Build a 13-week cash forecast.
Done by an agent. Checked twice.
Thirteen weeks of cash laid out week by week, with the lowest week and every week under the minimum named.
Real files in
- payables.csv
Twenty-six rows of supplier bills, with an overdue bill, a bill on hold, an undated bill and a repeated row. 27 lines, 1 KB. - receivables.csv
Twenty-seven rows of expected customer receipts, with overdue, disputed and undated invoices and a repeated row. 28 lines, 2 KB. - recurring.csv
Seven repeating items such as payroll, rent and weekly sales, one of which ends partway through. 8 lines, 421 bytes. - start.md
The forecast start date, the opening bank balance and the minimum cash the owner wants to keep. 6 lines, 226 bytes.
Sample data written by Atris. Not a real company.
Three steps
- 1.Read the starting point and known cash
- 2.Place every item in its week
- 3.Write the result
Claude Sonnet 5.5, 49 seconds. No web. It never saw the check.
Checked twice
- Passed: 13 of 13 weeks are present and start on the right day
- Passed: 13 of 13 weekly receipt totals equal the totals rebuilt from the input files
- Passed: 13 of 13 weekly payment totals equal the totals rebuilt from the input files
- Passed: 13 of 13 weeks open at the last closing balance and close at opening plus receipts minus payments
- Passed: forecast.md names week 9, starting 2027-02-01, as the lowest week at 6,946.50 and lists 3 of 3 payments due that week with their amounts
- Passed: forecast.md names 2 of 2 weeks that close below the minimum of 15,000.00 and 0 weeks that do not
- Passed: 7 of 7 items left out of the forecast are in left_out.csv once with the right kind, amount and reason and 0 are there by mistake
- Passed: forecast.md says the lowest balance sits 8,053.50 below the minimum and lists 7 of 7 items left out
- Passed: 19 of 19 figures and dates in forecast.md equal a figure of the item or week named on that line or a figure of the lowest week
Second review by Claude Opus 5.5: pass.
Then we broke a copy of the output 9 ways on purpose. The check caught 9.
week,week_start,opening,receipts,payments,closing 1,2026-12-07,38460.25,35825.50,23835.60,50450.15 2,2026-12-14,50450.15,13260.00,12315.00,51395.15 3,2026-12-21,51395.15,9610.00,20800.40,40204.75 4,2026-12-28,40204.75,12580.00,7740.00,45044.75 5,2027-01-04,45044.75,15850.00,24328.25,36566.50 6,2027-01-11,36566.50,14075.00,6555.00,44086.50 7,2027-01-18,44086.50,9380.00,31690.00,21776.50 8,2027-01-25,21776.50,18000.00,19540.00,20236.50 9,2027-02-01,20236.50,15300.00,28590.00,6946.50 10,2027-02-08,6946.50,22925.00,3865.00,26006.50 11,2027-02-15,26006.50,8325.00,22342.80,11988.70 12,2027-02-22,11988.70,23080.00,8550.00,26518.70 13,2027-03-01,26518.70,15850.00,23060.00,19308.70
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 2 of 2 on 1 December 2026. The agent took 49 seconds. Run 1 passed both checks and the job was run again later. Each run starts clean. Between runs the written flow was made clearer, or a check was corrected or made stricter. Nobody edits the output.
What the second review verified
- I rebuilt all 13 weeks independently from the inputs and every opening, receipts, payments and closing figure matches output/forecast.csv. Week 1 is 38,460.25 + 35,825.50 − 23,835.60 = 50,450.15, and week 13 closes at 19,308.70.
- Week 9 (starting 2027-02-01) has the lowest closing balance at 6,946.50. Its payments are B-7716 6,990.00, Payroll 14,800.00 and Workshop rent 6,800.00, which add up to 28,590.00. That is 8,053.50 below the 15,000.00 minimum.
- Weeks 9 (6,946.50) and 11 (11,988.70) are the only weeks that close below 15,000.00, which matches 'Weeks below minimum: 9, 11'.
- The left-out list is complete and has the right reasons: S-3303 is disputed, S-3314 and B-7721 have no date, B-7708 is on hold, and S-3323 (2027-03-10), S-3325 (2027-03-24) and B-7724 (2027-03-12) fall after week 13 ends on 2027-03-07. The repeated S-3312 and B-7705 are counted once, and the paid S-3300 and B-7700 are ignored.
What this run does not show
- The forecast holds only receipts and payments already known and makes no guess about new sales or customers who pay late.
- The dates and amounts were written for this run and nothing was compared with a real bank account.
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 9 of 9. 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
# Build a 13-week cash forecast function: finance for: the owner or finance lead who has to know when cash gets tight goal: Thirteen weeks of cash are laid out from known receipts and payments and the week the bank balance runs lowest is named. ## 1. Read the starting point and known cash Read input/start.md for the start date, the opening bank balance and the minimum cash. Read input/receivables.csv, input/payables.csv and input/recurring.csv. Week 1 begins on the start date and each week runs seven days. The forecast covers 13 weeks. A row that repeats an invoice_id or bill_id counts once. ## 2. Place every item in its week Customer receipts and supplier bills: - Ignore rows with status `paid`. That cash has already moved. - Leave out a receipt with status `disputed` and a bill with status `on_hold`. - Leave out a row with no date, with the reason `no_date`. - Leave out a row dated after the last day of week 13, with the reason `beyond_13_weeks`. - Put every other row in the week that holds its date. A date before the start date is overdue and goes in week 1. Recurring items repeat from first_date: `weekly` every 7 days, `every_2_weeks` every 14 days and `monthly` on the same day of each month. Count only the dates that fall inside the 13 weeks and on or before last_date when one is given. A recurring date before the start date has already happened, so it is not carried into week 1. direction `in` is a receipt and `out` is a payment. For each week, closing is opening plus receipts minus payments. Week 1 opens at the opening bank balance and every later week opens at the closing balance of the week before. ## 3. Write the result Write output/forecast.csv with columns week,week_start,opening,receipts,payments,closing. Thirteen rows, week numbered 1 to 13, week_start as a date, money with two decimals. Write output/left_out.csv with columns id,kind,amount,reason. One row per receipt or bill left out. kind is `receipt` or `payment`. reason is `disputed`, `on_hold`, `no_date` or `beyond_13_weeks`. Write output/forecast.md for the owner with these two lines exactly in this shape: `Lowest week: week <number>, starting <date>, closing <amount>` for the week with the lowest closing balance (the earliest if two tie). Then `Weeks below minimum: <week numbers separated by commas>` or `Weeks below minimum: none`. Then list every payment in the lowest week by bill_id or item name with its amount, say how far the lowest balance sits below or above the minimum and list what was left out. ## Done looks like - Thirteen weeks, each opening at the closing balance of the week before. - Every receipt, bill and recurring date is in exactly one week or in left_out.csv. - The lowest week and every week below the minimum are named.
input/payables.csv · 27 lines, 1 KB
bill_id,supplier,amount,due_date,status,note B-7701,Kestrel Timber,6840.00,2026-12-01,open,Overdue B-7702,Harlow Fixings,1215.60,2026-12-10,open, B-7703,Brightline Freight,980.00,2026-12-12,open, B-7704,Meridian Tool Hire,1460.00,2026-12-15,open, B-7705,Kestrel Timber,7325.00,2026-12-18,open, B-7706,Orchard Paints,2110.40,2026-12-21,open, B-7707,Dunmore Glass,3890.00,2026-12-24,open, B-7708,Northgate Signs,640.00,2026-12-29,on_hold,Sign arrived damaged B-7709,Harlow Fixings,1388.25,2027-01-05,open, B-7710,Kestrel Timber,8140.00,2027-01-08,open, B-7711,Calloway Vehicle Leasing,2275.00,2027-01-12,open, B-7712,Orchard Paints,1930.00,2027-01-14,open, B-7713,Sawmill Machinery Co,14250.00,2027-01-20,open,New panel saw balance on delivery B-7714,Meridian Tool Hire,1460.00,2027-01-22,open, B-7715,Tax office,18600.00,2027-01-31,open,Quarterly sales tax B-7705,Kestrel Timber,7325.00,2026-12-18,open, B-7716,Kestrel Timber,6990.00,2027-02-04,open, B-7717,Dunmore Glass,2740.00,2027-02-09,open, B-7718,Brightline Freight,1125.00,2027-02-11,open, B-7719,Greywell Insurance,4860.00,2027-02-15,open,Annual workshop cover B-7720,Harlow Fixings,1502.80,2027-02-18,open, B-7721,Orchard Paints,2245.00,,open,Supplier has not sent the invoice date B-7722,Kestrel Timber,7610.00,2027-02-26,open, B-7723,Meridian Tool Hire,1460.00,2027-03-02,open, B-7724,Accountant year end fee,3200.00,2027-03-12,open, B-7700,Dunmore Glass,2980.00,2026-11-28,paid,Paid 2026-12-03
input/receivables.csv · 28 lines, 2 KB
invoice_id,customer,amount,expected_date,status,note S-3301,Wexcombe Hotel,8400.00,2026-11-27,open,Promised last week and still not in S-3302,Ashgrove Clinic,3150.00,2026-12-02,open,Chased twice S-3304,Brannock Dental Group,6275.50,2026-12-09,open, S-3305,Tiverton School Trust,12800.00,2026-12-11,open,Stage 2 of classroom fit-out S-3306,Fenmoor Garden Centre,2940.00,2026-12-16,open, S-3307,Lindqvist Motors,5120.00,2026-12-18,open, S-3308,Castlereach Vets,4410.00,2026-12-22,open, S-3303,Greywell Brewery,9650.00,2026-12-23,disputed,Customer says the bar top is the wrong finish S-3309,Pell Street Pharmacy,7380.00,2026-12-30,open, S-3310,Dovecote Nursery School,3890.00,2027-01-06,open, S-3311,Quillfeather Books,2260.00,2027-01-08,open, S-3312,Wexcombe Hotel,6900.00,2027-01-13,open, S-3313,Halvorsen Farm Shop,1975.00,2027-01-15,open, S-3314,Sorrel Bakery,3340.00,,open,Customer has not given a date S-3315,Tiverton School Trust,12800.00,2027-01-29,open,Stage 3 of classroom fit-out S-3316,Brannock Dental Group,4180.00,2027-01-21,open, S-3317,Ashgrove Clinic,5600.00,2027-02-03,open, S-3318,Lindquist Motors,7745.00,2027-02-10,open, S-3319,Marrow Lane Logistics,9980.00,2027-02-12,open, S-3320,Castlereach Vets,3125.00,2027-02-19,open, S-3312,Wexcombe Hotel,6900.00,2027-01-13,open, S-3321,Fenmoor Garden Centre,2480.00,2027-02-24,open, S-3322,Pell Street Pharmacy,6150.00,2027-03-03,open, S-3323,Northgate Dental,8800.00,2027-03-10,open,Not due until after the fit-out is signed off S-3324,Osprey Hotel Group,15400.00,2027-02-26,open,Deposit for spring refit S-3325,Harbor Gate Hotel,11250.00,2027-03-24,open, S-3300,Quillfeather Books,1890.00,2026-11-30,paid,Paid 2026-12-01
input/recurring.csv · 8 lines, 421 bytes
item,direction,amount,frequency,first_date,last_date Payroll,out,14800.00,every_2_weeks,2026-11-27, Workshop rent,out,6800.00,monthly,2026-01-01, Bank loan repayment,out,2350.00,monthly,2026-06-15,2027-01-15 Counter and card sales,in,5200.00,weekly,2026-12-04, Software and phones,out,1180.00,monthly,2026-03-20, Vehicle insurance,out,940.00,monthly,2026-02-28, Maintenance contract income,in,4500.00,monthly,2026-09-05,
input/start.md · 6 lines, 226 bytes
# Cash forecast inputs Company: Thornbury Joinery (an invented company) Forecast start date: 2026-12-07 (a Monday) Opening bank balance on the start date: 38,460.25 Minimum cash the owner wants to keep in the bank: 15,000.00
output/forecast.csv · written by the agent, 14 lines
week,week_start,opening,receipts,payments,closing 1,2026-12-07,38460.25,35825.50,23835.60,50450.15 2,2026-12-14,50450.15,13260.00,12315.00,51395.15 3,2026-12-21,51395.15,9610.00,20800.40,40204.75 4,2026-12-28,40204.75,12580.00,7740.00,45044.75 5,2027-01-04,45044.75,15850.00,24328.25,36566.50 6,2027-01-11,36566.50,14075.00,6555.00,44086.50 7,2027-01-18,44086.50,9380.00,31690.00,21776.50 8,2027-01-25,21776.50,18000.00,19540.00,20236.50 9,2027-02-01,20236.50,15300.00,28590.00,6946.50 10,2027-02-08,6946.50,22925.00,3865.00,26006.50 11,2027-02-15,26006.50,8325.00,22342.80,11988.70 12,2027-02-22,11988.70,23080.00,8550.00,26518.70 13,2027-03-01,26518.70,15850.00,23060.00,19308.70
output/forecast.md · written by the agent, 19 lines
Lowest week: week 9, starting 2027-02-01, closing 6,946.50 Weeks below minimum: 9, 11 Payments in week 9: - B-7716 Kestrel Timber (2027-02-04): 6,990.00 - Payroll (2027-02-05): 14,800.00 - Workshop rent (2027-02-01): 6,800.00 - Total payments: 28,590.00 The lowest closing balance is 8,053.50 below the minimum cash of 15,000.00. Left out of the forecast: - S-3303 (receipt, 9,650.00): disputed - S-3314 (receipt, 3,340.00): no_date - S-3323 (receipt, 8,800.00): beyond_13_weeks - S-3325 (receipt, 11,250.00): beyond_13_weeks - B-7708 (payment, 640.00): on_hold - B-7721 (payment, 2,245.00): no_date - B-7724 (payment, 3,200.00): beyond_13_weeks
output/left_out.csv · written by the agent, 8 lines
id,kind,amount,reason S-3303,receipt,9650.00,disputed S-3314,receipt,3340.00,no_date S-3323,receipt,8800.00,beyond_13_weeks S-3325,receipt,11250.00,beyond_13_weeks B-7708,payment,640.00,on_hold B-7721,payment,2245.00,no_date B-7724,payment,3200.00,beyond_13_weeks
check.py · the check, written before the run
"""Rebuilds the 13-week forecast from the input files and compares it with what the agent wrote."""
import csv, re, sys
from datetime import date, timedelta
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)
def money(s):
try: return round(float(str(s).replace(",", "").replace("$", "").strip()), 2)
except ValueError: return None
# 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
txt = (root/"input/start.md").read_text()
start = date.fromisoformat(re.search(r"start date:\s*(\d{4}-\d\d-\d\d)", txt).group(1))
opening = money(re.search(r"Opening bank balance[^:]*:\s*([\d,]+\.\d\d)", txt).group(1))
floor = money(re.search(r"Minimum cash[^:]*:\s*([\d,]+\.\d\d)", txt).group(1))
end = start + timedelta(days=13 * 7 - 1)
rec, pay, left, paid_in = [0.0] * 13, [0.0] * 13, {}, [[] for _ in range(13)]
src, amt_in = {}, [{} for _ in range(13)] # every source row by id, and the amount of each payment by week
def wk(d): return max((d - start).days, 0) // 7
for name, idc, datec, bad, tgt in (("receivables.csv", "invoice_id", "expected_date", "disputed", rec),
("payables.csv", "bill_id", "due_date", "on_hold", pay)):
seen = set()
for r in csv.DictReader((root/"input"/name).open()):
if r[idc] in seen: continue
seen.add(r[idc]); st = r["status"].strip(); src[r[idc]] = {"kind": "receipt" if tgt is rec else "payment", "amount": float(r["amount"]), "dates": {r[datec].strip()}}
if st == "paid": continue
if st == bad: left[r[idc]] = bad
elif not r[datec].strip(): left[r[idc]] = "no_date"
elif date.fromisoformat(r[datec]) > end: left[r[idc]] = "beyond_13_weeks"
else:
w = wk(date.fromisoformat(r[datec])); tgt[w] += float(r["amount"])
if tgt is pay: paid_in[w].append(r[idc]); amt_in[w][r[idc]] = float(r["amount"])
for r in csv.DictReader((root/"input/recurring.csv").open()):
first = date.fromisoformat(r["first_date"]); last = date.fromisoformat(r["last_date"]) if r["last_date"].strip() else end
out = r["direction"].strip() == "out"; dates = []
src[r["item"]] = {"kind": "payment" if out else "receipt", "amount": float(r["amount"]), "dates": {r["first_date"], r["last_date"].strip()}}
if r["frequency"].strip() == "monthly":
y, m = first.year, first.month
while date(y, m, first.day) <= end:
dates.append(date(y, m, first.day)); y, m = (y + 1, 1) if m == 12 else (y, m + 1)
else:
d = first
while d <= end: dates.append(d); d += timedelta(days=7 if r["frequency"].strip() == "weekly" else 14)
for d in dates:
if start <= d <= min(end, last):
(pay if out else rec)[wk(d)] += float(r["amount"])
src[r["item"]]["dates"].add(d.isoformat())
if out: paid_in[wk(d)].append(r["item"]); amt_in[wk(d)][r["item"]] = float(r["amount"])
close, bal = [], opening
for i in range(13):
bal = round(bal + rec[i] - pay[i], 2); close.append(bal)
path = root/"output/forecast.csv"
if not path.exists():
say(False, "output/forecast.csv was not written"); sys.exit(1)
rows = list(csv.DictReader(path.open()))
def cell(i, col):
return (rows[i].get(col) or "").strip() if i < len(rows) else ""
d_ok = sum(1 for i in range(13) if cell(i, "week") == str(i + 1) and cell(i, "week_start") == str(start + timedelta(days=7 * i)))
say(d_ok == 13 and len(rows) == 13, f"{d_ok} of 13 weeks are present and start on the right day")
r_ok = sum(1 for i in range(13) if money(cell(i, "receipts")) == round(rec[i], 2))
say(r_ok == 13, f"{r_ok} of 13 weekly receipt totals equal the totals rebuilt from the input files")
p_ok = sum(1 for i in range(13) if money(cell(i, "payments")) == round(pay[i], 2))
say(p_ok == 13, f"{p_ok} of 13 weekly payment totals equal the totals rebuilt from the input files")
c_ok = sum(1 for i in range(13) if money(cell(i, "closing")) == close[i] and money(cell(i, "opening")) == (opening if i == 0 else close[i - 1]))
say(c_ok == 13, f"{c_ok} of 13 weeks open at the last closing balance and close at opening plus receipts minus payments")
md = (root/"output/forecast.md").read_text() if (root/"output/forecast.md").exists() else ""
low = min(range(13), key=lambda i: (close[i], i))
m = re.search(r"Lowest week:\W*week\s*(\d+)(.*)", md)
low_start = str(start + timedelta(days=7 * low))
low_ok = bool(m) and int(m.group(1)) == low + 1 and close[low] in {money(x) for x in re.findall(r"-?\d[\d,]*\.\d{2}", m.group(2))} and low_start in m.group(2)
named = sum(1 for x in paid_in[low] if any(x.lower() in ln.lower() and dec(amt_in[low][x]) in figs(ln) for ln in md.splitlines()))
say(low_ok and named == len(paid_in[low]),
f"forecast.md names week {low + 1}, starting {low_start}, as the lowest week at {close[low]:,.2f} and lists {named} of {len(paid_in[low])} payments due that week with their amounts")
under = {i + 1 for i in range(13) if close[i] < floor}
m = re.search(r"Weeks below minimum:(.*)", md)
said = set() if not m else {int(x) for x in re.findall(r"\b\d{1,2}\b", re.sub(r"\d{4}-\d\d-\d\d|\d[\d,]*\.\d{2}", " ", m.group(1)))}
say(bool(m) and said == under, f"forecast.md names {len(said & under)} of {len(under)} weeks that close below the minimum of {floor:,.2f} and {len(said - under)} weeks that do not")
lpath = root/"output/left_out.csv"
lrows = list(csv.DictReader(lpath.open())) if lpath.exists() else []
lgot = {(r.get("id") or "").strip(): r for r in lrows}
l_ok = sum(1 for k, v in left.items() if k in lgot and (lgot[k].get("reason") or "").strip() == v and (lgot[k].get("kind") or "").strip() == src[k]["kind"]
and money(lgot[k].get("amount")) == round(src[k]["amount"], 2))
say(l_ok == len(left) and set(lgot) == set(left) and len(lrows) == len(left),
f"{l_ok} of {len(left)} items left out of the forecast are in left_out.csv once with the right kind, amount and reason and {len(set(lgot) - set(left))} are there by mistake")
gap = round(abs(close[low] - floor), 2)
listed = sum(1 for k in left if k in md)
say(dec(gap) in [f for ln in md.splitlines() for f in figs(ln)] and listed == len(left),
f"forecast.md says the lowest balance sits {gap:,.2f} {'below' if close[low] < floor else 'above'} the minimum and lists {listed} of {len(left)} items left out")
# every figure and date in forecast.md must belong to the item or week named on its line, or be a figure of the lowest week
held = {k: {dec(v["amount"])} | {d for d in v["dates"] if d} for k, v in src.items()}
for i in range(13):
week = {dec(i + 1), str(start + timedelta(days=7 * i)), dec(opening if i == 0 else close[i - 1]), dec(rec[i]), dec(pay[i]), dec(close[i]), dec(round(abs(close[i] - floor), 2))}
held[f"week {i + 1}"] = held[f"Week {i + 1}"] = week
if i == low: lowest = week
sums = {dec(round(sum(src[k]["amount"] for k in left if src[k]["kind"] == kind), 2)) for kind in ("receipt", "payment")} | {dec(round(sum(src[k]["amount"] for k in left), 2))}
facts = lowest | sums | {str(start), str(end), dec(opening), dec(floor), dec(13), dec(len(left)), dec(len(paid_in[low])), dec(len(under))} | {dec(w) for w in under}
ID = "|".join(sorted((re.escape(k) for k in held), key=len, reverse=True))
seen_n, bad = writeup(md, ID, held, facts)
say(seen_n > 0 and not bad, f"{seen_n - len(bad)} of {seen_n} figures and dates in forecast.md equal a figure of the item or week named on that line or a figure of the lowest week")
sys.exit(0 if ok else 1)
payables.csv: sample data written by Atris. Fingerprint f0d7c0fddde2.
receivables.csv: sample data written by Atris. Fingerprint c049758ca000.
recurring.csv: sample data written by Atris. Fingerprint 64e96fe3fea4.
start.md: sample data written by Atris. Fingerprint d9376967663d.
forecast.csv: written by the agent. Fingerprint 1abe0616353a.
forecast.md: written by the agent. Fingerprint e4117a32febb.
left_out.csv: written by the agent. Fingerprint 81a2ba3fb9c4.