Skip to content
The AI Agent Factory

Chapter 33. Building a Durable AI Worker on Cloudflare with Claude Code

Approved by
Panaversity ·
11 min read

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.

A map of the Part IV platform in four rows, foundation at the bottom, then work, platform and learning, with economics as a column on the right. Highlighted as this chapter, Chapter 33: The AP worker (one object per conversation); Waits, approvals (asleep, not paying). Already built in earlier chapters: Harness (Pi Durable); Runtime (Durable Objects); Process engine (Cloudflare Workflows); Model access (any model, AI Gateway). Dashed, later in this part: Memory (facts, source, expiry); Dreaming (proposes candidates); Evaluation (held-out cases, new judge); Releases (approve, canary, roll back); Control plane (workers, versions, tenants); Model router (filter, then rank); Role contracts (one authorize check); Tenant walls (and proven deletion); Workflows (month-end reconciliation); Events, schedules (per-worker coordinator); economics (Chapter 37). A note at the foot says approved knowledge, case files and the governed door are stand-ins here, and Part VI replaces them with the systems of record.

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.

WordMeaning
StepOne saved unit of a run: a model turn or one tool call
ReplayRunning a step again after a crash, from the last checkpoint
Replay-safeA step that has the same effect whether it runs once or twice
Side effectA change outside the worker: a payment, an email, a ledger entry
ParkTo stop a tool call and wait for an event, with the run saved and the object asleep
SignalA message that wakes a parked tool call, such as a person's answer
DeadlineThe time after which a parked call stops waiting and takes another path
Role contractThe written rules of a worker's job: what it may do, for whom, with what limits
ReleaseOne fixed version of the worker: its code, prompt, tools and model, with a number
Recorded answersSaved 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.

  1. 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.
  2. 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.
  3. An approval that waits for days for the named reviewer, with a signed link, and costs nothing while it waits.
  4. Hard tests: a crash right after a payment, two messages at once, a rejection, a deadline.
  5. 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.

Owner
Panaversity