# Too many subrequests: Cloudflare Workflow step retries share one fetch budget

"Too many subrequests by single Worker invocation" in a Cloudflare Workflow can come from a step that is well under the limit on any single try. Workflows retries a failed step inside the same invocation, so every try spends fetches from the same budget. One step of my daily index report made 40 fetches, timed out twice, and failed its third try on the first fetch. The budget for a step is the fetches per try multiplied by `1 + retries`, and it has to fit the plan's cap.

## What does "Too many subrequests by single Worker invocation" mean?

Every Worker invocation may make a limited number of outgoing `fetch()` calls, which Cloudflare calls subrequests. On the plan I use the limit is 50. Call number 51 throws instead of making the request.

Workflows build on Workers. Each step runs inside a Worker invocation, and that matters as soon as the step makes many calls to an external API.

## How the error showed up

My site runs a daily Search Console report as a Cloudflare Workflow. A parent instance reads the sitemap and fans out to child instances. Each child's `inspect` step called the URL Inspection API for a batch of 40 URLs in parallel, plus one call for a Google token: 41 fetches, under 50.

On 20 November the report stored 486 of its 793 rows as `Too many subrequests by single Worker invocation`.

`wrangler workflows instances describe` on one child showed what happened:

1. Try 1: `WorkflowTimeoutError` after the step's 2-minute timeout.
2. Try 2: `WorkflowTimeoutError` again.
3. Try 3: "succeeded" in 0 seconds, and every row it returned was the subrequest error.

Tries 1 and 2 had already spent 82 fetches between them. Try 3 was over the cap before it started.

## Why retries share the budget

I had budgeted each try as if it ran in a fresh invocation. It doesn't. The retries run inside the same invocation, so their fetches add up:

| | Per try | Tries | Total |
| --- | --- | --- | --- |
| Before | 1 token + 40 inspections = 41 | 3 | 123, cap 50 |
| After | 1 token + 14 inspections = 15 | 3 | 45, cap 50 |

The fix was to size the batch for the worst case. The step keeps two retries, and each child now inspects 14 URLs:

```ts
/** Extra tries of a child's inspect step. The runtime retries inside the same invocation, so the fetch cap covers every try. */
export const INSPECT_RETRIES = 2
/** URLs per child instance of the IndexReport: one fetch each, plus the child's token, on each of 1 + INSPECT_RETRIES tries. */
export const INSPECT_PER_STEP = 14
```

More children means more Workflow steps per day, and the free plan caps those too. My budget for that is 2,700 steps a day, 90% of the 3,000 limit, and 790 URLs in batches of 14 fits easily.

## Smaller batches also stopped the timeouts

The timeouts were the first failure, not only the trigger for the retries. A Worker keeps at most 6 outgoing connections open at a time, and every other fetch waits in a queue. Forty fetches meant about 7 rounds of slow API calls inside a 2-minute timeout. Fourteen fetches is 3 rounds. The step now finishes on its first try, so the retries rarely run at all.

## Pin the budget with a test

The earlier version had a test, and the test passed. It budgeted one try per invocation and forgot the retries. The guard now multiplies by the number of tries, and it failed when I set the batch back to 40:

```ts
test('each instance fits the per-invocation fetch budget', () => {
  const { budget } = LIMITS.subrequests
  assert.ok(REPORT_PREAMBLE_FETCHES <= budget, `parent ${REPORT_PREAMBLE_FETCHES} > ${budget}`)
  const tries = 1 + INSPECT_RETRIES
  assert.ok((1 + INSPECT_PER_STEP) * tries <= budget, `child (1 + ${INSPECT_PER_STEP}) × ${tries} tries > ${budget}`)
})
```

`LIMITS` is one module that lists every Cloudflare limit the site depends on, with a budget below each limit:

```js
subrequests: { limit: 50, unit: 'external fetches', period: 'invocation', budget: 45 },
```

The cap is written down once, not as a `50` spread across the code, and the guard aims at 45 so a small change can't land exactly on the edge.

## Don't store errors as results

Try 3 "succeeded": the step returned normally with a list of error rows, and the report stored them as the day's data. The check that decides whether a day is done only knew about one failure, the [Search Console quota 429](https://www.mohamedgado.com/writing/search-console-url-inspection-api-quota). It now reruns the day when most rows are any failure that says nothing about the URL itself: a 429, a 5xx, the fetch cap, or a timeout.

## Takeaways

- The subrequest limit applies per invocation, and Workflow step retries run in the same invocation. Budget fetches × (1 + retries).
- Size batches for 6 concurrent connections too. A batch that takes several rounds of slow calls times out and starts using up the retries.
- Pin the budget with a test that includes the retries, and keep the platform limits in one module.
- Classify failures by kind before you store results, so a day full of platform errors is retried, not reported.

This report was written for Cloudflare during my [move from Vercel to Cloudflare](https://www.mohamedgado.com/writing/vercel-to-cloudflare-zero-downtime). Vercel functions have no per-invocation fetch cap, so nothing I had built before had ever been tested against one.
