Long-running agents

An agent that finishes in eight seconds is a request. An agent that runs for six hours is a workflow, and everything about it changes: it must survive a restart, be cancellable mid-flight, and never repeat the step it already completed.

How it works

  1. Persist state after every step, not at the end.
  2. Make each step resumable: given the state, the next step is deterministic.
  3. Give every run a cancel switch that is checked between steps.
  4. Report progress in terms a human understands, not step numbers.
  5. Decide what a partial result is worth, because that is what a cancellation produces.

Optional: load a real model

See it work

When it pays, and when it does not

Use it whenSkip it when
Runs outlive a request timeoutEverything finishes in seconds
Steps write to other systemsEverything is read-only, where a restart just wastes time
A restart during a run is likelyRuns are short enough that a restart means retrying from scratch
Humans approve steps mid-runNo human interaction until the end

How it fails

No cancellation

A wrong agent running for six hours with no stop button is the story that ends a pilot.

Progress in step numbers

Step 14 of unknown means nothing. Say what it is doing and what is left.

Resuming into a changed world

The state it saved is stale after an hour. Re-verify preconditions on resume rather than trusting the checkpoint.

What it costs

EngineeringSubstantially more than a simple loop. Only pay it when the duration forces it.
InfrastructureA queue, a state store, and a worker. This is a real system.
DebuggingEasier than a long loop, because the state at every checkpoint is inspectable.
Checkpoint after every step or accept duplicates. There is no third option, and the systems that pretend otherwise discover it during their first restart.

Related: Retries and idempotency · Budgets and limits · Tracing · all patterns · agent jobs

Free from AI School - no signup, everything runs in your browser.