Skip to main content

A composable FastAPI LLM gateway with unified multi-provider support

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_llm_gateway-0.1.2.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_llm_gateway-0.1.2-py3-none-any.whl (40.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_llm_gateway-0.1.2.tar.gz.

File metadata

  • Download URL: fastapi_llm_gateway-0.1.2.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for fastapi_llm_gateway-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0cc1ea18a1fa6b912b37f7a2b9602d45a8c09c86e44e6a347470c16761c647ea
MD5 c759f6f495c63dd704cdad01706aafc8
BLAKE2b-256 73de13cf6a20352d8b847b2451446660ab8c9aef8e66324fb676ff744833216a

See more details on using hashes here.

File details

Details for the file fastapi_llm_gateway-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_llm_gateway-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 654c8f3ac2f7f832521c427234f5d4fc052e2f54336045559c17adce4efc7803
MD5 1ab4d54725c345a33f8b984ae5342d10
BLAKE2b-256 b62e5892677756a1e52281426f39bd8655886b78625425f72086202f5c001aea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page