> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiI3Y2ViZTRkMy1lYjBlLTRlM2QtODExNi00MDA2MjNmNmQ1YzUiLCJleHAiOjE3NzgzODAyOTgsImlhdCI6MTc3ODM3OTk5OH0.1T7FmaxnNpQTYHGaE_2djoV9yj4JcU8Q6VeRruPytII
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 指数退避重试

> 了解如何在 Fern SDK 中配置指数退避的自动重试。控制重试限制并自定义可重试状态码。

<Warning title="专业版和企业版功能">
  此功能仅适用于[专业版和企业版计划](https://buildwithfern.com/pricing)。如需开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。
</Warning>

Fern SDK 将自动使用指数退避重试失败的请求。只要请求被认为是可重试的，且重试次数未超过配置的重试限制，请求就会被重试。

### 可重试状态码

当返回以下任何 HTTP 状态码时，请求被认为是可重试的：

* [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (超时)
* [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (请求过多)
* [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (内部服务器错误)

注意，您也可以配置可重试状态码列表。例如，如果您想从可重试状态码列表中移除 `429` 状态码，您可以这样做。

### 覆盖重试限制

默认情况下，SDK 将最多重试失败的请求 2 次。SDK 用户可以在实例化客户端时覆盖全局默认重试限制。

<CodeBlocks>
  <CodeBlock title="TypeScript">
    ```ts {4}
    import { ImdbClient } from "imdb";

    const client = new ImdbClient({
      maxRetries: 1 // overrides the default retry limit to 1
    });
    ```
  </CodeBlock>

  <CodeBlock title="Python">
    ```python {4, 8}
    from imdb.client import Imdb, AsyncImdb

    client = Imdb({
      max_retries: 1 # overrides the default retry limit to 1
    })

    async_client = AsyncImdb({
      max_retries: 1 # overrides the default retry limit to 1
    })
    ```
  </CodeBlock>

  <CodeBlock title="Java">
    ```java {4}
    import com.imdb.ImdbClient;

    ImdbClient client = new ImdbClient.Builder()
      .maxRetries(1) // overrides the default retry limit to 1
      .build();
    ```
  </CodeBlock>

  <CodeBlock title="Go">
    ```go {7}
      import (
        imdbclient "github.com/fern-workos/workos-go/client"
        "github.com/fern-workos/workos-go/option"
      )

      client := imdbclient.NewClient(
        option.WithMaxAttempts(1), // overrides the default retry limit to 1
      )
    ```
  </CodeBlock>

  <CodeBlock title="C#">
    ```csharp {4}
    using Imdb;

    var client = new ImdbClient(new ClientOptions {
      MaxRetries = 1 // overrides the default retry limit to 1
    });
    ```
  </CodeBlock>

  <CodeBlock title="Swift">
    ```swift {5}
    import Imdb

    let client = ImdbClient(
      baseURL: "https://api.imdb.com",
      maxRetries: 1 // overrides the default retry limit to 1
    )
    ```
  </CodeBlock>

  <CodeBlock title="PHP">
    ```php {4}
    use Imdb\ImdbClient;

    $client = new ImdbClient([
      "maxRetries" => 1 // overrides the default retry limit to 1
    ]);
    ```
  </CodeBlock>
</CodeBlocks>

也可以在每个请求的基础上覆盖重试限制。

<CodeBlocks>
  <CodeBlock title="TypeScript">
    ```ts {2}
      client.movie.get("tt0111161", {
        maxRetries: 3 // overrides the default retry limit to 3
      });
    ```
  </CodeBlock>

  <CodeBlock title="Python">
    ```python {2}
      client.movie.get("tt0111161", {
        max_retries: 3 // overrides the default retry limit to 3
      })
    ```
  </CodeBlock>

  <CodeBlock title="Java">
    ```java {2}
      client.movie().get("tt0111161", RequestOptions.builder()
        .maxRetries(3) // overrides the default retry limit to 3
        .build());
    ```
  </CodeBlock>

  <CodeBlock title="Go">
    ```go {4}
      response, err := client.Movies.Get(
        ctx,
        "tt0111161",
        option.WithMaxAttempts(1),
      )
    ```
  </CodeBlock>

  <CodeBlock title="C#">
    ```csharp {2}
      var movie = await client.Movie.GetAsync("tt0111161", new RequestOptions {
        MaxRetries = 3 // overrides the default retry limit to 3
      });
    ```
  </CodeBlock>

  <CodeBlock title="Swift">
    ```swift {2}
      let movie = try await client.movie.get("tt0111161", requestOptions: RequestOptions(
        maxRetries: 3 // overrides the default retry limit to 3
      ))
    ```
  </CodeBlock>

  <CodeBlock title="PHP">
    ```php {2}
      $movie = $client->movie->get("tt0111161", [
        "maxRetries" => 3 // overrides the default retry limit to 3
      ]);
    ```
  </CodeBlock>
</CodeBlocks>