# anthropics/anthropic-sdk-python architecture

> Architecture diagram of the GitHub repository anthropics/anthropic-sdk-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anthropics/anthropic-sdk-python
- Repository: https://github.com/anthropics/anthropic-sdk-python

## Overview

##### Main Components Structure

a) Client Layer
- Both `Anthropic` and `AsyncAnthropic` clients as entry points
- Specialized clients (`AnthropicBedrock`, `AnthropicVertex`)
- To configuration components (API keys, timeouts, retries)

b) Resource Layer
- Resources into main categories:
- Messages
- Completions
- Models
- Beta Resources (Messages, Batches)
- Inheritance relationships between resource classes

c) Types Layer
- Extensive type system
- Types into categories:
- Request Parameters
- Response Models
- Error Types
- Streaming Types

d) Core Infrastructure
- HTTP Client abstraction (based on httpx)
- Authentication handling
- Error handling
- Streaming support
- Pagination handling

##### Interaction Flows

a) Request Flow
- Client initialization → Resource selection → Request building → HTTP transmission
- How types are used for request validation
- Retry and timeout mechanisms

b) Response Flow
- Raw HTTP response → Response parsing → Type conversion
- Error handling paths
- Streaming response handling

##### Integration Points

External integration capabilities:
- AWS Bedrock connection
- Google Vertex AI connection
- Custom HTTP client configuration
- Authentication methods

##### Specific Implementation Details

Technical details such as:
- Support for Python 3.8+
- Async/sync dual implementation
- Type system based on TypedDict and Pydantic
- Streaming support via SSE
- Pagination mechanisms
- Retry logic with exponential backoff

##### Notes Section

Annotations for:
- Configuration options
- Common usage patterns
- Error handling strategies
- Performance considerations

This system design should help developers understand both the high-level architecture and the detailed implementation aspects of the SDK, making it easier to use, maintain, and extend the library.

Remember to include version information and note that this is a client SDK, meaning all external communication ultimately goes to Anthropic's API endpoints, with optional routing through AWS Bedrock or Google Vertex AI services.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Client Layer"
        AC["Anthropic Client"]:::client
        AAC["Async Anthropic Client"]:::client
        ABC["Anthropic Bedrock Client"]:::client
        AVC["Anthropic Vertex Client"]:::client
        
        Config["Configuration"]:::config
    end

    subgraph "Resource Layer"
        Messages["Messages"]:::resource
        Completions["Completions"]:::resource
        Models["Models"]:::resource
        
        subgraph "Beta Resources"
            BetaMessages["Beta Messages"]:::beta
            BetaBatches["Beta Batches"]:::beta
        end
    end

    subgraph "Types Layer"
        ReqParams["Request Parameters"]:::types
        RespModels["Response Models"]:::types
        ErrorTypes["Error Types"]:::types
        StreamTypes["Streaming Types"]:::types
    end

    subgraph "Core Infrastructure"
        HTTPClient["HTTP Client"]:::infra
        AuthHandler["Authentication"]:::infra
        ErrorHandler["Error Handling"]:::infra
        StreamSupport["Streaming Support"]:::infra
        PaginationHandler["Pagination"]:::infra
    end

    subgraph "Integration Points"
        AWS["AWS Bedrock"]:::external
        GCP["Google Vertex AI"]:::external
        AnthropicAPI["Anthropic API"]:::external
    end

    subgraph "Response Processing"
        RespProcess["Response Processor"]:::process
        StreamProcess["Stream Processor"]:::process
        ErrorProcess["Error Processor"]:::process
    end

    %% Relationships
    AC --> Config
    AAC --> Config
    ABC --> Config
    AVC --> Config

    AC --> Messages
    AC --> Completions
    AC --> Models
    AC --> BetaMessages
    AC --> BetaBatches

    Messages --> ReqParams
    Messages --> RespModels
    Completions --> ReqParams
    Completions --> RespModels

    HTTPClient --> AuthHandler
    HTTPClient --> ErrorHandler
    HTTPClient --> StreamSupport
    HTTPClient --> PaginationHandler

    ABC --> AWS
    AVC --> GCP
    AWS --> AnthropicAPI
    GCP --> AnthropicAPI
    HTTPClient --> AnthropicAPI

    HTTPClient --> RespProcess
    StreamSupport --> StreamProcess
    ErrorHandler --> ErrorProcess

    %% Click Events
    click AC "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_client.py"
    click ABC "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/bedrock/_client.py"
    click AVC "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/vertex/_client.py"
    click Messages "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/resources/messages/messages.py"
    click Completions "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/resources/completions.py"
    click Models "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/resources/models.py"
    click BetaMessages "https://github.com/anthropics/anthropic-sdk-python/tree/main/src/anthropic/resources/beta/"
    click ReqParams "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_types.py"
    click StreamTypes "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/types/raw_message_stream_event.py"
    click ErrorTypes "https://github.com/anthropics/anthropic-sdk-python/tree/main/src/anthropic/types/shared/"
    click AuthHandler "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/bedrock/_auth.py"
    click ErrorHandler "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_exceptions.py"
    click StreamSupport "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_streaming.py"
    click PaginationHandler "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/pagination.py"
    click AWS "https://github.com/anthropics/anthropic-sdk-python/tree/main/src/anthropic/lib/bedrock/"
    click GCP "https://github.com/anthropics/anthropic-sdk-python/tree/main/src/anthropic/lib/vertex/"
    click RespProcess "https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_response.py"

    %% Styling
    classDef client fill:#2374ab
    classDef config fill:#57a773
    classDef resource fill:#41658a
    classDef beta fill:#ffd400
    classDef types fill:#89a4c7
    classDef infra fill:#4a5859
    classDef external fill:#d64933
    classDef process fill:#916dd5

    %% Legend
    subgraph Legend
        L1["Client Components"]:::client
        L2["Configuration"]:::config
        L3["Resources"]:::resource
        L4["Beta Features"]:::beta
        L5["Type System"]:::types
        L6["Infrastructure"]:::infra
        L7["External Services"]:::external
        L8["Processing"]:::process
    end
```
