AIF-55Recommended
Let runs pause and resume
Save an agent run's state after every step, so it can wait for approval and survive a restart. Expire pending approvals after a set time, such as 24 hours.
How to do it
Store the conversation, the completed steps and the pending action in the database, not in memory. When a tool needs a person's approval, save the run as waiting and end the request. Resume it when the user approves, from the saved state. Check before resuming that the data the action depends on has not changed. Expire waiting runs after the set time, and tell the user.
Examples
TypeScript example
export async function approve(runId: string, user: User) {
const run = await agentRuns.load(runId, user.id);
if (run.status !== 'waiting_for_approval') throw new Conflict('Nothing to approve');
if (Date.now() - run.waitingSince.getTime() > APPROVAL_TTL_MS) return run.finish('expired');
await run.executePendingAction();
return runAgent(run);
}
Python example
def approve(run_id: str, user: User) -> RunStatus:
run = agent_runs.load(run_id, user.id)
if run.status != "waiting_for_approval":
raise Conflict("Nothing to approve")
if datetime.now(UTC) - run.waiting_since > APPROVAL_TTL:
return run.finish("expired")
run.execute_pending_action()
return run_agent(run)
How to check
Start a run that needs approval, restart the service, then approve. The run finishes from where it stopped. An approval older than the limit is refused with an expired message.
Sources
Related rules
- AIF-70 Ask a person before an agent does what cannot be undone in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/agents-in-products/resumable-agent-runs">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.