Dev.to WebDev 🛠 Dev 👁 2 📖 2 min read

# Building an OpenAI-Compatible AI Gateway with FastAPI

AI development often involves working with multiple model providers and local inference platforms. Each provider may have its own API format, authentication method, and configuration. I wanted to simplify that process,

AI development often involves working with multiple model providers and local inference platforms. Each provider may have its own API format, authentication method, and configuration.

I wanted to simplify that process, so I started building AI Gateway Manager — a centralized gateway for managing different AI model endpoints through a unified API.

The Problem

When working with different AI providers and self-hosted models, developers often need to:

  • Manage multiple API endpoints.
  • Handle different authentication credentials.
  • Adapt to different API formats.
  • Manage model availability and failures.
  • Monitor usage, latency, and errors.

Maintaining separate integrations can make an application unnecessarily complicated.

The Solution

AI Gateway Manager provides a centralized layer between applications and AI model providers.

The goal is simple: expose a consistent, OpenAI-compatible API while allowing the underlying model provider to change.

Supported Providers

The platform is designed to work with:

  • vLLM
  • Ollama
  • LM Studio
  • LocalAI
  • Custom HTTP endpoints

OpenAI-Compatible API

The gateway exposes familiar API endpoints:

Endpoint Purpose
/v1/chat/completions Chat completions
/v1/embeddings Generate embeddings
/v1/models List available models

It also supports Server-Sent Events (SSE) for streaming responses.

For example, an application can send a request to its configured gateway:

curl http://localhost:8000/v1/chat/completions \
  -H "Authorization: Bearer YOUR_GATEWAY_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-model-alias",
    "messages": [
      {
        "role": "user",
        "content": "Hello, how are you?"
      }
    ]
  }'

The gateway handles routing the request to the configured provider.

Key Features

Some of the main capabilities include:

  • Model aliases: Use consistent model names across providers.
  • Provider routing: Route requests to different AI backends.
  • Failover chains: Switch to alternative providers when configured failure conditions occur.
  • Credential protection: Encrypt stored provider credentials.
  • Rate limiting: Apply request limits using Redis, with an in-memory fallback.
  • Usage analytics: Track tokens, latency, errors, and estimated costs.
  • Streaming: Support SSE responses.

Technology Stack

The project uses:

  • Backend: Python, FastAPI
  • Frontend: Next.js
  • Database: PostgreSQL
  • Caching and rate limiting: Redis
  • Deployment: Docker Compose, Nginx

What I Learned

Building this project helped me explore several important backend engineering concepts:

  • Designing API-compatible interfaces.
  • Integrating multiple external services.
  • Managing provider-specific configurations.
  • Handling streaming HTTP responses.
  • Implementing credential encryption.
  • Designing fallback and routing mechanisms.

It also gave me practical experience connecting a Python backend with a modern Next.js frontend.

Final Thoughts

AI Gateway Manager is an ongoing personal project focused on making AI model integration easier to manage.

My goal is to provide developers with a consistent interface for working with local and remote AI models without tying their applications to a single provider.

Project: AI Gateway Manager (currently private)

Developer: Rasel Ahammed

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.