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.sdk 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.fastapi 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.0.tar.gz (32.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.0-py3-none-any.whl (39.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastapi_llm_gateway-0.1.0.tar.gz
Algorithm Hash digest
SHA256 635cc81996427ff0e1bd201ff673afa3e13edbdb5bb7407ebf41b0e8f12038fb
MD5 719124b3bad76d0020964b5e72b720f3
BLAKE2b-256 109e951a03daff6300291ce738866dffec609fc1a4a5260d4f650d6893162146

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_llm_gateway-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0513def2f26a0a8fc2f75c7db54d23d50d2e866f3216d3294afa553f90918907
MD5 7987d4b80f15dea824ef334dac1a1081
BLAKE2b-256 84b61ac0ad1921f3381636ff950099fe8ad917c91269300701d0c344be10b923

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