fastapi-llm-gateway
A composable FastAPI LLM gateway with unified multi-provider support.
Features:
- 🚀 Multiple Usage Patterns: Use as SDK, integrate into FastAPI apps, or run as standalone service
- 🔌 Unified API: Single interface for OpenAI, Anthropic, Cohere, VolcEngine, and 100+ providers
- 🔄 Smart Routing: Built-in load balancing, failover, and model aliasing
- 💰 Cost Tracking: Automatic cost calculation per request
- 🌍 Zero Config: Works out-of-the-box with environment variables
- 📦 PyPI Ready: Install with a single pip command
Installation
pip install fastapi-llm-gateway
Quick Start
Pattern 1: SDK - Programmatic Access
from fastapi_llm_gateway import GatewayClient
# Option A: Pass API keys directly
client = GatewayClient(api_keys={"openai": "sk-..."})
# Option B: Use environment variables (recommended)
export OPENAI_API_KEY="sk-..."
client = GatewayClient() # Automatically reads from env vars
# Make requests
response = client.chat(
messages=[{"role": "user", "content": "Hello!"}],
provider="openai",
model="gpt-4o"
)
print(response.content)
print(f"Cost: ${response.cost}")
Pattern 2: FastAPI Integration (Recommended)
from fastapi import FastAPI
from fastapi_llm_gateway import GatewayRouter
app = FastAPI()
app.include_router(GatewayRouter(prefix="/api/llm"))
# Your app now has LLM gateway endpoints:
# POST /api/llm/v1/chat/completions
# GET /api/llm/models
# GET /api/llm/health
Pattern 3: Standalone Service
# Set your API keys as environment variables
export OPENAI_API_KEY="sk-..."
# Start the server
fastapi-llm-gateway-serve
# Or with custom options
fastapi-llm-gateway-serve --host 0.0.0.0 --port 8080 --workers 4
Configuration
Environment Variables
| Variable | Description | Required |
|---|---|---|
OPENAI_API_KEY |
OpenAI API key | No |
ANTHROPIC_API_KEY |
Anthropic API key | No |
COHERE_API_KEY |
Cohere API key | No |
VOLCENGINE_API_KEY |
VolcEngine API key | No |
LLM_GATEWAY_CONFIG |
Path to custom YAML config | No |
LLM_GATEWAY_PORT |
Server port (default: 8001) | No |
LLM_GATEWAY_HOST |
Server host (default: 0.0.0.0) | No |
Built-in Models
The package includes built-in definitions for common models:
- OpenAI: gpt-4o, gpt-4o-mini, gpt-3.5-turbo
- Anthropic: claude-3-5-sonnet, claude-3-opus
- Cohere: command-r, command-r-plus
- VolcEngine: doubao-pro-32k (requires endpoint_id config)
License
MIT License - see LICENSE for details.
Metadata
Release files for fastapi-llm-gateway 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_llm_gateway-0.1.2.tar.gz | 33.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_llm_gateway-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 74.0 kB
Release files / fastapi_llm_gateway-0.1.2.tar.gz
| Download URL | fastapi_llm_gateway-0.1.2.tar.gz |
|---|---|
| Size | 33.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0cc1ea18a1fa6b912b37f7a2b9602d45a8c09c86e44e6a347470c16761c647ea
|
|
BLAKE2b-256 checksum How to use checksums |
73de13cf6a20352d8b847b2451446660ab8c9aef8e66324fb676ff744833216a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.9
|
Release files / fastapi_llm_gateway-0.1.2-py3-none-any.whl
| Download URL | fastapi_llm_gateway-0.1.2-py3-none-any.whl |
|---|---|
| Size | 40.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
654c8f3ac2f7f832521c427234f5d4fc052e2f54336045559c17adce4efc7803
|
|
BLAKE2b-256 checksum How to use checksums |
b62e5892677756a1e52281426f39bd8655886b78625425f72086202f5c001aea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.9
|