Chapter 33. Building a Durable AI Worker on Cloudflare with Claude Code
- Approved by
- Panaversity ·
The point
A worker that can lose its place, or pay twice, cannot be trusted with money. In Chapter 32 you built a small worker on Pi Durable. You watched it carry on after a crash. This chapter builds the real one: Brightline's AP worker, with the tools that read invoices, ask a person to approve and post payments.
Two things make this worker hard, and both are about time. First, a crash can happen at any moment, including the moment after the worker has paid a supplier and before it has written down that it did. Second, a person may take three days to answer an approval. A worker that stays awake for three days pays for three days of nothing. A worker that stops can forget where it was.
The open stack you chose answers both. Pi Durable saves every step before the next one starts, and lets a tool wait for a person while the Durable Object sleeps. Your job is to use those features correctly, and to check that Claude Code did.
You stay the engineer in charge. As in every chapter of this part, you write the spec and its failure cases. Claude Code turns the cases into tests, and you read them. Then Claude Code writes the code until the tests pass. You review it with a checklist.

Where this chapter sits in Part IV. The same map opens every chapter, with that chapter's parts highlighted.
Why it matters
This story is invented.
A small finance team uses an AP worker that someone built in a weekend. It works for a month. Then the cloud provider restarts a machine at 4:12 in the afternoon, a few milliseconds after the worker has told the bank to pay a supplier $8,400. The worker restarts, reads its saved state, sees a payment that has not been made yet, and makes it again. The supplier is paid twice. The team finds out at the end of the month, from the supplier.
Nothing in that worker was badly written, line by line. It simply had no rule for what happens between "the outside world changed" and "I wrote it down". This chapter is about that gap, and about the other gap, between "I asked a person" and "the person answered".
Words this chapter uses
The words of Chapter 32 keep their meaning: harness, durable, checkpoint, Durable Object, hibernate, idempotency key.
| Word | Meaning |
|---|---|
| Step | One saved unit of a run: a model turn or one tool call |
| Replay | Running a step again after a crash, from the last checkpoint |
| Replay-safe | A step that has the same effect whether it runs once or twice |
| Side effect | A change outside the worker: a payment, an email, a ledger entry |
| Park | To stop a tool call and wait for an event, with the run saved and the object asleep |
| Signal | A message that wakes a parked tool call, such as a person's answer |
| Deadline | The time after which a parked call stops waiting and takes another path |
| Role contract | The written rules of a worker's job: what it may do, for whom, with what limits |
| Release | One fixed version of the worker: its code, prompt, tools and model, with a number |
| Recorded answers | Saved model replies the tests use instead of a live model, so they need no key |
What you will build
Where you are. Chapter 32 gave you the setup for the whole part, the build method, and a small worker on Pi Durable with three tools. Its post_invoice saved its key with api.memo(), its Pi registry was created inside the object, and after npm run crash the ledger showed one post.
What you build.
- A one-page spec and its failure cases, written by you, before any code. Claude Code turns the cases into tests, and you read them.
- The worker, built by Claude Code step by step. It has a Cloudflare Worker (the small web program that receives requests, as in Part III; not the AP worker itself), a Durable Object that hosts Pi Durable, the three invoice tools, and one object per conversation. A record of the worker's identity lives in Postgres.
- An approval that waits for days for the named reviewer, with a signed link, and costs nothing while it waits.
- Hard tests: a crash right after a payment, two messages at once, a rejection, a deadline.
- A cost report for 50 invoices, compared with Chapter 32's pricing.
By the end of this chapter, you can direct Claude Code to build a durable worker. You can also find the five mistakes it is most likely to make, before they cost money.
In this chapter
- 33.1 The ideas first
- 33.2 Write the spec
- 33.3 Build it with Claude Code, step by step
- 33.4 One object per conversation, and the worker's record
- 33.5 Waiting for a person
- 33.6 Test it the hard way
- 33.7 Deploy and read the bill
- 33.8 When Claude Code gets it wrong
- 33.9 Limits, and how today's tools realize it
- Lab 33 Build the AP worker that waits and does not pay twice
What this chapter is built on
The chapter follows sections 4.1, 6.1, 6.2 and 6.4 of Panaversity's paper Building Affordable, Self-Improving AI Workers (29 November 2026). The code patterns were tested for this book on 29 November 2026 with agents 0.34.1 and @earendil-works/pi-durable 1.6.1, on Wrangler's local runtime with recorded model answers. Costs come from the paper's cost model, model.ts.
Sources
- Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), sections 4.1, 6.1, 6.2 and 6.4
- Pi harness (Cloudflare Agents documentation, verified 2026-11-29)
- Owner
- Panaversity