Infrastructure & API Architecture

Self-Hosting an LLM Gateway: Architecture, Failover, and Configuration

When an organization builds multiple applications that utilize external and internal AI models, a tangle of direct API integrations quickly arises. A self-hosted LLM gateway acts as a central proxy between your software and model providers. In this article, we cover the architecture, features, prerequisites, and a practical configuration example for a robust production setup.

The Problem with Direct API Integrations

In the early stages of AI adoption, software teams integrate an LLM provider directly into the code of a specific service. For example, a backend microservice directly calls the REST API of OpenAI or Anthropic. As long as it involves a single application, this approach works perfectly. However, as the number of applications, teams, or AI models used grows, clear bottlenecks emerge regarding manageability, security, and costs.

When applications communicate directly with external providers, secret key management becomes fragmented across dozens of repositories and environments. This significantly increases the risk of insecure API keys. Additionally, there is no central overview of total consumption and incurred costs per department or user. Each provider also has its own API specification, error handling, and rate-limiting logic, forcing developers to repeatedly 'reinvent the wheel' whenever a new model needs to be integrated or when testing with an alternative vendor.

What is an LLM Gateway?

An LLM gateway (also called an AI proxy or API aggregator) is a lightweight, central intermediary layer that handles network traffic between your internal applications and external or internal AI model providers. Applications no longer talk directly to the APIs of external parties, but instead send their requests to the gateway's uniform endpoint.

The gateway receives a standardized request (often based on the common OpenAI-compatible API format), processes it according to predefined rules, adds the required authentication, optionally inserts a cache check into the chain, and forwards the call to the correct target location. The response is then sent back to the calling application.

The Core Functions of a Central Gateway

By centralizing interaction with LLMs in a single controlled intermediary layer, the infrastructure team gains control over five crucial aspects of AI integration:

1. Central Key and Authentication Management

Keys from external providers such as OpenAI, Anthropic, Mistral, or Azure OpenAI are stored exclusively in the secure environment of the gateway. The underlying applications authenticate with the gateway using an internally generated API key or token. If an internal key is leaked, it can be revoked directly on the gateway without having to replace external API keys with the vendor.

2. Quotas, Rate Limiting, and Cost Control

Without central control, a bug in a loop or a sudden spike in user traffic can lead to unexpectedly high monthly bills. A gateway allows administrators to set strict quotas per team, user, or application (for example, a maximum monthly budget or a maximum number of tokens per minute). Read more about setting up these kinds of restrictions in our article on rate limits and cost control.

3. Observability, Logging, and Audit Trails

To comply with laws and regulations (such as the GDPR and the EU AI Act), it is essential to know what data is leaving the organization and to which processors it is being sent. A gateway provides central instrumentation for observability and logging, including metrics such as latency, token usage, error rates, and prompt logs.

4. Smart Response Caching

Many applications regularly send identical or highly similar prompts to a model. By applying response caching at the gateway level, identical requests are answered directly from memory or a fast datastore. This yields a drastic reduction in latency and saves tokens. See also our guide on caching LLM responses for an in-depth look at exact versus semantic caching.

5. Model Routing and Vendor Independence

One of the biggest advantages of a gateway is vendor abstraction. Applications request a specific type of functionality (for example, general-chat-fast) instead of a hardcoded model name. The gateway determines which physical model is called based on rules. For more advanced strategies, you can consult our article on dynamic model routing.

What an LLM Gateway Does and Does Not Solve

While a gateway is a powerful building block in your AI infrastructure, it is important to have clear expectations about its scope.

Solved by the Gateway Not Solved by the Gateway
Centralization of API keys and secrets Data preparation, chunking, and vector embedding logic
Cost caps, rate limits, and budget alerts per team Prompt engineering and application-specific context retrieval
Automatic retries, timeouts, and failover during outages Quality assurance of the actual LLM output content
Insight into latency, error rates, and token usage Fine-tuning or training proprietary models

In short, a gateway is a network and management layer. It does not replace application logic such as a RAG (Retrieval-Augmented Generation) pipeline. If you set up a RAG system, the application still handles document retrieval and vector searches, while the gateway facilitates the final generation call to the language model. Check out the RAG for beginners guide on leren.llmnet.nl for more insight into the division of these responsibilities.

Architecture of a Robust Gateway: Health Checks and Failover

When all applications become dependent on a single central gateway, the gateway turns into a critical single point of failure. A robust architecture therefore requires high availability and automatic failover mechanisms.

A professional gateway setup uses active and passive health checks. An active health check periodically sends a minimal ping call to the configured model providers to check if the API is responding and what the current latency is. Passive health checks monitor actual production calls. If a provider suddenly returns 503 Service Unavailable or 429 Too Many Requests error messages, the gateway temporarily marks this provider as unhealthy.

Failover Principle: As soon as the primary provider (for example, Provider A) fails or reaches its rate limit, the gateway automatically and transparently switches the request to a secondary provider (Provider B) with a similar model. The calling client does not notice this, except for a minimal increase in response time.

Practical Configuration Example

The YAML example below illustrates a vendor-independent declarative configuration of a self-hosted LLM gateway. It defines two providers, including a combined fallback route, rate limiting, caching, and health checks.

# Configuration file for self-hosted LLM Gateway
version: "1.0"

server:
  host: "0.0.0.0"
  port: 8080
  timeout_seconds: 30

# External model providers and their secret keys from environment variables
providers:
  - id: openai-main
    type: openai
    api_key: "${OPENAI_API_KEY}"
    health_check:
      enabled: true
      interval_seconds: 15
      path: "/v1/models"

  - id: anthropic-backup
    type: anthropic
    api_key: "${ANTHROPIC_API_KEY}"
    health_check:
      enabled: true
      interval_seconds: 15

# Global caching settings
caching:
  enabled: true
  type: memory
  ttl_seconds: 3600 # 1 hour caching for identical prompts

# Configured virtual routes for internal applications
routes:
  - path: "/v1/chat/completions"
    name: "standard-chat-route"
    # Primary provider with automatic failover
    targets:
      - provider: openai-main
        model: "gpt-4o-mini"
        weight: 100
      - provider: anthropic-backup
        model: "claude-3-5-haiku-20241022"
        is_fallback: true

    # Retry and outage policy
    policies:
      max_retries: 2
      retry_on_status: [429, 500, 502, 503, 504]
      rate_limiting:
        requests_per_minute: 120
        tokens_per_minute: 100000

In this configuration example, the system routes all requests to gpt-4o-mini via OpenAI by default. If OpenAI returns an error code 500 or 429, the gateway automatically fires the request to claude-3-5-haiku via Anthropic after two retries. The backend application simply receives a successful response without complex handling logic in its own codebase.

Key Considerations for Self-Hosting

The decision to self-host a gateway (for example, in Kubernetes or on a dedicated virtual server via Docker) brings operational responsibilities:

  • Streaming and Latency Overhead: An extra network hop adds latency. Ensure the gateway is written in a high-performance language or framework (such as Go, Rust, or an optimized Nginx/Envoy module) and that Server-Sent Events (SSE) for streaming responses are forwarded efficiently.
  • Network Security: Place the gateway within the internal network (VPC) and enforce TLS encryption. Ensure the management interface is not publicly accessible on the internet.
  • Stateless Scaling: Preferably design the gateway to be stateless. Store session data, rate-limit counters, and cache indexes in a shared in-memory datastore like Redis, so the gateway can easily scale horizontally as traffic increases.

Conclusion

Self-hosting an LLM gateway is an essential step for organizations looking to professionally scale AI applications. It decouples application development from the rapidly changing vendor market, increases the security of API keys, and provides necessary mechanisms for cost control and outage protection. By setting up a well-thought-out gateway architecture from the start, your infrastructure remains flexible, robust, and prepared for future developments in the AI landscape.