Agents in products Recommended
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.
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);
}
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.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers ai features playbook: https://agentwranglers.se/playbook/ai-features/agents-in-products/resumable-agent-runs.