Durable multi-agent jobs that survive a restart
For engineers
Long-running, durable and coordinated agents with a cost ceiling and reliability targets.
A durable multi-agent job writes down what it has finished after every step, so a restart, a crash or a slow human costs one step and never repeats a payment.
The case
Ledgerly is a 30-person software company with a night job that reconciles 4,000 vendor invoices. A lead agent splits the work across four matcher agents, a reviewer agent checks each batch, and any invoice over 5,000 EUR waits for a human to approve it, sometimes for days. At 02:10 a deploy restarted the workers while the job was at invoice 2,100. The first design started over, emailed 2,100 approvers twice and paid one vendor twice. The team rebuilt it in five parts.
Step 1: Put the work on a queue and keep state outside the worker
Each invoice is one job on a queue. A job that fails repeatedly goes to a dead-letter queue after a few attempts, so one malformed PDF cannot occupy a worker for hours. The visibility timeout, how long the queue waits before it hands a job to another worker, must be longer than the slowest model call. Otherwise two workers handle the same invoice. The status of every invoice lives in a database, not in the worker's memory.
Step 2: Make every side effect safe to repeat
Retrying a model call is cheap. Retrying an email or a payment is not. Every side effect carries an idempotency key built from the invoice, such as pay-INV-20418, so a retry after a timeout is treated as the same request. Save the result of each completed step, including the id the service returned. On restart the job reads those results and skips finished work.
await payments.charge(
{ vendor: inv.vendor, amount: inv.total },
{ idempotencyKey: `pay-${inv.id}` },
);
await db.update(inv.id, { step: "paid" });
Step 3: Wait for humans with a durable timer
The approval step waits for a signal with a 72-hour timeout and an escalation path. It does not poll in a loop. A durable engine such as Temporal or a checkpointing framework such as LangGraph stores the history, so the wait survives any deploy.
Step 4: Keep long runs on rails
The lead keeps a goal file, a progress file with each feature marked pass or fail with evidence, and commits after each batch of 500. When the context fills with old tool output and the agent drifts, the team ends the session and starts a new one that reads those files. Workers get their own worktrees and their own files, and report in a fixed schema (matched ids, unmatched ids, error) that the lead validates. A watchdog checks the log and CPU time and, when both are flat, stops the job by its recorded process id, never by name.
Step 5: Cap the cost and plan for outages
Every agent re-reads its context on every turn, so cost grows with agents times context times turns. Ledgerly sets a cap on dollars, turns and spawned agents, and each cap has a defined action when reached: stop and report. Usage is tagged by agent, so a worker stuck in a retry loop shows up in the numbers. Timeouts are set from observed latency, not guesses. A circuit breaker pauses the queue during a provider outage, and any fallback model runs the same invoice evals before it is trusted.
Before and after
Before: one script loops over invoices in memory, retries everything three times and polls for approval. After: a queued job per invoice, keyed side effects, a signal-driven wait, capped agents and a runbook for outages. The same deploy at 02:10 now costs the invoice in flight, and nothing else.
What goes wrong
- A visibility timeout shorter than the model call. The queue redelivers the job while the first run is still working, and the invoice is handled twice.
- Trusting a progress file. A feature marked pass without a recorded test makes the next session skip work that is not done.
- A cap with no action. A limit set to continue on reaching it is a comment, not a control.
- Auto-approving fallback results. A weaker path is fine when its output is marked and checked afterwards. Skipping the check removes the safety.
Steps
Queue the work and move state into a database
One invoice becomes one queued job with a status row that moves from queued to matched, emailed, approved and paid. A dead-letter queue catches a job after four failed attempts, and the visibility timeout is set above the slowest model call.
Invoice status rows after the 02:10 restart The restarted job reads these rows and skips finished work Invoice Step Result id Attempts INV-20417 paid pay_88f1 1 INV-20418 emailed msg_41c9 1 INV-20419 queued 0 Key every side effect and keep the wait durable
The email and the payment each carry a key built from the invoice id, so a retry is the same request. The approval wait is a signal with a 72-hour timeout and an escalation to the finance lead, not a polling loop.
Give the lead a goal file, a progress file and commits
After each batch of 500 invoices the lead updates the progress file and commits. A feature is marked pass only with a recorded test run. When drift shows, the session ends and a fresh one starts from the files.
Split the workers by file and fix the message shape
Each matcher gets its own worktree and files. Reports use a schema with matched ids, unmatched ids and an error field, and the lead rejects any report that does not fit it.
Cap the spend, tag it by agent and read the report
The run stops at 40 dollars, 12 spawned agents and 60 turns per agent, and the action on a cap is to stop and report. Usage is tagged by agent and run id, so a loop such as 410 retries in one matcher shows in the table.
Write the outage runbook and test the fallback
A circuit breaker pauses the queue when the provider fails, and the job probes with one request before it resumes. A fallback model handles invoices only after the invoice evals pass on it, and its results are marked unverified until checked.
Skills this scenario uses
Where to study
- Temporal documentation: workflows (opens in a new tab)DocsFree
Durable workflows, timers and signals, the model for waits that survive restarts.
- Persistence (LangGraph docs) (opens in a new tab)DocsFree
Checkpointers that save agent state after each step so a run can resume.
- Idempotent requests (Stripe API reference) (opens in a new tab)DocsFree
How an idempotency key makes a repeated payment request safe.
- How we built our multi-agent research system (Anthropic) (opens in a new tab)ArticleFree
Real lessons on coordinating agents, token cost and resuming from failures.
- Effective harnesses for long-running agents (Anthropic) (opens in a new tab)ArticleFree
Progress files, feature lists and clean hand-offs between sessions.
- Addressing Cascading Failures (Google SRE book) (opens in a new tab)BookFree
Load shedding, retry limits and breakers when a dependency fails.
- Agent2Agent protocol documentation (opens in a new tab)DocsFree
Open protocol for task states and messages between agents from different vendors.