Skip to content

Cloudflare Workflows

Wrangler 4

A Workflow is ordinary async code whose step.do() calls are checkpoints. When an instance sleeps, waits, retries, or the engine restarts, run() executes again from the top, and every step that already succeeded returns its stored result instead of running. Code between steps can therefore run many times, so side effects belong inside steps, step names must be stable, and anything a step returns must be serializable.

PiecePurpose
extends WorkflowEntrypointThe Workflow class
run(event, step)Entry point; may return a result
event.payloadParams passed at creation
event.instanceIdThis run’s ID
"workflows" in configBinds it as env.NAME

A Workflow is a class in a Worker module. The workflows binding gives the Worker create(), get(), and friends; a Worker can instead declare the class under exports and call it through ctx.exports. run() may return a value, which appears in the instance status.

import {
WorkflowEntrypoint,
} from "cloudflare:workers";
export class Signup extends WorkflowEntrypoint {
async run(event, step) {
const { email } = event.payload;
const user = await step.do("create user",
() => this.env.API.createUser(email));
await step.do("send welcome",
() => this.env.MAIL.welcome(user.id));
return { userId: user.id };
}
}
{
"workflows": [
{
"name": "signup",
"binding": "SIGNUP",
"class_name": "Signup"
}
]
}
RuleWhy
Side effects inside step.doOutside code reruns
Deterministic step namesThe name is the cache key
Build state from step resultsMemory is lost on hibernation
Make steps idempotentA step can retry
Return ≤ 1 MiB per stepLarger results fail

The engine hibernates instances with nothing to do and replays run() when they wake, so variables set outside steps are gone. Treat each step as one unit of work that can be retried, and key external calls on something stable so a retry does not charge a card twice. Branch only on event.payload or earlier step results — never on Date.now() or Math.random() outside a step.

// Wrong: runs again on every replay
const startedAt = Date.now();
console.log("charging");
// Right: recorded once, replayed from storage
const charged = await step.do("charge", () =>
pay(event.payload.orderId, {
idempotencyKey: event.instanceId,
}));

Gotcha: A step named with a timestamp or random value — `fetch ${Date.now()}` — never matches its cached result, so it re-executes on every replay.

OptionDefault
retries.limit5
retries.delay10,000 ms
retries.backoff"exponential"
timeout (per attempt)10 minutes
NonRetryableErrorFails the step at once

Pass a config object as the second argument to step.do(). backoff is constant, linear, or exponential, and delay can be a function of the attempt and error. Throw NonRetryableError for failures that retrying cannot fix. A rollback handler, passed last, runs if the instance later fails; rollbacks run in reverse step-start order.

const order = await step.do("reserve stock",
{ retries: { limit: 3, delay: "5 seconds",
backoff: "exponential" },
timeout: "2 minutes" },
async () => {
const r = await reserve(event.payload);
if (!r.ok) {
throw new NonRetryableError("sold out");
}
return { reservationId: r.id };
},
{ rollback: async ({ output }) =>
release(output.reservationId) },
);

Note: NonRetryableError is imported from cloudflare:workflows. Uncaught at the top of run(), it fails the instance.

CallDoes
step.sleep(name, "3 days")Pauses for a duration
step.sleepUntil(name, date)Pauses until a time
step.waitForEvent(name, opts)Waits for sendEvent
instance.sendEvent({ type, payload })Delivers the event

A sleeping or waiting instance moves to the waiting state: it consumes no CPU and does not count toward the concurrency limit, so millions can wait at once. waitForEvent matches on type and throws when its timeout expires; the default is 24 hours. Sleeps can last up to 365 days and do not count toward the step limit.

// in run():
const approval = await step.waitForEvent(
"manager approval",
{ type: "approved", timeout: "7 days" },
);
// in a Worker handling the webhook:
const inst = await env.SIGNUP.get(id);
await inst.sendEvent({
type: "approved",
payload: { by: "ada" },
});

Tip: Prefer waitForEvent to a long step timeout. Cloudflare recommends step timeouts of 30 minutes or less; anything longer is a wait, not a step.

CallEffect
env.WF.create({ id, params })Starts an instance
env.WF.createBatch(...)Up to 100 at once
env.WF.get(id)Handle to an instance
inst.status(){ status, output, error }
pause / resume / restartControl a run
terminate({ rollback })Stops it

Statuses are queued, running, paused, waiting, waitingForPause, complete, errored, terminated, and unknown. A custom ID must be unique among retained instances: create() throws on a duplicate, while createBatch() skips it and reports it in errors. For periodic runs, put cron expressions in schedules on the binding instead of writing a separate scheduled handler.

export default {
async fetch(req, env) {
const order = await req.json();
const inst = await env.SIGNUP.create({
id: `order-${order.id}`,
params: order,
});
return Response.json({
id: inst.id,
...(await inst.status()),
});
},
};

Gotcha: Instance IDs are never reused while the instance is retained. Re-running “the same” order with create() fails; call restart() on the existing instance or choose a new ID.

LimitFreePaid
CPU per step10 ms30 s, up to 5 min
Steps per Workflow1,02410,000, up to 25,000
Concurrent running instances10050,000
Instance creation rate100/s300/s per account
State per instance100 MB1 GB
Completed-state retention3 days30 days max

Wall-clock time per step is unlimited: waiting on network I/O is free of CPU limits. Raise CPU with limits.cpu_ms and steps with limits.steps in the Wrangler config. Paid billing covers requests, CPU time, steps (500,000 included per month, then $0.80 per 100,000), and stored state (1 GB-month included, then $0.20 per GB-month).

{
"limits": { "cpu_ms": 300000 },
"workflows": [
{
"name": "signup",
"binding": "SIGNUP",
"class_name": "Signup",
"limits": { "steps": 25000 },
"schedules": ["0 9 * * MON-FRI"]
}
]
}

Note: On Workers Paid, Workflows created on or after 10 September 2026 keep finished instances for 7 days by default; set retention on create() to change it.

ToolUse
wrangler workflows instances describeStep-by-step status
wrangler workflows triggerStart an instance
inst.subscribe()Stream instance events
introspectWorkflowInstance()Mock steps in Vitest
disableSleeps()Skip sleeps in tests

The dashboard and instances describe show each step’s attempts, output, and errors. In tests with @cloudflare/vitest-plugin, an introspector from cloudflare:test can disable sleeps, mock step results and errors, and inject events, then wait for a final status.

Terminal window
npx wrangler workflows trigger signup \
'{"email":"ada@example.com"}'
npx wrangler workflows instances \
describe signup latest
npx wrangler workflows trigger signup --local