What a self-hosted LLM API gateway does

Do we need a gateway in front of our model APIs, and how do we set its limits? That question comes up when a second team ships an LLM feature and nobody can say who spent what.

The gateway is a server you run in front of every model backend your apps call. A backend is anything that answers model requests: an inference API, a dedicated endpoint (a model on GPUs reserved for you) or your own server.

Your apps call one address and the gateway forwards each request. On the way, it checks the caller's key, counts tokens and applies limits. Tokens are the word pieces a model reads and writes, and they are what you pay for.

The gateway earns its place once a second app or a second backend arrives. Without one, every app keeps its own backend keys, its own retry code and its own cost tracking.

LLM API gateway architecture: three jobs in front of your models

A gateway does three jobs for every request.

  1. Entry and control. It speaks the OpenAI API format, so your apps keep their existing SDKs. An API key is the secret an app sends to prove who it is. The gateway gives each app its own key, then applies that key's budget and rate limit. A rate limit caps requests or tokens per minute.
  2. Routing and recovery. Routing means choosing the backend for each request, for example by model name. When a backend fails, the gateway retries it. If it keeps failing, the gateway sends the request to a fallback, a backup model you named in advance.
  3. The backend registry. The gateway stores the address and the secret key of each backend. Only the gateway holds those keys, not your apps.

Some gateways add more on top. Kong lists PII sanitization (removing personal data from prompts) and semantic caching (reusing an answer to a similar earlier question). Both change what reaches the model or your users, so test them on your own traffic first.

Flow diagram: two apps, each with its own key and budget, call one self-hosted gateway, highlighted, which checks the key, applies the budget and rate limit, retries 429, 502 and 503 errors and falls back to a backup model. The gateway forwards requests to our serverless API, billed per token, or to your own model on a dedicated endpoint or a GPU virtual machine.
Each app calls the gateway with its own key; only the gateway holds the keys of our serverless API and of your own model.

Gateway tools: LiteLLM, Kong AI Gateway and the Kubernetes inference extension

LiteLLM, Kong AI Gateway and the Kubernetes inference extension can each run a self-hosted gateway. They differ in who runs them.

ToolWhat it isFits
LiteLLMA Python proxy that calls many model providers through the OpenAI format, with virtual keys, budgets and fallbacksAn ML or product team that wants one config file
Kong AI GatewayAI plugins for the Kong API gateway; AI Proxy takes OpenAI-format requests and translates them for each provider, streaming includedA platform team that already runs Kong
Gateway API Inference ExtensionAn official Kubernetes project, built on Kubernetes' standard routing API (the Gateway API), that routes requests to self-hosted models by model nameA team serving its own models on Kubernetes

The LiteLLM proxy calls more than 100 model APIs in the OpenAI format. A virtual key is a key the gateway issues itself, scoped to the models an app may call.

The inference extension routes between your own model servers inside Kubernetes, below the gateway your apps call. A prefix cache keeps the start of recent prompts in GPU memory. A LoRA adapter is a small add-on file that changes a base model's behaviour. The extension's router reads each model server's load, its prefix cache and its loaded LoRA adapters. It can favour a server that already holds the request's prompt start or its adapter. Its docs describe a stack with two layers. LiteLLM is the gateway your apps call. The extension is the router inside your cluster, between LiteLLM and the model servers.

Our serverless inference, open models you call through one API and pay for per token, also routes without a gateway. With the model name lyceum/router, our router picks one of our models for each request. You pay that model's price. It routes between our models only, not across your other backends.

Budgets and rate limits per key: a worked example

Take one hypothetical app: a support assistant that sends 300,000 requests a month. Each request has three parts:

  • a 1,500-token start: the system prompt and instructions, the same on every request;
  • 500 tokens of question and retrieved text, different every time;
  • an answer of 250 tokens.

It calls the model qwen/qwen3.8-27b on our API.

We are Lyceum, a sovereign European AI inference provider for open-source models: built and run in Europe. Our GPUs run in European data centres. On our inference products, prompts and outputs are processed, not stored, and never used for training.

Our models page listed these prices on 30 October 2026, per million tokens: $0.40 for input and $0.10 for cached input. Output costs $2.40.

Cached input is a repeated prompt start that is still in GPU memory. The start must match exactly, so keep the fixed text first in every request. We bill it at the cached price where a model lists one. A cache hit is not guaranteed, so budget for both ends:

tokens per month   = 300,000 x 1,500 = 450M repeated start
                     300,000 x   500 = 150M fresh input
                     300,000 x   250 =  75M output
best case (start always cached)
  = 150 x $0.40 + 450 x $0.10 + 75 x $2.40 = $60 + $45 + $180 = $285
worst case (start never cached)
  = (150 + 450) x $0.40 + 75 x $2.40       = $240 + $180     = $420
monthly key budget = $420 x 1.2 headroom  = $504

The budget follows the worst case, so a month with poor cache reuse still fits. The 20% headroom is our assumption for traffic growth. The example favours a calm month: it ignores retries, which also consume tokens, and traffic spikes.

Calculated example for a hypothetical support assistant with 300,000 requests a month on qwen/qwen3.8-27b at our listed prices of 30 October 2026: $285 a month if the repeated prompt start is always cached, $420 if it never is, and a key budget of $504, the worst case times 1.2.
Calculated example with hypothetical traffic: the support app's key budget is its uncached month, $420, plus 20% headroom.

Rate limits are set per minute, so take the busiest minute, not the monthly total. Assume this app peaks at 10 requests per second during that minute:

tokens per request = 1,500 + 500 + 250  = 2,250
requests per minute = 10 x 60           = 600
tokens per minute   = 600 x 2,250       = 1,350,000

In LiteLLM, both numbers go on the app's virtual key. A budget can reset on a schedule, such as every 30 days. The backend entry points at our OpenAI-compatible base URL:

model_list:
  - model_name: support-model
    litellm_params:
      model: openai/qwen/qwen3.8-27b
      api_base: https://api.lyceum.technology/openai/v1
      api_key: os.environ/LYCEUM_API_KEY

The openai/ prefix in the model name tells LiteLLM to treat the backend as OpenAI-compatible. Budgets in LiteLLM need its database. Without one, its docs say the budget check is skipped and requests keep going past the limit.

We recommend a second cap behind the gateway. Our API keys can carry a spend limit on serverless inference. Once a key reaches it, new requests are refused with HTTP 402 and the code api_key_spend_limit_reached. The limit is soft: we add up the charges every few minutes. Spending can overshoot by up to a few minutes of traffic, and a running request is never cut.

Retries, fallbacks and timeouts for LLM traffic

A retry is right for some errors and wrong for others. Our serverless inference docs list each error and what to do:

CodeMeaning on our APIWhat the gateway should do
429No free capacity for the model right now, or the model backend is rate limitedRetry with backoff
502, 503The model backend failed or is temporarily unavailableRetry with backoff
504The generation ran longer than 300 secondsStream, or shorten the request
Other 4xxThe request itself was rejectedDo not retry

Backoff means waiting longer before each new attempt. LiteLLM retries a call a set number of times. Then it falls back to another model group, a set of backends you listed under one model name. Its request timeout setting ends a call that takes too long. Set it above 300 seconds, our backend's limit, so the backend decides when a generation stops, not the gateway.

Streaming sends the answer token by token while it is written.

Every response from our API carries a trace ID, a code that identifies the request, in its traceparent header. Log it in the gateway, so a failed request can be traced with our engineers. For incidents, our public status page at status.lyceum.technology lists what is happening.

Where to run the gateway and what it logs

The gateway forwards requests and counts tokens; the GPUs do the model work. Run it close to your apps, and run at least two copies, so one restart does not stop every app.

Your gateway log is the record of who called which model. That makes it your log to secure and to delete on schedule. Decide what it stores: token counts and timings, or full prompts as well. On our side, the only record a request leaves is a usage record with organisation, user, model, token counts and time. For an agent, our guide to EU-ready agent infrastructure shows a per-step log that keeps no prompt text.

The gateway's backends can be our serverless inference or a model of your own. Our guide to private LLM endpoints compares a dedicated endpoint with your own vLLM container, a Docker image that runs vLLM, an open-source serving engine. Our guide to choosing an endpoint compares all routes.

Start with one app: give it a gateway key, a budget from its own worst case, and our error table as its retry rules.

Frequently asked questions

Can the gateway also call closed model APIs?

Yes. LiteLLM and Kong both translate the OpenAI format for many providers, so one gateway can front open and closed models side by side.

Do I still need spend limits on the backend keys?

They are a second safety net. A LiteLLM budget is skipped when the proxy runs without its database. A spend limit on the backend key still refuses new requests.

Sources

  1. konghq.com
  2. developer.konghq.com
  3. docs.litellm.ai
  4. docs.litellm.ai
  5. gateway-api-inference-extension.sigs.k8s.io
  6. docs.litellm.ai
  7. docs.litellm.ai
  8. docs.litellm.ai