Errors & Retrying
By default, errors thrown inside steps are retried. Additionally, Workflow SDK provides two new types of errors you can use to customize retries.
Default retrying
By default, steps retry up to 3 times on arbitrary errors. You can customize the number of retries by adding a maxRetries property to the step function.
Steps get enqueued immediately after a failure. Read on to see how this can be customized.
When a retried step performs external side effects (payments, emails, API writes), ensure those calls are idempotent to avoid duplicate side effects. See Idempotency for more information.
Intentional errors
When your step needs to intentionally throw an error and skip retrying, throw a FatalError.
Customize retry behavior
When you need to customize the delay on a retry, use RetryableError and set the retryAfter property.
Advanced example
This final example combines everything we've learned, along with getStepMetadata.
Setting maxRetries = 0 means the step will run once but will not
be retried on failure. The default is maxRetries = 3, meaning the
step can run up to 4 times total (1 initial attempt + 3 retries). Inside the
step, getStepMetadata().maxAttempts is that total, so a step can
handle its last attempt differently.
Serialization failures
A step whose arguments or return value cannot be serialized fails like a step whose body threw a FatalError: the failure is deterministic, so it skips the retry loop, and a try/catch around the step call observes the SerializationError:
Uncaught, the run fails immediately with the USER_ERROR code, without retrying. See serialization-failed for common causes and fixes.
Backend connection failures
On Vercel, backend connection failures and interrupted event streams use the existing retry policies, even when their error codes are unrecognized. This lets workflows recover from network failures instead of immediately failing with USER_ERROR. Persistent failures can still exhaust the retry budget.
The SDK also replaces its shared events connection pool after repeated HTTP/2 session failures, and retries event-log reads whose HTTP/2 stream the backend resets. An events request that receives no response data for 60 seconds fails and is retried instead of stalling the invocation. Invalid backend URLs, including unsupported protocols and embedded credentials, fail immediately. Fetch requests to blocked ports or with unsupported headers (such as Expect) also fail without retrying. Interrupted event writes retain their existing retries; caller cancellations do not trigger another write.
Writing to a stream forwarded from another run looks up that run's encryption key first. A transient lookup failure (a connection error, a timeout, an HTTP 429, or a 5xx) uses the step's retry policy. Any other lookup failure, such as an HTTP 404 when the owner run no longer exists, fails the step without retrying, with a EncryptionKeyLookupError whose cause is the original failure.
A connection failure does not prove that the backend rejected a write: it may have accepted it before the response was lost. Continue to make step side effects idempotent.
Failed-run logs include the underlying error causes and their codes, exposing socket, DNS, or TLS errors behind messages such as TypeError: fetch failed. If a cause cannot be read, the log includes [unavailable cause] and the run can still be recorded as failed.
Error codes
When a workflow run fails, the error includes an errorCode that classifies the failure, alongside the original thrown value (preserved as cause):
| Code | Meaning |
|---|---|
USER_ERROR | An error thrown in your workflow or step code (including propagated step failures like FatalError) |
MAX_EVENTS_EXCEEDED | The run reached the World's per-run event ceiling (for the Vercel World, see Workflow run limits). Run long-running workflows on the QuickJS VM with snapshotting, which is exempt from the default ceiling, or split unbounded loops into child workflows well before the ceiling. See max-events-exceeded |
MAX_DELIVERIES_EXCEEDED | The run exceeded the maximum number of queue deliveries |
REPLAY_TIMEOUT | A workflow replay exceeded the maximum allowed duration. See replay-timeout |
REPLAY_DIVERGENCE | A replay could not consume the event log deterministically, usually because of non-deterministic workflow code. |
CORRUPTED_EVENT_LOG | The event log cannot be replayed: it contains orphaned or mismatched events, or one of its stored payloads is no longer readable from the World's storage. If you see this, please file an issue |
STREAM_ERROR | Workflow stream infrastructure failed while reading or writing data. This is an SDK or backend failure rather than an error in workflow code; retry the run and report persistent failures with the runId |
WORLD_CONTRACT_ERROR | A World response violated the SDK contract; points at a World implementation bug |
DEPLOYMENT_MISMATCH | The run was delivered to a deployment other than the one it is pinned to, and automatic re-routing did not recover it. See deployment-mismatch |
RUNTIME_ERROR | An internal runtime error. If you see this, please file an issue |
The error code is also available on the run entity through the Workflow CLI (npx workflow inspect runs <runId>) in the error.code field, and as an OpenTelemetry span attribute (workflow.error.code) for observability.
Rolling back failed steps
When a workflow fails partway through, it can leave the system in an inconsistent state. A common pattern to address this is "rollbacks": for each successful step, record a corresponding rollback action that can undo it. If a later step fails, run the rollbacks in reverse order to roll back.
Key guidelines:
- Make rollbacks steps as well, so they are durable and benefit from retries.
- Ensure rollbacks are idempotent; they may run more than once.
- Only enqueue a compensation after its forward step succeeds.
Spec 9 (experimental)
The experimental spec 9 runtime retries steps differently: a failed step is retried after about a second by default on its one queue message, and a maxRetries = 0 step whose invocation dies after it started fails instead of running again. See Retries on spec 9 runs.