workflow 5.0.0-beta.1 → 5.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +4 -4
  2. package/dist/api-workflow.d.ts +1 -1
  3. package/dist/api-workflow.d.ts.map +1 -1
  4. package/dist/api-workflow.js +2 -2
  5. package/dist/api.d.ts +5 -1
  6. package/dist/api.d.ts.map +1 -1
  7. package/dist/api.js +14 -2
  8. package/dist/astro.js +1 -1
  9. package/dist/index.js +1 -1
  10. package/dist/internal/builtins.d.ts +17 -0
  11. package/dist/internal/builtins.d.ts.map +1 -1
  12. package/dist/internal/builtins.js +65 -1
  13. package/dist/internal/class-serialization.js +1 -1
  14. package/dist/internal/errors.js +1 -1
  15. package/dist/nest.js +1 -1
  16. package/dist/next.cjs +1 -1
  17. package/dist/nitro.js +1 -1
  18. package/dist/nuxt.js +1 -1
  19. package/dist/observability.d.ts +1 -1
  20. package/dist/observability.js +2 -2
  21. package/dist/runtime.d.ts +1 -1
  22. package/dist/runtime.d.ts.map +1 -1
  23. package/dist/runtime.js +2 -2
  24. package/dist/stdlib.js +1 -1
  25. package/dist/sveltekit.js +1 -1
  26. package/dist/typescript-plugin.cjs +1 -1
  27. package/dist/vite.js +1 -1
  28. package/dist/workflow.js +1 -1
  29. package/docs/ai/index.mdx +6 -5
  30. package/docs/ai/resumable-streams.mdx +1 -1
  31. package/docs/api-reference/vitest/index.mdx +28 -1
  32. package/docs/api-reference/workflow/create-webhook.mdx +37 -18
  33. package/docs/api-reference/workflow/experimental-set-attributes.mdx +63 -0
  34. package/docs/api-reference/workflow/get-workflow-metadata.mdx +34 -0
  35. package/docs/api-reference/workflow/index.mdx +3 -0
  36. package/docs/api-reference/workflow-ai/durable-agent.mdx +0 -4
  37. package/docs/api-reference/workflow-ai/index.mdx +0 -5
  38. package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +0 -4
  39. package/docs/api-reference/workflow-api/start.mdx +5 -4
  40. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
  41. package/docs/api-reference/workflow-next/with-workflow.mdx +34 -2
  42. package/docs/changelog/attributes-mvp.mdx +368 -0
  43. package/docs/changelog/eager-processing.mdx +595 -0
  44. package/docs/changelog/index.mdx +2 -1
  45. package/docs/cookbook/advanced/child-workflows.mdx +313 -0
  46. package/docs/cookbook/advanced/meta.json +9 -0
  47. package/docs/cookbook/advanced/publishing-libraries.mdx +336 -0
  48. package/docs/cookbook/advanced/serializable-steps.mdx +147 -0
  49. package/docs/cookbook/advanced/upgrading-workflows.mdx +195 -0
  50. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +156 -0
  51. package/docs/cookbook/agent-patterns/durable-agent.mdx +154 -0
  52. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +255 -0
  53. package/docs/cookbook/agent-patterns/meta.json +4 -0
  54. package/docs/cookbook/common-patterns/batching.mdx +105 -0
  55. package/docs/cookbook/common-patterns/idempotency.mdx +107 -0
  56. package/docs/cookbook/common-patterns/meta.json +15 -0
  57. package/docs/cookbook/common-patterns/rate-limiting.mdx +228 -0
  58. package/docs/cookbook/common-patterns/saga.mdx +247 -0
  59. package/docs/cookbook/common-patterns/scheduling.mdx +129 -0
  60. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +155 -0
  61. package/docs/cookbook/common-patterns/timeouts.mdx +99 -0
  62. package/docs/cookbook/common-patterns/webhooks.mdx +185 -0
  63. package/docs/cookbook/common-patterns/workflow-composition.mdx +111 -0
  64. package/docs/cookbook/index.mdx +38 -0
  65. package/docs/cookbook/integrations/ai-sdk.mdx +383 -0
  66. package/docs/cookbook/integrations/chat-sdk.mdx +307 -0
  67. package/docs/cookbook/integrations/meta.json +4 -0
  68. package/docs/cookbook/integrations/sandbox.mdx +520 -0
  69. package/docs/cookbook/meta.json +5 -0
  70. package/docs/deploying/building-a-world.mdx +1 -1
  71. package/docs/deploying/world/local-world.mdx +1 -1
  72. package/docs/deploying/world/postgres-world.mdx +6 -4
  73. package/docs/deploying/world/vercel-world.mdx +3 -1
  74. package/docs/errors/abort-signal-timeout-in-workflow.mdx +80 -0
  75. package/docs/errors/hook-conflict.mdx +56 -4
  76. package/docs/errors/index.mdx +3 -0
  77. package/docs/errors/runtime-decryption-failed.mdx +77 -0
  78. package/docs/errors/start-invalid-workflow-function.mdx +1 -1
  79. package/docs/foundations/cancellation.mdx +460 -0
  80. package/docs/foundations/errors-and-retries.mdx +7 -3
  81. package/docs/foundations/index.mdx +3 -3
  82. package/docs/foundations/meta.json +3 -2
  83. package/docs/foundations/serialization.mdx +78 -42
  84. package/docs/foundations/starting-workflows.mdx +6 -2
  85. package/docs/foundations/versioning.mdx +263 -0
  86. package/docs/getting-started/astro.mdx +6 -0
  87. package/docs/getting-started/index.mdx +13 -7
  88. package/docs/getting-started/meta.json +3 -1
  89. package/docs/getting-started/nestjs.mdx +8 -0
  90. package/docs/getting-started/next.mdx +5 -3
  91. package/docs/getting-started/nitro.mdx +22 -0
  92. package/docs/getting-started/python.mdx +165 -0
  93. package/docs/getting-started/sveltekit.mdx +6 -0
  94. package/docs/getting-started/tanstack-start.mdx +241 -0
  95. package/docs/how-it-works/cancellation.mdx +287 -0
  96. package/docs/how-it-works/code-transform.mdx +2 -2
  97. package/docs/how-it-works/event-sourcing.mdx +2 -2
  98. package/docs/how-it-works/meta.json +2 -1
  99. package/docs/internal/index.mdx +19 -0
  100. package/docs/internal/meta.json +5 -0
  101. package/docs/internal/serializable-abort-controller.mdx +148 -0
  102. package/docs/meta.json +1 -0
  103. package/docs/migration-guides/index.mdx +34 -0
  104. package/docs/migration-guides/meta.json +9 -0
  105. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +358 -0
  106. package/docs/migration-guides/migrating-from-inngest.mdx +304 -0
  107. package/docs/migration-guides/migrating-from-temporal.mdx +313 -0
  108. package/docs/migration-guides/migrating-from-trigger-dev.mdx +328 -0
  109. package/docs/observability/attributes.mdx +66 -0
  110. package/docs/observability/index.mdx +9 -1
  111. package/docs/observability/meta.json +1 -1
  112. package/package.json +14 -13
  113. package/docs/foundations/common-patterns.mdx +0 -265
@@ -0,0 +1,147 @@
1
+ ---
2
+ title: Serializable Steps
3
+ description: Wrap non-serializable third-party objects (like AI model providers) inside step factory functions so they can cross the workflow boundary.
4
+ type: guide
5
+ summary: Return a callback from a step to defer construction of a non-owned class (AI SDK models, cloud SDK clients) until execution time, making them usable inside durable workflows.
6
+ related:
7
+ - /docs/foundations/serialization
8
+ - /docs/foundations/serialization#custom-class-serialization
9
+ - /docs/api-reference/workflow/use-step
10
+ ---
11
+
12
+ <Callout>
13
+ This is an advanced guide. It dives into workflow internals and is not required reading to use workflow.
14
+ </Callout>
15
+
16
+ ## When to use this pattern
17
+
18
+ Workflow functions run inside a sandboxed VM where every value that crosses a function boundary must be serializable. There are two ways to get a non-serializable object across that boundary, depending on whether you own the class:
19
+
20
+ - **You own the class** — implement the [`WORKFLOW_SERIALIZE` / `WORKFLOW_DESERIALIZE` protocol](/docs/foundations/serialization#custom-class-serialization). The instance becomes a first-class serializable value: you can pass it as a workflow input, return it from a step, and call `"use step"` instance methods on it directly. This is the right tool when the class is yours to modify.
21
+ - **You don't own the class** — you can't add methods to `openai("gpt-4o")` from `@ai-sdk/openai` or `new S3Client({...})` from `@aws-sdk/client-s3`. Instead, wrap construction in a `"use step"` factory function and pass the factory across the boundary. That's what this page covers.
22
+
23
+ ## The Problem
24
+
25
+ AI SDK model providers — `openai("gpt-4o")`, `anthropic("claude-sonnet-4-20250514")`, etc. — return complex objects with methods, closures, and internal state. Passing one directly into a step causes a serialization error, and you can't bolt `WORKFLOW_SERIALIZE` onto a third-party class.
26
+
27
+ ```typescript lineNumbers
28
+ import { openai } from "@ai-sdk/openai";
29
+ import { DurableAgent } from "@workflow/ai/agent";
30
+ import { getWritable } from "workflow";
31
+ import type { UIMessageChunk } from "ai";
32
+
33
+ export async function brokenAgent(prompt: string) {
34
+ "use workflow";
35
+
36
+ const writable = getWritable<UIMessageChunk>();
37
+ const agent = new DurableAgent({
38
+ // This fails — the model object is not serializable
39
+ model: openai("gpt-4o"),
40
+ });
41
+
42
+ await agent.stream({ messages: [{ role: "user", content: prompt }], writable });
43
+ }
44
+ ```
45
+
46
+ ## The Solution: Step-as-Factory
47
+
48
+ Instead of passing the model object, pass a **callback function** that returns the model. Marking that callback with `"use step"` tells the compiler to serialize the *function reference* (which is just a string identifier) rather than its return value. The provider is only instantiated at execution time, inside the step's full Node.js runtime.
49
+
50
+ ```typescript lineNumbers
51
+ import { openai as openaiProvider } from "@ai-sdk/openai";
52
+
53
+ // Returns a step function, not a model object
54
+ export function openai(...args: Parameters<typeof openaiProvider>) {
55
+ return async () => {
56
+ "use step";
57
+ return openaiProvider(...args); // [!code highlight]
58
+ };
59
+ }
60
+ ```
61
+
62
+ The `DurableAgent` receives a function (`() => Promise<LanguageModel>`) instead of a model object. When the agent needs to call the LLM, it invokes the factory inside a step where the real provider can be constructed with full Node.js access.
63
+
64
+ ## How `@workflow/ai` Uses This
65
+
66
+ The `@workflow/ai` package ships pre-wrapped providers for all major AI SDK backends. Each one follows the same pattern:
67
+
68
+ ```typescript lineNumbers
69
+ // packages/ai/src/providers/anthropic.ts
70
+ import { anthropic as anthropicProvider } from "@ai-sdk/anthropic";
71
+
72
+ export function anthropic(...args: Parameters<typeof anthropicProvider>) {
73
+ return async () => {
74
+ "use step";
75
+ return anthropicProvider(...args); // [!code highlight]
76
+ };
77
+ }
78
+ ```
79
+
80
+ This means you import from `@workflow/ai` instead of `@ai-sdk/*` directly:
81
+
82
+ ```typescript lineNumbers
83
+ import { anthropic } from "@workflow/ai/anthropic";
84
+ import { DurableAgent } from "@workflow/ai/agent";
85
+ import { getWritable } from "workflow";
86
+ import type { UIMessageChunk } from "ai";
87
+
88
+ export async function chatAgent(prompt: string) {
89
+ "use workflow";
90
+
91
+ const writable = getWritable<UIMessageChunk>();
92
+ const agent = new DurableAgent({
93
+ model: anthropic("claude-sonnet-4-20250514"), // [!code highlight]
94
+ });
95
+
96
+ await agent.stream({ messages: [{ role: "user", content: prompt }], writable });
97
+ }
98
+ ```
99
+
100
+ ## Writing Your Own Serializable Wrapper
101
+
102
+ Apply the same pattern to any non-serializable dependency. The key rule: **the outer function captures serializable arguments, and the inner `"use step"` function constructs the real object at runtime**.
103
+
104
+ ```typescript lineNumbers
105
+ import type { S3Client as S3ClientType } from "@aws-sdk/client-s3";
106
+
107
+ // The arguments (region, bucket) are plain strings — serializable
108
+ export function createS3Client(region: string) {
109
+ return async (): Promise<S3ClientType> => {
110
+ "use step";
111
+ const { S3Client } = await import("@aws-sdk/client-s3");
112
+ return new S3Client({ region });
113
+ };
114
+ }
115
+
116
+ // Usage in a workflow
117
+ export async function processUpload(region: string, key: string) {
118
+ "use workflow";
119
+
120
+ const getClient = createS3Client(region); // [!code highlight]
121
+ // getClient is a serializable step reference, not an S3Client
122
+ await uploadFile(getClient, key);
123
+ }
124
+
125
+ async function uploadFile(
126
+ getClient: () => Promise<S3ClientType>,
127
+ key: string
128
+ ) {
129
+ "use step";
130
+ const client = await getClient(); // [!code highlight]
131
+ // Now you have a real S3Client with full Node.js access
132
+ await client.send(/* ... */);
133
+ }
134
+ ```
135
+
136
+ ## Why This Works
137
+
138
+ 1. **Compiler transformation**: `"use step"` tells the SWC plugin to extract the function into a separate bundle. The workflow VM only sees a serializable reference (function ID + captured arguments).
139
+ 2. **Closure tracking**: The compiler tracks which variables the step function closes over. Only serializable values (strings, numbers, plain objects) can be captured.
140
+ 3. **Deferred construction**: The actual provider/client is only constructed when the step executes in the Node.js runtime — never in the sandboxed workflow VM.
141
+
142
+ ## Key APIs
143
+
144
+ - [`"use step"`](/docs/api-reference/workflow/use-step) — marks a function for extraction and serialization
145
+ - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
146
+ - [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — accepts a model factory for durable AI agent streaming
147
+ - [Custom class serialization](/docs/foundations/serialization#custom-class-serialization) — the companion pattern for classes you own (`WORKFLOW_SERIALIZE` / `WORKFLOW_DESERIALIZE`)
@@ -0,0 +1,195 @@
1
+ ---
2
+ title: Upgrading Workflows
3
+ description: Identify a clean upgrade point in a long-running workflow and spawn a fresh run on the latest deployment carrying state forward.
4
+ type: guide
5
+ summary: 'Identify a clean upgrade point and hand off to a fresh run via `start(self, [state], { deploymentId: "latest" })` — either automatically on every iteration, or on demand via a dedicated upgrade hook.'
6
+ related:
7
+ - /docs/foundations/versioning
8
+ - /cookbook/common-patterns/workflow-composition
9
+ - /docs/api-reference/workflow-api/start
10
+ - /docs/foundations/hooks
11
+ ---
12
+
13
+ Workflows that block on external events for days, weeks, or months can outlive many deployments. **The key is to identify a clean upgrade point in the workflow** — a moment where it's safe to checkpoint state and start fresh — and then call [`start()`](/docs/api-reference/workflow-api/start) with `deploymentId: "latest"` to spawn a new run carrying that state forward. The current run ends; the next run begins on whatever deployment is live at that moment, so shipped fixes apply immediately without ever migrating an in-flight run.
14
+
15
+ <Callout type="info">
16
+ For the underlying model — why runs pin to a deployment by default, how cancel-and-rerun works, and how state crosses the version boundary — see [Versioning](/docs/foundations/versioning). This recipe focuses on event-driven workflows that need to keep advancing across deployments.
17
+ </Callout>
18
+
19
+ A clean upgrade point is any spot in the workflow where:
20
+
21
+ - All in-progress side effects have completed (or aren't needed by the next iteration)
22
+ - The relevant state can be serialized into the workflow's input arguments
23
+ - It's natural for the workflow to "checkpoint" — typically right after handling an external event, completing a batch, or finishing a logical phase
24
+
25
+ There are two ways to apply this:
26
+
27
+ 1. **Upgrade on every iteration** ([Method 1](#method-1-upgrade-on-every-iteration)). Each run handles a single event and unconditionally hands off to a fresh run on the latest deployment before exiting. Simple — no extra triggers — but every event pays the respawn cost.
28
+ 2. **Upgrade on demand via a dedicated hook** ([Method 2](#method-2-upgrade-on-demand-via-a-dedicated-hook)). A single long-lived run handles many events in a loop and only respawns when an `upgradeHook` fires. A separate endpoint resumes that hook from your control plane (e.g. after a deploy). More control and fewer respawns, at the cost of an explicit trigger.
29
+
30
+ ### When to use each
31
+
32
+ - **Method 1** when iterations are short and frequent, the work is cheap to checkpoint, and you want shipped fixes to apply on the very next event. Long-lived "session" workflows (subscriptions, queues, FSMs) that already process events one at a time fit this naturally.
33
+ - **Method 2** when iterations are infrequent or expensive (you don't want to respawn on every event), or when you need to roll out a fix to a fleet of in-flight runs after a deploy by fanning out to a control-plane endpoint. Also fits when "upgrade" should be an explicit operation rather than a side effect of handling each event.
34
+
35
+ ## Method 1: Upgrade on every iteration
36
+
37
+ Each run inherits state via its argument, blocks on a hook, processes the resume, then unconditionally hands off to its successor by calling `start()` directly from the workflow body with `deploymentId: "latest"`.
38
+
39
+ ```typescript lineNumbers
40
+ import { defineHook, getWorkflowMetadata } from "workflow";
41
+ import { start } from "workflow/api";
42
+
43
+ declare function processItem(itemId: string): Promise<void>; // @setup
44
+
45
+ interface QueueState {
46
+ processed: number;
47
+ cursor: string | null;
48
+ }
49
+
50
+ export const nextItemHook = defineHook<{ itemId: string }>();
51
+
52
+ export async function longRunningQueue(
53
+ state: QueueState = { processed: 0, cursor: null },
54
+ ): Promise<void> {
55
+ "use workflow";
56
+
57
+ const { workflowRunId } = getWorkflowMetadata();
58
+
59
+ // Block until something fires the hook — could be hours, days, or longer.
60
+ // Per-run hook tokens (workflowRunId) keep concurrent chains isolated.
61
+ const { itemId } = await nextItemHook.create({ token: workflowRunId }); // [!code highlight]
62
+
63
+ await processItem(itemId);
64
+
65
+ // Hand off to a fresh run on the latest deployment. THIS run ends here.
66
+ // `deploymentId: "latest"` resolves to whichever deployment is current
67
+ // when this spawn lands — NOT the deployment running this code.
68
+ await start( // [!code highlight]
69
+ longRunningQueue, // [!code highlight]
70
+ [{ processed: state.processed + 1, cursor: itemId }], // [!code highlight]
71
+ { deploymentId: "latest" }, // [!code highlight]
72
+ );
73
+ }
74
+ ```
75
+
76
+ ### Resuming the hook
77
+
78
+ Any server-side code can resume the currently-active iteration by calling `.resume()` with the run ID:
79
+
80
+ ```typescript
81
+ import { nextItemHook } from "@/workflows/long-running-queue";
82
+
83
+ export async function POST(req: Request) {
84
+ const { runId, itemId } = await req.json();
85
+
86
+ await nextItemHook.resume(runId, { itemId }); // [!code highlight]
87
+
88
+ return Response.json({ success: true });
89
+ }
90
+ ```
91
+
92
+ The caller tracks the active `runId` (e.g. in a database, KV, or returned from the previous iteration) and updates it whenever the chain advances.
93
+
94
+ ## Method 2: Upgrade on demand via a dedicated hook
95
+
96
+ Use a single long-running workflow that handles events in a loop. Define a second hook — `upgradeHook` — alongside the work hook, and race them. While only the work hook fires, the run keeps handling events on its current deployment. When `upgradeHook` resumes, the workflow captures current state and respawns on the latest deployment, then exits.
97
+
98
+ ```typescript lineNumbers
99
+ import { defineHook, getWorkflowMetadata } from "workflow";
100
+ import { start } from "workflow/api";
101
+
102
+ declare function processItem(itemId: string): Promise<void>; // @setup
103
+
104
+ interface QueueState {
105
+ processed: number;
106
+ cursor: string | null;
107
+ }
108
+
109
+ export const nextItemHook = defineHook<{ itemId: string }>();
110
+ export const upgradeHook = defineHook<{ reason?: string }>(); // [!code highlight]
111
+
112
+ export async function longRunningQueue(
113
+ state: QueueState = { processed: 0, cursor: null },
114
+ ): Promise<void> {
115
+ "use workflow";
116
+
117
+ const { workflowRunId } = getWorkflowMetadata();
118
+
119
+ while (true) {
120
+ // Race a normal work event against the upgrade signal.
121
+ const event = await Promise.race([ // [!code highlight]
122
+ nextItemHook
123
+ .create({ token: workflowRunId })
124
+ .then((payload) => ({ kind: "work" as const, payload })),
125
+ upgradeHook // [!code highlight]
126
+ .create({ token: workflowRunId }) // [!code highlight]
127
+ .then(() => ({ kind: "upgrade" as const })), // [!code highlight]
128
+ ]);
129
+
130
+ if (event.kind === "upgrade") { // [!code highlight]
131
+ // Checkpoint current state and hand off to a fresh run
132
+ // on whatever deployment is live now. THIS run ends here.
133
+ await start(longRunningQueue, [state], { // [!code highlight]
134
+ deploymentId: "latest", // [!code highlight]
135
+ }); // [!code highlight]
136
+ return; // [!code highlight]
137
+ }
138
+
139
+ await processItem(event.payload.itemId);
140
+ state = {
141
+ processed: state.processed + 1,
142
+ cursor: event.payload.itemId,
143
+ };
144
+ }
145
+ }
146
+ ```
147
+
148
+ ### Triggering the upgrade
149
+
150
+ Expose a separate endpoint that resumes `upgradeHook` for a given run. Call it from your deploy pipeline, an admin UI, or a fan-out script that iterates over every active run after shipping a fix.
151
+
152
+ ```typescript
153
+ import { upgradeHook } from "@/workflows/long-running-queue";
154
+
155
+ export async function POST(req: Request) {
156
+ const { runId, reason } = await req.json();
157
+
158
+ // The workflow exits its loop, captures state, and respawns
159
+ // on the latest deployment.
160
+ await upgradeHook.resume(runId, { reason }); // [!code highlight]
161
+
162
+ return Response.json({ success: true });
163
+ }
164
+ ```
165
+
166
+ To upgrade a fleet of runs after a deploy, list active runs (e.g. from a tracking store) and call this endpoint for each.
167
+
168
+ ## How it works
169
+
170
+ 1. **`deploymentId: "latest"` is the upgrade knob.** Without it, the spawn pins to the current deployment. With it, the new run resolves to whatever deployment is current when the runtime picks it up — so any shipped fix applies starting from that respawn. Both methods rely on this.
171
+ 2. **`start()` runs directly from the workflow body.** In v5, [`start()`](/docs/api-reference/workflow-api/start) is step-backed, so it can be called from a workflow function and still records a deterministic step boundary in the event log — no manual `"use step"` wrapper is required.
172
+ 3. **State carries through the function argument.** The accumulating context flows from run N to run N+1 as a serialized argument. No external store is required for the state itself.
173
+ 4. **Per-run hook tokens.** Using `workflowRunId` as the hook token scopes each iteration's wait to its own run, so multiple chains can run concurrently without interfering.
174
+ 5. **Method 1 vs Method 2 is just where the spawn happens.** In Method 1 every run spawns its successor unconditionally before exiting — there is no long-lived process to migrate. In Method 2 the spawn happens only when the upgrade hook fires; otherwise the loop keeps handling events on the same run.
175
+
176
+ ## Adapting to your use case
177
+
178
+ - **Combine with a sleep.** Race the hook against `sleep()` so iterations also tick on a timer: `Promise.race([hook, sleep("1d")])` lets the workflow advance even if no external event arrives.
179
+ - **Stateless successors.** If the next iteration doesn't need the previous state (e.g. a pure event router), call `start(longRunningQueue, [], { deploymentId: "latest" })` and skip the argument plumbing.
180
+ - **Persist state externally.** If state needs to be readable from outside the workflow (dashboards, debugging, recovery), write it to a database in a step before spawning the next run.
181
+ - **Track the active runId externally.** Whatever resumes the hook needs to know the current run. Capture the `runId` returned by `start()` and write it to a KV/database keyed by a stable session identifier (in a step) so resumers always look up the latest one.
182
+
183
+ ## Caveats
184
+
185
+ - **Backward compatibility matters.** Because the next run executes on a different deployment, the workflow's input arguments and return type must remain compatible across deployments. Adding required fields, removing fields, or changing types can cause serialization failures. See the [`deploymentId: "latest"` callout](/docs/api-reference/workflow-api/start#using-deploymentid-latest).
186
+ - **Workflow identity is the function name + file path.** Renaming the function or moving the file across a deployment changes the workflow ID — the next iteration will fail to resolve. Treat the workflow's name and location as stable interfaces.
187
+ - **There is a tiny gap between iterations.** The current run ends as soon as `start()` returns; the next run starts asynchronously. A resume that arrives in that window can fail with "hook not found." Make resumers retry, or have the API persist pending payloads and apply them once the next iteration is ready.
188
+ - **Method 2: track active runs externally.** Because Method 2's runs are long-lived, the set of in-flight runs only changes when one starts, completes, or upgrades. Persist run IDs (and clean them up on completion or upgrade) so a rollout script can fan out reliably. After resuming `upgradeHook`, also update the tracked run ID once the new run reports back, the same way you would in Method 1.
189
+
190
+ ## Key APIs
191
+
192
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps) — marks the orchestrator function
193
+ - [`start()`](/docs/api-reference/workflow-api/start) with [`deploymentId: "latest"`](/docs/api-reference/workflow-api/start#using-deploymentid-latest) — spawn the successor on the newest deployment
194
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook) — suspend the workflow until an external event resumes it
195
+ - [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) — exposes `workflowRunId` for per-run hook tokens
@@ -0,0 +1,156 @@
1
+ ---
2
+ title: Agent Cancellation
3
+ description: Cancel a running agent from the outside using AbortSignal — a hook fires the abort, the agent step bails out of the model stream, and the client gets a clean stop notification.
4
+ type: guide
5
+ summary: Cancel a running agent cooperatively with AbortController. A stop hook fires controller.abort(), the signal propagates into the agent step to cancel the model stream, and a data-stopped part is emitted to streaming clients before the workflow returns.
6
+ ---
7
+
8
+ Cancel a running agent from the outside — for example, a "Stop" button in a chat UI, an admin cancellation endpoint, or a timeout fallback.
9
+
10
+ ## Pattern
11
+
12
+ Create an `AbortController` in the workflow and race the agent (passing its signal) against a stop hook. When the hook fires, `controller.abort()` is called — the signal propagates into the agent step and cancels the underlying model stream. Before returning, a `data-stopped` part is written to the stream so any streaming clients can render a clean end state.
13
+
14
+ ```typescript lineNumbers
15
+ import { DurableAgent } from "@workflow/ai/agent";
16
+ import { defineHook, getWritable, getWorkflowMetadata } from "workflow";
17
+ import { z } from "zod";
18
+ import type { ModelMessage, UIMessageChunk } from "ai";
19
+
20
+ export const stopHook = defineHook({
21
+ schema: z.object({ reason: z.string().optional() }),
22
+ });
23
+
24
+ async function searchWeb({ query }: { query: string }) {
25
+ "use step";
26
+ await new Promise((r) => setTimeout(r, 1500));
27
+ return { results: [{ title: `${query} - Wikipedia`, snippet: `Overview of ${query}...` }] };
28
+ }
29
+
30
+ async function analyzeData({ topic }: { topic: string }) {
31
+ "use step";
32
+ await new Promise((r) => setTimeout(r, 1200));
33
+ return { summary: `Analysis of ${topic}: significant developments found.`, confidence: 0.85 };
34
+ }
35
+
36
+ async function emitStopSignal(details: { reason?: string }) {
37
+ "use step";
38
+ const writer = getWritable<UIMessageChunk>().getWriter();
39
+ try {
40
+ await writer.write({ type: "data-stopped", id: "stop-signal", data: details } as UIMessageChunk);
41
+ } finally {
42
+ writer.releaseLock();
43
+ }
44
+ }
45
+
46
+ export async function stoppableAgent(messages: ModelMessage[]) {
47
+ "use workflow";
48
+
49
+ const { workflowRunId } = getWorkflowMetadata();
50
+ const controller = new AbortController(); // [!code highlight]
51
+ const hook = stopHook.create({ token: `stop:${workflowRunId}` });
52
+
53
+ const agent = new DurableAgent({
54
+ model: "anthropic/claude-haiku-4.5",
55
+ instructions: "You are a research assistant. Search and analyze data as needed.",
56
+ tools: {
57
+ searchWeb: {
58
+ description: "Search the web for information",
59
+ inputSchema: z.object({ query: z.string() }),
60
+ execute: searchWeb,
61
+ },
62
+ analyzeData: {
63
+ description: "Analyze a piece of data",
64
+ inputSchema: z.object({ topic: z.string() }),
65
+ execute: analyzeData,
66
+ },
67
+ },
68
+ });
69
+
70
+ const result = await Promise.race([
71
+ agent
72
+ .stream({
73
+ messages,
74
+ writable: getWritable<UIMessageChunk>(),
75
+ abortSignal: controller.signal, // [!code highlight]
76
+ maxSteps: 15,
77
+ })
78
+ .then((r) => ({ type: "complete" as const, messages: r.messages })),
79
+ hook.then(({ reason }) => {
80
+ controller.abort(reason); // [!code highlight]
81
+ return { type: "stopped" as const, reason };
82
+ }),
83
+ ]);
84
+
85
+ if (result.type === "stopped") {
86
+ await emitStopSignal({ reason: result.reason });
87
+ }
88
+
89
+ return result;
90
+ }
91
+ ```
92
+
93
+ ### API Route to Trigger Stop
94
+
95
+ ```typescript lineNumbers
96
+ import { stopHook } from "@/workflows/stoppable-agent";
97
+
98
+ export async function POST(
99
+ request: Request,
100
+ { params }: { params: Promise<{ runId: string }> }
101
+ ) {
102
+ const { runId } = await params;
103
+ const { reason } = await request.json();
104
+
105
+ await stopHook.resume(`stop:${runId}`, {
106
+ reason: reason || "User requested stop",
107
+ });
108
+
109
+ return Response.json({ success: true });
110
+ }
111
+ ```
112
+
113
+ ### Client Stop Button
114
+
115
+ ```tsx lineNumbers
116
+ "use client";
117
+
118
+ export function StopButton({ runId }: { runId: string }) {
119
+ const handleStop = async () => {
120
+ await fetch(`/api/chat/${runId}/stop`, {
121
+ method: "POST",
122
+ headers: { "Content-Type": "application/json" },
123
+ body: JSON.stringify({ reason: "User clicked stop" }),
124
+ });
125
+ };
126
+
127
+ return (
128
+ <button type="button" onClick={handleStop}>
129
+ Stop Agent
130
+ </button>
131
+ );
132
+ }
133
+ ```
134
+
135
+ ## How it works
136
+
137
+ 1. An `AbortController` is created at the start of the workflow
138
+ 2. A hook is created with token `stop:${workflowRunId}`
139
+ 3. `Promise.race` runs the agent stream and the stop hook concurrently
140
+ 4. The agent receives `controller.signal` — when aborted, the underlying model stream is cancelled
141
+ 5. When the stop API resumes the hook, `controller.abort()` is called — the race resolves and the workflow exits
142
+ 6. `emitStopSignal` writes a `data-stopped` part to the stream so the client renders a clean stop state
143
+
144
+ ## Adapting this
145
+
146
+ * **Add a timeout** — race a third `sleep()` promise to auto-stop after a deadline
147
+ * **Audit logging** — include a `reason` field in the stop schema to record who stopped and why
148
+ * **Cross-process** — the hook token is deterministic, so any process can call `stopHook.resume()` with the run ID
149
+ * **Step limits** — combine with `maxSteps` on the agent to cap execution even without manual stop
150
+
151
+ ## Key APIs
152
+
153
+ * [`defineHook()`](/docs/api-reference/workflow/define-hook) — type-safe hook for the stop signal
154
+ * [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) — access the run ID for deterministic hook tokens
155
+ * [`getWritable()`](/docs/api-reference/workflow/get-writable) — stream output and the stop notification to the client
156
+ * [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — the agent that respects the abort signal via its `signal` option
@@ -0,0 +1,154 @@
1
+ ---
2
+ title: Durable Agent
3
+ description: Replace a stateless AI agent with a durable one that survives crashes, retries tool calls, and streams output.
4
+ type: guide
5
+ summary: Convert an AI SDK Agent into a DurableAgent backed by a workflow, with tools as retryable steps.
6
+ ---
7
+
8
+ Use this pattern to make any AI SDK agent durable. The agent becomes a workflow, tools become steps, and the framework handles retries, streaming, and state persistence automatically.
9
+
10
+ ## When to use this
11
+
12
+ - Any AI agent with tool calls that should survive crashes and restarts
13
+ - Agents where tool calls hit external APIs that need automatic retries
14
+ - Long-running agent sessions where losing progress is unacceptable
15
+ - Agents that need per-step observability in the workflow event log
16
+
17
+ <Callout type="info">
18
+ A durable agent run stays on the deployment that started it. For multi-turn agents that should pick up newer code between turns, see [Versioning](/docs/foundations/versioning) for patterns that start the next turn or next session run with `deploymentId: "latest"`.
19
+ </Callout>
20
+
21
+ ## Pattern
22
+
23
+ Replace `Agent` with `DurableAgent`, wrap the function in `"use workflow"`, mark each tool with `"use step"`, and stream output through `getWritable()`.
24
+
25
+ ### Workflow
26
+
27
+ ```typescript
28
+ import { DurableAgent } from "@workflow/ai/agent";
29
+ import { getWritable } from "workflow";
30
+ import { z } from "zod";
31
+ import type { ModelMessage, UIMessageChunk } from "ai";
32
+
33
+ async function searchFlights({ from, to, date }: {
34
+ from: string;
35
+ to: string;
36
+ date: string;
37
+ }) {
38
+ "use step"; // [!code highlight]
39
+ const res = await fetch(
40
+ `https://api.example.com/flights?from=${from}&to=${to}&date=${date}`
41
+ );
42
+ if (!res.ok) throw new Error(`Search failed: ${res.status}`);
43
+ return res.json();
44
+ }
45
+
46
+ async function bookFlight({ flightId, passenger }: {
47
+ flightId: string;
48
+ passenger: string;
49
+ }) {
50
+ "use step"; // [!code highlight]
51
+ const res = await fetch("https://api.example.com/bookings", {
52
+ method: "POST",
53
+ headers: { "Content-Type": "application/json" },
54
+ body: JSON.stringify({ flightId, passenger }),
55
+ });
56
+ if (!res.ok) throw new Error(`Booking failed: ${res.status}`);
57
+ return res.json();
58
+ }
59
+
60
+ async function checkWeather({ city }: { city: string }) {
61
+ "use step"; // [!code highlight]
62
+ const res = await fetch(`https://api.weather.com/forecast?city=${city}`);
63
+ return res.json();
64
+ }
65
+
66
+ export async function flightAgent(messages: ModelMessage[]) {
67
+ "use workflow";
68
+
69
+ const agent = new DurableAgent({ // [!code highlight]
70
+ model: "anthropic/claude-haiku-4.5",
71
+ instructions: "You are a helpful flight booking assistant.",
72
+ tools: {
73
+ searchFlights: {
74
+ description: "Search for available flights between two airports",
75
+ inputSchema: z.object({
76
+ from: z.string().describe("Departure airport code"),
77
+ to: z.string().describe("Arrival airport code"),
78
+ date: z.string().describe("Travel date (YYYY-MM-DD)"),
79
+ }),
80
+ execute: searchFlights,
81
+ },
82
+ bookFlight: {
83
+ description: "Book a specific flight for a passenger",
84
+ inputSchema: z.object({
85
+ flightId: z.string().describe("Flight ID from search results"),
86
+ passenger: z.string().describe("Passenger full name"),
87
+ }),
88
+ execute: bookFlight,
89
+ },
90
+ checkWeather: {
91
+ description: "Check the weather forecast for a city",
92
+ inputSchema: z.object({
93
+ city: z.string().describe("City name"),
94
+ }),
95
+ execute: checkWeather,
96
+ },
97
+ },
98
+ });
99
+
100
+ const result = await agent.stream({ // [!code highlight]
101
+ messages,
102
+ writable: getWritable<UIMessageChunk>(), // [!code highlight]
103
+ maxSteps: 10,
104
+ });
105
+
106
+ return { messages: result.messages };
107
+ }
108
+ ```
109
+
110
+ ### API route
111
+
112
+ ```typescript
113
+ import type { UIMessage } from "ai";
114
+ import { convertToModelMessages, createUIMessageStreamResponse } from "ai";
115
+ import { start } from "workflow/api";
116
+ import { flightAgent } from "@/app/workflows/flight-agent";
117
+
118
+ export async function POST(req: Request) {
119
+ const { messages }: { messages: UIMessage[] } = await req.json();
120
+ const modelMessages = await convertToModelMessages(messages); // [!code highlight]
121
+
122
+ const run = await start(flightAgent, [modelMessages]); // [!code highlight]
123
+
124
+ return createUIMessageStreamResponse({ // [!code highlight]
125
+ stream: run.readable,
126
+ headers: {
127
+ "x-workflow-run-id": run.runId,
128
+ },
129
+ });
130
+ }
131
+ ```
132
+
133
+ ## How it works
134
+
135
+ 1. **DurableAgent wraps Agent** — same API as AI SDK's `Agent`, but backed by a workflow. If the process crashes, the agent resumes from the last completed step on replay.
136
+ 2. **Tools as steps** — each tool's `execute` function uses `"use step"`, giving it automatic retries, full Node.js access, and an entry in the workflow event log.
137
+ 3. **Streaming** — `getWritable<UIMessageChunk>()` streams the agent's output (text chunks, tool calls, tool results) to the client in real time via `createUIMessageStreamResponse`.
138
+ 4. **maxSteps** — limits the total number of LLM calls the agent can make, preventing runaway tool loops.
139
+
140
+ ## Adapting to your use case
141
+
142
+ - **Change the model** — replace `"anthropic/claude-haiku-4.5"` with any AI Gateway model string (e.g. `"openai/gpt-4o"`, `"anthropic/claude-sonnet-4-5"`).
143
+ - **Add tools** — define a new `"use step"` function with a Zod schema. Each tool automatically gets retries and persistence.
144
+ - **Workflow-level tools** — if a tool needs workflow primitives like `sleep()` or `createHook()`, omit `"use step"` so it runs in the workflow context instead.
145
+ - **Multi-turn** — pass `result.messages` plus new user messages to subsequent `agent.stream()` calls for multi-turn conversations.
146
+ - **Client integration** — use `useChat()` from `@ai-sdk/react` with `WorkflowChatTransport` from `@workflow/ai` for a full chat UI with reconnection support.
147
+
148
+ ## Key APIs
149
+
150
+ - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
151
+ - [`"use step"`](/docs/api-reference/workflow/use-step) — declares step functions with retries and full Node.js access
152
+ - [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — durable wrapper around AI SDK's Agent
153
+ - [`getWritable()`](/docs/api-reference/workflow/get-writable) — streams agent output to the client
154
+ - [`start()`](/docs/api-reference/workflow-api/start) — starts a workflow run from an API route