Skip to content

Commit a174f52

Browse files
docs: Add Cloudflare Workflows integration documentation for Agents
Sync documentation from cloudflare/agents PR #799 - Add comprehensive guide for Cloudflare Workflows integration with Agents - Document new AgentWorkflow class with typed Agent access - Add API reference for workflow tracking, progress reporting, and lifecycle callbacks - Include patterns for background processing, human-in-the-loop, and state sync - Update run-workflows API reference with AgentWorkflow integration Related PR: cloudflare/agents#799 Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 724f82a commit a174f52

File tree

2 files changed

+947
-3
lines changed

2 files changed

+947
-3
lines changed

src/content/docs/agents/api-reference/run-workflows.mdx

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ sidebar:
88

99
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

11-
Agents can trigger asynchronous [Workflows](/workflows/), allowing your Agent to run complex, multi-step tasks in the background. This can include post-processing files that a user has uploaded, updating the embeddings in a [vector database](/vectorize/), and/or managing long-running user-lifecycle email or SMS notification workflows.
11+
Agents can trigger asynchronous [Workflows](/workflows/), allowing your Agent to run complex, multi-step tasks in the background. This can include post-processing files that a user has uploaded, updating the embeddings in a [vector database](/vectorize/), and managing long-running user-lifecycle email or SMS notification workflows.
1212

13-
Because an Agent is just like a Worker script, it can create Workflows defined in the same project (script) as the Agent _or_ in a different project.
13+
The Agents SDK provides two ways to work with Workflows:
14+
15+
1. **Basic integration** - Trigger workflows from Agents like any Worker script
16+
2. **AgentWorkflow integration** - Extended workflow class with typed Agent access, automatic tracking, progress reporting, and bidirectional communication
17+
18+
For comprehensive documentation on the AgentWorkflow integration, refer to [Integrate Cloudflare Workflows with Agents](/agents/guides/cloudflare-workflows-integration/).
1419

1520
:::note[Agents vs. Workflows]
1621

1722
Agents and Workflows have some similarities: they can both run tasks asynchronously. For straightforward tasks that are linear or need to run to completion, a Workflow can be ideal: steps can be retried, they can be cancelled, and can act on events.
1823

19-
Agents do not have to run to completion: they can loop, branch and run forever, and they can also interact directly with users (over HTTP or WebSockets). An Agent can be used to trigger multiple Workflows as it runs, and can thus be used to co-ordinate and manage Workflows to achieve its goals.
24+
Agents do not have to run to completion: they can loop, branch and run forever, and they can also interact directly with users (over HTTP or WebSockets). An Agent can be used to trigger multiple Workflows as it runs, and can thus be used to coordinate and manage Workflows to achieve its goals.
2025

2126
:::
2227

@@ -106,3 +111,82 @@ You can also call a Workflow that is defined in a different Workers script from
106111
</WranglerConfig>
107112

108113
Refer to the [cross-script calls](/workflows/build/workers-api/#cross-script-calls) section of the Workflows documentation for more examples.
114+
115+
## AgentWorkflow integration
116+
117+
For advanced workflow integration with automatic tracking, progress reporting, and bidirectional communication, use the `AgentWorkflow` class:
118+
119+
<TypeScriptExample>
120+
121+
```ts
122+
import { AgentWorkflow } from "agents";
123+
import type { AgentWorkflowEvent, AgentWorkflowStep } from "agents";
124+
125+
export class ProcessingWorkflow extends AgentWorkflow<MyAgent, TaskParams> {
126+
async run(event: AgentWorkflowEvent<TaskParams>, step: AgentWorkflowStep) {
127+
const params = event.payload;
128+
129+
// Call Agent methods via RPC
130+
await this.agent.updateStatus(params.taskId, "processing");
131+
132+
// Report progress (non-durable, for frequent updates)
133+
await this.reportProgress({
134+
step: "process",
135+
percent: 0.5,
136+
message: "Halfway done"
137+
});
138+
139+
// Durable state update via step (broadcasts to clients)
140+
await step.mergeAgentState({ taskProgress: 0.5 });
141+
142+
const result = await step.do("process", async () => {
143+
return processData(params.data);
144+
});
145+
146+
await step.reportComplete(result);
147+
return result;
148+
}
149+
}
150+
```
151+
152+
</TypeScriptExample>
153+
154+
Start the workflow from your Agent using `runWorkflow()`:
155+
156+
<TypeScriptExample>
157+
158+
```ts
159+
export class MyAgent extends Agent {
160+
async startTask(taskId: string, data: string) {
161+
// Automatically tracked in Agent's database
162+
const workflowId = await this.runWorkflow("PROCESSING_WORKFLOW", {
163+
taskId,
164+
data
165+
});
166+
return { workflowId };
167+
}
168+
169+
// Lifecycle callbacks
170+
async onWorkflowProgress(workflowName: string, workflowId: string, progress: unknown) {
171+
// Handle progress updates
172+
this.broadcast(JSON.stringify({ type: "progress", progress }));
173+
}
174+
175+
async onWorkflowComplete(workflowName: string, workflowId: string, result?: unknown) {
176+
// Handle completion
177+
}
178+
}
179+
```
180+
181+
</TypeScriptExample>
182+
183+
The `AgentWorkflow` class provides:
184+
185+
- **Typed Agent access** - Call Agent methods via RPC with full type safety
186+
- **Automatic tracking** - Workflows are tracked in the Agent's SQLite database
187+
- **Progress reporting** - Report progress to the Agent with custom types
188+
- **State synchronization** - Update Agent state from workflows (broadcasts to clients)
189+
- **Lifecycle callbacks** - React to workflow events in your Agent
190+
- **Human-in-the-loop** - Built-in approval flow support
191+
192+
For complete API reference and patterns, refer to [Integrate Cloudflare Workflows with Agents](/agents/guides/cloudflare-workflows-integration/).

0 commit comments

Comments
 (0)