Home / Class/ AsyncAPIClient Class — anthropic-sdk-python Architecture

AsyncAPIClient Class — anthropic-sdk-python Architecture

Architecture documentation for the AsyncAPIClient class in _base_client.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  3224f719_8046_78c8_59e1_47301a46ddd4["AsyncAPIClient"]
  6ba97a89_3a56_ffff_f6de_ddab1cb32b30["BaseAPIResponse"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|extends| 6ba97a89_3a56_ffff_f6de_ddab1cb32b30
  8968fc97_d184_e3da_727e_c38832cb23a5["AsyncAPIResponse"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|extends| 8968fc97_d184_e3da_727e_c38832cb23a5
  31e60ad8_cac8_652d_176d_4f7cf7dda1ad["_base_client.py"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|defined in| 31e60ad8_cac8_652d_176d_4f7cf7dda1ad
  2b375217_7122_a1a1_40c0_15d4fde4fbdf["__init__()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| 2b375217_7122_a1a1_40c0_15d4fde4fbdf
  ad9e51e2_673c_032b_df14_e7677a141892["is_closed()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| ad9e51e2_673c_032b_df14_e7677a141892
  0e13a01b_fec2_4139_7de2_ed2a384e3e75["close()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| 0e13a01b_fec2_4139_7de2_ed2a384e3e75
  e84635fb_c572_9fb9_908d_76d21f7732e9["__aenter__()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| e84635fb_c572_9fb9_908d_76d21f7732e9
  e9c72be3_f691_b4fb_8696_ff3512ad5832["__aexit__()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| e9c72be3_f691_b4fb_8696_ff3512ad5832
  5bb40160_f1b8_a3f3_32ba_9dbf41afa634["_prepare_options()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| 5bb40160_f1b8_a3f3_32ba_9dbf41afa634
  c1f459dc_3f00_6b8c_fee5_b43f715e3c89["_prepare_request()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| c1f459dc_3f00_6b8c_fee5_b43f715e3c89
  c21c2971_d83f_0afc_9811_0642b34cfcd3["request()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| c21c2971_d83f_0afc_9811_0642b34cfcd3
  45cb7b8c_88f3_ad66_a029_ae39dee52385["_sleep_for_retry()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| 45cb7b8c_88f3_ad66_a029_ae39dee52385
  ae4395b9_e9e7_d7f2_e944_36fea3691100["_process_response()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| ae4395b9_e9e7_d7f2_e944_36fea3691100
  c43c8185_35e3_c567_7514_4d3de7e9bbd3["_request_api_list()"]
  3224f719_8046_78c8_59e1_47301a46ddd4 -->|method| c43c8185_35e3_c567_7514_4d3de7e9bbd3

Relationship Graph

Source Code

src/anthropic/_base_client.py lines 1548–2083

class AsyncAPIClient(BaseClient[httpx.AsyncClient, AsyncStream[Any]]):
    _client: httpx.AsyncClient
    _default_stream_cls: type[AsyncStream[Any]] | None = None

    def __init__(
        self,
        *,
        version: str,
        base_url: str | URL,
        _strict_response_validation: bool,
        max_retries: int = DEFAULT_MAX_RETRIES,
        timeout: float | Timeout | None | NotGiven = not_given,
        http_client: httpx.AsyncClient | None = None,
        custom_headers: Mapping[str, str] | None = None,
        custom_query: Mapping[str, object] | None = None,
    ) -> None:
        if not is_given(timeout):
            # if the user passed in a custom http client with a non-default
            # timeout set then we use that timeout.
            #
            # note: there is an edge case here where the user passes in a client
            # where they've explicitly set the timeout to match the default timeout
            # as this check is structural, meaning that we'll think they didn't
            # pass in a timeout and will ignore it
            if http_client and http_client.timeout != HTTPX_DEFAULT_TIMEOUT:
                timeout = http_client.timeout
            else:
                timeout = DEFAULT_TIMEOUT

        if http_client is not None and not isinstance(http_client, httpx.AsyncClient):  # pyright: ignore[reportUnnecessaryIsInstance]
            raise TypeError(
                f"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got {type(http_client)}"
            )

        super().__init__(
            version=version,
            base_url=base_url,
            # cast to a valid type because mypy doesn't understand our type narrowing
            timeout=cast(Timeout, timeout),
            max_retries=max_retries,
            custom_query=custom_query,
            custom_headers=custom_headers,
            _strict_response_validation=_strict_response_validation,
        )
        self._client = http_client or AsyncHttpxClientWrapper(
            base_url=base_url,
            # cast to a valid type because mypy doesn't understand our type narrowing
            timeout=cast(Timeout, timeout),
        )

    def is_closed(self) -> bool:
        return self._client.is_closed

    async def close(self) -> None:
        """Close the underlying HTTPX client.

        The client will *not* be usable after this.
        """
        await self._client.aclose()

    async def __aenter__(self: _T) -> _T:
        return self

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        await self.close()

    async def _prepare_options(
        self,
        options: FinalRequestOptions,  # noqa: ARG002
    ) -> FinalRequestOptions:
        """Hook for mutating the given options"""
        return options

    async def _prepare_request(
        self,
        request: httpx.Request,  # noqa: ARG002

Frequently Asked Questions

What is the AsyncAPIClient class?
AsyncAPIClient is a class in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is AsyncAPIClient defined?
AsyncAPIClient is defined in src/anthropic/_base_client.py at line 1548.
What does AsyncAPIClient extend?
AsyncAPIClient extends BaseAPIResponse, AsyncAPIResponse.

Analyze Your Own Codebase

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

Try Supermodel Free