Long-Running AI Agents
35+ framework-agnostic patterns for context, memory, durable execution, guardrails and human approval.
Building an AI agent that works for five minutes is easy. Building one that works for five hours, unattended, is a different problem.
Leave an agent running overnight and it breaks in predictable ways. Its context fills up and it starts mixing up customers. The server restarts at 2 AM and nobody knows whether that refund went through. It gets stuck in a loop and burns money until morning. It marks work as "done" when nothing was done. And sometimes it takes an action nobody can undo.
The whole course follows Nightshift, an agent working the night shift at Pebblecart, a small online store with 600 support tickets left over from Black Friday. Nightshift has real tools: it looks up orders, reads the refund policy, emails customers and issues refunds.
The first night goes badly. Section by section, you give Nightshift the patterns it needs, and in the last lesson it finishes a full night: tickets resolved, risky refunds waiting for approval, hard cases handed to humans, and a sensible bill.
The 5 ways long-running agents break
- 1
Forgetting
Compaction, clearing tool results, just-in-time retrieval, sub-agents, progress files, structured work lists, goal anchoring.
- 2
Dying
Checkpoints, event logs and replay, idempotent tools, workflow engines, suspend and resume with webhooks, work queues with leases.
- 3
Getting stuck
Retries with backoff, loop detection, token and money budgets, stop conditions, circuit breakers and fallback models.
- 4
Lying
Verification with facts instead of the agent's word, evaluator–optimizer reviewers, overnight reconciliation.
- 5
Acting recklessly
Approval gates, async human approval, escalation, and defending against prompt injection in customer emails.
What you'll learn
- Recognize the 5 ways long-running AI agents fail (forgetting, dying, getting stuck, lying, acting recklessly) and pick the right fix for each
- Keep an agent's context lean over hours of work with compaction, tool-result clearing, just-in-time retrieval and sub-agents
- Give agents memory across sessions with progress files, structured work lists, action logs and the initializer–worker pattern
- Make agents survive crashes and restarts with checkpoints, event logs, replay and idempotent tools
- Decide when to hand-roll durable execution and when to adopt a workflow engine like Temporal, Inngest or Cloudflare Workflows
- Run agents in the background on a schedule, pause them while waiting for webhooks, and process work queues safely in parallel
- Stop runaway agents with retries, loop detection, token and money budgets, stop conditions and circuit breakers
- Verify an agent's work with deterministic checks, independent reviewer agents and periodic reconciliation instead of trusting its word
- Put irreversible actions behind approval gates, handle async human approval, and defend unattended agents against prompt injection
- Design orchestrator–worker systems, handoffs, tracing and cost controls for agents that run all night
Course content
Section 1Course Introduction: Why Long-Running Is Hard
- 1Course introduction: what does your agent do while you sleep?Learners know whether the course is right for them and what they'll get out of it.
- 2The agent loop in 5 minutesUnderstand the minimal agent loop that every code card builds on.
- 3Five ways a long-running agent breaksRecognize the 5 failure modes through the story of one failed night.
Section 2Managing Context
- 4The context window is RAM, not a hard driveUnderstand why long context is slower, pricier and less accurate, even before it's full.
- 5Compaction: summarize history at the right timeDesign automatic history compaction.
- 6Clearing old tool resultsCut most of the token load by shrinking tool outputs that are no longer needed.
- 7Just-in-time retrievalLet the agent fetch data with tools instead of stuffing everything into the prompt up front.
- 8Sub-agents for context isolationHand "read a lot, conclude a little" work to sub-agents so the main context stays lean.
Section 3Memory and External State
- 9Progress file: the agent's notes to its next sessionDesign a "shift handover log" so a fresh session knows exactly where to pick up.
- 10A structured work listTrack progress in a structured file instead of relying on the agent's memory.
- 11Action log and compensating actionsRecord every action with external effects, and know how to "undo" what can't be undone.
- 12Initializer and worker: two roles for two kinds of sessionSeparate the first session (set up the scaffolding) from later sessions (do one piece at a time).
Section 4Planning and Staying on Track
- 13Plan-and-execute with a todo listHave the agent make a short plan before acting and update it as it goes.
- 14Re-planning: when to drop the old planRecognize the signals that call for a new plan and set re-planning points.
- 15Goal anchoring: no drifting after hundreds of stepsKeep the goal and constraints "fresh" in context.
- 16How big should a session be?Size an agent session so it's neither too big nor too fragmented.
Section 5Durable Execution: Surviving Crashes
- 17Why while true in one process isn't enoughUnderstand why processes die and what losing state costs.
- 18Checkpoint and resumeDesign state saving after every step and restarting from the last one.
- 19Event log: replay history to restore stateUnderstand event sourcing and how workflow engines use it to resume.
- 20Idempotent tools: retry without refunding twiceDesign tools that are safe to call again after a crash.
- 21When to use a workflow engineKnow when hand-rolled checkpoints are enough and when to adopt an existing engine.
Section 6Time and Async
- 22Where does the agent run when you close your laptop?Know the options for running an agent off your machine and on a schedule.
- 23Waiting for external events: webhooks instead of wait loopsLet the agent pause, free its resources and wake up when an event arrives.
- 24Ambient agents: run in the background, speak up only when neededUnderstand how to design an agent driven by events rather than chat commands.
- 25Work queues and concurrency limitsProcess many tickets at once without hitting rate limits or stepping on each other.
Section 7Reliability and Guardrails
- 26Retry, backoff and timeoutsHandle transient model and tool errors correctly.
- 27Detecting a stuck or looping agentAutomatically detect and break useless loops.
- 28Budgets: capping tokens, money, time and stepsSet spending caps for the whole run and for each ticket.
- 29Stop conditions: when the agent should give upDesign clear stop conditions for each ticket and for the whole run.
- 30Fallback models and circuit breakersKeep the agent running when a provider or external API fails.
Section 8Self-Verification
- 31Why agents say "done" too earlyUnderstand the common ways agents "lie" and why they do it.
- 32Verify with facts, not the agent's wordUse deterministic checks as the definition of done.
- 33Evaluator–optimizer: one agent works, another gradesAdd an independent reviewer for what machines can't check.
- 34Overnight reconciliationCatch accumulated errors early instead of finding out at 7 AM.
Section 9Humans in the Loop and Safety
- 35Approval gates for irreversible actionsClassify tools by risk and put dangerous ones behind an approval gate.
- 36Customer emails are data, not commandsProtect an unattended agent from prompt injection in its input data.
- 37Async approval: a human approves at 8 AMLet the agent request approval, park that ticket and keep working on others meanwhile.
- 38Escalation and handing off to a humanTeach the agent when to hand a ticket to a human and how to hand it off so nobody has to reread everything.
Section 10Multi-Agent for Long Work
- 39Orchestrator–worker: split the work, merge the resultsDesign an orchestrator that assigns work to multiple workers running in parallel.
- 40Handoff: passing state between agentsDesign a handoff package with enough information without passing the whole context.
Section 11Operations and Wrap-Up
- 41Tracing: replaying an 8-hour nightKnow what to record so a long run can be debugged after it ends.
- 42Cost optimization for long-running agentsCut the cost of a night's run without lowering quality.
- 43One full night and the pattern mapReview every pattern by the 5 failure modes and know what to learn next.
What makes this course different
Patterns, not a framework
Everything applies whether you use the Claude Agent SDK, LangGraph, Cloudflare Agents, Temporal, Inngest or your own loop. Each lesson ends with where the pattern already exists in popular platforms.
Short, focused lessons
Every video is 3 to 6 minutes and teaches one pattern.
Animated diagrams, not slides
Every idea is drawn and animated step by step, so you can see the mechanism, not just hear about it.
Pseudocode code cards
Each pattern ends with a short code card you can translate into any language. The harness grows lesson by lesson.
Nothing to install
No setup and no repo to maintain. Watch on your phone, on the train, or between meetings.
Also included
- A scenario quiz at the end of every section, based on real incidents from Nightshift's nights
- A final assignment: design the harness for your own agent on one page
- Pattern cards (PDF): one card per pattern with the problem, diagram, pseudocode and trade-offs
- Pattern map (PDF): every pattern on one page, organized by failure mode
- Overnight checklist (PDF): what to check before you let an agent run unattended
Who this course is for
- Developers who have built an agent demo and now need it to run for hours without supervision
- Tech leads and software architects designing agent systems for production
- Product engineers adding background agents (support, operations, research, coding) to a real product
- Engineers using an agent framework who want to understand what it does under the hood and what it's missing
Not for you if you want a step-by-step coding tutorial for one specific framework: this course teaches patterns you can apply in any of them
Requirements
- You know what an AI agent is at a basic level: a model calling tools in a loop
- You can read simple pseudocode (if, for, functions). No specific programming language is required
- Having built or used an agent before helps, but is not required
- No software, accounts or API keys needed: the course is diagrams and pseudocode, with nothing to install
This course is independent and is not affiliated with or endorsed by any of the companies or products mentioned. Pebblecart and Nightshift are fictional names created for this course.