workflow 5.0.0-beta.5 → 5.0.0-beta.50

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 (256) hide show
  1. package/README.md +68 -23
  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 +1 -1
  5. package/dist/api.d.ts +3 -3
  6. package/dist/api.d.ts.map +1 -1
  7. package/dist/api.js +5 -7
  8. package/dist/index.d.ts +1 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +6 -1
  11. package/dist/internal/builtins.d.ts +20 -3
  12. package/dist/internal/builtins.d.ts.map +1 -1
  13. package/dist/internal/builtins.js +68 -4
  14. package/dist/internal/errors.d.ts +1 -1
  15. package/dist/internal/errors.d.ts.map +1 -1
  16. package/dist/internal/errors.js +2 -2
  17. package/dist/nest-builder.d.ts +2 -0
  18. package/dist/nest-builder.d.ts.map +1 -0
  19. package/dist/nest-builder.js +2 -0
  20. package/dist/nest-vercel-builder.d.ts +2 -0
  21. package/dist/nest-vercel-builder.d.ts.map +1 -0
  22. package/dist/nest-vercel-builder.js +2 -0
  23. package/dist/observability.d.ts +1 -1
  24. package/dist/observability.js +2 -2
  25. package/dist/runtime.d.ts +2 -1
  26. package/dist/runtime.d.ts.map +1 -1
  27. package/dist/runtime.js +4 -1
  28. package/docs/ai/chat-session-modeling.mdx +29 -26
  29. package/docs/ai/defining-tools.mdx +6 -7
  30. package/docs/ai/human-in-the-loop.mdx +11 -11
  31. package/docs/ai/index.mdx +50 -45
  32. package/docs/ai/message-queueing.mdx +16 -16
  33. package/docs/ai/meta.json +1 -0
  34. package/docs/ai/resumable-streams.mdx +40 -28
  35. package/docs/ai/sleep-and-delays.mdx +10 -10
  36. package/docs/ai/streaming-updates-from-tools.mdx +6 -6
  37. package/docs/api-reference/index.mdx +24 -0
  38. package/docs/api-reference/meta.json +8 -0
  39. package/docs/api-reference/vitest/index.mdx +9 -15
  40. package/docs/api-reference/workflow/create-hook.mdx +89 -10
  41. package/docs/api-reference/workflow/create-webhook.mdx +16 -15
  42. package/docs/api-reference/workflow/define-hook.mdx +35 -33
  43. package/docs/api-reference/workflow/fatal-error.mdx +30 -8
  44. package/docs/api-reference/workflow/fetch.mdx +14 -10
  45. package/docs/api-reference/workflow/get-step-metadata.mdx +2 -2
  46. package/docs/api-reference/workflow/get-workflow-metadata.mdx +3 -3
  47. package/docs/api-reference/workflow/get-writable.mdx +7 -7
  48. package/docs/api-reference/workflow/index.mdx +4 -1
  49. package/docs/api-reference/workflow/retryable-error.mdx +1 -1
  50. package/docs/api-reference/workflow/set-attributes.mdx +61 -0
  51. package/docs/api-reference/workflow/sleep.mdx +4 -4
  52. package/docs/api-reference/workflow-ai/durable-agent.mdx +48 -86
  53. package/docs/api-reference/workflow-ai/index.mdx +3 -3
  54. package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +67 -24
  55. package/docs/api-reference/workflow-api/get-hook-by-token.mdx +26 -12
  56. package/docs/api-reference/workflow-api/get-run.mdx +43 -8
  57. package/docs/api-reference/workflow-api/index.mdx +6 -10
  58. package/docs/api-reference/workflow-api/resume-hook.mdx +73 -12
  59. package/docs/api-reference/workflow-api/resume-webhook.mdx +11 -9
  60. package/docs/api-reference/workflow-api/start.mdx +60 -13
  61. package/docs/api-reference/workflow-astro/index.mdx +18 -0
  62. package/docs/api-reference/workflow-astro/meta.json +4 -0
  63. package/docs/api-reference/workflow-astro/workflow.mdx +45 -0
  64. package/docs/api-reference/workflow-errors/entity-conflict-error.mdx +4 -4
  65. package/docs/api-reference/workflow-errors/hook-conflict-error.mdx +60 -0
  66. package/docs/api-reference/workflow-errors/hook-not-found-error.mdx +8 -8
  67. package/docs/api-reference/workflow-errors/index.mdx +88 -0
  68. package/docs/api-reference/workflow-errors/meta.json +6 -0
  69. package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +68 -0
  70. package/docs/api-reference/workflow-errors/run-expired-error.mdx +2 -2
  71. package/docs/api-reference/workflow-errors/run-not-supported-error.mdx +58 -0
  72. package/docs/api-reference/workflow-errors/step-not-registered-error.mdx +5 -5
  73. package/docs/api-reference/workflow-errors/throttle-error.mdx +2 -2
  74. package/docs/api-reference/workflow-errors/too-early-error.mdx +2 -2
  75. package/docs/api-reference/workflow-errors/workflow-error.mdx +52 -0
  76. package/docs/api-reference/workflow-errors/workflow-not-registered-error.mdx +5 -6
  77. package/docs/api-reference/workflow-errors/workflow-run-cancelled-error.mdx +6 -6
  78. package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +5 -5
  79. package/docs/api-reference/workflow-errors/workflow-run-not-completed-error.mdx +58 -0
  80. package/docs/api-reference/workflow-errors/workflow-run-not-found-error.mdx +4 -4
  81. package/docs/api-reference/workflow-errors/workflow-runtime-error.mdx +58 -0
  82. package/docs/api-reference/workflow-errors/workflow-world-error.mdx +8 -8
  83. package/docs/api-reference/workflow-globals.mdx +14 -10
  84. package/docs/api-reference/workflow-nest/configure-workflow-controller.mdx +33 -0
  85. package/docs/api-reference/workflow-nest/index.mdx +31 -0
  86. package/docs/api-reference/workflow-nest/meta.json +9 -0
  87. package/docs/api-reference/workflow-nest/nest-local-builder.mdx +64 -0
  88. package/docs/api-reference/workflow-nest/workflow-controller.mdx +40 -0
  89. package/docs/api-reference/workflow-nest/workflow-module.mdx +74 -0
  90. package/docs/api-reference/workflow-next/with-workflow.mdx +39 -17
  91. package/docs/api-reference/workflow-nitro/index.mdx +60 -0
  92. package/docs/api-reference/workflow-nuxt/index.mdx +48 -0
  93. package/docs/api-reference/workflow-observability/hydrate-data.mdx +35 -0
  94. package/docs/api-reference/workflow-observability/hydrate-resource-io.mdx +62 -0
  95. package/docs/api-reference/workflow-observability/index.mdx +62 -0
  96. package/docs/api-reference/workflow-observability/meta.json +11 -0
  97. package/docs/api-reference/workflow-observability/observability-revivers.mdx +50 -0
  98. package/docs/api-reference/workflow-observability/parse-class-name.mdx +41 -0
  99. package/docs/api-reference/workflow-observability/parse-step-name.mdx +40 -0
  100. package/docs/api-reference/workflow-observability/parse-workflow-name.mdx +55 -0
  101. package/docs/api-reference/workflow-runtime/create-world.mdx +39 -0
  102. package/docs/api-reference/workflow-runtime/get-world-handlers.mdx +44 -0
  103. package/docs/api-reference/{workflow-api → workflow-runtime}/get-world.mdx +11 -14
  104. package/docs/api-reference/workflow-runtime/health-check.mdx +50 -0
  105. package/docs/api-reference/workflow-runtime/index.mdx +41 -0
  106. package/docs/api-reference/workflow-runtime/meta.json +12 -0
  107. package/docs/api-reference/workflow-runtime/set-world.mdx +51 -0
  108. package/docs/api-reference/workflow-runtime/workflow-entrypoint.mdx +43 -0
  109. package/docs/api-reference/workflow-runtime/world/analytics.mdx +315 -0
  110. package/docs/api-reference/workflow-runtime/world/index.mdx +60 -0
  111. package/docs/api-reference/workflow-runtime/world/meta.json +4 -0
  112. package/docs/api-reference/workflow-runtime/world/queue.mdx +88 -0
  113. package/docs/api-reference/{workflow-api → workflow-runtime}/world/storage.mdx +98 -34
  114. package/docs/api-reference/{workflow-api → workflow-runtime}/world/streams.mdx +8 -8
  115. package/docs/api-reference/workflow-serde/index.mdx +1 -2
  116. package/docs/api-reference/workflow-serde/workflow-deserialize.mdx +3 -4
  117. package/docs/api-reference/workflow-serde/workflow-serialize.mdx +8 -8
  118. package/docs/api-reference/workflow-sveltekit/index.mdx +18 -0
  119. package/docs/api-reference/workflow-sveltekit/meta.json +4 -0
  120. package/docs/api-reference/workflow-sveltekit/workflow-plugin.mdx +42 -0
  121. package/docs/api-reference/workflow-vite/index.mdx +18 -0
  122. package/docs/api-reference/workflow-vite/meta.json +4 -0
  123. package/docs/api-reference/workflow-vite/workflow.mdx +48 -0
  124. package/docs/changelog/attributes-mvp.mdx +380 -0
  125. package/docs/changelog/batched-event-writes.mdx +79 -0
  126. package/docs/changelog/eager-processing.mdx +110 -436
  127. package/docs/changelog/index.mdx +4 -2
  128. package/docs/changelog/lazy-event-creation.md +127 -0
  129. package/docs/changelog/lazy-hook-resume.mdx +78 -0
  130. package/docs/changelog/meta.json +11 -1
  131. package/docs/changelog/resilient-resume.mdx +32 -0
  132. package/docs/changelog/resilient-start.mdx +33 -285
  133. package/docs/changelog/step-message-ownership.mdx +360 -0
  134. package/docs/changelog/turbo-mode.md +87 -0
  135. package/docs/comparisons/index.mdx +66 -0
  136. package/docs/comparisons/meta.json +11 -0
  137. package/docs/comparisons/workflow-sdk-vs-aws-agentcore.mdx +55 -0
  138. package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +111 -0
  139. package/docs/comparisons/workflow-sdk-vs-cloudflare-workflows.mdx +71 -0
  140. package/docs/comparisons/workflow-sdk-vs-inngest.mdx +102 -0
  141. package/docs/comparisons/workflow-sdk-vs-temporal.mdx +123 -0
  142. package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +104 -0
  143. package/docs/configuration/build-and-diagnostics.mdx +70 -0
  144. package/docs/configuration/cli-and-web-ui.mdx +241 -0
  145. package/docs/configuration/framework-options.mdx +165 -0
  146. package/docs/configuration/index.mdx +32 -0
  147. package/docs/configuration/meta.json +12 -0
  148. package/docs/configuration/runtime-tuning.mdx +376 -0
  149. package/docs/configuration/worlds.mdx +313 -0
  150. package/docs/cookbook/advanced/child-workflows.mdx +211 -264
  151. package/docs/cookbook/advanced/meta.json +6 -1
  152. package/docs/cookbook/advanced/publishing-libraries.mdx +65 -56
  153. package/docs/cookbook/advanced/serializable-steps.mdx +28 -20
  154. package/docs/cookbook/advanced/upgrading-workflows.mdx +199 -0
  155. package/docs/cookbook/agent-patterns/agent-cancellation.mdx +27 -19
  156. package/docs/cookbook/agent-patterns/durable-agent.mdx +14 -142
  157. package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +30 -22
  158. package/docs/cookbook/common-patterns/batching.mdx +18 -14
  159. package/docs/cookbook/common-patterns/idempotency.mdx +41 -53
  160. package/docs/cookbook/common-patterns/rate-limiting.mdx +8 -4
  161. package/docs/cookbook/common-patterns/saga.mdx +23 -19
  162. package/docs/cookbook/common-patterns/scheduling.mdx +34 -22
  163. package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +29 -25
  164. package/docs/cookbook/common-patterns/timeouts.mdx +26 -21
  165. package/docs/cookbook/common-patterns/webhooks.mdx +10 -6
  166. package/docs/cookbook/common-patterns/workflow-composition.mdx +30 -27
  167. package/docs/cookbook/index.mdx +22 -21
  168. package/docs/cookbook/integrations/ai-sdk.mdx +85 -47
  169. package/docs/cookbook/integrations/chat-sdk.mdx +50 -33
  170. package/docs/cookbook/integrations/sandbox.mdx +62 -45
  171. package/docs/deploying.mdx +95 -0
  172. package/docs/errors/abort-signal-timeout-in-workflow.mdx +16 -12
  173. package/docs/errors/corrupted-event-log.mdx +29 -18
  174. package/docs/errors/deployment-mismatch.mdx +71 -0
  175. package/docs/errors/fetch-in-workflow.mdx +11 -7
  176. package/docs/errors/hook-conflict.mdx +69 -13
  177. package/docs/errors/index.mdx +2 -36
  178. package/docs/errors/node-js-module-in-workflow.mdx +9 -5
  179. package/docs/errors/replay-divergence.mdx +27 -0
  180. package/docs/errors/run-expired.mdx +85 -0
  181. package/docs/errors/runtime-decryption-failed.mdx +77 -0
  182. package/docs/errors/serialization-failed.mdx +44 -12
  183. package/docs/errors/start-invalid-workflow-function.mdx +9 -5
  184. package/docs/errors/step-executed-multiple-times.mdx +23 -0
  185. package/docs/errors/step-not-registered.mdx +6 -6
  186. package/docs/errors/timeout-in-workflow.mdx +12 -8
  187. package/docs/errors/webhook-invalid-respond-with-value.mdx +18 -18
  188. package/docs/errors/webhook-response-not-sent.mdx +20 -16
  189. package/docs/errors/workflow-not-registered.mdx +5 -5
  190. package/docs/foundations/cancellation.mdx +31 -32
  191. package/docs/foundations/errors-and-retries.mdx +42 -11
  192. package/docs/foundations/hooks.mdx +64 -35
  193. package/docs/foundations/idempotency.mdx +244 -12
  194. package/docs/foundations/index.mdx +1 -23
  195. package/docs/foundations/meta.json +2 -1
  196. package/docs/foundations/serialization.mdx +21 -22
  197. package/docs/foundations/starting-workflows.mdx +106 -30
  198. package/docs/foundations/streaming.mdx +107 -59
  199. package/docs/foundations/versioning.mdx +263 -0
  200. package/docs/foundations/workflows-and-steps.mdx +9 -9
  201. package/docs/getting-started/astro.mdx +22 -18
  202. package/docs/getting-started/express.mdx +15 -11
  203. package/docs/getting-started/fastify.mdx +15 -11
  204. package/docs/getting-started/hono.mdx +15 -11
  205. package/docs/getting-started/index.mdx +10 -3
  206. package/docs/getting-started/meta.json +3 -1
  207. package/docs/getting-started/nestjs.mdx +87 -20
  208. package/docs/getting-started/next.mdx +22 -16
  209. package/docs/getting-started/nitro.mdx +22 -18
  210. package/docs/getting-started/nuxt.mdx +15 -11
  211. package/docs/getting-started/python.mdx +135 -40
  212. package/docs/getting-started/react-router/index.mdx +33 -0
  213. package/docs/getting-started/react-router/meta.json +5 -0
  214. package/docs/getting-started/react-router/v7.mdx +237 -0
  215. package/docs/getting-started/react-router/v8.mdx +232 -0
  216. package/docs/getting-started/sveltekit.mdx +20 -16
  217. package/docs/getting-started/tanstack-start.mdx +17 -13
  218. package/docs/getting-started/vite.mdx +15 -11
  219. package/docs/how-it-works/cancellation.mdx +63 -63
  220. package/docs/how-it-works/code-transform.mdx +83 -67
  221. package/docs/how-it-works/encryption.mdx +30 -26
  222. package/docs/how-it-works/event-sourcing.mdx +98 -34
  223. package/docs/how-it-works/framework-integrations.mdx +96 -337
  224. package/docs/how-it-works/understanding-directives.mdx +22 -22
  225. package/docs/internal/index.mdx +6 -4
  226. package/docs/internal/meta.json +6 -1
  227. package/docs/internal/nitro-native-build.mdx +38 -0
  228. package/docs/internal/nitro-web-ui.mdx +24 -0
  229. package/docs/internal/serializable-abort-controller.mdx +7 -7
  230. package/docs/meta.json +3 -2
  231. package/docs/observability/attributes.mdx +134 -0
  232. package/docs/observability/index.mdx +32 -10
  233. package/docs/observability/meta.json +1 -1
  234. package/docs/observability/retention.mdx +93 -0
  235. package/docs/observability/tracing.mdx +124 -0
  236. package/docs/testing/index.mdx +36 -36
  237. package/docs/testing/server-based.mdx +10 -10
  238. package/docs/whats-new.mdx +186 -0
  239. package/package.json +17 -14
  240. package/docs/api-reference/workflow-api/world/index.mdx +0 -58
  241. package/docs/api-reference/workflow-api/world/meta.json +0 -4
  242. package/docs/api-reference/workflow-api/world/observability.mdx +0 -164
  243. package/docs/api-reference/workflow-api/world/queue.mdx +0 -86
  244. package/docs/deploying/building-a-world.mdx +0 -251
  245. package/docs/deploying/index.mdx +0 -95
  246. package/docs/deploying/meta.json +0 -4
  247. package/docs/deploying/world/local-world.mdx +0 -84
  248. package/docs/deploying/world/meta.json +0 -4
  249. package/docs/deploying/world/postgres-world.mdx +0 -224
  250. package/docs/deploying/world/vercel-world.mdx +0 -179
  251. package/docs/migration-guides/index.mdx +0 -34
  252. package/docs/migration-guides/meta.json +0 -9
  253. package/docs/migration-guides/migrating-from-aws-step-functions.mdx +0 -363
  254. package/docs/migration-guides/migrating-from-inngest.mdx +0 -314
  255. package/docs/migration-guides/migrating-from-temporal.mdx +0 -318
  256. package/docs/migration-guides/migrating-from-trigger-dev.mdx +0 -337
@@ -1,15 +1,23 @@
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
- Cancel a running agent from the outside — for example, a "Stop" button in a chat UI, an admin cancellation endpoint, or a timeout fallback.
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/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
+ />
11
+
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
+
14
+ <Callout type="warn">
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.
16
+ </Callout>
9
17
 
10
18
  ## Pattern
11
19
 
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.
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.
13
21
 
14
22
  ```typescript lineNumbers
15
23
  import { DurableAgent } from "@workflow/ai/agent";
@@ -90,7 +98,7 @@ export async function stoppableAgent(messages: ModelMessage[]) {
90
98
  }
91
99
  ```
92
100
 
93
- ### API Route to Trigger Stop
101
+ ### API route to trigger stop
94
102
 
95
103
  ```typescript lineNumbers
96
104
  import { stopHook } from "@/workflows/stoppable-agent";
@@ -110,7 +118,7 @@ export async function POST(
110
118
  }
111
119
  ```
112
120
 
113
- ### Client Stop Button
121
+ ### Client stop button
114
122
 
115
123
  ```tsx lineNumbers
116
124
  "use client";
@@ -134,23 +142,23 @@ export function StopButton({ runId }: { runId: string }) {
134
142
 
135
143
  ## How it works
136
144
 
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
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.
143
151
 
144
152
  ## Adapting this
145
153
 
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
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.
150
158
 
151
159
  ## Key APIs
152
160
 
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
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`.
@@ -1,150 +1,22 @@
1
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.
2
+ title: DurableAgent is now WorkflowAgent
3
+ description: Use AI SDK v7's WorkflowAgent for durable, resumable AI agents.
4
4
  type: guide
5
- summary: Convert an AI SDK Agent into a DurableAgent backed by a workflow, with tools as retryable steps.
5
+ summary: Build durable, resumable AI agents with AI SDK v7's WorkflowAgent.
6
6
  ---
7
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.
8
+ <CopyPrompt
9
+ text="Convert this stateless AI SDK agent to a durable agent with AI SDK v7's `WorkflowAgent`. Install/use `@ai-sdk/workflow` and `workflow`. Replace the manual `streamText` loop with `new WorkflowAgent(...)` from `@ai-sdk/workflow`, run inside an exported function with `&quot;use workflow&quot;`. Mark side-effectful tool `execute` functions with `&quot;use step&quot;` so they run as durable workflow steps with retries and observability. Stream `ModelCallStreamPart` chunks with `getWritable()` from `workflow`, convert the run stream to UI message chunks with `createModelCallToUIChunkTransform()` in the API route, and expose a route that starts or reconnects to the run. If migrating from the deprecated `DurableAgent`, follow the migration guide at https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent. Verify tool retries, stream output, and resume/reconnect behavior."
10
+ />
9
11
 
10
- ## When to use this
12
+ ## WorkflowAgent from AI SDK v7
11
13
 
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
14
+ Use AI SDK v7's [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent) for new durable agent work. It replaces `DurableAgent` and keeps the current agent pattern in the AI SDK package.
16
15
 
17
- ## Pattern
16
+ - Import `WorkflowAgent` from `@ai-sdk/workflow` and run it inside a `"use workflow"` function.
17
+ - Stream `ModelCallStreamPart` chunks with `getWritable()`, then convert the run stream to UI message chunks with `createModelCallToUIChunkTransform()` in your route.
18
+ - Mark tool `execute` functions with `"use step"` when they should run as durable workflow steps with retry and observability behavior.
18
19
 
19
- Replace `Agent` with `DurableAgent`, wrap the function in `"use workflow"`, mark each tool with `"use step"`, and stream output through `getWritable()`.
20
-
21
- ### Workflow
22
-
23
- ```typescript
24
- import { DurableAgent } from "@workflow/ai/agent";
25
- import { getWritable } from "workflow";
26
- import { z } from "zod";
27
- import type { ModelMessage, UIMessageChunk } from "ai";
28
-
29
- async function searchFlights({ from, to, date }: {
30
- from: string;
31
- to: string;
32
- date: string;
33
- }) {
34
- "use step"; // [!code highlight]
35
- const res = await fetch(
36
- `https://api.example.com/flights?from=${from}&to=${to}&date=${date}`
37
- );
38
- if (!res.ok) throw new Error(`Search failed: ${res.status}`);
39
- return res.json();
40
- }
41
-
42
- async function bookFlight({ flightId, passenger }: {
43
- flightId: string;
44
- passenger: string;
45
- }) {
46
- "use step"; // [!code highlight]
47
- const res = await fetch("https://api.example.com/bookings", {
48
- method: "POST",
49
- headers: { "Content-Type": "application/json" },
50
- body: JSON.stringify({ flightId, passenger }),
51
- });
52
- if (!res.ok) throw new Error(`Booking failed: ${res.status}`);
53
- return res.json();
54
- }
55
-
56
- async function checkWeather({ city }: { city: string }) {
57
- "use step"; // [!code highlight]
58
- const res = await fetch(`https://api.weather.com/forecast?city=${city}`);
59
- return res.json();
60
- }
61
-
62
- export async function flightAgent(messages: ModelMessage[]) {
63
- "use workflow";
64
-
65
- const agent = new DurableAgent({ // [!code highlight]
66
- model: "anthropic/claude-haiku-4.5",
67
- instructions: "You are a helpful flight booking assistant.",
68
- tools: {
69
- searchFlights: {
70
- description: "Search for available flights between two airports",
71
- inputSchema: z.object({
72
- from: z.string().describe("Departure airport code"),
73
- to: z.string().describe("Arrival airport code"),
74
- date: z.string().describe("Travel date (YYYY-MM-DD)"),
75
- }),
76
- execute: searchFlights,
77
- },
78
- bookFlight: {
79
- description: "Book a specific flight for a passenger",
80
- inputSchema: z.object({
81
- flightId: z.string().describe("Flight ID from search results"),
82
- passenger: z.string().describe("Passenger full name"),
83
- }),
84
- execute: bookFlight,
85
- },
86
- checkWeather: {
87
- description: "Check the weather forecast for a city",
88
- inputSchema: z.object({
89
- city: z.string().describe("City name"),
90
- }),
91
- execute: checkWeather,
92
- },
93
- },
94
- });
95
-
96
- const result = await agent.stream({ // [!code highlight]
97
- messages,
98
- writable: getWritable<UIMessageChunk>(), // [!code highlight]
99
- maxSteps: 10,
100
- });
101
-
102
- return { messages: result.messages };
103
- }
104
- ```
105
-
106
- ### API route
107
-
108
- ```typescript
109
- import type { UIMessage } from "ai";
110
- import { convertToModelMessages, createUIMessageStreamResponse } from "ai";
111
- import { start } from "workflow/api";
112
- import { flightAgent } from "@/app/workflows/flight-agent";
113
-
114
- export async function POST(req: Request) {
115
- const { messages }: { messages: UIMessage[] } = await req.json();
116
- const modelMessages = await convertToModelMessages(messages); // [!code highlight]
117
-
118
- const run = await start(flightAgent, [modelMessages]); // [!code highlight]
119
-
120
- return createUIMessageStreamResponse({ // [!code highlight]
121
- stream: run.readable,
122
- headers: {
123
- "x-workflow-run-id": run.runId,
124
- },
125
- });
126
- }
127
- ```
128
-
129
- ## How it works
130
-
131
- 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.
132
- 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.
133
- 3. **Streaming** — `getWritable<UIMessageChunk>()` streams the agent's output (text chunks, tool calls, tool results) to the client in real time via `createUIMessageStreamResponse`.
134
- 4. **maxSteps** — limits the total number of LLM calls the agent can make, preventing runaway tool loops.
135
-
136
- ## Adapting to your use case
137
-
138
- - **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"`).
139
- - **Add tools** — define a new `"use step"` function with a Zod schema. Each tool automatically gets retries and persistence.
140
- - **Workflow-level tools** — if a tool needs workflow primitives like `sleep()` or `createHook()`, omit `"use step"` so it runs in the workflow context instead.
141
- - **Multi-turn** — pass `result.messages` plus new user messages to subsequent `agent.stream()` calls for multi-turn conversations.
142
- - **Client integration** — use `useChat()` from `@ai-sdk/react` with `WorkflowChatTransport` from `@workflow/ai` for a full chat UI with reconnection support.
143
-
144
- ## Key APIs
145
-
146
- - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
147
- - [`"use step"`](/docs/api-reference/workflow/use-step) — declares step functions with retries and full Node.js access
148
- - [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — durable wrapper around AI SDK's Agent
149
- - [`getWritable()`](/docs/api-reference/workflow/get-writable) — streams agent output to the client
150
- - [`start()`](/docs/api-reference/workflow-api/start) — starts a workflow run from an API route
20
+ <Callout type="warn">
21
+ `DurableAgent` is deprecated and remains documented for existing code only. See the [migration guide](https://ai-sdk.dev/v7/docs/agents/workflow-agent#migrating-from-durableagent), or the [`DurableAgent` API reference](/docs/api-reference/workflow-ai/durable-agent) while migrating.
22
+ </Callout>
@@ -5,14 +5,22 @@ type: guide
5
5
  summary: Use defineHook with the tool call ID to suspend an agent for human approval, with an optional timeout.
6
6
  ---
7
7
 
8
- Use this pattern when an AI agent needs human confirmation before performing a consequential action like booking, purchasing, or publishing. The workflow suspends without consuming resources until the human responds.
8
+ <CopyPrompt
9
+ text="Add a human approval gate to this AI workflow. Define a typed hook with `defineHook()` from `workflow` for approval payloads. At the approval point, create the hook once with a stable token, await it inside the `&quot;use workflow&quot;` function, and branch on approved/rejected input. Add a server route that receives the human decision and calls `resumeHook(token, payload)` from `workflow/api`. If the approval should expire, race the hook against `sleep()` from `workflow`. Update the UI to show the pending approval and call the resume route. Verify approve, reject, timeout, and duplicate resume behavior."
10
+ />
11
+
12
+ <Callout type="warn">
13
+ 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 human-in-the-loop pattern here (hooks, `Promise.race`, approval gating) applies to either API.
14
+ </Callout>
15
+
16
+ Use this pattern when an AI agent needs human confirmation before performing a consequential action like booking, purchasing, or publishing. The workflow suspends without consuming resources until the human responds through a user interface (UI) or API.
9
17
 
10
18
  ## When to use this
11
19
 
12
20
  - Booking confirmations where users must approve before charges are made
13
21
  - Content publishing gates where an editor must sign off
14
- - Any agent action where the cost of getting it wrong justifies a human check
15
- - Actions with side effects that can't be easily undone
22
+ - Agent actions where the cost of an error justifies a human check
23
+ - Actions with side effects that are difficult to reverse
16
24
 
17
25
  ## Pattern
18
26
 
@@ -59,7 +67,7 @@ async function confirmBooking({ flightId, passenger }: {
59
67
  }
60
68
 
61
69
  // Stream a custom data part so the client can render the approval UI.
62
- // This MUST run before the hook suspends the workflow — otherwise
70
+ // This MUST run before the hook suspends the workflow, otherwise
63
71
  // the tool-invocation won't appear in the stream until the tool returns,
64
72
  // and the client would have no way to show approval buttons.
65
73
  async function emitApprovalRequest(details: {
@@ -99,7 +107,7 @@ async function emitApprovalResolved(details: {
99
107
  }
100
108
  }
101
109
 
102
- // No "use step" — hooks are workflow-level primitives
110
+ // No "use step": hooks are workflow-level primitives
103
111
  async function requestBookingApproval(
104
112
  { flightId, passenger, price }: {
105
113
  flightId: string;
@@ -230,26 +238,26 @@ const approvalResult = messages
230
238
 
231
239
  ## How it works
232
240
 
233
- 1. **`defineHook()` with schema** — creates a typed hook with Zod validation. The approval payload is validated before the workflow receives it.
234
- 2. **`toolCallId` as token** — the approval tool uses the tool call ID as the hook token, naturally linking the hook to the specific tool invocation.
235
- 3. **`emitApprovalRequest` step** — writes a `data-approval-needed` custom data part to the stream *before* the hook suspends. Without this, the client would never see the approval controls because tool invocations don't stream until the tool returns.
236
- 4. **No `"use step"` on the approval tool** — the tool runs at the workflow level because `defineHook().create()` is a workflow primitive. It calls step functions (`emitApprovalRequest`, `emitApprovalResolved`, `confirmBooking`) for I/O.
237
- 5. **`Promise.race` with sleep** — the approval races against a durable timeout. If nobody responds, the workflow continues with an expiration message.
238
- 6. **`emitApprovalResolved` step** — writes the outcome to the stream so the client can update the card immediately, without waiting for the tool-invocation result.
241
+ 1. **`defineHook()` with schema**: Creates a typed hook with Zod validation. The approval payload is validated before the workflow receives it.
242
+ 2. **`toolCallId` as token**: Uses the tool call ID as the hook token, linking the hook to the specific tool invocation.
243
+ 3. **`emitApprovalRequest` step**: Writes a `data-approval-needed` custom data part to the stream *before* the hook suspends. Without this step, the client wouldn't see the approval controls because tool invocations don't stream until the tool returns.
244
+ 4. **No `"use step"` on the approval tool**: Runs the tool at the workflow level because `defineHook().create()` is a workflow primitive. The tool calls step functions (`emitApprovalRequest`, `emitApprovalResolved`, and `confirmBooking`) for I/O.
245
+ 5. **`Promise.race` with sleep**: Races the approval against a durable timeout. If nobody responds, the workflow continues with an expiration message.
246
+ 6. **`emitApprovalResolved` step**: Writes the outcome to the stream so the client can update the card immediately without waiting for the tool-invocation result.
239
247
 
240
248
  ## Adapting to your use case
241
249
 
242
- - **Change the approval schema** — add fields like `reason`, `amount`, `reviewerEmail` to match your domain.
243
- - **Multiple approval gates** — the pattern works for any number of tools. Each tool creates its own hook with its own `toolCallId`.
244
- - **Escalation** — if the first approver doesn't respond, use `sleep()` + another hook to escalate to a backup reviewer.
245
- - **Adjust timeout** — use `"24h"` for production, shorter durations for demos.
246
- - **Workflow-level vs step tools** — tools that use `sleep()`, `defineHook()`, or other workflow primitives must NOT use `"use step"`. Tools with only I/O (API calls, DB queries) should use `"use step"` for retries.
250
+ - **Change the approval schema**: Add fields such as `reason`, `amount`, and `reviewerEmail` to match your domain.
251
+ - **Multiple approval gates**: Apply the pattern to any number of tools. Each tool creates its own hook with its own `toolCallId`.
252
+ - **Escalation**: If the first approver doesn't respond, use `sleep()` and another hook to escalate to a backup reviewer.
253
+ - **Adjust the timeout**: Use `"24h"` for production and shorter durations for demos.
254
+ - **Workflow-level versus step tools**: Tools that use `sleep()`, `defineHook()`, or other workflow primitives must not use `"use step"`. Tools with only I/O, such as API calls and database queries, should use `"use step"` for retries.
247
255
 
248
256
  ## Key APIs
249
257
 
250
- - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) — declares the orchestrator function
251
- - [`"use step"`](/docs/api-reference/workflow/use-step) — declares step functions with retries
252
- - [`defineHook()`](/docs/api-reference/workflow/define-hook) — type-safe hook with schema validation
253
- - [`sleep()`](/docs/api-reference/workflow/sleep) — durable timeout for approval expiry
254
- - [`getWritable()`](/docs/api-reference/workflow/get-writable) — stream custom data parts from steps
255
- - [`DurableAgent`](/docs/api-reference/workflow-ai/durable-agent) — durable agent with tool definitions
258
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
259
+ - [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Declares step functions with retries.
260
+ - [`defineHook()`](/docs/api-reference/workflow/define-hook): Defines a type-safe hook with schema validation.
261
+ - [`sleep()`](/docs/api-reference/workflow/sleep): Provides a durable timeout for approval expiration.
262
+ - [`getWritable()`](/docs/api-reference/workflow/get-writable): Streams custom data parts from steps.
263
+ - [`WorkflowAgent`](https://ai-sdk.dev/v7/docs/agents/workflow-agent#workflowagent): Provides AI SDK's durable agent and replaces `DurableAgent`.
@@ -5,11 +5,15 @@ type: guide
5
5
  summary: Split items into fixed-size batches, process each batch concurrently with Promise.allSettled, and pace batches with sleep to avoid overloading downstream services.
6
6
  ---
7
7
 
8
+ <CopyPrompt
9
+ text="Implement durable batch processing. Import `sleep` from `workflow`. In an exported &quot;use workflow&quot; function, split the input records into chunks of a fixed `batchSize`. For each batch, call a &quot;use step&quot; helper such as `processRecord(record)` for every record using `Promise.allSettled` so one record failure does not hide the rest. Record successes and failures in a serializable result object. Between batches, `await sleep(&quot;1s&quot;)` or another configured delay to respect downstream rate limits. Make the step idempotent using record IDs or external idempotency keys. Verify all-success, partial-failure, and rate-paced execution paths."
10
+ />
11
+
8
12
  Use batching when you need to process a large list of items in parallel while controlling concurrency. Items are split into fixed-size batches, each batch runs concurrently, and failures in one batch don't affect others.
9
13
 
10
14
  ## When to use this
11
15
 
12
- - Bulk data imports (contacts, orders, products from a CSV)
16
+ - Bulk data imports (contacts, orders, or products from a comma-separated values (CSV) file)
13
17
  - Processing hundreds or thousands of items against external APIs
14
18
  - Calling rate-limited APIs where you need to control concurrency
15
19
  - Any fan-out where you want failure isolation between groups
@@ -17,7 +21,7 @@ Use batching when you need to process a large list of items in parallel while co
17
21
  ## How it works
18
22
 
19
23
  1. Records are split into fixed-size batches.
20
- 2. Each batch runs in parallel via `Promise.allSettled` — failures in one record don't affect others.
24
+ 2. Each batch runs in parallel through `Promise.allSettled`, so failures in one record don't affect others.
21
25
  3. A `sleep()` between batches paces requests to avoid overloading downstream services.
22
26
  4. After all batches, a summary is returned with succeeded/failed counts.
23
27
 
@@ -41,7 +45,7 @@ export async function batchImport(records: Record[], batchSize: number) {
41
45
  for (let i = 0; i < records.length; i += batchSize) {
42
46
  const batch = records.slice(i, i + batchSize);
43
47
 
44
- // Run batch in parallel — failures are isolated per record
48
+ // Run batch in parallel: failures are isolated per record
45
49
  const outcomes = await Promise.allSettled( // [!code highlight]
46
50
  batch.map((record) => processRecord(record))
47
51
  );
@@ -85,21 +89,21 @@ async function processRecord(record: Record): Promise<string> {
85
89
 
86
90
  ## Adapting to your use case
87
91
 
88
- - Replace the `Record` type with your actual data shape (orders, images, products, etc.).
89
- - Replace `processRecord()` with your real import logic — DB upserts, API calls, file processing.
92
+ - Replace the `Record` type with your actual data shape, such as orders, images, or products.
93
+ - Replace `processRecord()` with your import logic, such as database upserts, API calls, or file processing.
90
94
  - Tune `batchSize` and the `sleep()` duration to match your downstream rate limits.
91
- - Add or remove tracking as needed — the pattern works with any item type.
95
+ - Add or remove tracking as needed; the pattern works with any item type.
92
96
 
93
97
  ## Tips
94
98
 
95
- - **Use `Promise.allSettled` over `Promise.all`** when you want to continue even if some items fail. `Promise.all` rejects on the first failure; `allSettled` waits for everything and tells you what failed.
96
- - **Tune batch size to your downstream API limits.** If the API allows 10 concurrent requests, use `batchSize: 10`.
97
- - **Add pacing with `sleep()`** between batches to respect rate limits. The sleep is durable — it survives cold starts.
98
- - **Each `processRecord` call is an independent step.** If one fails, it retries up to 3 times without affecting other items in the batch.
99
+ - **Use `Promise.allSettled` instead of `Promise.all`**: Use this pattern when you want to continue even if some items fail. `Promise.all` rejects on the first failure, while `allSettled` waits for everything and identifies failures.
100
+ - **Tune batch size to your downstream API limits**: If the API allows 10 concurrent requests, use `batchSize: 10`.
101
+ - **Add pacing with `sleep()`**: Add a delay between batches to respect rate limits. The sleep is durable and survives cold starts.
102
+ - **Treat each `processRecord` call as an independent step**: If one call fails, it retries up to three times without affecting other items in the batch.
99
103
 
100
104
  ## Key APIs
101
105
 
102
- - [`"use workflow"`](/docs/foundations/workflows-and-steps) -- marks the orchestrator function
103
- - [`"use step"`](/docs/foundations/workflows-and-steps) -- marks functions that run with full Node.js access
104
- - [`sleep()`](/docs/api-reference/workflow/sleep) -- pacing delay between batches
105
- - [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) -- runs items in parallel, isolating failures
106
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps): Marks the orchestrator function.
107
+ - [`"use step"`](/docs/foundations/workflows-and-steps): Marks functions that run with full Node.js access.
108
+ - [`sleep()`](/docs/api-reference/workflow/sleep): Adds a pacing delay between batches.
109
+ - [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled): Runs items in parallel and isolates failures.
@@ -1,45 +1,29 @@
1
1
  ---
2
2
  title: Idempotency
3
- description: Ensure external side effects happen exactly once, even when steps are retried or workflows are replayed.
3
+ description: Make step retries safe and coordinate duplicate workflow starts with hook tokens.
4
4
  type: guide
5
- summary: Use step IDs as idempotency keys for external APIs like Stripe so that retries and replays don't create duplicate charges.
5
+ summary: Use step IDs for retry-safe external calls, and use deterministic hook tokens when duplicate requests must route to one active workflow.
6
6
  ---
7
7
 
8
- Workflow steps can be retried (on failure) and replayed (on cold start). If a step calls an external API that isn't idempotent, retries could create duplicate charges, send duplicate emails, or double-process records. Use idempotency keys to make these operations safe.
8
+ <CopyPrompt
9
+ text="Make this workflow's side effects idempotent. For retry-safe external calls, read the deterministic step ID inside the &quot;use step&quot; function with `getStepMetadata()` from `workflow` and pass `stepId` as the idempotency key to the external API (for example Stripe's `Idempotency-Key` header) so step retries deduplicate. For duplicate workflow starts, derive a deterministic hook token from the domain key (for example `order:${orderId}`): in the API route, look up the active hook with `getHookByToken(token)` from `workflow/api` (catching `HookNotFoundError` from `workflow/errors`) and reuse its `runId`, otherwise call `start(...)`; inside the workflow, create the hook with the same token and check `await hook.getConflict()` before duplicate-sensitive work. Verify retried steps deduplicate, duplicate starts reuse the active run, and conflicts are handled."
10
+ />
11
+
12
+ Use idempotency when a retry or duplicate request should not repeat the underlying work. In Workflow, there are two common patterns: use the step ID for retry-safe external calls, and use hook tokens to coordinate duplicate workflow starts.
9
13
 
10
14
  ## When to use this
11
15
 
12
- - Charging a payment (Stripe, PayPal)
13
- - Sending transactional emails or SMS
14
- - Creating records in external systems where duplicates are harmful
15
- - Any step that has side effects in systems you don't control
16
+ - A step charges a payment, sends an email, enqueues work, or creates an external record.
17
+ - A route may receive duplicate requests that should map to one active workflow run.
16
18
 
17
- ## Pattern: Step ID as idempotency key
19
+ ## Step idempotency
18
20
 
19
21
  Every step has a unique, deterministic `stepId` available via `getStepMetadata()`. Pass this as the idempotency key to external APIs:
20
22
 
21
23
  ```typescript
22
24
  import { getStepMetadata } from "workflow";
23
25
 
24
- declare function createCharge(customerId: string, amount: number): Promise<{ id: string }>; // @setup
25
- declare function sendReceipt(customerId: string, chargeId: string): Promise<void>; // @setup
26
-
27
- export async function chargeCustomer(customerId: string, amount: number) {
28
- "use workflow";
29
-
30
- const charge = await createCharge(customerId, amount);
31
- await sendReceipt(customerId, charge.id);
32
-
33
- return { customerId, chargeId: charge.id, status: "completed" };
34
- }
35
- ```
36
-
37
- ### Step function with idempotency key
38
-
39
- ```typescript
40
- import { getStepMetadata } from "workflow";
41
-
42
- async function createCharge(
26
+ export async function createCharge(
43
27
  customerId: string,
44
28
  amount: number
45
29
  ): Promise<{ id: string }> {
@@ -69,39 +53,43 @@ async function createCharge(
69
53
 
70
54
  return charge.json();
71
55
  }
72
-
73
- async function sendReceipt(customerId: string, chargeId: string): Promise<void> {
74
- "use step";
75
-
76
- const { stepId } = getStepMetadata();
77
-
78
- await fetch("https://api.example.com/receipts", {
79
- method: "POST",
80
- headers: { "Idempotency-Key": stepId },
81
- body: JSON.stringify({ customerId, chargeId }),
82
- });
83
- }
84
56
  ```
85
57
 
86
- ## Race condition caveats
58
+ See [Step Idempotency](/docs/foundations/idempotency#step-idempotency) for why `stepId` is stable across retries and how to think about external API conflicts.
87
59
 
88
- Workflow does not currently provide distributed locking or true exactly-once delivery across concurrent runs. If two workflow runs could process the same entity concurrently:
60
+ ## Run idempotency
89
61
 
90
- - **Rely on the external API's idempotency** (like Stripe's `Idempotency-Key`) rather than checking a local flag.
91
- - **Don't use check-then-act patterns** like "read a flag, then write if not set" -- another run could read the same flag between your read and write.
62
+ For duplicate workflow-start requests, derive a hook token from your domain key. You can avoid obvious duplicate starts by checking whether an active hook already owns that token before calling `start()`:
92
63
 
93
- If your external API doesn't support idempotency keys natively, consider adding a deduplication layer (e.g., a database unique constraint on the operation ID).
64
+ ```typescript
65
+ import { getHookByToken, start } from "workflow/api";
66
+ import { HookNotFoundError } from "workflow/errors";
67
+ import { processOrder } from "./workflows/process-order";
68
+
69
+ export async function POST(request: Request) {
70
+ const { orderId } = await request.json();
71
+ const token = `order:${orderId}`;
72
+
73
+ try {
74
+ const hook = await getHookByToken(token); // [!code highlight]
75
+ return Response.json({ runId: hook.runId, reused: true });
76
+ } catch (error) {
77
+ if (!HookNotFoundError.is(error)) throw error;
78
+ }
94
79
 
95
- ## Tips
80
+ const run = await start(processOrder, [orderId]); // [!code highlight]
81
+ return Response.json({ runId: run.runId, reused: false });
82
+ }
83
+ ```
96
84
 
97
- - **`stepId` is deterministic.** It's the same value across retries and replays of the same step, making it a reliable idempotency key.
98
- - **Always provide idempotency keys for non-idempotent external calls.** Even if you think a step won't be retried, cold-start replay will re-execute it.
99
- - **Handle 409/conflict as success.** If an external API returns "already processed," treat that as a successful result, not an error.
100
- - **Make your own APIs idempotent** where possible. Accept an idempotency key and return the cached result on duplicate requests.
85
+ The workflow should create the deterministic hook and check `await hook.getConflict()` before duplicate-sensitive work: awaiting `getConflict()` suspends the workflow to commit the hook registration and resolves with the conflicting run when another active run already owns the token (or `null` once the hook is registered). See [Run idempotency](/docs/foundations/idempotency#run-idempotency) for the full pattern, including how to steer an active run with `resumeHook()` and how to handle the current race between `start()` and hook registration.
101
86
 
102
87
  ## Key APIs
103
88
 
104
- - [`"use workflow"`](/docs/api-reference/workflow/use-workflow) -- declares the orchestrator function
105
- - [`"use step"`](/docs/api-reference/workflow/use-step) -- declares step functions with full Node.js access
106
- - [`getStepMetadata()`](/docs/api-reference/step/get-step-metadata) -- provides the deterministic `stepId` for idempotency keys
107
- - [`start()`](/docs/api-reference/workflow-api/start) -- starts a new workflow run
89
+ - [`"use workflow"`](/docs/foundations/workflows-and-steps#workflow-functions): Declares the orchestrator function.
90
+ - [`"use step"`](/docs/foundations/workflows-and-steps#step-functions): Declares step functions with full Node.js access.
91
+ - [`getStepMetadata()`](/docs/api-reference/workflow/get-step-metadata): Provides the deterministic `stepId` for idempotency keys.
92
+ - [`createHook()`](/docs/api-reference/workflow/create-hook): Creates a hook with an optional deterministic token.
93
+ - [`getHookByToken()`](/docs/api-reference/workflow-api/get-hook-by-token): Finds the hook that owns a token.
94
+ - [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook): Resumes the active hook when the duplicate request carries data.
95
+ - [`start()`](/docs/api-reference/workflow-api/start): Starts a new workflow run.
@@ -5,6 +5,10 @@ type: guide
5
5
  summary: When an external API returns 429, throw RetryableError with the Retry-After value so the workflow runtime automatically reschedules the step after the specified delay.
6
6
  ---
7
7
 
8
+ <CopyPrompt
9
+ text="Add durable rate-limit handling to external API steps. Import `RetryableError`, `FatalError`, `getStepMetadata`, and `sleep` from `workflow` as needed. In each HTTP-calling &quot;use step&quot; helper, if the response status is 429, read the `Retry-After` header and throw `new RetryableError(&quot;Rate limited&quot;, { retryAfter })`. For transient 5xx/network failures, throw `RetryableError` with an exponential delay based on `getStepMetadata().attempt`. For permanent 4xx failures, throw `FatalError`. If the dependency is completely down, add a workflow-level circuit breaker that `await sleep(&quot;30s&quot;)` before probing again. Make requests idempotent with stable keys, and verify 429, 5xx, fatal 4xx, and circuit-breaker behavior."
10
+ />
11
+
8
12
  Use this pattern when calling external APIs that enforce rate limits. Instead of writing manual retry loops, throw `RetryableError` with a `retryAfter` value and let the workflow runtime handle rescheduling.
9
13
 
10
14
  ## When to use this
@@ -63,7 +67,7 @@ async function upsertToWarehouse(contactId: string, contact: unknown) {
63
67
  }
64
68
  ```
65
69
 
66
- ## Pattern: Exponential backoff
70
+ ## Pattern: exponential backoff
67
71
 
68
72
  Use `getStepMetadata()` to access the current attempt number and calculate increasing delays:
69
73
 
@@ -86,7 +90,7 @@ async function callFlakeyApi(endpoint: string) {
86
90
  }
87
91
  ```
88
92
 
89
- ## Pattern: Circuit breaker with sleep
93
+ ## Pattern: circuit breaker with sleep
90
94
 
91
95
  When a dependency is completely down, stop hitting it for a cooldown period using `sleep()`, then probe with a single test request:
92
96
 
@@ -134,7 +138,7 @@ async function callService(requestNum: number): Promise<boolean> {
134
138
  }
135
139
  ```
136
140
 
137
- ## Pattern: Custom max retries
141
+ ## Pattern: custom max retries
138
142
 
139
143
  Override the default retry count (3) for steps that need more or fewer attempts:
140
144
 
@@ -224,5 +228,5 @@ export async function downloadWithRetry(url: string) {
224
228
  - [`"use step"`](/docs/foundations/workflows-and-steps) -- marks functions that run with full Node.js access
225
229
  - [`RetryableError`](/docs/api-reference/workflow/retryable-error) -- signals the runtime to retry after a delay
226
230
  - [`FatalError`](/docs/api-reference/workflow/fatal-error) -- signals a permanent failure, skipping retries
227
- - [`getStepMetadata()`](/docs/api-reference/step/get-step-metadata) -- provides the current attempt number and step ID
231
+ - [`getStepMetadata()`](/docs/api-reference/workflow/get-step-metadata) -- provides the current attempt number and step ID
228
232
  - [`sleep()`](/docs/api-reference/workflow/sleep) -- durable pause for circuit breaker cooldowns