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

_calculate_retry_timeout() — anthropic-sdk-python Function Reference

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

Function python AnthropicClient SyncAPI calls 2 called by 2

Entity Profile

Dependency Diagram

graph TD
  f6d7d851_1218_0312_84fe_55254f0ab693["_calculate_retry_timeout()"]
  842ce0be_ea56_b939_960f_13eb61230daf["BaseClient"]
  f6d7d851_1218_0312_84fe_55254f0ab693 -->|defined in| 842ce0be_ea56_b939_960f_13eb61230daf
  e28246be_5825_36c7_9258_c1395a05766e["_sleep_for_retry()"]
  e28246be_5825_36c7_9258_c1395a05766e -->|calls| f6d7d851_1218_0312_84fe_55254f0ab693
  45cb7b8c_88f3_ad66_a029_ae39dee52385["_sleep_for_retry()"]
  45cb7b8c_88f3_ad66_a029_ae39dee52385 -->|calls| f6d7d851_1218_0312_84fe_55254f0ab693
  614e9b3b_4fa0_b0b8_c33c_36259a5398eb["_parse_retry_after_header()"]
  f6d7d851_1218_0312_84fe_55254f0ab693 -->|calls| 614e9b3b_4fa0_b0b8_c33c_36259a5398eb
  fa54541f_e19a_261b_1d26_4ba1cc3ee993["get_max_retries()"]
  f6d7d851_1218_0312_84fe_55254f0ab693 -->|calls| fa54541f_e19a_261b_1d26_4ba1cc3ee993
  style f6d7d851_1218_0312_84fe_55254f0ab693 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/_base_client.py lines 775–797

    def _calculate_retry_timeout(
        self,
        remaining_retries: int,
        options: FinalRequestOptions,
        response_headers: Optional[httpx.Headers] = None,
    ) -> float:
        max_retries = options.get_max_retries(self.max_retries)

        # If the API asks us to wait a certain amount of time (and it's a reasonable amount), just do what it says.
        retry_after = self._parse_retry_after_header(response_headers)
        if retry_after is not None and 0 < retry_after <= 60:
            return retry_after

        # Also cap retry count to 1000 to avoid any potential overflows with `pow`
        nb_retries = min(max_retries - remaining_retries, 1000)

        # Apply exponential backoff, but not more than the max.
        sleep_seconds = min(INITIAL_RETRY_DELAY * pow(2.0, nb_retries), MAX_RETRY_DELAY)

        # Apply some jitter, plus-or-minus half a second.
        jitter = 1 - 0.25 * random()
        timeout = sleep_seconds * jitter
        return timeout if timeout >= 0 else 0

Subdomains

Frequently Asked Questions

What does _calculate_retry_timeout() do?
_calculate_retry_timeout() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is _calculate_retry_timeout() defined?
_calculate_retry_timeout() is defined in src/anthropic/_base_client.py at line 775.
What does _calculate_retry_timeout() call?
_calculate_retry_timeout() calls 2 function(s): _parse_retry_after_header, get_max_retries.
What calls _calculate_retry_timeout()?
_calculate_retry_timeout() is called by 2 function(s): _sleep_for_retry, _sleep_for_retry.

Analyze Your Own Codebase

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

Try Supermodel Free