Freelance, for a restructuring advisor · Aug 2026 – present

An agent for 13-week cash flow forecasts in restructurings

An agent that turns messy, multi-entity financial data into an auditable 13-week cash flow forecast, with a human signing off every judgment call.

Python 3.12 · pydantic v2 · pandas · openpyxl · pdfplumber · pytest

Status: in progress. Phase 0, a backtested proof of concept. The private repository isn’t public.

What it is

When a company is in financial distress, during a Chapter 11 restructuring or an out-of-court workout, its financial advisor sends a 13-week cash flow forecast (TWCF) to the lenders, the DIP agent and the creditors’ committee every week. It’s the company’s liquidity report: how much cash comes in, how much goes out, and whether the money lasts.

This agent builds that forecast and rolls it forward. It reads raw financial data, sorts every line into a bankruptcy-specific set of categories, consolidates across legal entities, forecasts 13 weeks ahead, tracks actuals against the forecast each week, and produces an Excel workbook with a full audit trail. A person reviews every judgment call before anything goes out.

Problem

The TWCF is a required exhibit in DIP financing and cash collateral motions, and it’s rebuilt every week for the life of a case. It’s usually put together by junior associates from messy data spread across several legal entities and formats: bank statements, receivables and payables ageing, payroll registers, and monthly operating reports that often arrive as PDFs.

The industry estimate in the problem set is 2–4 days for the first build and 4–8 hours every week to roll it forward. That makes it repetitive, highly standardised, high-volume manual work: a strong candidate for automation, as long as a human stays in the loop.

The goal: turn a 2–4 day manual build into a reviewed draft on the same day. That’s the target, not a measured result yet.

Constraints

  • Every number must be auditable. In a court context, every figure has to trace back to a source transaction or a written assumption. No black-box numbers.
  • The logic must be explainable. Anyone should be able to see why a line was put in a category and why a week was forecast the way it was.
  • The inputs are messy. CSV, Excel and PDF (sometimes scanned), incomplete and sometimes contradictory. Getting data in is arguably harder than forecasting it.
  • Never invent data. A missing week is flagged, never filled in with a guess.
  • A human signs off. A bankruptcy court won’t accept a forecast nobody reviewed. The agent drafts; the practitioner signs.
  • Spend carefully. The whole pipeline was built and tested before spending anything on LLM API calls.
  • The data is sensitive. Engagement data is court-sensitive, so the design keeps each client’s data separate.

Architecture

A pipeline of six stages: A ingest (pandas, pdfplumber), B classify (19-category taxonomy), C consolidate (intercompany eliminations, optional), D forecast (13 weeks, each with an assumption), E variance (actual vs forecast with narratives) and F render (openpyxl). F produces an Excel workbook with a TWCF Detail sheet of live formulas, a Summary sheet of formulas and a hidden Audit Trail sheet that links every cell back to its source rows. Below all stages, G is a review queue where every judgment call becomes a review item that a human can override.
Seven stages, each one tested on its own, with a human review layer across all of them. Tap to open it full size.

The agent is a pipeline of seven stages. Each one is a separate, independently tested Python package. They pass data to each other through typed pydantic models, not loose dictionaries, so a badly shaped record fails at the boundary instead of quietly spreading.

A. Ingest. Detects each file’s type and sends it to the right parser: pandas for CSV and Excel, pdfplumber for PDF tables. Everything becomes one standard line item: entity, period, label, amount and a reference back to the source. A gap in the weekly sequence raises a flag; it’s never filled in.

B. Classify. Sorts every line into a fixed taxonomy of 19 bankruptcy categories, such as customer collections, payroll, critical vendors, DIP draws, adequate protection, professional fees, U.S. Trustee fees and pre-petition claims. A per-engagement cache means the same label is never classified twice. Low-confidence results go to a human. A separate check on frequency and size suggests which vendors might be critical.

C. Consolidate. Finds transfers between sibling legal entities, pairs each one with its equal-and-opposite partner within one week either side, and eliminates both, like an elimination entry in consolidated accounts, but over transactions. Transfers that can’t be paired stay visible, with the reason.

D. Forecast. Projects each category 13 weeks ahead. Phase 0 uses a trailing average. Every projection carries a plain-English assumption, so it can be defended on a lender call.

E. Variance. Compares actuals with the forecast by category and week, flags material differences (at least $5,000 or 10%) and drafts a short explanation for each one.

F. Render. Writes an Excel workbook with three sheets. The detail sheet uses live =SUM formulas rather than pasted numbers, so a lender can check the maths in Excel. The summary sheet is pure formulas over the detail sheet, so the two can never drift apart. A hidden audit sheet maps every cell back to the rows it came from.

G. Review. Every judgment call (a category, an assumption, an intercompany pairing, a variance explanation) becomes an item in a review queue. A person can override it, the correction flows back into the output, and every override is kept in an append-only history.

How it was built

I designed and directed it on my own: the problem framing, the architecture, and every design decision. The implementation and code review ran through an AI-agent workflow with Claude Code.

Each module went through the same cycle: brainstorm, a written design spec, a step-by-step implementation plan with tests written first, a build in an isolated git worktree, a review of the whole branch by a stronger model, a round of fixes, and then a merge.

Key decisions and why

Typed contracts between every stage. Bad data fails loudly at the boundary, instead of silently corrupting a number in a court exhibit.

Formulas, not values, in the workbook, plus a hidden audit sheet. Practitioners and lenders check and adjust these models in Excel, and every number has to trace back to its source. Building the audit trail as you go is far easier than reconstructing it later.

A free stand-in classifier behind the real interface. Classification starts as a simple keyword-rule classifier with exactly the same signature the Claude-backed classifier will use. The whole pipeline was built and tested without any LLM spend, and swapping in the real classifier is a one-line change.

A human in the loop, by design. Every module that makes a judgment call creates a review item and accepts an override. The tool drafts; a person signs.

Flag, never guess. Missing weeks, low-confidence categories and unpaired intercompany transfers are surfaced, not smoothed over.

The same input always gives the same workbook. Fixed sort orders everywhere, which matters when results are compared week to week.

What went wrong

Undoing one elimination silently inflated the numbers. This was caught in review and fixed before merge. Reversing an intercompany elimination looked rows up by their source reference, but that reference wasn’t unique: one table row produces one line item per week, and they all share it. Undoing one elimination brought back every week’s copy, inflating the consolidated history and every forecast after it, with no error. The fix records the exact positions of the matched pair when they’re matched, restores only those, and raises an error if anything doesn’t add up. The root cause, that line items need a truly unique identity, is first on the list before running on real data.

A transaction could eliminate itself. A transfer whose counterparty was its own entity paired with itself and vanished from the consolidated view: cash silently lost. Now such rows are treated as ordinary external lines, which is the safe way to fail.

A known limitation, kept visible on purpose. Weekly totals are summed by size, so a charge and its reversal in the same week add up instead of cancelling out. It’s documented, and it has to be fixed before running on real actuals that include credit memos.

Results

  • All seven modules (A to G) are built, reviewed and merged.
  • 172 automated tests pass, including an end-to-end test that ingests a real fixture, classifies it and round-trips an override.
  • About 1,600 lines of source code across 31 modules.
  • Every module went through the same cycle: design spec, plan, isolated build, whole-branch review, fixes, merge.

Not done yet. The modules aren’t wired into a single end-to-end run. The backtest against Bed Bath & Beyond’s public Chapter 11 filings, comparing the agent’s forecast with what the company actually reported, is still to come. Until it is, I’m making no claims about time saved or forecast accuracy.

Next: settle the open questions about the data’s meaning, build the end-to-end run, then run the Bed Bath & Beyond backtest.

← All case studies