workflow 5.7.2 → 5.8.1
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.
- package/dist/internal/errors.d.ts +1 -1
- package/dist/internal/errors.js +2 -2
- package/docs/advanced/quickjs-runtime.mdx +4 -4
- package/docs/ai/sleep-and-delays.mdx +1 -1
- package/docs/api-reference/workflow/fetch.mdx +1 -1
- package/docs/api-reference/workflow/index.mdx +3 -0
- package/docs/api-reference/workflow/rate-limit.mdx +114 -0
- package/docs/api-reference/workflow-errors/in-band-superseded-error.mdx +5 -1
- package/docs/api-reference/workflow-errors/precondition-failed-error.mdx +6 -4
- package/docs/api-reference/workflow-errors/throttle-error.mdx +1 -1
- package/docs/api-reference/workflow-runtime/world/queue.mdx +5 -1
- package/docs/comparisons/workflow-sdk-vs-aws-step-functions.mdx +1 -1
- package/docs/comparisons/workflow-sdk-vs-inngest.mdx +49 -5
- package/docs/comparisons/workflow-sdk-vs-temporal.mdx +1 -0
- package/docs/comparisons/workflow-sdk-vs-trigger-dev.mdx +3 -1
- package/docs/configuration/runtime-tuning.mdx +478 -168
- package/docs/cookbook/common-patterns/batching.mdx +2 -2
- package/docs/cookbook/common-patterns/rate-limiting.mdx +27 -2
- package/docs/cookbook/index.mdx +1 -1
- package/docs/cookbook/integrations/chat-sdk.mdx +1 -1
- package/docs/errors/max-events-exceeded.mdx +1 -0
- package/docs/errors/rate-limit-max-wait.mdx +31 -0
- package/docs/foundations/errors-and-retries.mdx +2 -0
- package/docs/foundations/meta.json +1 -0
- package/docs/foundations/rate-limits.mdx +181 -0
- package/docs/performance.mdx +93 -59
- package/docs/worlds/building-a-world.mdx +6 -3
- package/docs/worlds/local.mdx +6 -2
- package/docs/worlds/postgres.mdx +9 -3
- package/docs/worlds/upgrading-to-v5.mdx +2 -2
- package/docs/worlds/vercel.mdx +2 -2
- package/package.json +11 -11
|
@@ -15,7 +15,7 @@ Use batching when you need to process a large list of items in parallel while co
|
|
|
15
15
|
|
|
16
16
|
- Bulk data imports (contacts, orders, or products from a comma-separated values (CSV) file)
|
|
17
17
|
- Processing hundreds or thousands of items against external APIs
|
|
18
|
-
- Calling rate-limited APIs where you need to control concurrency
|
|
18
|
+
- Calling rate-limited APIs where you need to control concurrency (for a calls-per-period limit, see [Rate Limits](/docs/foundations/rate-limits))
|
|
19
19
|
- Any fan-out where you want failure isolation between groups
|
|
20
20
|
- High-concurrency fan-out, where one flat `Promise.all` over the whole list would put more work in flight than your downstream services or the run's event log should carry at once
|
|
21
21
|
|
|
@@ -102,7 +102,7 @@ async function processRecord(record: Record): Promise<string> {
|
|
|
102
102
|
- **Batching bounds concurrency, not total event count**: Every record processed in its own step adds events to the same [event log](/docs/how-it-works/event-sourcing#how-fast-a-log-grows), regardless of batch size.
|
|
103
103
|
- **Use the QuickJS engine for long-running batches**: [Set `WORKFLOW_VM=quickjs`](/docs/configuration/runtime-tuning#workflow_vm) to run the workflow on QuickJS, which snapshots the workflow VM and replays only the events recorded since the last snapshot on resume. This reduces replay overhead as the log grows and exempts eligible runs from the default [per-run event limit](/worlds/vercel#per-run-limits) ([`MAX_EVENTS_EXCEEDED`](/docs/errors/max-events-exceeded)). It does not reduce event count or speed up the work inside each step.
|
|
104
104
|
- **Choose the retry and run boundaries you need**: Keep one step per record when independent retries and visibility matter. To write fewer events, process multiple records within each step and make the grouped work idempotent, since a retry can repeat the whole group. Use a [child workflow](/cookbook/advanced/child-workflows) per batch when you need separate run histories and failure boundaries.
|
|
105
|
-
- **
|
|
105
|
+
- **Pace calls with a rate limit**: To stay under an API's calls-per-period limit, call [`experimental_rateLimit()`](/docs/foundations/rate-limits) before each call. The limit is shared by every run, so concurrent imports stay under it together. A `sleep()` between batches also adds a durable delay, but only paces a single run.
|
|
106
106
|
- **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.
|
|
107
107
|
|
|
108
108
|
## Key APIs
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: Rate Limiting & Retries
|
|
3
|
-
description:
|
|
3
|
+
description: Stay under an API's rate limit with experimental_rateLimit(), and handle 429 responses and transient failures with RetryableError and exponential backoff.
|
|
4
4
|
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
|
---
|
|
@@ -9,14 +9,38 @@ summary: When an external API returns 429, throw RetryableError with the Retry-A
|
|
|
9
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 "use step" helper, if the response status is 429, read the `Retry-After` header and throw `new RetryableError("Rate limited", { 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("30s")` before probing again. Make requests idempotent with stable keys, and verify 429, 5xx, fatal 4xx, and circuit-breaker behavior."
|
|
10
10
|
/>
|
|
11
11
|
|
|
12
|
-
Use
|
|
12
|
+
Use these patterns when calling external APIs that enforce rate limits. Call [`experimental_rateLimit()`](/docs/foundations/rate-limits) before the call to keep every run under the API's limit, and throw `RetryableError` with a `retryAfter` value for the 429s that still get through, instead of writing manual retry loops.
|
|
13
13
|
|
|
14
14
|
## When to use this
|
|
15
15
|
|
|
16
|
+
- Calling an API with a published rate limit from many runs at once
|
|
16
17
|
- Calling APIs that return 429 (Too Many Requests) with `Retry-After` headers
|
|
17
18
|
- Any step that hits transient failures and needs backoff
|
|
18
19
|
- Syncing data with third-party services (Stripe, CRMs, scrapers)
|
|
19
20
|
|
|
21
|
+
## Pattern: stay under the limit with experimental_rateLimit()
|
|
22
|
+
|
|
23
|
+
Call `experimental_rateLimit()` in the workflow before the step that calls the API. Every run that uses the same key shares its limit, and a run whose slot is later waits for it like a `sleep()`:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { experimental_rateLimit } from "workflow";
|
|
27
|
+
|
|
28
|
+
declare function fetchFromCrm(contactId: string): Promise<unknown>; // @setup
|
|
29
|
+
declare function upsertToWarehouse(contactId: string, contact: unknown): Promise<void>; // @setup
|
|
30
|
+
|
|
31
|
+
export async function syncContact(contactId: string) {
|
|
32
|
+
"use workflow";
|
|
33
|
+
|
|
34
|
+
await experimental_rateLimit({ key: "crm", limit: 100, period: "1m" }); // [!code highlight]
|
|
35
|
+
const contact = await fetchFromCrm(contactId);
|
|
36
|
+
await upsertToWarehouse(contactId, contact);
|
|
37
|
+
|
|
38
|
+
return { contactId, status: "synced" };
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
A step's retries don't wait for a new slot, so keep the 429 handling below in the step too. See [Rate Limits](/docs/foundations/rate-limits) for keys, `maxWait`, and `burst`.
|
|
43
|
+
|
|
20
44
|
## Pattern: RetryableError with Retry-After
|
|
21
45
|
|
|
22
46
|
A step function calls an external API. On 429, it reads the `Retry-After` header and throws `RetryableError`. The runtime reschedules the step automatically.
|
|
@@ -224,6 +248,7 @@ export async function downloadWithRetry(url: string) {
|
|
|
224
248
|
|
|
225
249
|
## Key APIs
|
|
226
250
|
|
|
251
|
+
- [`experimental_rateLimit()`](/docs/foundations/rate-limits) -- waits for a slot under a rate limit every run shares
|
|
227
252
|
- [`"use workflow"`](/docs/foundations/workflows-and-steps) -- marks the orchestrator function
|
|
228
253
|
- [`"use step"`](/docs/foundations/workflows-and-steps) -- marks functions that run with full Node.js access
|
|
229
254
|
- [`RetryableError`](/docs/api-reference/workflow/retryable-error) -- signals the runtime to retry after a delay
|
package/docs/cookbook/index.mdx
CHANGED
|
@@ -18,7 +18,7 @@ Use these workflow patterns and copy-paste code examples to implement common use
|
|
|
18
18
|
- [**Workflow Composition**](/cookbook/common-patterns/workflow-composition): Call workflows from other workflows by direct await or background spawn via `start()`
|
|
19
19
|
- [**Saga**](/cookbook/common-patterns/saga): Coordinate multi-step transactions with automatic rollback when a step fails
|
|
20
20
|
- [**Batching**](/cookbook/common-patterns/batching): Process large collections in parallel batches with failure isolation
|
|
21
|
-
- [**Rate Limiting**](/cookbook/common-patterns/rate-limiting):
|
|
21
|
+
- [**Rate Limiting**](/cookbook/common-patterns/rate-limiting): Stay under an API's rate limit with experimental_rateLimit(), and handle 429 responses with RetryableError and backoff
|
|
22
22
|
- [**Scheduling**](/cookbook/common-patterns/scheduling): Use durable sleep to schedule actions minutes, hours, or weeks ahead
|
|
23
23
|
- [**Timeouts**](/cookbook/common-patterns/timeouts): Add deadlines to slow steps, hooks, and webhooks by racing them against a durable sleep
|
|
24
24
|
- [**Idempotency**](/cookbook/common-patterns/idempotency): Ensure side effects and duplicate starts are safe to retry
|
|
@@ -28,7 +28,7 @@ Workflow SDK complements it by making bot **sessions** durable. Each conversatio
|
|
|
28
28
|
One thread mapped to one workflow run also means the thread stays on the deployment that started it. For channels where each message should use newer code, see [Versioning](/docs/foundations/versioning) for explicit child-run and handoff patterns using `deploymentId: "latest"`.
|
|
29
29
|
</Callout>
|
|
30
30
|
|
|
31
|
-
The rest of this page covers the integration pattern.
|
|
31
|
+
The rest of this page covers the integration pattern. If you don't have a Chat SDK bot yet, start with the [Slack bot with Next.js and Redis guide](https://vercel.com/kb/guide/how-to-build-a-slack-bot-with-next-js-and-redis), then use this page to make its sessions durable.
|
|
32
32
|
|
|
33
33
|
## How it fits together
|
|
34
34
|
|
|
@@ -56,6 +56,7 @@ A run qualifies when its `executionContext` records `workflowVm: "quickjs"` and
|
|
|
56
56
|
|
|
57
57
|
- Only runs started after the change are exempt. Runs keep the VM and snapshot policy they started with.
|
|
58
58
|
- Runs that have already failed with `MAX_EVENTS_EXCEEDED` are not revived. Start them again.
|
|
59
|
+
- On the Local World and Postgres World, snapshots are only stored when [`WORKFLOW_SNAPSHOT_ALLOW_UNENCRYPTED=1`](/docs/configuration/runtime-tuning#workflow_snapshot_allow_unencrypted) is set. Without it, exempt runs still replay the full log on every invocation.
|
|
59
60
|
|
|
60
61
|
### Split the work into child workflows
|
|
61
62
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: rate-limit-max-wait
|
|
3
|
+
description: An experimental_rateLimit() call could not get a slot within its maxWait.
|
|
4
|
+
type: troubleshooting
|
|
5
|
+
summary: Handle RateLimitMaxWaitError when a rate-limited call's next slot is later than its maxWait.
|
|
6
|
+
prerequisites:
|
|
7
|
+
- /docs/api-reference/workflow/rate-limit
|
|
8
|
+
related:
|
|
9
|
+
- /docs/foundations/rate-limits
|
|
10
|
+
- /docs/api-reference/workflow/rate-limit
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
This error is thrown by [`experimental_rateLimit()`](/docs/api-reference/workflow/rate-limit) when the key's next free slot is later than the call's `maxWait`. The call reserved nothing, so it used up none of the key's capacity.
|
|
14
|
+
|
|
15
|
+
It is also thrown, after waiting, by a call whose run woke [so late](/docs/api-reference/workflow/rate-limit#calls-that-wake-late) that it was given new slots and ended up past its `maxWait`. That call had reserved a slot.
|
|
16
|
+
|
|
17
|
+
## Error message
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
Rate limit "<key>" has no slot within the call's maxWait (next slot at <time>)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Why this happens
|
|
24
|
+
|
|
25
|
+
Every call on a key takes the next slot, `period / limit` after the one before. When more calls arrive than the limit admits, slots move into the future, and a call whose slot would be past its `maxWait` fails at once instead of waiting.
|
|
26
|
+
|
|
27
|
+
## How to fix it
|
|
28
|
+
|
|
29
|
+
- Catch it with `RateLimitMaxWaitError.is(error)` from `workflow/errors` and skip, defer or report the work. `error.wouldAcquireAt` is when the call would have gone ahead (for a call that woke late, the slot it missed).
|
|
30
|
+
- Raise the `maxWait`, or leave it out to wait as long as it takes (up to 30 days).
|
|
31
|
+
- Spread the work across keys that the upstream limits separately.
|
|
@@ -97,6 +97,8 @@ async function callApi(endpoint: string) {
|
|
|
97
97
|
}
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
+
To keep calls under an upstream's rate limit across every run in the first place, call [`experimental_rateLimit()`](/docs/foundations/rate-limits) in the workflow before the step. A step's retries don't wait for a new slot, so keep handling 429 in the step too.
|
|
101
|
+
|
|
100
102
|
## Advanced example
|
|
101
103
|
|
|
102
104
|
This final example combines everything we've learned, along with [`getStepMetadata`](/docs/api-reference/workflow/get-step-metadata).
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Rate Limits
|
|
3
|
+
description: Use experimental_rateLimit() to keep calls to a shared upstream API under its rate limit across every workflow run.
|
|
4
|
+
type: conceptual
|
|
5
|
+
summary: Keep calls to an upstream API under its rate limit across all workflow runs.
|
|
6
|
+
prerequisites:
|
|
7
|
+
- /docs/foundations/workflows-and-steps
|
|
8
|
+
related:
|
|
9
|
+
- /docs/api-reference/workflow/rate-limit
|
|
10
|
+
- /docs/errors/rate-limit-max-wait
|
|
11
|
+
- /docs/foundations/errors-and-retries
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Rate limits keep the calls your workflows make to an upstream API, such as a model provider or a payments API, under the limit that API enforces. Every run of your project shares the same limit, so a thousand runs started at once still call the upstream at the rate you set instead of all at once.
|
|
15
|
+
|
|
16
|
+
<Callout type="warn">
|
|
17
|
+
`experimental_rateLimit()` is experimental and is available in `workflow` 5.8.0 and later.
|
|
18
|
+
</Callout>
|
|
19
|
+
|
|
20
|
+
## Limit calls to an upstream API
|
|
21
|
+
|
|
22
|
+
Call [`experimental_rateLimit()`](/docs/api-reference/workflow/rate-limit) in a workflow before the step that calls the upstream. The call resolves when the key has a free slot for it:
|
|
23
|
+
|
|
24
|
+
```typescript lineNumbers
|
|
25
|
+
import { experimental_rateLimit } from "workflow";
|
|
26
|
+
|
|
27
|
+
async function summarizeDoc(doc: string) {
|
|
28
|
+
"use step";
|
|
29
|
+
const response = await fetch("https://api.example.com/summarize", {
|
|
30
|
+
method: "POST",
|
|
31
|
+
body: JSON.stringify({ doc }),
|
|
32
|
+
});
|
|
33
|
+
return response.json();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function summarizeAll(docs: string[]) {
|
|
37
|
+
"use workflow";
|
|
38
|
+
const summaries = [];
|
|
39
|
+
for (const doc of docs) {
|
|
40
|
+
await experimental_rateLimit({ key: "summarize-api", limit: 60, period: "1m" }); // [!code highlight]
|
|
41
|
+
summaries.push(await summarizeDoc(doc));
|
|
42
|
+
}
|
|
43
|
+
return summaries;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
With `limit: 60` and `period: "1m"`, calls on the `summarize-api` key go ahead at most one per second, across every run that uses the key.
|
|
48
|
+
|
|
49
|
+
## How a rate limit decides when a call goes ahead
|
|
50
|
+
|
|
51
|
+
A rate limit uses the [generic cell rate algorithm (GCRA)](https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm), which spaces calls `period / limit` apart. Each key keeps one number, its next free slot, and every call on the key takes that slot and moves it on by one interval:
|
|
52
|
+
|
|
53
|
+
- **A call whose slot is now** resolves at once, and the workflow continues.
|
|
54
|
+
- **A call whose slot is later** suspends the run until its slot, the same way [`sleep()`](/docs/api-reference/workflow/sleep) does, so a long wait uses no compute.
|
|
55
|
+
- **Calls from different runs** take slots in the order the World receives them, so no run can skip ahead of another.
|
|
56
|
+
|
|
57
|
+
Play the scenarios below, or change the options, to see runs ask a key for a slot, wait for it, and go ahead:
|
|
58
|
+
|
|
59
|
+
<RateLimitExplainer />
|
|
60
|
+
|
|
61
|
+
Calls in parallel each take their own slot. With `limit: 10, period: "1s"`, the first call in this `Promise.all()` resolves at once and the rest follow 100 ms apart:
|
|
62
|
+
|
|
63
|
+
```typescript lineNumbers
|
|
64
|
+
import { experimental_rateLimit } from "workflow";
|
|
65
|
+
declare function sendEmail(to: string): Promise<void>; // @setup
|
|
66
|
+
|
|
67
|
+
export async function notifyAll(recipients: string[]) {
|
|
68
|
+
"use workflow";
|
|
69
|
+
await Promise.all(
|
|
70
|
+
recipients.map(async (to) => {
|
|
71
|
+
await experimental_rateLimit({ key: "email", limit: 10, period: "1s" }); // [!code highlight]
|
|
72
|
+
await sendEmail(to);
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## How to choose a rate limit key
|
|
79
|
+
|
|
80
|
+
A key names one limit that an upstream enforces. Calls with the same key share it across every run and every workflow in the project. On Vercel each environment has its own: preview and production deployments never share a key's capacity.
|
|
81
|
+
|
|
82
|
+
- **One key per upstream limit**: Use `"openai"` for a limit that covers your whole account.
|
|
83
|
+
- **One key per tenant**: Build the key from an ID, such as `` `stripe:${accountId}` ``, when the upstream limits each account separately. Each tenant then gets its own rate.
|
|
84
|
+
- **Keep keys free of personal data**: Keys are stored as-is in the run's event log.
|
|
85
|
+
|
|
86
|
+
A key is 1 character to 512 bytes (UTF-8). Keys that start with `$` are reserved for Workflow SDK libraries, and `experimental_rateLimit()` throws for one.
|
|
87
|
+
|
|
88
|
+
## Give up when the wait is too long
|
|
89
|
+
|
|
90
|
+
Set `maxWait` when a call that waits too long isn't worth making. A call whose slot is further out than `maxWait` rejects at once with [`RateLimitMaxWaitError`](/docs/errors/rate-limit-max-wait) and uses up none of the key's capacity:
|
|
91
|
+
|
|
92
|
+
```typescript lineNumbers
|
|
93
|
+
import { experimental_rateLimit } from "workflow";
|
|
94
|
+
import { RateLimitMaxWaitError } from "workflow/errors";
|
|
95
|
+
declare function sendSms(userId: string): Promise<void>; // @setup
|
|
96
|
+
|
|
97
|
+
export async function notify(userId: string) {
|
|
98
|
+
"use workflow";
|
|
99
|
+
try {
|
|
100
|
+
await experimental_rateLimit({ key: "sms", limit: 10, period: "1s", maxWait: "30s" }); // [!code highlight]
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (RateLimitMaxWaitError.is(error)) return "skipped";
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
await sendSms(userId);
|
|
106
|
+
return "sent";
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Without `maxWait`, a call waits up to 30 days, which is also the longest `maxWait` can be.
|
|
111
|
+
|
|
112
|
+
## Let a burst through at once
|
|
113
|
+
|
|
114
|
+
Set `burst` when the upstream allows short bursts above its steady rate. An idle key then admits up to `burst` calls at once and refills one every `period / limit`, like a token bucket of that size:
|
|
115
|
+
|
|
116
|
+
```typescript lineNumbers
|
|
117
|
+
import { experimental_rateLimit } from "workflow";
|
|
118
|
+
declare function fetchPage(url: string): Promise<void>; // @setup
|
|
119
|
+
|
|
120
|
+
export async function crawl(urls: string[]) {
|
|
121
|
+
"use workflow";
|
|
122
|
+
for (const url of urls) {
|
|
123
|
+
// Up to 100 at once on an idle key, then one every 600 ms.
|
|
124
|
+
await experimental_rateLimit({ key: "crawler", limit: 100, period: "1m", burst: 100 }); // [!code highlight]
|
|
125
|
+
await fetchPage(url);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The default `burst` of 1 is no burst. A larger `burst` keeps the long-run rate at `limit` per `period`, but one rolling `period` can see up to `limit + burst - 1` calls. Set `burst` to the burst size the upstream publishes, or keep `limit + burst - 1` under the most it allows in any window. A `burst` can be larger than `limit` as long as it saves up at most 24 hours of the rate: `limit: 1, period: "1s", burst: 60` lets a key that was idle for a minute send 60 calls at once.
|
|
131
|
+
|
|
132
|
+
## Use one setting per key
|
|
133
|
+
|
|
134
|
+
Use the same `limit`, `period`, and `burst` everywhere you use a key. A key records only its next free slot, not a limit, so each call applies its own settings:
|
|
135
|
+
|
|
136
|
+
- **A limit changed in a new deployment** applies only to runs on the new deployment, and after the slots that calls at the old limit already reserved. Runs still on the old deployment keep calling with the old settings, which they were deployed with, so the key sees both until those runs finish.
|
|
137
|
+
- **A call with a longer interval** holds up every call after it. A `limit: 1, period: "24h"` call on a shared key makes every later call on that key wait a day.
|
|
138
|
+
- **A call with a larger `burst`** can go ahead of calls already waiting with a smaller one.
|
|
139
|
+
|
|
140
|
+
To change a limit, give the new limit a new key. See [One key with different settings](/docs/api-reference/workflow/rate-limit#one-key-with-different-settings) for the details.
|
|
141
|
+
|
|
142
|
+
## Handle the upstream's rate limit errors too
|
|
143
|
+
|
|
144
|
+
A rate limit controls when your workflow starts a call, but the upstream still decides whether to accept it. Keep handling its rate limit responses in the step, for two reasons:
|
|
145
|
+
|
|
146
|
+
- **Step retries skip the rate limit**: A step that fails is retried without running the workflow code before it again, so its retries don't wait for a new slot.
|
|
147
|
+
- **A run that wakes late can go ahead late**: A call goes ahead if its run wakes within a grace of its slot, which is 10% of the `period`, at least 5 seconds and at most 60 seconds. After a stall, calls queued in that window can go ahead together. See [Calls that wake late](/docs/api-reference/workflow/rate-limit#calls-that-wake-late).
|
|
148
|
+
|
|
149
|
+
Throw a [`RetryableError`](/docs/api-reference/workflow/retryable-error) with the wait the upstream asks for, so the retry doesn't run before the upstream accepts it:
|
|
150
|
+
|
|
151
|
+
```typescript lineNumbers
|
|
152
|
+
import { RetryableError } from "workflow";
|
|
153
|
+
|
|
154
|
+
async function callModel(prompt: string) {
|
|
155
|
+
"use step";
|
|
156
|
+
const response = await fetch("https://api.example.com/v1/complete", {
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: JSON.stringify({ prompt }),
|
|
159
|
+
});
|
|
160
|
+
if (response.status === 429) {
|
|
161
|
+
const seconds = Number(response.headers.get("retry-after") ?? "1");
|
|
162
|
+
throw new RetryableError("Rate limited by the upstream", { // [!code highlight]
|
|
163
|
+
retryAfter: seconds * 1000, // [!code highlight]
|
|
164
|
+
}); // [!code highlight]
|
|
165
|
+
}
|
|
166
|
+
return response.json();
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## When to use a rate limit
|
|
171
|
+
|
|
172
|
+
- **Use a rate limit** when many runs call the same upstream and it limits how often you can call it, such as a model provider's requests per minute.
|
|
173
|
+
- **Use [`sleep()`](/docs/api-reference/workflow/sleep)** when one run needs to wait a fixed time, independent of other runs.
|
|
174
|
+
- **Use [`RetryableError`](/docs/foundations/errors-and-retries#customize-retry-behavior) alone** when calls are rare and an occasional 429 retry is cheaper than spacing every call.
|
|
175
|
+
|
|
176
|
+
## Related documentation
|
|
177
|
+
|
|
178
|
+
- [`experimental_rateLimit()` API reference](/docs/api-reference/workflow/rate-limit)
|
|
179
|
+
- [`RateLimitMaxWaitError`](/docs/errors/rate-limit-max-wait)
|
|
180
|
+
- [Errors & Retrying](/docs/foundations/errors-and-retries)
|
|
181
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep)
|
package/docs/performance.mdx
CHANGED
|
@@ -10,7 +10,7 @@ related:
|
|
|
10
10
|
- /docs/foundations/streaming
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
-
We benchmark the Workflow SDK runtime on Vercel at each point where a workflow waits on it: starting a run, moving from one step to the next, fanning out, resuming from a hook, and delivering a stream. This page explains what each benchmark measures and how, and then shows the [latest results](#latest-results), which compare
|
|
13
|
+
We benchmark the Workflow SDK runtime on Vercel at each point where a workflow waits on it: starting a run, moving from one step to the next, fanning out, resuming from a hook, and delivering a stream, plus a five-turn agent loop that combines them. This page explains what each benchmark measures and how, and then shows the [latest results](#latest-results), which compare the latest release with earlier ones.
|
|
14
14
|
|
|
15
15
|
## What we measure
|
|
16
16
|
|
|
@@ -38,11 +38,17 @@ A workflow creates a [hook](/docs/foundations/hooks) and waits on it for 1 s, th
|
|
|
38
38
|
|
|
39
39
|
One step replays a recorded AI agent's token stream, 2,593 chunks over 52.4 s, in real time to a [namespaced stream](/docs/foundations/streaming#namespaced-streams) with [`getWritable()`](/docs/api-reference/workflow/get-writable), and a second step in the same run reads it with `getRun(runId).getReadable()`. Chunk trip time runs from the writer writing a chunk until the reader receives it. The same benchmark records how long the whole response takes to reach the reader and how many runs had a chunk delayed by over 1 s. See [Streams](#streams) for the latest results.
|
|
40
40
|
|
|
41
|
+
### Agent loop overhead
|
|
42
|
+
|
|
43
|
+
A five-turn agent built from plain steps, shaped like a durable AI agent. Each turn is one model step that streams 10 tokens over 100 ms to [`getWritable()`](/docs/api-reference/workflow/get-writable). On turns 1 to 4 the model then calls two tools, which run in parallel as 100 ms steps, and turn 5 answers. Each message is 1 KB of JSON, and the conversation passed to every step grows each turn. The overhead runs from `start()` until the first line of a step after the last turn, minus the 900 ms of model and tool work on the critical path. The loop runs with and without streaming its tokens. See [Agent loop](#agent-loop) for the latest results.
|
|
44
|
+
|
|
45
|
+
Every step uses the default retry settings. A model step marked [`maxRetries = 0`](/docs/foundations/errors-and-retries), as some agent libraries do, waits until its start is recorded before it runs, which added about 70 ms per turn on v5.8.0 in our measurements.
|
|
46
|
+
|
|
41
47
|
## Methodology
|
|
42
48
|
|
|
43
|
-
- **Platform**: Each version runs the same benchmark app on Vercel production in `iad1`, against the managed [Vercel World](/worlds/vercel), on its own deployment. The versions are measured at the same time. Before
|
|
49
|
+
- **Platform**: Each version runs the same benchmark app on Vercel production in `iad1`, against the managed [Vercel World](/worlds/vercel), on its own deployment. The versions are measured at the same time. Before each sweep of a benchmark, every deployment runs one warm-up that is not counted: a five-step workflow before the 1,020-step run, and one run of the benchmark otherwise. The agent loop runs on a separate app, deployed the same way once per version, which runs one loop with and one without streaming before measuring.
|
|
44
50
|
- **Workload**: Every step takes 1 KB of JSON in and out and does 100 ms of simulated work, which the overhead measurements exclude.
|
|
45
|
-
- **Runs**: Each version runs every benchmark several times, and each result pools the samples from all of its runs. The run counts are listed with
|
|
51
|
+
- **Runs**: Each version runs every benchmark several times, and each result pools the samples from all of its runs. The run counts are listed with [all results](#all-results).
|
|
46
52
|
- **Percentiles**: Each percentile pools every sample across a version's runs and uses the nearest-rank method. Half of the samples are at or below the p50 (the median), three quarters at or below the p75, and all but the slowest 1% at or below the p99. With 100 or fewer samples, a p99 is the slowest sample.
|
|
47
53
|
|
|
48
54
|
### Limits
|
|
@@ -56,132 +62,160 @@ One step replays a recorded AI agent's token stream, 2,593 chunks over 52.4 s, i
|
|
|
56
62
|
<Callout type="info">
|
|
57
63
|
{/* generated:performance latest */}
|
|
58
64
|
|
|
59
|
-
**Latest results:** Workflow SDK v5.
|
|
65
|
+
**Latest results:** Workflow SDK v5.8.0 compared with v5.2.0 and v4.8.18, measured November 27, 2026. We run these benchmarks regularly and publish updated results here.
|
|
60
66
|
|
|
61
67
|
{/* end generated:performance latest */}
|
|
62
68
|
</Callout>
|
|
63
69
|
|
|
64
70
|
{/* generated:performance headline */}
|
|
65
71
|
|
|
66
|
-
At p75, v5.
|
|
72
|
+
At p75 (the median for stream delivery), v5.8.0 is as fast as or faster than v5.2.0 on every benchmark, and faster than v4.8.18 on every benchmark. The largest change from v5.2.0 is in step-to-step overhead: 818 ms on v4.8.18, 260 ms on v5.2.0 and 7 ms on v5.8.0.
|
|
67
73
|
|
|
68
74
|
{/* end generated:performance headline */}
|
|
69
75
|
|
|
70
|
-
{/* generated:performance caveats */}
|
|
71
|
-
|
|
72
|
-
Both versions ran at the same time and shared the platform. The exception is the 1,020-step benchmark on v4: one of its five runs ran alongside v5.2.0's, and the other four ran on their own afterwards, the same day with the same configuration. Each version ran the sequential workflow 10 times, the agent stream 10 times, the fan-out 25 times, the resume benchmark 25 times, and the 1,020-step run 5 times.
|
|
73
|
-
|
|
74
|
-
{/* end generated:performance caveats */}
|
|
75
|
-
|
|
76
76
|
<Image
|
|
77
|
-
srcLight="/performance/v5.
|
|
78
|
-
srcDark="/performance/v5.
|
|
79
|
-
alt="Dot plot of how many times faster the
|
|
77
|
+
srcLight="/performance/v5.8.0/01-overview-light.png"
|
|
78
|
+
srcDark="/performance/v5.8.0/01-overview-dark.png"
|
|
79
|
+
alt="Dot plot of how many times faster the latest version is than each earlier one on each benchmark, on a log scale. The full values are in the results table below."
|
|
80
80
|
width={2016}
|
|
81
|
-
height={
|
|
81
|
+
height={2204}
|
|
82
82
|
/>
|
|
83
83
|
|
|
84
84
|
### Steps and long runs
|
|
85
85
|
|
|
86
86
|
{/* generated:performance sequential */}
|
|
87
87
|
|
|
88
|
-
At p75, a five-step workflow reaches its first step in
|
|
88
|
+
At p75, a five-step workflow reaches its first step in 662 ms on v4.8.18, 189 ms on v5.2.0 and 197 ms on v5.8.0. Between steps, it adds 818 ms on v4.8.18, 260 ms on v5.2.0 and 7 ms on v5.8.0.
|
|
89
89
|
|
|
90
90
|
{/* end generated:performance sequential */}
|
|
91
91
|
|
|
92
92
|
{/* generated:performance long-run */}
|
|
93
93
|
|
|
94
|
-
Through a 1,020-step run, step-to-step overhead
|
|
94
|
+
Through a 1,020-step run, from steps 1 to 20 to steps 1,001 to 1,020, step-to-step overhead at p75 grows from 828 ms to 1,933 ms on v4.8.18, falls from 144 ms to 73 ms on v5.2.0, and falls from 10 ms to 9 ms on v5.8.0. The median run takes 21.8 min on v4.8.18, 3.0 min on v5.2.0 and 1.8 min on v5.8.0.
|
|
95
95
|
|
|
96
96
|
{/* end generated:performance long-run */}
|
|
97
97
|
|
|
98
98
|
<Image
|
|
99
|
-
srcLight="/performance/v5.
|
|
100
|
-
srcDark="/performance/v5.
|
|
101
|
-
alt="Three charts comparing the
|
|
99
|
+
srcLight="/performance/v5.8.0/02-sequential-light.png"
|
|
100
|
+
srcDark="/performance/v5.8.0/02-sequential-dark.png"
|
|
101
|
+
alt="Three charts comparing the three versions: time to first step and step-to-step overhead for a five-step workflow, step-to-step overhead through a 1,020-step run at p75 on a log scale, and the time to finish all 1,020 steps. The full values are in the results table below."
|
|
102
102
|
width={2016}
|
|
103
|
-
height={
|
|
103
|
+
height={2746}
|
|
104
104
|
/>
|
|
105
105
|
|
|
106
106
|
### Fan-out
|
|
107
107
|
|
|
108
108
|
{/* generated:performance fanout */}
|
|
109
109
|
|
|
110
|
-
At p75,
|
|
110
|
+
At p75, the first of 64 branches starts in 706 ms on v4.8.18, 1,914 ms on v5.2.0 and 226 ms on v5.8.0. The slowest branch starts in 1,781 ms on v4.8.18, 3,282 ms on v5.2.0 and 1,400 ms on v5.8.0, and the join takes 702 ms on v4.8.18, 717 ms on v5.2.0 and 264 ms on v5.8.0. The first branch started after more than 1 s in 1 of 25 runs on v4.8.18, 8 on v5.2.0 and 5 on v5.8.0. On v5.2.0 those runs are more than a quarter of the 25, so they set its p75.
|
|
111
111
|
|
|
112
112
|
{/* end generated:performance fanout */}
|
|
113
113
|
|
|
114
114
|
<Image
|
|
115
|
-
srcLight="/performance/v5.
|
|
116
|
-
srcDark="/performance/v5.
|
|
117
|
-
alt="
|
|
115
|
+
srcLight="/performance/v5.8.0/03-fanout-light.png"
|
|
116
|
+
srcDark="/performance/v5.8.0/03-fanout-dark.png"
|
|
117
|
+
alt="Chart of a 64-branch fan-out at p75, comparing the three versions for the first branch to start, the slowest branch to start, and the join. The full values are in the results table below."
|
|
118
118
|
width={2016}
|
|
119
|
-
height={
|
|
119
|
+
height={1056}
|
|
120
120
|
/>
|
|
121
121
|
|
|
122
122
|
### Resuming a workflow
|
|
123
123
|
|
|
124
124
|
{/* generated:performance resume */}
|
|
125
125
|
|
|
126
|
-
At p75, a workflow waiting on a hook resumes in
|
|
126
|
+
At p75, a workflow waiting on a hook resumes in 953 ms on v4.8.18, 562 ms on v5.2.0 and 220 ms on v5.8.0.
|
|
127
127
|
|
|
128
128
|
{/* end generated:performance resume */}
|
|
129
129
|
|
|
130
130
|
<Image
|
|
131
|
-
srcLight="/performance/v5.
|
|
132
|
-
srcDark="/performance/v5.
|
|
133
|
-
alt="
|
|
131
|
+
srcLight="/performance/v5.8.0/04-resume-light.png"
|
|
132
|
+
srcDark="/performance/v5.8.0/04-resume-dark.png"
|
|
133
|
+
alt="Chart of time to resume after 1 s idle at p50 and p75, comparing the three versions. The full values are in the results table below."
|
|
134
134
|
width={2016}
|
|
135
|
-
height={
|
|
135
|
+
height={832}
|
|
136
136
|
/>
|
|
137
137
|
|
|
138
138
|
### Streams
|
|
139
139
|
|
|
140
140
|
{/* generated:performance streams */}
|
|
141
141
|
|
|
142
|
-
The agent produced its 2,593-chunk response over 52.4 s. On v4.8.
|
|
142
|
+
The agent produced its 2,593-chunk response over 52.4 s. On v4.8.18 the writer step manages about 12 chunks per second, so the response takes 215.1 s to reach the reader. v5.2.0 and v5.8.0 write streams over a WebSocket by default, keep up at about 49 chunks per second, and deliver the response in 54.1 s on v5.2.0 and 53.5 s on v5.8.0.
|
|
143
143
|
|
|
144
|
-
At p75, a chunk reaches the reader in
|
|
144
|
+
At p75, a chunk reaches the reader in 160 ms on v4.8.18, 94 ms on v5.2.0 and 93 ms on v5.8.0. A chunk was delayed by over 1 s in 6 of 10 runs on v4.8.18, 0 of 10 runs on v5.2.0 and 0 of 10 runs on v5.8.0.
|
|
145
145
|
|
|
146
146
|
{/* end generated:performance streams */}
|
|
147
147
|
|
|
148
148
|
On the Vercel World, [`WORKFLOW_STREAMS_TRANSPORT`](/docs/configuration/worlds#workflow_streams_transport) sets the transport for stream writes.
|
|
149
149
|
|
|
150
150
|
<Image
|
|
151
|
-
srcLight="/performance/v5.
|
|
152
|
-
srcDark="/performance/v5.
|
|
153
|
-
alt="Three charts of a replayed agent stream comparing the
|
|
151
|
+
srcLight="/performance/v5.8.0/05-streams-light.png"
|
|
152
|
+
srcDark="/performance/v5.8.0/05-streams-dark.png"
|
|
153
|
+
alt="Three charts of a replayed agent stream comparing the three versions: the time to deliver the whole response against the recording's length, chunk trip time at p50 and p75, and the number of runs with a chunk delayed by over 1 s. The full values are in the results table below."
|
|
154
|
+
width={2016}
|
|
155
|
+
height={2154}
|
|
156
|
+
/>
|
|
157
|
+
|
|
158
|
+
### Agent loop
|
|
159
|
+
|
|
160
|
+
{/* generated:performance agent-loop */}
|
|
161
|
+
|
|
162
|
+
The loop runs on a benchmark app of its own, deployed once per version, and each version ran it 15 times with streaming and 15 times without.
|
|
163
|
+
|
|
164
|
+
At p75, the five-turn loop that streams its tokens adds 9,814 ms on v4.8.18, 3,394 ms on v5.2.0 and 892 ms on v5.8.0. Without streaming, it adds 7,078 ms on v4.8.18, 2,252 ms on v5.2.0 and 366 ms on v5.8.0, of which 198 ms on v5.8.0 is the time to reach the first step (at p75).
|
|
165
|
+
|
|
166
|
+
On v5.8.0, the streaming loop waits after each model step until its last tokens are stored: the tools start 130 ms after the model step at p75, against 7 ms without streaming.
|
|
167
|
+
|
|
168
|
+
{/* end generated:performance agent-loop */}
|
|
169
|
+
|
|
170
|
+
<Image
|
|
171
|
+
srcLight="/performance/v5.8.0/06-agent-loop-light.png"
|
|
172
|
+
srcDark="/performance/v5.8.0/06-agent-loop-dark.png"
|
|
173
|
+
alt="Chart of the five-turn agent loop's overhead at p75, with and without streaming, comparing the three versions. The full values are in the results table below."
|
|
154
174
|
width={2016}
|
|
155
|
-
height={
|
|
175
|
+
height={872}
|
|
156
176
|
/>
|
|
157
177
|
|
|
178
|
+
|
|
158
179
|
### All results
|
|
159
180
|
|
|
160
181
|
Every measured value from the latest results. Percentiles follow the [methodology](#methodology) above.
|
|
161
182
|
|
|
183
|
+
{/* generated:performance caveats */}
|
|
184
|
+
|
|
185
|
+
All three versions ran at the same time and shared the platform. The fan-out and resume benchmarks ran as three back-to-back sweeps of 10, 10 and 5 runs, and the 1,020-step run as five sweeps of one run, each with all three versions at once. v4.8.18 takes longer to finish the agent stream and the 1,020-step run, so most of its runs of those two finished after the v5 versions had. Each version ran the sequential workflow 10 times, the agent stream 10 times, the fan-out 25 times, the resume benchmark 25 times, the 1,020-step run 5 times, and the agent loop 15 times with streaming and 15 times without.
|
|
186
|
+
|
|
187
|
+
{/* end generated:performance caveats */}
|
|
188
|
+
|
|
162
189
|
{/* generated:performance table */}
|
|
163
190
|
|
|
164
|
-
| Metric | v4.8.
|
|
165
|
-
| --- | --: | --: | --: | --: |
|
|
166
|
-
| **Sequential workflow, 5 steps, 10 runs** | | | | |
|
|
167
|
-
| Time to first step, p75 |
|
|
168
|
-
|
|
|
169
|
-
| Step-to-step overhead,
|
|
170
|
-
|
|
|
171
|
-
|
|
|
172
|
-
| Step overhead, steps
|
|
173
|
-
| Step overhead, steps
|
|
174
|
-
|
|
|
175
|
-
|
|
|
176
|
-
|
|
|
177
|
-
|
|
|
178
|
-
|
|
|
179
|
-
|
|
|
180
|
-
|
|
|
181
|
-
| Time to resume,
|
|
182
|
-
|
|
|
183
|
-
|
|
|
184
|
-
|
|
|
185
|
-
|
|
|
191
|
+
| Metric | v4.8.18 | v5.2.0 | v5.8.0 | v5.8.0 vs v5.2.0 | v5.8.0 vs v4.8.18 | Samples per version |
|
|
192
|
+
| --- | --: | --: | --: | --: | --: | --: |
|
|
193
|
+
| **Sequential workflow, 5 steps, 10 runs** | | | | | | |
|
|
194
|
+
| Time to first step, p75 | 662 ms | 189 ms | 197 ms | No change | 3.4× faster | 10 runs |
|
|
195
|
+
| Step-to-step overhead, p75 | 818 ms | 260 ms | 7 ms | 37.1× faster | 116.9× faster | 40 gaps |
|
|
196
|
+
| Step-to-step overhead, p99 | 987 ms | 684 ms | 381 ms | 1.8× faster | 2.6× faster | 40 gaps |
|
|
197
|
+
| **Long run, 1,020 steps, 5 runs** | | | | | | |
|
|
198
|
+
| Step overhead, steps 1 to 20, p75 | 828 ms | 144 ms | 10 ms | 14.4× faster | 82.8× faster | 95 gaps |
|
|
199
|
+
| Step overhead, steps 101 to 120, p75 | 884 ms | 98 ms | 7 ms | 14.0× faster | 126.3× faster | 95 gaps |
|
|
200
|
+
| Step overhead, steps 1,001 to 1,020, p75 | 1,933 ms | 73 ms | 9 ms | 8.1× faster | 214.8× faster | 95 gaps |
|
|
201
|
+
| Whole run, median wall time | 21.8 min | 3.0 min | 1.8 min | 1.6× faster | 11.8× faster | 5 runs |
|
|
202
|
+
| **Fan-out, 64 parallel branches, 25 runs** | | | | | | |
|
|
203
|
+
| First branch starts, p75 | 706 ms | 1,914 ms | 226 ms | 8.5× faster | 3.1× faster | 25 runs |
|
|
204
|
+
| Slowest branch to start, p75 | 1,781 ms | 3,282 ms | 1,400 ms | 2.3× faster | 1.3× faster | 25 runs |
|
|
205
|
+
| Join, p75 | 702 ms | 717 ms | 264 ms | 2.7× faster | 2.7× faster | 25 runs |
|
|
206
|
+
| **Resume after 1 s idle, 25 runs of 5 resumes** | | | | | | |
|
|
207
|
+
| Time to resume, p50 | 875 ms | 493 ms | 177 ms | 2.8× faster | 4.9× faster | 125 resumes |
|
|
208
|
+
| Time to resume, p75 | 953 ms | 562 ms | 220 ms | 2.6× faster | 4.3× faster | 125 resumes |
|
|
209
|
+
| **Agent stream, 52.4 s recording, 10 runs** | | | | | | |
|
|
210
|
+
| Delivery, median wall time | 215.1 s | 54.1 s | 53.5 s | No change | 4.0× faster | 10 runs |
|
|
211
|
+
| Chunk trip time, p75 | 160 ms | 94 ms | 93 ms | No change | 1.7× faster | 25,930 chunks |
|
|
212
|
+
| **Agent loop, 5 turns, streaming tokens, 15 runs** | | | | | | |
|
|
213
|
+
| Overhead, p50 | 9,523 ms | 2,913 ms | 695 ms | 4.2× faster | 13.7× faster | 15 runs |
|
|
214
|
+
| Overhead, p75 | 9,814 ms | 3,394 ms | 892 ms | 3.8× faster | 11.0× faster | 15 runs |
|
|
215
|
+
| Model step to tools, p75 | 719 ms | 507 ms | 130 ms | 3.9× faster | 5.5× faster | 60 turns |
|
|
216
|
+
| **Agent loop, 5 turns, without streaming, 15 runs** | | | | | | |
|
|
217
|
+
| Overhead, p50 | 6,812 ms | 2,015 ms | 288 ms | 7.0× faster | 23.7× faster | 15 runs |
|
|
218
|
+
| Overhead, p75 | 7,078 ms | 2,252 ms | 366 ms | 6.2× faster | 19.3× faster | 15 runs |
|
|
219
|
+
| Model step to tools, p75 | 764 ms | 339 ms | 7 ms | 48.4× faster | 109.1× faster | 60 turns |
|
|
186
220
|
|
|
187
221
|
{/* end generated:performance table */}
|