Chapter 33. Building a Durable AI Worker on Cloudflare with Claude Code
In this section
- Chapter 33. Building a Durable AI Worker on Cloudflare with Claude CodeA guided build: you write the spec and the failure tests, Claude Code builds Brightline's full AP worker on Pi Durable in a Durable Object, and the worker survives crashes and waits days for a person without paying for the wait.
- 33.1 The ideas firstWhat a harness does for a worker, why every step is saved before the next one, which steps are safe to run twice, and what it means for a worker to wait.
- 33.2 Write the specThe one-page spec for Brightline's AP worker, shown in full: its tools, which are safe to replay, the approval, the budget, and the failure tests written before any code.
- 33.3 Build it with Claude Code, step by stepThree build steps with Claude Code: scaffold the Worker, host Pi Durable in a Durable Object with PiHarness and Lifecycle, and add the invoice tools with saved idempotency keys. Each step shows the prompt, the result, what to check and what goes wrong.
- 33.4 One object per conversation, and the worker's recordRoute each conversation to its own Durable Object, accept each message once, and keep the worker's identity, role contract and release in Postgres where the whole company can see them.
- 33.5 Waiting for a personHow a Pi Durable tool parks while it waits for an approval, a deadline or another session, why the wait costs nothing, and how to make sure only the named reviewer can answer, once.
- 33.6 Test it the hard wayTests that break the worker on purpose: a crash right after a payment, a restart during a wait, two messages at once, and a check that a waiting object really sleeps. All from recorded model answers, with no API key.
- 33.7 Deploy and read the billDeploy the worker on the free plan, run 50 invoices, and price them line by line with the paper's cost model: model tokens, active object time and the waits that cost nothing.
- 33.8 When Claude Code gets it wrongThe mistakes Claude Code makes most often when it builds a durable worker: invented Pi APIs, side effects in a tool marked safe, code that keeps the object awake, and trust in a signal. How to catch each one in review.
- 33.9 Limits, and how today's tools realize itWhat Pi Durable 1.6 on Cloudflare does not do yet, how to build a subagent without one, who keeps a worker durable on each foundation, and the exact versions this chapter was built and tested on.
- Lab 33: The AP worker that waits and does not pay twiceLab 33: from your own spec and failure tests, build Brightline's AP worker with Claude Code on Pi Durable in a Durable Object, crash it after a payment, make it wait for an approval, price 50 invoices, and port it to an economy model.
- Check yourselfRecall and practice for the whole chapter: the flashcards, and nine questions with outline answers.