# 34.4 Build the reconciliation Workflow with Claude Code (/affordable-self-improving-ai-workers/workers-delegate-workflows-execute/build-the-reconciliation-workflow)

---
type: Document
title: "34.4 Build the reconciliation Workflow with Claude Code"
description: "Spec Brightline's ten-step month-end reconciliation, write its failure tests, and have Claude Code build it as a Cloudflare Workflow that the AP worker starts and reports on."
status: stable
order: 434.04
ksor:
  owner: team:panaversity
  audience: [ public ]
  approval:
    by: process:panaversity
    at: 2026-11-30T08:18:55Z
chapter: "34"
part: IV
expert_status: manager
concepts: [ "34.4" ]
last_verified: 2026-11-29
sources:
  - id: paper
    title: "Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), sections 7.4 and 7.5"
    resource: "Panaversity paper, version 1.0, 29 November 2026 (not yet published)"
  - id: cf-workflows
    title: "Cloudflare Workflows (Cloudflare documentation, verified 2026-11-29)"
    resource: https://developers.cloudflare.com/workflows/
generated:
  at: 2026-11-30T08:18:55Z
  by: human:zia
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** A bakery writes its bread recipe as steps: mix, rest an hour, shape, rest again, bake. If the oven breaks during baking, nobody starts again from the flour. They bake the shaped loaves when the oven is fixed.

**What you will learn.** How to spec a business process as steps, which steps may call a model, and how Claude Code builds it as a Workflow that the worker starts.

**Why it matters.** A process you can read as a list of steps is a process you can review, retry and explain to an auditor.

### The request

Ana Morales, Brightline's AP manager, writes to the worker: *"Reconcile November's supplier invoices for Acme and prepare an exception report."* The worker reads the request. It checks that Ana may ask for it, and that reconciliation is in its role contract. Then it starts the process. Then it tells Ana: "November's reconciliation has started. I'll tell you when the report is ready for review."

### The ten steps

You write this in `spec-reconcile.md`, from the paper's example:

| Step | What it does | May it call a model? | Retry rule |
| --- | --- | --- | --- |
| 1 | Fetch the month's supplier invoices | No | Retry 5 times, with backoff |
| 2 | Fetch the related purchase orders | No | Retry 5 times, with backoff |
| 3 | Clean the records: dates, amounts, supplier names | No | Retry 3 times |
| 4 | Match invoices to purchase orders by rule | No | Retry 3 times |
| 5 | For unclear cases only, ask the model to suggest a match | Yes, with a budget | Retry 2 times, then mark "needs a person" |
| 6 | Write the exception report | No | Retry 3 times |
| 7 | Ask for review | No | Retry 3 times |
| 8 | Wait for the review, up to 5 working days | No | No retry: wait, then escalate |
| 9 | Publish the approved report | No | Retry with an idempotency key |
| 10 | Tell the worker the result | No | Retry 5 times |

Notice step 5. A Workflow can use a model. But the model works on one bounded question, "does invoice 1043 match PO-778?", with its own budget. It does not decide what step comes next. The process does.

And the failure tests, written before any code:

1. A step fails twice and then succeeds: the run carries on from that step, not from step 1.
2. The run is interrupted during step 4: it resumes at step 4, and steps 1 to 3 do not run again.
3. The reviewer rejects the report: nothing is published, the worker is told why, and the run ends as rejected.
4. No review in 5 working days: the run escalates to the AP manager's manager and does not publish.
5. Step 9 runs twice: the report is published once.
6. The worker is revoked while the run waits for review: the run stops before step 9.
7. The model step uses up its budget: remaining unclear cases are marked "needs a person".

### Build it, step by step

> Step 2. Read spec-reconcile.md and the installed Workflows documentation. Build src/reconcile.ts as a Workflow class. Each of the ten steps is its own step.do with the retry rule from the spec. Step 5 calls the model through the same model setting as the worker, with the budget from the spec, using recorded answers in tests. Step 8 waits for a review event with a timeout. Write tests 1, 2 and 7 first, and run them.

What Claude Code produced, shortened:

```ts
export class Reconcile extends WorkflowEntrypoint<Env, ReconcileParams> {
  async run(event: WorkflowEvent<ReconcileParams>, step: WorkflowStep) {
    const { client, month, processId } = event.payload;

    const invoices = await step.do("fetch invoices",
      { retries: { limit: 5, delay: "10 seconds", backoff: "exponential" } },
      () => fetchInvoices(this.env, client, month));
    const orders = await step.do("fetch purchase orders", RETRY_5,
      () => fetchOrders(this.env, client, month));
    const clean = await step.do("clean records", RETRY_3, () => normalize(invoices, orders));
    const matched = await step.do("match by rule", RETRY_3, () => matchByRule(clean));
    const suggested = await step.do("model on unclear cases", RETRY_2,
      () => suggestMatches(this.env, matched.unclear, { budgetUsd: 0.50 }));
    const report = await step.do("write report", RETRY_3,
      () => writeReport(processId, matched, suggested));
    await step.do("ask for review", RETRY_3, () => requestReview(this.env, processId, report));

    const review = await step.waitForEvent("wait for review",
      { type: "report-review", timeout: "7 days" });
    // ... check the review (34.6), then publish or stop, then tell the worker
  }
}
```

Each `step.do` is saved when it finishes. If the run is interrupted, the Workflow starts `run` again from the top, but every finished step returns its saved result at once, without running. So the run does real work only from the step that did not finish. That is test 2.

A step's result must be something that can be saved: plain data, not a database connection or a class instance. Keep results small. A step that returns every invoice of the month, with every line item, makes every later restart slower. Return IDs and totals, and keep the full records in your own storage.

**What to check.**

- Is each of the ten steps its own `step.do`? Two business steps in one `step.do` means a failure in the second repeats the first.
- Does each step have the retry rule from the spec, not a single default for all?
- Does the model step have a budget, and what happens when the budget runs out?
- Is the timeout on the wait 7 calendar days, covering 5 working days? Is the escalation in the code?

### The worker starts it, and reports on it

> Step 3. Add a start_reconciliation tool to the AP worker. It checks that the request is allowed, then starts the Reconcile Workflow with the process ID recon-{client}-{month}. Add a reconciliation_status tool that reads the process record. Step 10 of the Workflow sends the result to the conversation that started it, as a message with an operation ID.

The worker now has two new tools. One starts the process. The other answers "where is November's reconciliation?" from the process record in Postgres, not from the Workflow's own state, which is kept only for a limited time. When step 10 runs, the result arrives in the conversation as a new message, and the worker explains it to Ana.

Starting the process is where it can go wrong. That is the next page.

> **The invariant.** A process is a list of steps you can read. Each step is saved on its own and retried by its own rule.

**Check yourself.** Claude Code puts steps 1 and 2 in one `step.do` to save code. Step 2's API fails three times, then works. What happens to step 1? *(It runs four times, because the whole `step.do` is retried. Split them, so a failure in fetching orders does not fetch invoices again.)*
