Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Workflow Bindings, ctx.exports, Instance Lifecycle, and Local Development

How to configure Cloudflare Workflows, call them via bindings or ctx.exports, manage instances in batch, and test the full lifecycle with Wrangler locally.

This page summarises Cloudflare documentation on Workflows: declaring them in Wrangler, triggering and controlling instances from Worker code, and running the same operations against a local wrangler dev session with --local.

workflows binding vs exports: how to declare a Workflow in Wrangler

Section titled “workflows binding vs exports: how to declare a Workflow in Wrangler”

A workflows binding lets any Worker script create and manage instances through env.<BINDING_NAME>. You set binding, name, and class_name (which must match the exported class extending WorkflowEntrypoint). Optional fields include schedules, default_retention, jurisdiction, and script_name when the Workflow class lives in another Worker script.

Workflows allow you to build durable, multi-step applications using the Workers platform. A Workflow binding enables your Worker to create and manage Workflow instances programmatically.

— Cloudflare, Wrangler configuration — Workflows

The exports field (Wrangler 4.139.0+) declares Workflows the same Worker defines, keyed by class name, with type: "workflow" and the same settings as a binding (limits, schedules, default_retention, jurisdiction). On deploy, Wrangler creates or updates the Workflow resource.

You can now declare the Workflows a Worker defines in the exports field of your Wrangler configuration file. Previously, a Worker could only define a Workflow through a workflows binding, even when the Worker never called the Workflow itself.

— Cloudflare, Declare Workflows in the exports configuration

Approach Typical use How you call it from the defining Worker
workflows binding Cross-script access, explicit env.MY_WORKFLOW env.MY_WORKFLOW.create()
exports entry Workflow defined and run in one script ctx.exports.MyWorkflow.create()
Both Migration or tests that need a binding Same instances; settings must agree

You may declare the same Workflow as both binding and export if they use the same class and identical settings. A binding to a Workflow in another Worker cannot reuse the name of an export in this Worker—Workflow names are unique per account.

schedules on the Workflow: cron without a separate scheduled handler

Section titled “schedules on the Workflow: cron without a separate scheduled handler”

Cron expressions on the binding or export create new instances automatically, so you do not need top-level triggers.crons plus a separate handler for Workflow-only schedules. Scheduled instances carry the matching cron and trigger time on event.schedule. Use a Wrangler release that supports Workflow schedules; update Wrangler if the schema does not recognise the field yet (Build your first Workflow — Configure your Workflow).

ctx.exports: call a Workflow without a workflows binding

Section titled “ctx.exports: call a Workflow without a workflows binding”

ctx.exports exposes loopback bindings for top-level exports. For each export declared as a Workflow in exports, you get a Workflow handle keyed by class name, with the same API as env.MY_WORKFLOW.

A Worker can now call the Workflows it declares in the exports field of its Wrangler configuration through ctx.exports. You no longer need a workflows binding to call a Workflow from the Worker that defines it.

— Cloudflare, Call Workflows declared in exports through ctx.exports

Enable the enable_ctx_exports compatibility flag until it becomes default (Context — exports). After changing Wrangler config, run npx wrangler types so TypeScript sees ctx.exports.MyWorkflow as Workflow<Params> (Wrangler 4.142.0+).

A binding and an export with the same Workflow share instances, including instances created before you added the export—so you can move from binding-only to exports without losing history (Call a Workflow through ctx.exports).

create() and createBatch(): trigger Workflow instances from code

Section titled “create() and createBatch(): trigger Workflow instances from code”

From a binding or ctx.exports, create(options?) starts one instance. Wrangler can generate an ID if you omit it (Workers API — create). create() throws if the ID is already in use by an instance still within its retention window; use restart() on the existing instance to re-run with the same ID.

createBatch(options) creates up to 100 instances per call. Prefer it over many sequential create() calls to stay within creation rate limits (Rules of Workflows — Batch multiple Workflow invocations).

createBatch() now accepts an options object that creates up to 100 Workflow instances in one call. The result lists the created instances and explains why any others were not created.

— Cloudflare, Create Workflow instance batches by count or list

Use either:

  • count — shared params (and optional retention); each instance gets a generated ID.
  • instances — per-entry id and params (same shape as create()).

Unlike create(), createBatch does not throw on duplicate IDs; it returns errors with codes 10405 (ID already exists) or 10415 (duplicate within the batch). Passing a plain array to createBatch is deprecated; use { instances: [...] } and read created (Workers API — createBatch). For local dev and types for the object form, use Wrangler 4.148.0+.

Instance IDs: uniqueness and mapping to users

Section titled “Instance IDs: uniqueness and mapping to users”

Workflow instance IDs are unique per Workflow for the life of retention—they tie logs, metrics, and state to one run (Rules of Workflows — Instance IDs are unique). Avoid reusing a bare user ID as the instance ID; use order or transaction IDs, or a composite ID plus a database mapping.

Instance lifecycle: status, pause, resume, restart, terminate, delete

Section titled “Instance lifecycle: status, pause, resume, restart, terminate, delete”

get(id) returns a WorkflowInstance handle. status() reports values including queued, running, paused, errored, terminated, complete, waiting, and others, plus optional output, error, and rollback (Trigger Workflows — Inspect a Workflow’s status).

Method Effect
pause() / resume() Pause running work; resume when a concurrency slot is available
terminate({ rollback?: true }) Stop immediately; optional rollback handlers first
restart({ from?: ... }) Re-run from the beginning or a named step; clears intermediate state
delete() Remove stored state; stops execution without rollback

Deleting an instance frees its stored state and stops its current execution. Storage billing is based on the average daily peak.

— Cloudflare, Delete Workflow instances individually or in batches

deleteBatch(ids) deletes up to 100 IDs in one call, with per-ID errors for missing instances (Workers API — deleteBatch). If a Workflow calls delete() on its own instance, execution stops at await instance.delete() and code after it does not run.

Retention follows instance options, then default_retention on the binding or export, then account limits (Workers API — Default instance retention).

wrangler workflows –local: manage instances on wrangler dev

Section titled “wrangler workflows –local: manage instances on wrangler dev”

While wrangler dev is running, append --local to wrangler workflows commands so they talk to the emulated Workflow in the dev session (default port 8787, override with --port). Requires Wrangler 4.79.0+ for --local on commands; 4.142.0+ for Workflows declared only in exports (Workflows local development).

All wrangler workflows commands now accept a –local flag to target a Workflow running in a local wrangler dev session instead of the production API.

— Cloudflare, All Wrangler commands for Workflows now support local development

You can list workflows, trigger, and manage instances (list, describe, pause, resume, restart, terminate, send-event, delete) locally. Example:

npx wrangler workflows trigger my-workflow --local
npx wrangler workflows instances describe my-workflow latest --local

In code during wrangler dev, pause(), resume(), restart(), and terminate() on instance handles are supported locally (Workflow instances now support pause(), resume(), restart(), and terminate() methods in local development). Full command reference: Wrangler commands — Workflows and Wrangler commands (reference).

instances describe shows per-step status, retries, errors, sleep wake time, and truncated step output (Build your first Workflow — Deploy your Workflow). The observability setting in Wrangler records pipeline status and logs; failed runners block downstream stages that depend on their output.

Workflows are not supported as remote bindings or with wrangler dev --remote. Local dev ignores jurisdiction on a Workflow (Local Development — Known Issues).

Vitest and ctx.exports: test Workflows declared in exports

Section titled “Vitest and ctx.exports: test Workflows declared in exports”

wrangler dev, the Cloudflare Vite plugin, and Workers Vitest run export-declared Workflows on ctx.exports (Wrangler 4.142.0+, @cloudflare/vite-plugin 1.61.0+, @cloudflare/vitest-plugin 1.3.0+).

introspectWorkflow() and introspectWorkflowInstance() require a binding name on env—they error if you pass a handle from ctx.exports alone. For export-only declarations, add a test-only binding under minifare.workflows in Vitest config (Test APIs — Introspect Workflows declared in exports). Complex bundles with wildcard re-exports may omit exports on ctx.exports unless you set additionalExports in the Vitest plugin (Known issues — Missing properties on ctx.exports).

  • Reusing instance IDs that are still within retention, then expecting create() to start a fresh run—use a new ID or restart() on the existing handle.
  • Looping create() for many instances instead of createBatch(), increasing the chance of hitting creation rate limits.
  • Forgetting --local while debugging, so CLI commands hit production APIs instead of the dev session.
  • Mismatched binding and export settings for the same class (different schedules, limits, or names).
  • Relying on Agent helpers for local pause/terminate—the Agents runWorkflow API documents those methods as production-only in some cases, while Wrangler local dev now supports instance methods on bindings during wrangler dev.
  1. Confirm workflows or exports in wrangler.jsonc / wrangler.toml matches your exported WorkflowEntrypoint class and run npx wrangler types (Build your first Workflow).
  2. Start local dev, trigger with npx wrangler workflows trigger <NAME> --local, and inspect with npx wrangler workflows instances describe <NAME> latest --local (Workflows local development).
  3. From a Worker request, create an instance and read status(); compare with CLI describe output (Trigger Workflows).
  4. After deploy, run npx wrangler workflows instances describe <NAME> latest without --local and check the Cloudflare dashboard Workflows view for the same instance (Artifacts — View build status).
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match your project or the official docs, email yimasynthetic@gmail.com with the page URL and the exact sentence you are questioning so it can be corrected or clarified.