All Agent Engineering

Connecting a Cloudflare Agent to a Durable Workflow

A design sheet for starting, reporting, querying and recovering Workflows from an Agent, based only on the official Run Workflows page.

AINavWritten / Updated
On this page

At a glance

An interactive Agent handles real-time communication and state, while a durable Workflow handles long, multi-step, retryable background work. This guide lists the points to confirm where the two meet: who starts the work, which channel carries progress, how status is queried, and how to choose a recovery action for paused or errored instances. Three parties are involved: the Agent server, the Workflow runtime and the connected client. The only basis is the Run Workflows page, and nothing it leaves unstated is inferred.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Cloudflare Agents SDK Connecting a Cloudflare Agent to a Durable Workflow

Execution locations and responsibilities

  • Agent and separate AgentWorkflow runtime in Cloudflare Workers, plus clients Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Decide whether a Workflow is needed → 2. Configure bindings and split the steps → 3. Start from a name-addressed Agent → 4. Assign progress and result channels → 5. Design status queries and human approval → 6. Decide recovery actions for paused and errored instances → 7. Set retention and limits. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Decide whether a Workflow is needed2. Configure bindings and split the steps3. Start from a name-addressed Agent4. Assign progress and result channels5. Design status queries and human approval6. Decide recovery actions for paused and erroredinstances7. Set retention and limits

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • The Agent starts an instance with runWorkflow(), passing a Workflow binding name, params and optional options.metadata, and receives an instance ID. The Wrangler configuration needs a Durable Object binding, a workflows binding (binding, class_name) and migrations. Callbacks re-resolve the Agent by name, so start from a name-addressed Agent and keep class names intact.The quick start shows a new_sqlite_classes migration for a new Agent class. Existing deployments inspect migration history rather than adding the same migration for every Workflow integration.

When not to use

  • A Workflow started by a sub-agent is tracked in that sub-agent's own table, so the parent's getWorkflows() will not see it; write wrapper methods on the child and aggregate yourself
  • A Workflow cannot open a WebSocket; for live browser updates use broadcastToClients() on AgentWorkflow inside the Workflow (it forwards through the originating Agent) and broadcast() inside Agent callbacks
  • reportProgress() and broadcastToClients() can repeat on retry, so they must not carry state that may happen only once, such as billing or deduction
  • restartWorkflow() starts from the beginning. Independently review potentially repeated external actions without promising side-effect safety or recovery results. The documented controls are limited in wrangler dev and require checking actual behavior after deployment.

Prerequisites and input

  • A server-side class extending Agent, plus a background job you plan to build as an AgentWorkflow subclass
  • Access to edit the Wrangler configuration and to view Agent logs or messages received by a connected client

Engineering checks

Start layer: bindings and identity — The Agent starts an instance with runWorkflow(), passing a Workflow binding name, params and optional options.metadata, and receives an instance ID. The Wrangler configuration needs a Durable Object binding, a workflows binding (binding, class_name) and migrations. Callbacks re-resolve the Agent by name, so start from a name-addressed Agent and keep class names intact.The quick start shows a new_sqlite_classes migration for a new Agent class. Existing deployments inspect migration history rather than adding the same migration for every Workflow integration.

Channel layer: non-durable and durable reports — reportProgress() and broadcastToClients() on AgentWorkflow are non-durable and may repeat on retry; step.reportComplete(), step.reportError(), step.sendEvent(), step.updateAgentState() and step.mergeAgentState() are durable and idempotent. A Workflow cannot open WebSocket connections itself; broadcastToClients() broadcasts to connected clients through the originating Agent, while inside the Agent's own callbacks you use broadcast().

Recovery layer: tracking and control — Instances started with runWorkflow() are tracked in the Agent's own database and can be read with getWorkflowStatus(), getWorkflow() and getWorkflows(). Status values are queued, running, paused, waiting, complete, errored and terminated. Approval uses waitForApproval() together with approveWorkflow() or rejectWorkflow(); a paused instance continues with resumeWorkflow(), and restartWorkflow() restarts the same ID from the beginning.

1. Decide whether a Workflow is needed

Where: the design review. Input: a description of the background work the interactive Agent will trigger. Action: the page suggests Agents alone for chat, messaging and quick API calls, and Agent plus Workflow for tasks over 30 seconds, multi-step pipelines and human approval. Completion check: each piece of work is labeled with its owner. Stop condition: the work is short and needs no retry or approval, so skip the Workflow.

2. Configure bindings and split the steps

Where: Wrangler configuration, migration history and Workflow class. Input: actual Agent class, existing deployment and task split. Check Durable Object and workflows bindings. The quick-start new-class example uses new_sqlite_classes; existing classes first inspect migration history and apply only actual needed changes rather than duplicate it. Put durable Workflow work in step.do and align binding names with code. Completion: bindings and the migration decision have actual evidence. Stop unknown history or naming before adding migrations to match a sample.

3. Start from a name-addressed Agent

Where: an Agent method. Input: task params and any information you will filter on later. Action: call runWorkflow(), return the instance ID to the caller, and put filter data in options.metadata, which is not passed to the Workflow. Also confirm the bundler preserves class names (the page cites esbuild keepNames: true). Completion check: the start path uses a name rather than a raw Durable Object ID. Stop condition: the Agent is addressed by raw ID, so callbacks would land on another instance; switch to names first.

4. Assign progress and result channels

Where: AgentWorkflow steps and Agent callbacks. Input: state changes, a successful result or a failure. step.updateAgentState() and step.mergeAgentState() update Agent state and broadcast it to clients. step.reportComplete() maps to onWorkflowComplete; failures use step.reportError() and onWorkflowError. State synchronization is not a completion callback. Send live messages with AgentWorkflow.broadcastToClients(), or Agent.broadcast() inside an Agent callback. Completion: state, live messages and success/error reports have the correct owners and callbacks. Stop when restart-persistent information exists only as a live broadcast; move it into durable state first.

5. Design status queries and human approval

Where: the Agent's query methods. Input: the instance ID and metadata. Action: refresh one instance with getWorkflowStatus() and page through others with getWorkflows() filtered by status or metadata; for approval, call waitForApproval() in the Workflow and approveWorkflow() or rejectWorkflow() from the Agent. Completion check: every status value has a UI or log treatment. Stop condition: a status value has no handling; add it before continuing.

6. Decide recovery actions for paused and errored instances

Where: operational methods on the Agent server. Input: the IDs of instances in paused, errored or waiting status. Action: continue a paused instance with resumeWorkflow(); for an errored one, read the error delivered to onWorkflowError first, then decide on restartWorkflow(), which the page says restarts the same ID from the beginning, resets timestamps and error fields by default, and keeps the original timestamps with resetTracking: false; only for the documented running-instance case, use terminateWorkflow() when no longer wanted, which sets status to terminated. Before any restart, review each external action that would run again. Completion check: a fresh getWorkflowStatus() or getWorkflow() shows the expected status, and the chosen action and reason are recorded. Stop condition: do not restart if the side effects of repetition cannot be confirmed safe. The page says pause, resume, terminate and restart are not yet supported under wrangler dev and work only when deployed, so where they cannot be verified locally do not record them as confirmed recovery paths.The errored branch reviews the error and deliberately chooses restart; this source does not establish termination of an errored instance.

7. Set retention and limits

Where: the Agent's cleanup logic. Input: the expected growth of the tracking table. Action: choose delete-on-completion, periodic cleanup with deleteWorkflows(), or retention for auditing; call migrateWorkflowBinding() when a binding is renamed. Compare the plan with the listed limits, such as 30 minutes per step and a one-year maximum event wait. Completion check: a retention period is written down. Stop condition: a step's duration or state size approaches a documented limit; split the work.

Task example

This is a synthetic walkthrough, not an actual run. Imagine a document-review Agent: a user message triggers startTask, which calls runWorkflow() with metadata. The Workflow preprocesses in step.do, calls reportProgress(), then waitForApproval(). Expected: connected clients receive a progress broadcast and the status reads waiting; after an administrator approves, it moves to running and onWorkflowComplete eventually fires. If rejected, waitForApproval() throws WorkflowRejectedError and the Workflow should take its error path. If an instance reaches errored, an operator reads the error and considers restartWorkflow() only after confirming the external actions are safe to repeat. All of this is hypothetical and needs separate confirmation in a real deployment.

Completion checks

  • Every piece of background work has an owner: the Agent alone or a Workflow
  • Binding name, class name and migration configuration agree, and the starter is a name-addressed Agent
  • Every report is labeled durable or non-durable, and critical state uses durable methods only
  • Status queries cover every status value, and the record notes that instances started by a sub-agent are visible only in that sub-agent's storage
  • Paused and errored instances each have a written recovery decision, a repeated-side-effect review and a status re-check, and controls unsupported locally are flagged as requiring deployed verification

Verification scope

This guide comes from reading the official Run Workflows page (updated 10 November 2026). Installation, login, API execution and output were not tested, and every observation in the example is hypothetical.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Cloudflare Agents · Cloudflare Agents documentation: cf-workflow

Found an error? Tell us →