Workflows
Orchestrate complex multi-agent processes that never fail. Cloudflare Workflows enables your AI agents to execute long-running, multi-step processes with guaranteed completion, automatic retries, and durable state management. Perfect for complex tasks that involve multiple agents, external APIs, and time-based operations.
Key Features
- Durable Execution: Workflows survive outages and continue from where they left off
- Automatic Retries: Failed steps are automatically retried with exponential backoff
- Visual Debugging: Track workflow execution in real-time through the dashboard
- Type Safety: Full TypeScript support with compile-time validation
- Global Scale: Workflows run on Cloudflare's edge network worldwide
Getting Started
Wrangler Configuration
First, add workflows in your wrangler.json:
Hello World Workflow
Create a simple workflow that runs every 60 seconds:
Agent Integration
Start the workflow from your agent class:
MCP Integration
Workflows are powerful tools to run complex durable tasks for MCP Tools.
TypeScript API Reference
You can access the workflow api via the env of the Agent or MCP Tool
Workflow Management
create(options: { id: string, params: any }): Promise<WorkflowInstance>
Creates a new workflow instance with the specified parameters. Returns a workflow instance with unique ID that can be used for tracking and management.
createBatch(batch: Array<{ id: string, params: any }>): Promise<WorkflowInstance[]>
Creates multiple workflow instances in a single API call for improved efficiency. Can create up to 100 instances at once. Returns an array of workflow instances. This is the preferred method when creating multiple workflows simultaneously as it reduces API call overhead and improves throughput.
get(workflowId: string): Promise<WorkflowInstance>
Retrieves an existing workflow instance by ID for status checking and event sending.
pause(workflowId: string): Promise<void>
Pauses a running workflow instance. The workflow will stop execution and can be resumed later.
resume(workflowId: string): Promise<void>
Resumes a paused workflow instance from where it left off.
terminate(workflowId: string): Promise<void>
Terminates a running workflow instance permanently. The workflow will stop execution and cannot be resumed.
Step Execution Methods
step.do(stepName: string, handler: Function): Promise<any>
Executes a single step in your workflow. Steps are automatically checkpointed, so if the workflow fails, it resumes from the last completed step.
step.sleep(stepName: string, duration: string | number): Promise<void>
Pauses workflow execution for the specified duration. Unlike regular timeouts, workflow sleep is durable and survives worker restarts.
step.sleepUntil(stepName: string, timestamp: Date | number): Promise<void>
Pauses workflow execution until a specific date/time.
step.waitForEvent(stepName: string, options: EventOptions): Promise<any>
Waits for an external event to continue workflow execution. Events can be triggered by other agents, webhooks, or scheduled tasks.
Examples
Multi-Agent Research Workflow
Customer Onboarding Workflow
Data Processing Pipeline Workflow
Human-in-the-Loop Workflow
Approval Worker (Event Broadcasting)
Best Practices
Multiple Workflows Strategy
Granular Business Logic
Error Handling Strategy
State Management
Advanced Configuration
Error Handling
Retry Configuration: Configure retries per step using the third parameter of step.do:
NonRetryableError: Force a workflow to fail without retries:
Retry Policies
Multiple Workflows in Wrangler
Limitations
- Execution Time: Maximum 30 days per workflow execution
- Step Size: Maximum 128MB input/output per step
- Concurrency: Maximum 100 parallel branches per workflow
- Events: Maximum 10,000 events per workflow
Related Services
- Queues - Message passing between workflow steps
- Pub/Sub - Event-driven workflow triggers
- Memory Store - Persistent state between workflow executions