How devfullcycle/mba-aigateway-litellm works
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: main.py Validator · .env.example Secrets env · config.yaml Gateway config · docker-compose.yml Compose stack
Components 14
Application
Local Python client and business validation boundary
- Support app · Entry point for the local support-ticket classifier
app/main.py - User ticket · Incoming support message to classify
- Proxy request · Builds a request for the gateway using the proxy base URL and master key
app/main.py - Validator · Checks the returned payload matches the required JSON classification format
app/main.py - Classification · Expected JSON category output: billing, technical, account, or other
AI Gateway
LiteLLM proxy and routing configuration
- LiteLLM proxy · Single endpoint that hides provider selection, credentials, and retries
- Gateway config · Maps internal capability names to physical models and fallback behavior
litellm/config.yaml - Support classifier · Internal capability name used by the app instead of provider-specific models
- Primary model · Main provider/model used by the proxy for the capability
- Fallback model · Backup provider/model used if the primary model fails technically
Deployment
Container and environment wiring
- Compose stack · Starts the LiteLLM proxy as a local service
docker-compose.yml - Secrets env · Holds provider API keys, proxy master key, and model wiring
.env.example
Providers
Upstream model providers behind the proxy
- OpenAI · Upstream provider available behind the gateway
- Anthropic · Upstream provider available behind the gateway
Connections 16
- 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