# Retries

Copy for LLM

The Apify client automatically retries requests that fail due to:

* Network errors
* Internal errors in the Apify API (HTTP status codes 500 and above)
* Rate limit errors (HTTP status code 429)

By default, the client retries a failed request up to 8 times. The retry intervals use an exponential backoff strategy:

* The first retry occurs after 500 to 1,000 milliseconds.
* The second retry occurs after 1,000 to 2,000 milliseconds, and so on.

The delay is spread over a random factor between one and two, so clients that failed together don't come back at the API in lockstep.

You can customize this behavior using the following options in the [`ApifyClient`](https://docs.apify.com/api/client/js/reference/class/ApifyClient.md) constructor:

* `maxRetries`: Defines the maximum number of retry attempts.
* `minDelayBetweenRetriesMillis`: Sets the minimum delay between retries in milliseconds.

Retries with exponential backoff help reduce the load on the server and increase the chances of a successful request.

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({

    token: 'MY-APIFY-TOKEN',

    maxRetries: 8,

    minDelayBetweenRetriesMillis: 500,

});
```

When the retries are exhausted, the client throws the error of the final attempt. Which transport errors count as retryable is decided by the HTTP client the requests go through. The built-in client retries every network failure, timeouts included. A custom client classifies its own errors. For details, see [HTTP clients](https://docs.apify.com/api/client/js/docs/concepts/custom-http-clients.md).

A request that times out is retried the same way, with a longer timeout on each attempt. For how the timeouts are set, see [Timeouts](https://docs.apify.com/api/client/js/docs/concepts/timeouts.md).

## Retry a request body read from a file[](#retry-a-request-body-read-from-a-file)

A request whose body is a stream, either a Node.js `Readable` or a web `ReadableStream`, gets a single attempt, since the failed attempt has already consumed part of the stream. To upload a file with retries, pass it as a `Blob`, for example one from `fs.openAsBlob()`. The client reads the blob again for every attempt and doesn't hold it in memory. If reading the blob itself fails, for example because the file was deleted, the client throws that error right away. Its `cause` is the transport error, unless the transport error already wraps it.

## Wait for resources to start a run[](#wait-for-resources-to-start-a-run)

Starting a run fails with an HTTP 402 error when the account doesn't have enough free memory for the run, or when it already runs as many Actors as its plan allows. The error `type` is `actor-memory-limit-exceeded` or `concurrent-runs-limit-exceeded`. The client doesn't retry these errors on its own, since they clear only after other runs or builds of the account finish.

To keep retrying the start until the resources free up, set the `waitForResources` option of [`ActorClient.start()`](https://docs.apify.com/api/client/js/reference/class/ActorClient.md#start), [`ActorClient.call()`](https://docs.apify.com/api/client/js/reference/class/ActorClient.md#call), or the same methods of [`TaskClient`](https://docs.apify.com/api/client/js/reference/class/TaskClient.md). The client then retries the start every 10 seconds, and the option value sets how long:

* `true` retries until the run starts.
* A number of seconds stops retrying after that time and throws the last error.

Any other error throws right away. A run that asks for more memory than the account's whole memory limit gets the same `actor-memory-limit-exceeded` error and never starts, so with `true` the client retries it forever. In `call()`, the time spent retrying doesn't count toward `waitSecs`.

```
import { ApifyClient } from 'apify-client';



const client = new ApifyClient({ token: 'MY-APIFY-TOKEN' });



// Retry the start until the account has the resources for the run.

const run = await client.actor('username/actor-name').call(undefined, { waitForResources: true });



// Stop retrying after 10 minutes and throw the last error.

const startedRun = await client.task('username~task-name').start(undefined, { waitForResources: 600 });
```
