# devfullcycle/mba-aigateway-litellm architecture

> Architecture diagram of the GitHub repository devfullcycle/mba-aigateway-litellm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/devfullcycle/mba-aigateway-litellm
- Repository: https://github.com/devfullcycle/mba-aigateway-litellm

## Overview

1. **What this repo is**
   - A small reference architecture for an **AI Gateway** pattern.
   - The application never calls OpenAI or Anthropic directly; it talks to a **LiteLLM Proxy** that hides provider selection, keys, and fallback behavior.

2. **Main subsystems**
   - `app/main.py`: the business application, implemented in Python.
   - `litellm/config.yaml`: the gateway configuration that maps internal capability names to real providers/models.
   - `docker-compose.yml`: runs the LiteLLM Proxy as a local service.
   - `.env` / `.env.example`: stores provider API keys, the proxy master key, and model wiring.

3. **Core architectural boundary**
   - The key boundary is between **application logic** and **AI infrastructure**.
   - The app knows only one internal capability name: `support-ticket-classifier`.
   - The proxy knows which physical model/provider backs that capability and whether fallback is enabled.

4. **Request flow**
   - A user provides a support message to the Python app, typically via the command line in this example.
   - The app sends a request to the LiteLLM Proxy using the proxy’s `base_url` and `LITELLM_MASTER_KEY`.
   - The proxy forwards the request to the configured provider/model.
   - The response returns through the proxy to the app.

5. **Business contract**
   - The expected output is a **JSON classification** for a support ticket.
   - The category set is fixed: `billing`, `technical`, `account`, or `other`.
   - This means the app is not just consuming text; it is enforcing a response contract.

6. **Fallback behavior**
   - The proxy can be configured with a **primary model** and a **fallback model**.
   - If the primary fails technically, the proxy can automatically retry with the backup provider.
   - This fallback is invisible to the app; from its perspective, it just receives a successful response or an error.

7. **Validation boundary**
   - The repo explicitly shows that **technical success is not enough**.
   - In the final state, the fallback model may return HTTP 200 but still violate the app’s expected contract, such as wrapping JSON in markdown.
   - The **application** must validate the returned payload and reject responses that do not match the business format.

8. **Configuration model**
   - Provider credentials are kept out of the application and live in environment variables.
   - The proxy configuration determines which providers are available, which model is primary, and which one is fallback.
   - Changing provider choice is a configuration change, not an application code change.

9. **Runtime/deployment shape**
   - The proxy is containerized and started separately with Docker Compose on port `4000`.
   - The Python app is run locally against that proxy endpoint.
   - This creates a clean two-tier setup: local client app + centralized AI gateway.

10. **Evolution of the design**
   - The README describes four steps, but `main` represents the final one.
   - The progression is: single-capability proxy, multiple capabilities, proxy-level fallback, then application-level validation.
   - That evolution matters because it shows the split of responsibility: the proxy handles routing and resilience; the app handles correctness of the returned business payload.

11. **Diagram boundaries to draw**
   - Draw the app as a client of a single gateway endpoint, not as a client of individual providers.
   - Draw the proxy as the place where model/provider mapping, secrets usage, and fallback live.
   - Draw OpenAI and Anthropic as external upstreams behind the proxy.
   - Show validation logic inside the app, after the proxy response, because it is not delegated to the gateway.

Main entry points: [`app/main.py`](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py) (Validator), [`.env.example`](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/.env.example) (Secrets env), [`litellm/config.yaml`](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/litellm/config.yaml) (Gateway config), [`docker-compose.yml`](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/docker-compose.yml) (Compose stack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Application"]
  node_main["Support app<br/>python cli<br/>[main.py]"]
  node_input["User ticket<br/>support text"]
  node_request["Proxy request<br/>llm client<br/>[main.py]"]
  node_validate["Validator<br/>contract check<br/>[main.py]"]
  node_result["Classification<br/>business output"]
end

subgraph group_gateway["AI Gateway"]
  node_proxy["LiteLLM proxy<br/>gateway service"]
  node_config["Gateway config<br/>proxy yaml<br/>[config.yaml]"]
  node_capability{{"Support classifier<br/>logical model"}}
  node_primary["Primary model<br/>provider route"]
  node_fallback["Fallback model<br/>provider route"]
end

subgraph group_deployment["Deployment"]
  node_compose["Compose stack<br/>docker compose<br/>[docker-compose.yml]"]
  node_env["Secrets env<br/>env vars<br/>[.env.example]"]
end

subgraph group_external["Providers"]
  node_openai(("OpenAI<br/>external provider"))
  node_anthropic(("Anthropic<br/>external provider"))
end

node_input -->|"submit"| node_main
node_main -->|"prepare"| node_request
node_request -->|"call"| node_proxy
node_compose -->|"run"| node_proxy
node_env -->|"auth and keys"| node_proxy
node_env -->|"wiring"| node_config
node_config -->|"maps"| node_capability
node_capability -->|"uses"| node_primary
node_capability -->|"retries"| node_fallback
node_primary -->|"upstream"| node_openai
node_fallback -->|"upstream"| node_anthropic
node_proxy -->|"forward"| node_openai
node_proxy -->|"fallback"| node_anthropic
node_proxy -->|"return"| node_validate
node_validate -->|"accept"| node_result
node_validate -->|"reject"| node_main

click node_main "https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py"
click node_request "https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py"
click node_validate "https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py"
click node_compose "https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/docker-compose.yml"
click node_config "https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/litellm/config.yaml"
click node_env "https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/.env.example"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_input,node_request,node_validate,node_result toneBlue
class node_proxy,node_config,node_capability,node_primary,node_fallback toneAmber
class node_compose,node_env toneMint
class node_openai,node_anthropic toneRose
```

## Components

### Application

Local Python client and business validation boundary

- [Support app](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py): python cli · Entry point for the local support-ticket classifier · `app/main.py`
- **User ticket**: support text · Incoming support message to classify
- [Proxy request](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py): llm client · Builds a request for the gateway using the proxy base URL and master key · `app/main.py`
- [Validator](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/app/main.py): contract check · Checks the returned payload matches the required JSON classification format · `app/main.py`
- **Classification**: business output · Expected JSON category output: billing, technical, account, or other

### AI Gateway

LiteLLM proxy and routing configuration

- **LiteLLM proxy**: gateway service · Single endpoint that hides provider selection, credentials, and retries
- [Gateway config](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/litellm/config.yaml): proxy yaml · Maps internal capability names to physical models and fallback behavior · `litellm/config.yaml`
- **Support classifier**: logical model · Internal capability name used by the app instead of provider-specific models
- **Primary model**: provider route · Main provider/model used by the proxy for the capability
- **Fallback model**: provider route · Backup provider/model used if the primary model fails technically

### Deployment

Container and environment wiring

- [Compose stack](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/docker-compose.yml): docker compose · Starts the LiteLLM proxy as a local service · `docker-compose.yml`
- [Secrets env](https://github.com/devfullcycle/mba-aigateway-litellm/blob/main/.env.example): env vars · Holds provider API keys, proxy master key, and model wiring · `.env.example`

### Providers

Upstream model providers behind the proxy

- **OpenAI**: external provider · Upstream provider available behind the gateway
- **Anthropic**: external provider · Upstream provider available behind the gateway

## Connections

- User ticket → Support app: submit · A support message is passed into the Python app
- Support app → Proxy request: prepare · The app creates a gateway-bound inference request
- Proxy request → LiteLLM proxy: call · The app calls the proxy endpoint using its base URL and master key
- Compose stack → LiteLLM proxy: run · Docker Compose starts the proxy container locally
- Secrets env → LiteLLM proxy: auth and keys · The proxy reads provider credentials and gateway secrets from environment variables
- Secrets env → Gateway config: wiring · Environment values support provider and model mapping
- Gateway config → Support classifier: maps · Configuration binds the internal capability to a route
- Support classifier → Primary model: uses · The capability points to the primary provider/model
- Support classifier → Fallback model: retries · The capability can retry through a fallback model if needed
- Primary model → OpenAI: upstream · One possible provider target behind the proxy
- Fallback model → Anthropic: upstream · Backup provider target behind the proxy
- LiteLLM proxy → OpenAI: forward · The gateway forwards requests to the selected provider
- LiteLLM proxy → Anthropic: fallback · The gateway can transparently switch providers on technical failure
- LiteLLM proxy → Validator: return · Responses come back through the proxy to the application
- Validator → Classification: accept · Valid JSON classification is accepted as the business output
- Validator → Support app: reject · Responses that violate the contract are rejected by the app
