Skip to main content
A long-running hosted agent can be interrupted at any time by a crash, an out-of-memory kill, a redeploy, or a scale-in. This article shows how to make your agent’s background responses recoverable and how to resume a recovered run from its last checkpoint.
Long-running agents are in preview. APIs and package versions are subject to change.

Turn on crash recovery

Crash recovery is off by default. Enable it explicitly for the surface you use.

Responses protocol

Set resilient_background=True on ResponsesServerOptions:
Recovery applies only to responses that are stored and run in the background - that is, requests with store=true and background=true. When you enable the opt-in and the container crashes mid-response, the framework reinvokes your handler on restart, replays persisted stream events to reconnecting clients, and preserves conversation state.
Without resilient_background=True, a background response that crashes is marked failed with error.code="server_error" - the framework does not reinvoke the handler. Foreground (background=false) responses are always marked failed on crash, because their client connection is already gone.

Invocations / task primitives

When you build directly on the task primitives, declaring a @task or @multi_turn_task handler automatically enables the startup recovery scan. If you register tasks lazily after host startup, force-enable the scan before startup:

What you get for free

When you turn on recovery, you get the framework half with no handler changes: A naive recovered handler still produces a correct response - it just reruns the whole turn. Making the recovered attempt resume where it left off is the handler half you take on when you need it.

Detect a recovered entry

On reinvocation, branch on the recovery marker rather than reconstructing the original request.

Choose a resume strategy

Pick a strategy based on where your progress state lives. Prefer phase boundaries that checkpoint cleanly: complete one output item per phase, then checkpoint. If a phase crashes before its checkpoint it reruns; after the checkpoint the recovered attempt skips it.

Fence non-idempotent side effects

Before an action an upstream system can’t deduplicate (for example, sending an email or charging a card), stamp and flush a watermark, then clear it after the side effect commits:

Handle graceful shutdown

Graceful shutdown is different from terminal failure. A handler that can’t finish during shutdown should defer for recovery so the record stays in progress and a later lifetime reclaims it:
Crash recovery reenters the same attempt state; it doesn’t consume retry budget, and a wall-clock timeout doesn’t reset because the process restarted.