Integrate Cloudflare Workflows with Agents for durable, multi-step background processing while Agents handle real-time communication.
Quick Links
Introduction
What are Cloudflare Workflows?
Cloudflare Workflows provide durable, multi-step execution that survives failures, retries automatically, and can pause to wait for external events. They’re ideal for:- Long-running background tasks (data processing, report generation)
- Multi-step pipelines with retry logic
- Human-in-the-loop approval flows
- Tasks that shouldn’t block user requests
Why Integrate with Agents?
Agents excel at real-time communication and state management, while Workflows excel at durable execution. Together they provide:When to Use What
Quick Start
1. Define Your Workflow
Create a Workflow that extendsAgentWorkflow to get typed access to the originating Agent:
2. Start Workflow from Agent
UserunWorkflow() to start a workflow with automatic tracking:
3. Configure Wrangler
API Reference
AgentWorkflow
Base class for Workflows that integrate with Agents. Type Parameters:AgentType- The Agent class type (for typed RPC)Params- User params passed to the workflow (optional)ProgressType- Type for progress reporting (defaults toDefaultProgress)Env- Environment type (defaults toCloudflare.Env)
agent- Typed stub for calling Agent methods via RPCinstanceId- The workflow instance IDworkflowName- The workflow binding nameenv- Environment bindings
this (non-durable, may repeat on retry):
Methods on
step (durable, idempotent, won’t repeat on retry):
DefaultProgress Type:
Agent Workflow Methods
runWorkflow()
Start a workflow and track it in the Agent’s database.workflowName- Workflow binding name fromenvparams- Params to pass to the workflowoptions.id- Custom workflow ID (auto-generated if not provided)options.metadata- Optional metadata stored for querying (not passed to workflow)options.agentBinding- Agent binding name (auto-detected from class name if not provided)
sendWorkflowEvent()
Send an event to a running workflow.getWorkflowStatus()
Get the status of a workflow and update tracking record.getWorkflow()
Get a tracked workflow by ID.getWorkflows()
Query tracked workflows with cursor-based pagination. Returns aWorkflowPage with workflows, total count, and cursor for the next page.
WorkflowPage type:
Workflow Control Methods
deleteWorkflow()
Delete a single workflow tracking record.deleteWorkflows()
Delete workflow tracking records matching criteria. Useful for cleanup.terminateWorkflow()
Terminate a running workflow immediately."terminated". Throws if the workflow is not found in the tracking table. Cloudflare will throw if the workflow is already completed, errored, or terminated.
pauseWorkflow()
Pause a running workflow. The workflow can be resumed later withresumeWorkflow().
resumeWorkflow()
Resume a paused workflow.restartWorkflow()
Restart a workflow instance from the beginning with the same ID.resetTracking option (default: true) controls whether to reset the created_at timestamp and clear error fields.
Lifecycle Callbacks
Override these methods in your Agent to handle workflow events:Approval Methods
Convenience methods for human-in-the-loop approval flows:Workflow Tracking
Workflows started withrunWorkflow() are automatically tracked in the Agent’s SQLite database.
cf_agents_workflows Table
Workflow params and output are not stored by default. Use
metadata to store queryable information, and store large payloads in your own tables if needed.Workflow Status Values
queued- Waiting to startrunning- Currently executingpaused- Paused by userwaiting- Waiting for eventcomplete- Finished successfullyerrored- Failed with errorterminated- Manually terminated
Patterns
Background Processing with Progress
Human-in-the-Loop Approval
Durable Task Queue with Retries
State Synchronization
Workflows can update the Agent’s state directly (durably via step), which automatically broadcasts to all connected clients:Custom Progress Types
Define custom progress types for domain-specific reporting:Bidirectional Communication
Workflow → Agent
Agent → Workflow
Best Practices
1
Keep workflows focused
One workflow per logical task
2
Use meaningful step names
Helps with debugging and observability
3
Report progress regularly
Keeps users informed
4
Handle errors gracefully
Use
reportError() before throwing5
Clean up completed workflows
The
cf_agents_workflows table can grow unbounded, so implement a retention policy6
Handle workflow binding renames carefully
If you rename a workflow binding in
wrangler.jsonc, existing tracked workflows will reference the old name. Use migrateWorkflowBinding() to update them.