Black Friday Deals For Authors Are On Now!

Leanpub Header

Skip to main content

Long-Running AI Agents

35+ Design Patterns for Agents That Work All Night

Long-Running AI Agents

Your agent works for five minutes while you watch, but left alone for eight hours it forgets what it was doing, dies halfway through a refund, loops until morning, reports work it never did and takes actions nobody approved. This book gives you 35+ framework-agnostic patterns for the harness that prevents each of those failures, from context and durable execution to guardrails, verification and human approval, told through one agent's bad night at a small online store and fixed chapter by chapter until it gets through to morning.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

An agent that works for five minutes while you watch is a demo. An agent that works for eight hours while you sleep is a system, and it fails in ways the demo never shows.

It forgets what it was doing. It dies halfway through a payment. It loops until morning. It reports work as done that it never did. And it takes actions nobody would have approved, because nobody was there to approve them.

This book is about the code that keeps that from happening. Not the model, and not the prompt, but the harness around them: the code that decides what the model sees, what it may do, what gets checked, and what happens when something breaks at three in the morning.

ONE STORE, ONE AGENT, ONE NIGHT

The whole book follows a single story. Pebblecart is a small online store for home goods. After Black Friday its support team of four is six hundred tickets behind, and on Sunday at 23:00 they hand the queue to Nightshift, an agent that is supposed to work until 07:00 on Monday.

On the first night, Nightshift fails in five ways, one after another. Each chapter takes one of those failures and fixes it, so that by the last chapter the same model, given the same tickets, gets through the night. Because it is one story, the patterns build on each other the way they do in real systems: the action log of Chapter 3 is what makes the verify gate of Chapter 8 possible, and the checkpoints of Chapter 5 are what make the approvals of Chapter 9 cheap.

THE FIVE FAILURES, AND THE PATTERNS THAT FIX THEM

- Forgets: compaction, clearing old tool results, just-in-time retrieval, sub-agents, progress files, structured work lists, plan-and-execute, re-planning, goal anchoring.
- Dies: action logs and compensating actions, checkpoint and resume, event logs and replay, idempotent tools, workflow engines, suspend and resume on webhooks, ambient agents, work queues with leases.
- Gets stuck: retries with backoff and jitter, timeouts, loop detection, token and money budgets, stop conditions, circuit breakers and fallbacks.
- Lies: a verify gate that checks facts instead of the agent's word, evaluator–optimizer reviewers with a concrete rubric, overnight reconciliation.
- Acts recklessly: approval gates for irreversible actions, treating customer text as data (prompt injection), async approval with signed links, escalation packets.

The last two chapters cover orchestrator–worker systems and handoffs between agents, tracing an eight-hour run, cutting the cost of a night without making it worse, and one full night with every pattern in place.

HOW EVERY PATTERN IS WRITTEN

- The problem, as a concrete moment in Nightshift's night: a time, a customer, an amount.
- The idea, with a hand-drawn diagram.
- A code card: a few lines of Python-like pseudocode showing where the pattern sits in the harness.
- Trade-offs: what the pattern costs and where it goes wrong.
- On other platforms: where the pattern already exists, in the Claude Agent SDK, LangGraph, Temporal, the OpenAI Agents SDK and others, and the two questions to ask of any tool that claims to provide it.

WHAT'S INSIDE

- 11 chapters and 36 patterns, about 50,000 words
- 160+ illustrations and diagrams in the BitSketch hand-drawn style
- About 60 pseudocode code cards: nothing to install, no API keys
- A checklist at the end of every chapter to run against your own agent, and five "check yourself" questions with answers
- Appendices: a one-line catalog of every pattern, a printable long-running checklist, a glossary and sources for further reading

WHO IT'S FOR

Developers who build, or are about to build, agents that run for a long time without a person watching: overnight support queues, coding agents working through a backlog, research agents that read for hours, back-office agents that process records in bulk. You know what a model call and a tool call are, and you can read a few lines of Python. You don't need any particular framework; if you already use one, the book helps you see which parts of the problem it solves and which it leaves to you.

By the end, you'll be able to look at any agent, predict how it will fail on a long run, and choose the patterns that keep it on track until morning.

Prefer to watch? Every pattern in this book is also an animated lesson in the Long-Running AI Agents video course from BitSketch Academy.

Pebblecart, Nightshift and every customer in this book are fictional. This book is independent and is not affiliated with or endorsed by any of the companies or products mentioned.

Author

About the Author

BitSketch Academy

BitSketch Academy makes short, visual explainers on AI agents and system design for developers: animated diagrams instead of slides, one idea at a time. This book is the written companion to the Long-Running AI Agents video course. More at bitsketchacademy.com and youtube.com/@BitSketchAcademy.

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub