# portkey-ai/portkey-python-sdk architecture

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

- Interactive diagram: https://gitdiagram.com/portkey-ai/portkey-python-sdk
- Repository: https://github.com/portkey-ai/portkey-python-sdk

## Overview

##### Main Components
- Client Application: User code that imports and calls the SDK (using constructs like Portkey and AsyncPortkey).
- API Gateway/SDK Core: The top-level interface that users interact with. This central component handles the unified API signature and orchestrates calls.
- API Resources Modules: Organized under portkey_ai/api_resources and its subdirectories. There are individual modules (e.g., chat, completions, embeddings, audio, batches, moderations, images, assistants, threads, vector stores) representing separate API endpoints.
- Instrumentation & Observability: Within api_resources/instrumentation, note the support for several observability tools (for logging, request tracing, and custom instrumentation patches for different providers). This is a stream of sub-components for integrating with vendor-specific instrumentation (e.g., openai, crewai, langgraph, litellm).
- Integration Wrappers: Additional integrations in directories like portkey_ai/langchain, portkey_ai/llamaindex, and portkey_ai/llms which provide custom callback handlers and adapters to work with popular AI orchestration frameworks.
- Configuration and Testing: The tests directory (with multiple config files and test cases) represents how various scenarios (single provider, load balancing, fallbacks, retries, etc.) are tested.

##### Depicting Relationships and Interactions
- Separate modules within the SDK Core:
– “Instrumentation & Observability” component that connects to the API Resources, emphasizing that each API call can be augmented with logging, request tracing, metadata, and feedback.
- Optionally, include a “Configuration & Utilities” section (drawing from the configuration files and test setups) that influences the behavior of the SDK.

##### Architectural Patterns or Design Principles
- Adapter Pattern: The SDK maps OpenAI calls to its own internal methods that add fallback, caching, and load balancing.
- API Gateway Pattern: The central Portkey interface serves as a gateway for routing client calls to various underlying provider-specific modules.
- Modularization: Clear separation between core API functionalities, instrumentation, external integrations, and test configurations.
- Observability: Show that every API interaction can optionally go through the instrumentation pipeline—making observability and logging a first-class concern.

##### Relevant Technologies and Frameworks
- Python as the base language.
- Asynchronous programming (AsyncPortkey) for improved concurrency.
- Integration with third-party libraries (LangChain, LlamaIndex) to support additional use cases.
- OpenAI as the primary reference provider, with additional abstraction to support “any provider” via a unified interface.
- Test frameworks and configuration management (pytest, Makefile, setup.cfg, pyproject.toml).

│
▼
│
│      ├── Chat Module
│      ├── Completions Module
│      ├── Embeddings Module
│      ├── Audio Module
│      └── … (Other endpoints)
│
│      ├── Logging
│      ├── Request Tracing
│      └── Custom Metadata / Feedback
│
├── LangChain Adapter
├── LlamaIndex Adapter
└── Others
│
▼

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level
    clientApp["Client Application"]:::client
    click clientApp "https://github.com/portkey-ai/portkey-python-sdk/blob/main/examples/demo.py"

    %% SDK Core / API Gateway Subgraph
    subgraph "SDK Core / API Gateway"
        apiGateway["Portkey API Gateway"]:::core
        click apiGateway "https://github.com/portkey-ai/portkey-python-sdk/blob/main/portkey_ai/__init__.py"
        portkeyScripts["Portkey Scripts"]:::core
        click portkeyScripts "https://github.com/portkey-ai/portkey-python-sdk/blob/main/portkey_ai/_portkey_scripts.py"
    end

    %% Subgraphs inside SDK Core for modular components
    subgraph "API Resources Modules"
        chat["Chat Module"]:::resource
        completions["Completions Module"]:::resource
        embeddings["Embeddings Module"]:::resource
        audio["Audio Module"]:::resource
        others["Other API Modules"]:::resource
    end
    click chat "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources"
    click completions "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources"
    click embeddings "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources"
    click audio "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources"
    click others "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources"

    subgraph "Instrumentation & Observability"
        logging["Logging"]:::inst
        tracing["Request Tracing"]:::inst
        metadata["Custom Metadata/Feedback"]:::inst
    end
    click logging "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources/instrumentation"
    click tracing "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources/instrumentation"
    click metadata "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/api_resources/instrumentation"

    subgraph "Integration Adapters"
        langchainAdapter["LangChain Adapter"]:::integration
        click langchainAdapter "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/langchain"
        click langchainAdapter "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/llms/langchain"
        llamaindexAdapter["LlamaIndex Adapter"]:::integration
        click llamaindexAdapter "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/llamaindex"
        click llamaindexAdapter "https://github.com/portkey-ai/portkey-python-sdk/tree/main/portkey_ai/llms/llama_index"
        othersAdapter["Other Adapters"]:::integration
    end

    configTesting["Configuration & Testing"]:::config
    click configTesting "https://github.com/portkey-ai/portkey-python-sdk/tree/main/tests"
    
    externalProviders["External AI Providers"]:::external

    %% Connections
    clientApp -->|"calls(sync/async)"| apiGateway
    clientApp -->|"calls(sync/async)"| portkeyScripts

    apiGateway -->|"routesTo"| chat
    apiGateway -->|"routesTo"| completions
    apiGateway -->|"routesTo"| embeddings
    apiGateway -->|"routesTo"| audio
    apiGateway -->|"routesTo"| others

    apiGateway -->|"invokes"| logging
    apiGateway -->|"invokes"| tracing
    apiGateway -->|"invokes"| metadata

    apiGateway -->|"integrates"| langchainAdapter
    apiGateway -->|"integrates"| llamaindexAdapter
    apiGateway -->|"integrates"| othersAdapter

    chat -->|"fallback+LB_VirtualKeys"| externalProviders
    completions -->|"fallback+LB_VirtualKeys"| externalProviders
    embeddings -->|"fallback+LB_VirtualKeys"| externalProviders
    audio -->|"fallback+LB_VirtualKeys"| externalProviders
    others -->|"fallback+LB_VirtualKeys"| externalProviders

    configTesting ---|"influences"| apiGateway
    configTesting ---|"influences"| portkeyScripts

    %% Styles
    classDef client fill:#ffdddd,stroke:#aa0000,stroke-width:2px;
    classDef core fill:#ddffdd,stroke:#008800,stroke-width:2px;
    classDef resource fill:#ddeeff,stroke:#003399,stroke-width:2px;
    classDef inst fill:#fff5cc,stroke:#b38f00,stroke-width:2px;
    classDef integration fill:#e6ffe6,stroke:#006600,stroke-width:2px;
    classDef external fill:#ffe6e6,stroke:#aa0000,stroke-width:2px;
    classDef config fill:#e0f7fa,stroke:#006064,stroke-width:2px;
```
