workflow 5.0.0-beta.43 → 5.0.0-beta.44

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 (182) hide show
  1. package/README.md +6 -4
  2. package/dist/internal/builtins.d.ts +4 -4
  3. package/dist/internal/builtins.js +6 -6
  4. package/docs/ai/chat-session-modeling.mdx +23 -24
  5. package/docs/ai/defining-tools.mdx +5 -6
  6. package/docs/ai/human-in-the-loop.mdx +11 -11
  7. package/docs/ai/index.mdx +20 -20
  8. package/docs/ai/message-queueing.mdx +6 -6
  9. package/docs/ai/meta.json +1 -0
  10. package/docs/ai/resumable-streams.mdx +28 -28
  11. package/docs/ai/sleep-and-delays.mdx +9 -9
  12. package/docs/ai/streaming-updates-from-tools.mdx +4 -4
  13. package/docs/api-reference/vitest/index.mdx +8 -8
  14. package/docs/api-reference/workflow/create-hook.mdx +15 -15
  15. package/docs/api-reference/workflow/create-webhook.mdx +15 -15
  16. package/docs/api-reference/workflow/define-hook.mdx +10 -10
  17. package/docs/api-reference/workflow/fatal-error.mdx +2 -2
  18. package/docs/api-reference/workflow/fetch.mdx +7 -7
  19. package/docs/api-reference/workflow/get-step-metadata.mdx +2 -2
  20. package/docs/api-reference/workflow/get-workflow-metadata.mdx +3 -3
  21. package/docs/api-reference/workflow/get-writable.mdx +7 -7
  22. package/docs/api-reference/workflow/index.mdx +1 -1
  23. package/docs/api-reference/workflow/retryable-error.mdx +1 -1
  24. package/docs/api-reference/workflow/set-attributes.mdx +2 -2
  25. package/docs/api-reference/workflow/sleep.mdx +3 -3
  26. package/docs/api-reference/workflow-ai/durable-agent.mdx +42 -42
  27. package/docs/api-reference/workflow-ai/index.mdx +3 -3
  28. package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +28 -28
  29. package/docs/api-reference/workflow-api/get-hook-by-token.mdx +11 -11
  30. package/docs/api-reference/workflow-api/get-run.mdx +10 -10
  31. package/docs/api-reference/workflow-api/index.mdx +2 -4
  32. package/docs/api-reference/workflow-api/resume-hook.mdx +14 -14
  33. package/docs/api-reference/workflow-api/resume-webhook.mdx +3 -3
  34. package/docs/api-reference/workflow-api/start.mdx +16 -15
  35. package/docs/api-reference/workflow-astro/workflow.mdx +3 -3
  36. package/docs/api-reference/workflow-errors/entity-conflict-error.mdx +4 -4
  37. package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +4 -4
  38. package/docs/api-reference/workflow-errors/hook-not-found-error.mdx +8 -8
  39. package/docs/api-reference/workflow-errors/index.mdx +6 -6
  40. package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +9 -9
  41. package/docs/api-reference/workflow-errors/run-expired-error.mdx +2 -2
  42. package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +4 -4
  43. package/docs/api-reference/workflow-errors/step-not-registered-error.mdx +5 -5
  44. package/docs/api-reference/workflow-errors/throttle-error.mdx +2 -2
  45. package/docs/api-reference/workflow-errors/too-early-error.mdx +2 -2
  46. package/docs/api-reference/workflow-errors/workflow-error.mdx +4 -4
  47. package/docs/api-reference/workflow-errors/workflow-not-registered-error.mdx +5 -6
  48. package/docs/api-reference/workflow-errors/workflow-run-cancelled-error.mdx +6 -6
  49. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +5 -5
  50. package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +4 -4
  51. package/docs/api-reference/workflow-errors/workflow-run-not-found-error.mdx +4 -4
  52. package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +2 -2
  53. package/docs/api-reference/workflow-errors/workflow-world-error.mdx +8 -8
  54. package/docs/api-reference/workflow-globals.mdx +12 -11
  55. package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +2 -2
  56. package/docs/api-reference/workflow-nest/nest-local-builder.mdx +4 -4
  57. package/docs/api-reference/workflow-nest/workflow-controller.mdx +1 -1
  58. package/docs/api-reference/workflow-nest/workflow-module.mdx +4 -4
  59. package/docs/api-reference/workflow-next/with-workflow.mdx +16 -16
  60. package/docs/api-reference/workflow-nitro/index.mdx +6 -6
  61. package/docs/api-reference/workflow-nuxt/index.mdx +4 -4
  62. package/docs/api-reference/workflow-observability/hydrate-data.mdx +5 -5
  63. package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +4 -4
  64. package/docs/api-reference/workflow-observability/index.mdx +6 -8
  65. package/docs/api-reference/workflow-observability/observability-revivers.mdx +2 -2
  66. package/docs/api-reference/workflow-observability/parse-class-name.mdx +3 -3
  67. package/docs/api-reference/workflow-observability/parse-step-name.mdx +4 -4
  68. package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +4 -4
  69. package/docs/api-reference/workflow-runtime/create-world.mdx +8 -8
  70. package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +6 -6
  71. package/docs/api-reference/workflow-runtime/get-world.mdx +4 -4
  72. package/docs/api-reference/workflow-runtime/health-check.mdx +1 -1
  73. package/docs/api-reference/workflow-runtime/index.mdx +2 -4
  74. package/docs/api-reference/workflow-runtime/set-world.mdx +15 -13
  75. package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +6 -5
  76. package/docs/api-reference/workflow-runtime/world/analytics.mdx +9 -9
  77. package/docs/api-reference/workflow-runtime/world/index.mdx +5 -3
  78. package/docs/api-reference/workflow-runtime/world/queue.mdx +11 -11
  79. package/docs/api-reference/workflow-runtime/world/storage.mdx +62 -28
  80. package/docs/api-reference/workflow-runtime/world/streams.mdx +7 -7
  81. package/docs/api-reference/workflow-serde/index.mdx +1 -1
  82. package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +2 -2
  83. package/docs/api-reference/workflow-serde/workflow-serialize.mdx +7 -7
  84. package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +3 -3
  85. package/docs/api-reference/workflow-vite/workflow.mdx +5 -5
  86. package/docs/changelog/attributes-mvp.mdx +39 -39
  87. package/docs/changelog/batched-event-writes.mdx +12 -12
  88. package/docs/changelog/eager-processing.mdx +63 -63
  89. package/docs/changelog/index.mdx +3 -3
  90. package/docs/changelog/lazy-event-creation.md +27 -27
  91. package/docs/changelog/resilient-resume.mdx +5 -5
  92. package/docs/changelog/resilient-start.mdx +14 -14
  93. package/docs/changelog/step-message-ownership.mdx +47 -47
  94. package/docs/changelog/turbo-mode.md +20 -20
  95. package/docs/comparisons/index.mdx +13 -13
  96. package/docs/comparisons/workflow-sdk-vs-aws-agentcore.mdx +15 -15
  97. package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +12 -12
  98. package/docs/comparisons/workflow-sdk-vs-cloudflare-workflows.mdx +11 -11
  99. package/docs/comparisons/workflow-sdk-vs-inngest.mdx +19 -19
  100. package/docs/comparisons/workflow-sdk-vs-temporal.mdx +23 -23
  101. package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +18 -17
  102. package/docs/configuration/build-and-diagnostics.mdx +5 -5
  103. package/docs/configuration/cli-and-web-ui.mdx +4 -4
  104. package/docs/configuration/runtime-tuning.mdx +86 -23
  105. package/docs/configuration/worlds.mdx +28 -14
  106. package/docs/cookbook/advanced/child-workflows.mdx +25 -25
  107. package/docs/cookbook/advanced/publishing-libraries.mdx +40 -40
  108. package/docs/cookbook/advanced/serializable-steps.mdx +21 -21
  109. package/docs/cookbook/advanced/upgrading-workflows.mdx +31 -31
  110. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +20 -20
  111. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +22 -22
  112. package/docs/cookbook/common-patterns/batching.mdx +14 -14
  113. package/docs/cookbook/common-patterns/idempotency.mdx +9 -9
  114. package/docs/cookbook/common-patterns/rate-limiting.mdx +3 -3
  115. package/docs/cookbook/common-patterns/saga.mdx +19 -19
  116. package/docs/cookbook/common-patterns/scheduling.mdx +23 -23
  117. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +26 -26
  118. package/docs/cookbook/common-patterns/timeouts.mdx +23 -23
  119. package/docs/cookbook/common-patterns/webhooks.mdx +6 -6
  120. package/docs/cookbook/common-patterns/workflow-composition.mdx +19 -19
  121. package/docs/cookbook/index.mdx +22 -22
  122. package/docs/cookbook/integrations/ai-sdk.mdx +43 -41
  123. package/docs/cookbook/integrations/chat-sdk.mdx +34 -34
  124. package/docs/cookbook/integrations/sandbox.mdx +46 -46
  125. package/docs/deploying.mdx +15 -15
  126. package/docs/errors/abort-signal-timeout-in-workflow.mdx +12 -12
  127. package/docs/errors/corrupted-event-log.mdx +11 -11
  128. package/docs/errors/deployment-mismatch.mdx +14 -14
  129. package/docs/errors/fetch-in-workflow.mdx +8 -8
  130. package/docs/errors/hook-conflict.mdx +11 -11
  131. package/docs/errors/index.mdx +1 -1
  132. package/docs/errors/node-js-module-in-workflow.mdx +5 -5
  133. package/docs/errors/replay-divergence.mdx +2 -2
  134. package/docs/errors/runtime-decryption-failed.mdx +12 -12
  135. package/docs/errors/serialization-failed.mdx +40 -12
  136. package/docs/errors/start-invalid-workflow-function.mdx +5 -5
  137. package/docs/errors/step-executed-multiple-times.mdx +2 -2
  138. package/docs/errors/step-not-registered.mdx +5 -5
  139. package/docs/errors/timeout-in-workflow.mdx +8 -8
  140. package/docs/errors/webhook-invalid-respond-with-value.mdx +18 -18
  141. package/docs/errors/webhook-response-not-sent.mdx +16 -16
  142. package/docs/errors/workflow-not-registered.mdx +5 -5
  143. package/docs/foundations/cancellation.mdx +31 -31
  144. package/docs/foundations/errors-and-retries.mdx +42 -11
  145. package/docs/foundations/hooks.mdx +35 -35
  146. package/docs/foundations/idempotency.mdx +9 -9
  147. package/docs/foundations/serialization.mdx +21 -22
  148. package/docs/foundations/starting-workflows.mdx +36 -37
  149. package/docs/foundations/streaming.mdx +46 -41
  150. package/docs/foundations/versioning.mdx +3 -3
  151. package/docs/foundations/workflows-and-steps.mdx +9 -9
  152. package/docs/getting-started/astro.mdx +16 -16
  153. package/docs/getting-started/express.mdx +8 -8
  154. package/docs/getting-started/fastify.mdx +8 -8
  155. package/docs/getting-started/hono.mdx +8 -8
  156. package/docs/getting-started/nestjs.mdx +18 -17
  157. package/docs/getting-started/next.mdx +11 -11
  158. package/docs/getting-started/nitro.mdx +16 -16
  159. package/docs/getting-started/nuxt.mdx +8 -8
  160. package/docs/getting-started/python.mdx +4 -4
  161. package/docs/getting-started/react-router/v7.mdx +1 -1
  162. package/docs/getting-started/react-router/v8.mdx +1 -1
  163. package/docs/getting-started/sveltekit.mdx +14 -14
  164. package/docs/getting-started/tanstack-start.mdx +12 -12
  165. package/docs/getting-started/vite.mdx +8 -8
  166. package/docs/how-it-works/cancellation.mdx +62 -62
  167. package/docs/how-it-works/code-transform.mdx +66 -54
  168. package/docs/how-it-works/encryption.mdx +25 -21
  169. package/docs/how-it-works/event-sourcing.mdx +53 -35
  170. package/docs/how-it-works/framework-integrations.mdx +12 -12
  171. package/docs/how-it-works/understanding-directives.mdx +21 -21
  172. package/docs/internal/index.mdx +6 -6
  173. package/docs/internal/nitro-native-build.mdx +2 -2
  174. package/docs/internal/nitro-web-ui.mdx +4 -4
  175. package/docs/internal/serializable-abort-controller.mdx +7 -7
  176. package/docs/observability/attributes.mdx +3 -3
  177. package/docs/observability/index.mdx +14 -10
  178. package/docs/observability/tracing.mdx +10 -10
  179. package/docs/testing/index.mdx +33 -33
  180. package/docs/testing/server-based.mdx +10 -10
  181. package/docs/whats-new.mdx +185 -0
  182. package/package.json +12 -12
@@ -2,7 +2,7 @@
2
2
  title: Publishing Libraries
3
3
  description: Structure and publish npm packages that export workflow functions for consumers to use with Workflow SDK.
4
4
  type: guide
5
- summary: Learn how to build, export, and test npm packages that ship workflow and step functions including package.json exports, re-exporting so the consumer's compiler discovers your workflows, keeping step I/O clean, and integration testing.
5
+ summary: Learn how to build, export, and test npm packages that ship workflow and step functions, including package.json exports, re-exporting so the consumer's compiler discovers your workflows, keeping step I/O clean, and integration testing.
6
6
  ---
7
7
 
8
8
  <CopyPrompt
@@ -15,7 +15,7 @@ import { File, Folder, Files } from "fumadocs-ui/components/files";
15
15
  This is an advanced guide for library authors who want to publish reusable workflow functions as npm packages. It assumes familiarity with `"use workflow"`, `"use step"`, and the workflow execution model.
16
16
  </Callout>
17
17
 
18
- ## Package Structure
18
+ ## Package structure
19
19
 
20
20
  A workflow library follows a standard TypeScript package layout with a dedicated `workflows/` directory. Each workflow file exports one or more workflow functions that consumers can import and pass to `start()`.
21
21
 
@@ -44,14 +44,14 @@ A workflow library follows a standard TypeScript package layout with a dedicated
44
44
 
45
45
  Key files:
46
46
 
47
- - **`src/index.ts`** Package entry point. Exports the public API.
48
- - **`src/types.ts`** Shared TypeScript types.
49
- - **`src/workflows/index.ts`** Re-exports every workflow so consumers can pull them in under one specifier (see [Entry Points and Exports](#entry-points-and-exports)).
50
- - **`src/workflows/*.ts`** One file per workflow function (e.g. `transcode.ts`, `generate-thumbnails.ts`).
51
- - **`src/lib/`** Internal helpers. Plain async code, *not* marked with `"use workflow"` or `"use step"`.
52
- - **`test-server/workflows.ts`** Re-export file used by integration tests (see [Testing Workflow Libraries](#testing-workflow-libraries)).
47
+ - **`src/index.ts`**: Package entry point that exports the public API.
48
+ - **`src/types.ts`**: Shared TypeScript types.
49
+ - **`src/workflows/index.ts`**: Re-exports every workflow so consumers can pull them in under one specifier (see [Entry points and exports](#entry-points-and-exports)).
50
+ - **`src/workflows/*.ts`**: One file per workflow function (for example, `transcode.ts` or `generate-thumbnails.ts`).
51
+ - **`src/lib/`**: Internal helpers with plain async code that is not marked with `"use workflow"` or `"use step"`.
52
+ - **`test-server/workflows.ts`**: Re-export file used by integration tests (see [Test workflow libraries](#test-workflow-libraries)).
53
53
 
54
- ### Entry Points and Exports
54
+ ### Entry points and exports
55
55
 
56
56
  Use the `exports` field in `package.json` to expose separate entry points for the main API and the raw workflow functions:
57
57
 
@@ -75,7 +75,7 @@ Use the `exports` field in `package.json` to expose separate entry points for th
75
75
 
76
76
  The main entry point (`@acme/media`) exports types, utilities, and convenience wrappers. The `./workflows` entry point (`@acme/media/workflows`) exports the raw workflow functions that consumers need for the build system.
77
77
 
78
- ### Source Files
78
+ ### Source files
79
79
 
80
80
  The package entry re-exports workflows alongside any utilities:
81
81
 
@@ -93,7 +93,7 @@ export * from "./transcode";
93
93
  export * from "./generate-thumbnails";
94
94
  ```
95
95
 
96
- ### Build Configuration
96
+ ### Build configuration
97
97
 
98
98
  Use a bundler like `tsup` with separate entry points for each export. Mark `workflow` as external so it's resolved from the consumer's project:
99
99
 
@@ -114,13 +114,13 @@ export default defineConfig({
114
114
  });
115
115
  ```
116
116
 
117
- ## Re-Exporting for Compiler Discovery
117
+ ## Re-exporting for compiler discovery
118
118
 
119
119
  The workflow compiler only transforms files it discovers, and discovery starts from the consumer's `workflows/` directory and follows imports out from there. A library's workflow functions are not on that graph by default, so nothing compiles them and the runtime has no definition to run.
120
120
 
121
121
  The fix is a **re-export file**. The consumer creates a file in their `workflows/` directory that re-exports the library's workflows, which pulls the library's source onto the discovery graph and gives its entry point an address the runtime can resolve.
122
122
 
123
- ### Consumer Setup
123
+ ### Consumer setup
124
124
 
125
125
  ```typescript lineNumbers
126
126
  // workflows/media.ts (in the consumer's project)
@@ -130,7 +130,7 @@ export * from "@acme/media/workflows"; // [!code highlight]
130
130
 
131
131
  This one-line file is all that's needed. The compiler follows the re-export into the package, transforms the workflow and step functions it finds, and registers them under IDs the runtime can resolve.
132
132
 
133
- ### Why This Is Necessary
133
+ ### Why this is necessary
134
134
 
135
135
  Without re-exporting, the workflow runtime cannot match a running workflow to its function definition. When a run is replayed after a cold start, the runtime looks up functions by their compiler-assigned IDs. If those functions were never compiled, the IDs don't exist and replay fails.
136
136
 
@@ -144,13 +144,13 @@ That is safe on worlds with deployment pinning, such as Vercel, because runs are
144
144
  The re-export file does not change any of this. An ID is derived from where the file lives, not from how it was imported, so a package file keeps its `name@version` ID whether or not a consumer re-exports it.
145
145
  </Callout>
146
146
 
147
- ## Keeping Step I/O Clean
147
+ ## Keeping step I/O clean
148
148
 
149
149
  When you publish a workflow library, every step function's inputs and outputs are recorded in the event log. This has two implications:
150
150
 
151
- ### 1. Everything Must Be Serializable
151
+ ### 1. Everything must be serializable
152
152
 
153
- Step inputs and outputs must be serializable. The workflow runtime supports a rich set of types beyond plain JSON including `Date`, `RegExp`, `Map`, `Set`, `BigInt`, `Uint8Array`, `URL`, `Error`, and class instances that implement [custom class serialization](/docs/foundations/serialization#custom-class-serialization). See the [serialization reference](/docs/foundations/serialization) for the full list of supported types. Do not pass or return:
153
+ Step inputs and outputs must be serializable. The workflow runtime supports a rich set of types beyond plain JSON, including `Date`, `RegExp`, `Map`, `Set`, `BigInt`, `Uint8Array`, `URL`, `Error`, and class instances that implement [custom class serialization](/docs/foundations/serialization#custom-class-serialization). See the [serialization reference](/docs/foundations/serialization) for the full list of supported types. Do not pass or return:
154
154
 
155
155
  - Functions or closures
156
156
  - `WeakRef`, `WeakMap`, or `WeakSet`
@@ -169,7 +169,7 @@ async function callExternalApi(endpoint: string, params: Record<string, string>)
169
169
  // Bad: pass a pre-constructed client object
170
170
  async function callExternalApi(client: ApiClient, params: Record<string, string>) {
171
171
  "use step";
172
- // ApiClient is not serializable this will fail on replay
172
+ // ApiClient is not serializable, so this will fail on replay
173
173
  return await client.request(params);
174
174
  }
175
175
  ```
@@ -199,13 +199,13 @@ async function fetchData(apiKey: string, query: string) {
199
199
 
200
200
  The choice is a matter of library API design preference. Resolving from environment variables keeps the step signature simpler, while passing credentials explicitly makes dependencies visible and can be easier to test.
201
201
 
202
- ## Testing Workflow Libraries
202
+ ## Testing workflow libraries
203
203
 
204
- Library authors need integration tests that exercise workflows through the full Workflow SDK runtime not just unit tests of individual functions.
204
+ Library authors need integration tests that exercise workflows through the full Workflow SDK runtime, rather than only unit tests of individual functions.
205
205
 
206
- ### Test Server Pattern
206
+ ### Test server pattern
207
207
 
208
- Create a minimal test server that re-exports your library's workflows, just like a consumer would:
208
+ Create a minimal test server that re-exports your library's workflows, like a consumer would:
209
209
 
210
210
  ```typescript lineNumbers
211
211
  // test-server/workflows.ts
@@ -214,7 +214,7 @@ export * from "@acme/media/workflows"; // [!code highlight]
214
214
 
215
215
  This test server acts as a stand-in consumer app. Point your test runner at it to exercise the full workflow lifecycle: start, replay, and completion.
216
216
 
217
- ### Vitest Configuration
217
+ ### Vitest configuration
218
218
 
219
219
  Use a dedicated Vitest config for integration tests that run against the Workflow SDK runtime:
220
220
 
@@ -241,22 +241,22 @@ pnpm vitest run tests/unit
241
241
  pnpm vitest run --config vitest.workflowsdk.config.ts
242
242
  ```
243
243
 
244
- ### What to Test
244
+ ### What to test
245
245
 
246
- - **Happy path**: workflow starts, all steps execute, and the final result is correct
247
- - **Serialization round-trip**: inputs and outputs survive the event log
248
- - **Replay**: kill and restart a workflow mid-execution to verify deterministic replay
249
- - **Error handling**: verify that step failures produce the expected errors
246
+ - **Happy path**: The workflow starts, all steps execute, and the final result is correct.
247
+ - **Serialization round-trip**: Inputs and outputs survive the event log.
248
+ - **Replay**: Stop and restart a workflow during execution to verify deterministic replay.
249
+ - **Error handling**: Step failures produce the expected errors.
250
250
 
251
- ## Working With and Without Workflow Installed
251
+ ## Working with and without Workflow installed
252
252
 
253
- Some libraries want to be useful to consumers who *aren't* using Workflow SDK at all — the library picks up durable behavior when a workflow runtime is present and falls back to plain async execution otherwise.
253
+ Some libraries need to support consumers who *aren't* using Workflow SDK. The library gains durable behavior when a workflow runtime is present and falls back to plain async execution otherwise.
254
254
 
255
255
  <Callout type="info">
256
256
  Two rules for isomorphic packages:
257
257
 
258
- 1. **Any runtime reference to the `workflow` package must be loaded via dynamic `import("workflow")` inside a try/catch.** A static top-level import makes the module fail to load for consumers who haven't installed workflow.
259
- 2. **The `"use workflow"` and `"use step"` directives are safe to keep in your library source.** When a consumer compiles your code with the Workflow SDK toolchain (via the [re-export pattern](#re-exporting-for-compiler-discovery) above), the SWC plugin transforms them into durable-execution glue. When they're not compiled plain Node, plain tests, a consumer without the runtime they are just string expression statements and run as no-ops.
258
+ 1. **Load any runtime reference to the `workflow` package through dynamic `import("workflow")` inside a try/catch.** A static top-level import makes the module fail to load for consumers who haven't installed Workflow.
259
+ 2. **Keep the `"use workflow"` and `"use step"` directives in your library source.** When a consumer compiles your code with the Workflow SDK toolchain (via the [re-export pattern](#re-export-for-compiler-discovery) above), the SWC plugin transforms the directives into durable-execution glue. When the directives aren't compiled (plain Node.js, plain tests, or a consumer without the runtime), they are string expression statements and run as no-ops.
260
260
  </Callout>
261
261
 
262
262
  ### Optional peer dependency
@@ -294,7 +294,7 @@ async function getWorkflowStepId(): Promise<string | null> { // [!code highlight
294
294
 
295
295
  ### A concrete use case: replay-safe idempotency keys
296
296
 
297
- A payments utility that uses the current workflow step ID as a Stripe idempotency key when available, and a fresh UUID otherwise:
297
+ A payments utility can use the current workflow step ID as a Stripe idempotency key when available and a fresh universally unique identifier (UUID) otherwise:
298
298
 
299
299
  ```typescript lineNumbers
300
300
  declare function getWorkflowStepId(): Promise<string | null>; // @setup (defined in the previous block)
@@ -315,14 +315,14 @@ export async function processPayment(amount: number, currency: string) {
315
315
  }
316
316
  ```
317
317
 
318
- When called from inside a workflow step, the utility gets a stable idempotency key for that step across retries Stripe dedupes retries for free. When called from a plain Node.js process, it behaves like any other function and a fresh UUID is generated. For more patterns, see [Idempotency](/docs/foundations/idempotency).
318
+ When called from inside a workflow step, the utility gets a stable idempotency key for that step across retries, so Stripe deduplicates retries. When called from a plain Node.js process, it behaves like any other function and generates a fresh UUID. For more patterns, see [Idempotency](/docs/foundations/idempotency).
319
319
 
320
320
  ### In production
321
321
 
322
322
  Packages in the wild built on Workflow SDK:
323
323
 
324
- - **[`@mux/ai`](https://github.com/muxinc/ai)** Reusable video AI workflows (summaries, chapters, content moderation, translation, embeddings) exported with `"use workflow"` / `"use step"` directives. In a standard Node environment the directives are no-ops and the SDK runs as a plain async library; in a Workflow SDK environment the consumer's compiler transforms them into durable, resumable steps with automatic retries and observability. Written up in detail in [*How Mux shipped durable video workflows with their @mux/ai SDK*](https://vercel.com/blog/how-mux-shipped-durable-video-workflows-with-their-mux-ai-sdk) on the Vercel blog.
325
- - **World ID** Human-in-the-loop "proof of human" primitive for agent workflows. Developers drop a World ID step into any workflow to require a zero-knowledge cryptographic proof that a real, unique human authorized a specific action (deploy approvals, large payments, sensitive data access, etc.). Because it runs as a workflow step, every verification is durable, replay-safe, and viewable inside the run's execution timeline giving you a provable audit record of which human approved what. Available on npm and announced in [*World ID for agents: Browserbase, Exa, Okta, and Vercel*](https://world.org/blog/announcements/browserbase-exa-okta-world-id-for-agentic-web) on the World blog.
324
+ - **[`@mux/ai`](https://github.com/muxinc/ai)**: Reusable video AI workflows (summaries, chapters, content moderation, translation, embeddings) exported with `"use workflow"` / `"use step"` directives. In a standard Node environment the directives are no-ops and the SDK runs as a plain async library; in a Workflow SDK environment the consumer's compiler transforms them into durable, resumable steps with automatic retries and observability. Written up in detail in [*How Mux shipped durable video workflows with their @mux/ai SDK*](https://vercel.com/blog/how-mux-shipped-durable-video-workflows-with-their-mux-ai-sdk) on the Vercel blog.
325
+ - **World ID**: Human-in-the-loop "proof of human" primitive for agent workflows. Developers drop a World ID step into any workflow to require a zero-knowledge cryptographic proof that a real, unique human authorized a specific action (deploy approvals, large payments, sensitive data access, etc.). Because it runs as a workflow step, every verification is durable, replay-safe, and viewable inside the run's execution timeline, giving you a provable audit record of which human approved what. Available on npm and announced in [*World ID for agents: Browserbase, Exa, Okta, and Vercel*](https://world.org/blog/announcements/browserbase-exa-okta-world-id-for-agentic-web) on the World blog.
326
326
 
327
327
  ## Checklist
328
328
 
@@ -339,7 +339,7 @@ Before publishing a workflow library:
339
339
 
340
340
  ## Key APIs
341
341
 
342
- - [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions) declares the orchestrator function
343
- - [`"use step"`](/docs/foundations/workflows-and-steps#step-functions) marks functions for durable execution
344
- - [`start`](/docs/api-reference/workflow-api/start) starts a workflow run
345
- - [`getWorkflowMetadata`](/docs/api-reference/workflow/get-workflow-metadata) runtime detection and run ID access
342
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
343
+ - [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Marks functions for durable execution.
344
+ - [`start`](/docs/api-reference/workflow-api/start): Starts a workflow run.
345
+ - [`getWorkflowMetadata`](/docs/api-reference/workflow/get-workflow-metadata): Provides runtime detection and run ID access.
@@ -10,7 +10,7 @@ related:
10
10
  ---
11
11
 
12
12
  <CopyPrompt
13
- text="Make this non-serializable dependency usable inside a durable workflow with the step-as-factory pattern. Instead of passing the object (AI SDK model, cloud SDK client) into the workflow, export a factory that returns an async callback marked with &quot;use step&quot; which constructs and returns the object at execution time, for example `export function openai(...args) { return async () => { &quot;use step&quot;; return openaiProvider(...args); }; }`. Pass the factory across the workflow boundary the compiler serializes the function reference, not the instance and invoke it inside steps where full Node.js access is available. Keep the factory's constructor arguments serializable. Verify the workflow builds, replays deterministically, and the dependency is only instantiated during step execution."
13
+ text="Make this non-serializable dependency usable inside a durable workflow with the step-as-factory pattern. Instead of passing the object (AI SDK model, cloud SDK client) into the workflow, export a factory that returns an async callback marked with &quot;use step&quot; which constructs and returns the object at execution time, for example `export function openai(...args) { return async () => { &quot;use step&quot;; return openaiProvider(...args); }; }`. Pass the factory across the workflow boundary (the compiler serializes the function reference, not the instance) and invoke it inside steps where full Node.js access is available. Keep the factory's constructor arguments serializable. Verify the workflow builds, replays deterministically, and the dependency is only instantiated during step execution."
14
14
  />
15
15
 
16
16
  <Callout>
@@ -21,12 +21,12 @@ This is an advanced guide. It dives into workflow internals and is not required
21
21
 
22
22
  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:
23
23
 
24
- - **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.
25
- - **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.
24
+ - **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.
25
+ - **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.
26
26
 
27
- ## The Problem
27
+ ## The problem
28
28
 
29
- 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.
29
+ 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.
30
30
 
31
31
  ```typescript lineNumbers
32
32
  import { openai } from "@ai-sdk/openai";
@@ -39,7 +39,7 @@ export async function brokenAgent(prompt: string) {
39
39
 
40
40
  const writable = getWritable<UIMessageChunk>();
41
41
  const agent = new DurableAgent({
42
- // This fails the model object is not serializable
42
+ // This fails: the model object is not serializable
43
43
  model: openai("gpt-4o"),
44
44
  });
45
45
 
@@ -47,9 +47,9 @@ export async function brokenAgent(prompt: string) {
47
47
  }
48
48
  ```
49
49
 
50
- ## The Solution: Step-as-Factory
50
+ ## The solution: step-as-factory
51
51
 
52
- 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.
52
+ 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 a string identifier) rather than its return value. The provider is only instantiated at execution time, inside the step's full Node.js runtime.
53
53
 
54
54
  ```typescript lineNumbers
55
55
  import { openai as openaiProvider } from "@ai-sdk/openai";
@@ -63,12 +63,12 @@ export function openai(...args: Parameters<typeof openaiProvider>) {
63
63
  }
64
64
  ```
65
65
 
66
- 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.
66
+ The `DurableAgent` receives a function (`() => Promise<LanguageModel>`) instead of a model object. When the agent needs to call the large language model (LLM), it invokes the factory inside a step where the real provider can be constructed with full Node.js access.
67
67
 
68
- ## How `@workflow/ai` Uses This
68
+ ## How `@workflow/ai` uses this
69
69
 
70
70
  <Callout type="warn">
71
- `@workflow/ai`'s pre-wrapped providers and `DurableAgent` are deprecated. AI SDK's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) resolves models from AI Gateway model strings (e.g. `"openai/gpt-4o"`), which usually removes the need for a model factory see the [migration guide](https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent). The serialization pattern on this page still applies to any non-serializable dependency you own (for example, cloud SDK clients).
71
+ `@workflow/ai`'s pre-wrapped providers and `DurableAgent` are deprecated. AI SDK's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) resolves models from AI Gateway model strings (for example, `"openai/gpt-4o"`), which usually removes the need for a model factory; see the [migration guide](https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent). The serialization pattern on this page still applies to any non-serializable dependency you own (for example, cloud SDK clients).
72
72
  </Callout>
73
73
 
74
74
  The `@workflow/ai` package ships pre-wrapped providers for all major AI SDK backends. Each one follows the same pattern:
@@ -105,14 +105,14 @@ export async function chatAgent(prompt: string) {
105
105
  }
106
106
  ```
107
107
 
108
- ## Writing Your Own Serializable Wrapper
108
+ ## Writing your own serializable wrapper
109
109
 
110
110
  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**.
111
111
 
112
112
  ```typescript lineNumbers
113
113
  import type { S3Client as S3ClientType } from "@aws-sdk/client-s3";
114
114
 
115
- // The arguments (region, bucket) are plain strings serializable
115
+ // The arguments (region, bucket) are plain strings, which are serializable
116
116
  export function createS3Client(region: string) {
117
117
  return async (): Promise<S3ClientType> => {
118
118
  "use step";
@@ -141,15 +141,15 @@ async function uploadFile(
141
141
  }
142
142
  ```
143
143
 
144
- ## Why This Works
144
+ ## Why this works
145
145
 
146
- 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).
147
- 2. **Closure tracking**: The compiler tracks which variables the step function closes over. Only serializable values (strings, numbers, plain objects) can be captured.
148
- 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.
146
+ 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 and captured arguments).
147
+ 2. **Closure tracking**: The compiler tracks which variables the step function closes over. The function can capture only serializable values, such as strings, numbers, and plain objects.
148
+ 3. **Deferred construction**: The step constructs the provider or client only when it executes in the Node.js runtime, never in the sandboxed workflow VM.
149
149
 
150
150
  ## Key APIs
151
151
 
152
- - [`"use step"`](/docs/foundations/workflows-and-steps#step-functions) marks a function for extraction and serialization
153
- - [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions) declares the orchestrator function
154
- - [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) AI SDK's durable agent (resolves models via AI Gateway strings; replaces `DurableAgent`)
155
- - [Custom class serialization](/docs/foundations/serialization#custom-class-serialization) the companion pattern for classes you own (`WORKFLOW_SERIALIZE` / `WORKFLOW_DESERIALIZE`)
152
+ - [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Marks a function for extraction and serialization.
153
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
154
+ - [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent): Provides AI SDK's durable agent, resolves models through AI Gateway strings, and replaces `DurableAgent`.
155
+ - [Custom class serialization](/docs/foundations/serialization#custom-class-serialization): Provides the companion pattern for classes you own (`WORKFLOW_SERIALIZE` and `WORKFLOW_DESERIALIZE`).
@@ -2,7 +2,7 @@
2
2
  title: Upgrading Workflows
3
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
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.'
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
6
  related:
7
7
  - /docs/foundations/versioning
8
8
  - /cookbook/common-patterns/workflow-composition
@@ -14,27 +14,27 @@ related:
14
14
  text="Add a safe self-upgrade point to this long-running workflow. Identify the loop boundary where no step is mid-side-effect. Define a serializable state object that contains all progress needed to continue. At the boundary, call `start(self, [state], { deploymentId: &quot;latest&quot; })` or the documented replacement workflow with the carried state, then return from the old run. If upgrades should be manual, add a `defineHook()` upgrade signal and resume it from an API route with `resumeHook()` from `workflow/api`. Make the handoff idempotent so retries do not start duplicate successor runs, and verify old-to-new handoff plus duplicate prevention."
15
15
  />
16
16
 
17
- 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.
17
+ 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.
18
18
 
19
19
  <Callout type="info">
20
- 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.
20
+ 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.
21
21
  </Callout>
22
22
 
23
23
  A clean upgrade point is any spot in the workflow where:
24
24
 
25
- - All in-progress side effects have completed (or aren't needed by the next iteration)
26
- - The relevant state can be serialized into the workflow's input arguments
27
- - It's natural for the workflow to "checkpoint" — typically right after handling an external event, completing a batch, or finishing a logical phase
25
+ - All in-progress side effects have completed or aren't needed by the next iteration.
26
+ - The relevant state can be serialized into the workflow's input arguments.
27
+ - The workflow can create a checkpoint after handling an external event, completing a batch, or finishing a logical phase.
28
28
 
29
29
  There are two ways to apply this:
30
30
 
31
- 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.
32
- 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.
31
+ 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. This method needs no extra triggers, but every event incurs the respawn cost.
32
+ 2. **Upgrade on demand through 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 respawns only when an `upgradeHook` fires. A separate endpoint resumes that hook from your control plane, for example, after a deployment. This method provides more control and fewer respawns at the cost of an explicit trigger.
33
33
 
34
34
  ### When to use each
35
35
 
36
- - **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.
37
- - **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.
36
+ - **Use Method 1** when iterations are short and frequent, the work is inexpensive to checkpoint, and you want shipped fixes to apply on the next event. Long-lived session workflows, such as subscriptions, queues, and finite-state machines (FSMs), that already process events one at a time fit this method.
37
+ - **Use Method 2** when iterations are infrequent or expensive, or when you need to roll out a fix to a fleet of in-flight runs after a deployment by fanning out to a control-plane endpoint. This method also fits when an upgrade should be an explicit operation rather than a side effect of handling each event.
38
38
 
39
39
  ## Method 1: Upgrade on every iteration
40
40
 
@@ -60,7 +60,7 @@ export async function longRunningQueue(
60
60
 
61
61
  const { workflowRunId } = getWorkflowMetadata();
62
62
 
63
- // Block until something fires the hook could be hours, days, or longer.
63
+ // Block until something fires the hook. Could be hours, days, or longer.
64
64
  // Per-run hook tokens (workflowRunId) keep concurrent chains isolated.
65
65
  const { itemId } = await nextItemHook.create({ token: workflowRunId }); // [!code highlight]
66
66
 
@@ -68,7 +68,7 @@ export async function longRunningQueue(
68
68
 
69
69
  // Hand off to a fresh run on the latest deployment. THIS run ends here.
70
70
  // `deploymentId: "latest"` resolves to whichever deployment is current
71
- // when this spawn lands NOT the deployment running this code.
71
+ // when this spawn lands, NOT the deployment running this code.
72
72
  await start( // [!code highlight]
73
73
  longRunningQueue, // [!code highlight]
74
74
  [{ processed: state.processed + 1, cursor: itemId }], // [!code highlight]
@@ -93,11 +93,11 @@ export async function POST(req: Request) {
93
93
  }
94
94
  ```
95
95
 
96
- 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.
96
+ The caller tracks the active `runId`, such as in a database or returned from the previous iteration, and updates it whenever the chain advances.
97
97
 
98
98
  ## Method 2: Upgrade on demand via a dedicated hook
99
99
 
100
- 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.
100
+ 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.
101
101
 
102
102
  ```typescript lineNumbers
103
103
  import { defineHook, getWorkflowMetadata } from "workflow";
@@ -151,7 +151,7 @@ export async function longRunningQueue(
151
151
 
152
152
  ### Triggering the upgrade
153
153
 
154
- 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.
154
+ Expose a separate endpoint that resumes `upgradeHook` for a given run. Call it from your deployment pipeline, an admin UI, or a fan-out script that iterates over every active run after shipping a fix.
155
155
 
156
156
  ```typescript
157
157
  import { upgradeHook } from "@/workflows/long-running-queue";
@@ -167,33 +167,33 @@ export async function POST(req: Request) {
167
167
  }
168
168
  ```
169
169
 
170
- To upgrade a fleet of runs after a deploy, list active runs (e.g. from a tracking store) and call this endpoint for each.
170
+ To upgrade a fleet of runs after a deployment, list active runs from a tracking store and call this endpoint for each run.
171
171
 
172
172
  ## How it works
173
173
 
174
- 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.
175
- 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.
174
+ 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.
175
+ 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, so no manual `"use step"` wrapper is required.
176
176
  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.
177
177
  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.
178
- 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.
178
+ 5. **Method 1 vs Method 2 is only 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.
179
179
 
180
180
  ## Adapting to your use case
181
181
 
182
- - **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.
183
- - **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.
184
- - **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.
185
- - **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
+ - **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.
183
+ - **Use stateless successors**: If the next iteration doesn't need the previous state, such as for a pure event router, call `start(longRunningQueue, [], { deploymentId: "latest" })` and skip the argument plumbing.
184
+ - **Persist state externally**: If state needs to be readable from outside the workflow for dashboards, debugging, or recovery, write it to a database in a step before spawning the next run.
185
+ - **Track the active `runId` externally**: The system that resumes the hook needs to know the current run. Capture the `runId` returned by `start()` and write it to a database keyed by a stable session identifier in a step, so resumers always look up the latest run.
186
186
 
187
187
  ## Caveats
188
188
 
189
- - **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).
190
- - **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.
191
- - **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.
192
- - **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
+ - **Maintain backward compatibility**: 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).
190
+ - **Keep the workflow identity stable**: The function name and file path form the workflow identity. Renaming the function or moving the file across a deployment changes the workflow ID, so the next iteration will fail to resolve.
191
+ - **Account for the gap between iterations**: The current run ends as soon as `start()` returns, and 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.
192
+ - **Track active Method 2 runs externally**: Because Method 2's runs are long-lived, the set of in-flight runs changes only 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`, update the tracked run ID once the new run reports back, as you would in Method 1.
193
193
 
194
194
  ## Key APIs
195
195
 
196
- - [`"use workflow"`](/docs/foundations/workflows-and-steps) marks the orchestrator function
197
- - [`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
198
- - [`defineHook()`](/docs/api-reference/workflow/define-hook) suspend the workflow until an external event resumes it
199
- - [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) exposes `workflowRunId` for per-run hook tokens
196
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps): Marks the orchestrator function.
197
+ - [`start()`](/docs/api-reference/workflow-api/start) with [`deploymentId: "latest"`](/docs/api-reference/workflow-api/start#using-deploymentid-latest): Spawns the successor on the newest deployment.
198
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook): Suspends the workflow until an external event resumes it.
199
+ - [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata): Exposes `workflowRunId` for per-run hook tokens.
@@ -1,15 +1,15 @@
1
1
  ---
2
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.
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
4
  type: guide
5
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
6
  ---
7
7
 
8
8
  <CopyPrompt
9
- text="Add cancellation to this durable AI agent. For hard cancellation, expose a server route that receives `runId` and calls `getRun(runId).cancel()` from `workflow/api`. For graceful stop, define `stopHook` with `defineHook()` from `workflow`, create it with a stable token such as the workflow run ID, and race the agent loop against the stop hook using `Promise.race`. Use `getWritable<UIMessageChunk>()` to emit a final stopped/cancelled message before returning. Wire the UI Stop button to the route that resumes the hook or falls back to `getRun(runId).cancel()`. Verify active model/tool work stops, cleanup runs for graceful stop, and stale run IDs are handled."
9
+ text="Add cancellation to this durable AI agent. For hard cancellation, expose a server route that receives `runId` and calls `getRun(runId).cancel()` from `workflow/api`. For graceful stop, define `stopHook` with `defineHook()` from `workflow`, create it with a stable token such as the workflow run ID, and race the agent loop against the stop hook using `Promise.race`. Use `getWritable<UIMessageChunk>()` to emit a final stopped/canceled message before returning. Wire the UI Stop button to the route that resumes the hook or falls back to `getRun(runId).cancel()`. Verify active model/tool work stops, cleanup runs for graceful stop, and stale run IDs are handled."
10
10
  />
11
11
 
12
- Cancel a running agent from the outside for example, a "Stop" button in a chat UI, an admin cancellation endpoint, or a timeout fallback.
12
+ Cancel a running agent from the outside through a **Stop** button in a chat user interface (UI), an admin cancellation endpoint, or a timeout fallback.
13
13
 
14
14
  <Callout type="warn">
15
15
  This recipe uses the deprecated `DurableAgent` API. For new agents, use AI SDK's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) and follow the [migration guide](https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent). The cancellation patterns here (`run.cancel()`, stop-signal hook + `Promise.race`, `AbortController`) apply to either API.
@@ -17,7 +17,7 @@ This recipe uses the deprecated `DurableAgent` API. For new agents, use AI SDK's
17
17
 
18
18
  ## Pattern
19
19
 
20
- 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.
20
+ 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.
21
21
 
22
22
  ```typescript lineNumbers
23
23
  import { DurableAgent } from "@workflow/ai/agent";
@@ -98,7 +98,7 @@ export async function stoppableAgent(messages: ModelMessage[]) {
98
98
  }
99
99
  ```
100
100
 
101
- ### API Route to Trigger Stop
101
+ ### API route to trigger stop
102
102
 
103
103
  ```typescript lineNumbers
104
104
  import { stopHook } from "@/workflows/stoppable-agent";
@@ -118,7 +118,7 @@ export async function POST(
118
118
  }
119
119
  ```
120
120
 
121
- ### Client Stop Button
121
+ ### Client stop button
122
122
 
123
123
  ```tsx lineNumbers
124
124
  "use client";
@@ -142,23 +142,23 @@ export function StopButton({ runId }: { runId: string }) {
142
142
 
143
143
  ## How it works
144
144
 
145
- 1. An `AbortController` is created at the start of the workflow
146
- 2. A hook is created with token `stop:${workflowRunId}`
147
- 3. `Promise.race` runs the agent stream and the stop hook concurrently
148
- 4. The agent receives `controller.signal` when aborted, the underlying model stream is cancelled
149
- 5. When the stop API resumes the hook, `controller.abort()` is called — the race resolves and the workflow exits
150
- 6. `emitStopSignal` writes a `data-stopped` part to the stream so the client renders a clean stop state
145
+ 1. The workflow creates an `AbortController` when it starts.
146
+ 2. The workflow creates a hook with the token `stop:${workflowRunId}`.
147
+ 3. `Promise.race` runs the agent stream and the stop hook concurrently.
148
+ 4. The agent receives `controller.signal`. When aborted, the signal cancels the underlying model stream.
149
+ 5. When the stop API resumes the hook, the workflow calls `controller.abort()`, resolves the race, and exits.
150
+ 6. `emitStopSignal` writes a `data-stopped` part to the stream so the client renders a clean stop state.
151
151
 
152
152
  ## Adapting this
153
153
 
154
- * **Add a timeout** race a third `sleep()` promise to auto-stop after a deadline
155
- * **Audit logging** include a `reason` field in the stop schema to record who stopped and why
156
- * **Cross-process** the hook token is deterministic, so any process can call `stopHook.resume()` with the run ID
157
- * **Step limits** combine with `maxSteps` on the agent to cap execution even without manual stop
154
+ - **Add a timeout**: Race a third `sleep()` promise to stop automatically after a deadline.
155
+ - **Audit logging**: Include a `reason` field in the stop schema to record who stopped the agent and why.
156
+ - **Cross-process**: The hook token is deterministic, so any process can call `stopHook.resume()` with the run ID.
157
+ - **Step limits**: Combine the pattern with `maxSteps` on the agent to cap execution without a manual stop.
158
158
 
159
159
  ## Key APIs
160
160
 
161
- * [`defineHook()`](/docs/api-reference/workflow/define-hook) type-safe hook for the stop signal
162
- * [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata) access the run ID for deterministic hook tokens
163
- * [`getWritable()`](/docs/api-reference/workflow/get-writable) stream output and the stop notification to the client
164
- * [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) AI SDK's durable agent that respects the abort signal (replaces `DurableAgent`)
161
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook): Defines a type-safe hook for the stop signal.
162
+ - [`getWorkflowMetadata()`](/docs/api-reference/workflow/get-workflow-metadata): Provides the run ID for deterministic hook tokens.
163
+ - [`getWritable()`](/docs/api-reference/workflow/get-writable): Streams output and the stop notification to the client.
164
+ - [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent): AI SDK's durable agent that respects the abort signal and replaces `DurableAgent`.