Home / Function/ _should_retry() — anthropic-sdk-python Function Reference

_should_retry() — anthropic-sdk-python Function Reference

Architecture documentation for the _should_retry() function in _base_client.py from the anthropic-sdk-python codebase.

Function python AnthropicClient SyncAPI calls 1 called by 2

Entity Profile

Dependency Diagram

graph TD
  640eadd6_0c4b_4c03_9fb4_8852116293ef["_should_retry()"]
  842ce0be_ea56_b939_960f_13eb61230daf["BaseClient"]
  640eadd6_0c4b_4c03_9fb4_8852116293ef -->|defined in| 842ce0be_ea56_b939_960f_13eb61230daf
  2cb1e2bf_1a02_d5ea_42a9_c0def162363a["request()"]
  2cb1e2bf_1a02_d5ea_42a9_c0def162363a -->|calls| 640eadd6_0c4b_4c03_9fb4_8852116293ef
  c21c2971_d83f_0afc_9811_0642b34cfcd3["request()"]
  c21c2971_d83f_0afc_9811_0642b34cfcd3 -->|calls| 640eadd6_0c4b_4c03_9fb4_8852116293ef
  afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7["get()"]
  640eadd6_0c4b_4c03_9fb4_8852116293ef -->|calls| afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7
  style 640eadd6_0c4b_4c03_9fb4_8852116293ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_base_client.py lines 799–832

    def _should_retry(self, response: httpx.Response) -> bool:
        # Note: this is not a standard header
        should_retry_header = response.headers.get("x-should-retry")

        # If the server explicitly says whether or not to retry, obey.
        if should_retry_header == "true":
            log.debug("Retrying as header `x-should-retry` is set to `true`")
            return True
        if should_retry_header == "false":
            log.debug("Not retrying as header `x-should-retry` is set to `false`")
            return False

        # Retry on request timeouts.
        if response.status_code == 408:
            log.debug("Retrying due to status code %i", response.status_code)
            return True

        # Retry on lock timeouts.
        if response.status_code == 409:
            log.debug("Retrying due to status code %i", response.status_code)
            return True

        # Retry on rate limits.
        if response.status_code == 429:
            log.debug("Retrying due to status code %i", response.status_code)
            return True

        # Retry internal errors.
        if response.status_code >= 500:
            log.debug("Retrying due to status code %i", response.status_code)
            return True

        log.debug("Not retrying")
        return False

Subdomains

Calls

Called By

Frequently Asked Questions

What does _should_retry() do?
_should_retry() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is _should_retry() defined?
_should_retry() is defined in src/anthropic/_base_client.py at line 799.
What does _should_retry() call?
_should_retry() calls 1 function(s): get.
What calls _should_retry()?
_should_retry() is called by 2 function(s): request, request.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free