Skip to content

Engineering

Why every system we ship has a model gateway

One entry point for every model call: routing, fallbacks, caching, budgets and a log. The least glamorous part of an AI system, and the one we never skip.
Written by
The PrizenLabs team
Published
Reading time
4 min

Early AI features call a model provider directly. A function builds a prompt, sends it with an API key from an environment variable and parses the reply. It works, and it is how most prototypes should start.

By the time a system reaches production, that pattern has usually multiplied. Several services hold their own keys. Each has its own retry logic, or none. Nobody can say what the AI bill is per feature, which prompts contain customer data, or what would happen if the main provider had an outage. Changing models means editing code in five places.

So every system we ship sends its model calls through one component: a model gateway. It is one of the accelerators we bring to every build, and the least glamorous of them. Here is what it does, and why we treat it as part of the first version.

What a gateway is

A model gateway is a service between your application and every model it uses, hosted or self-hosted. Applications send requests to it in one format, usually the OpenAI-compatible chat API that most providers and open-source servers such as vLLM now accept, and the gateway decides where each request goes and records what happened.

You can build one, or start from an open-source project such as LiteLLM, which puts an OpenAI-compatible proxy in front of many providers. The choice matters less than having exactly one.

Seven jobs, one place

1. Routing

Different requests need different models. Classifying a ticket does not need the model that drafts a legal summary. The gateway routes by request type, with rules first and, where the traffic justifies it, a learned router. The economics are in your AI bill is a routing problem: send each kind of request to the cheapest model that passes your evaluation for that kind, and escalate when it does not.

2. Fallbacks

Providers have outages, rate limits and slow days. The gateway retries with backoff, and when a provider is down it fails over to a second model that has passed the same evaluation. Applications see a slower answer instead of an error.

3. Caching

Many requests share a long, identical prefix: system instructions, tool definitions, reference documents. Providers including Anthropic and OpenAI offer prompt caching, which bills cached input at a lower rate and returns faster. The gateway keeps prompts structured so the stable part comes first, and it can cache whole responses for requests that repeat exactly.

4. Keys and budgets

Provider keys live in the gateway and nowhere else. Each team, feature or customer gets its own gateway key with a quota and a budget. When a new feature starts burning money, one dashboard shows it, and a budget stops it before the invoice does.

5. Data policy

Some data may go to any approved provider; some may only go to a model inside your network. The gateway enforces that by policy: requests tagged as sensitive are routed to a private deployment or refused, and personal data can be redacted before a request leaves. Policy in one place is policy you can show an auditor.

6. Logging and tracing

Every request is logged with the caller, model, prompt version, token counts, cost, latency and outcome, and linked to the trace of the user action that caused it. When an answer is wrong, you can see exactly what the model was sent. When the bill jumps, you can see which feature did it.

7. Swapping models

Because applications talk to the gateway rather than to a provider, changing a model is a configuration change. With an evaluation set for each request type, it is also a safe one: run the candidate against the set, compare the scorecards, switch the route.

What it is not

A gateway is not an agent framework, and it should not hold business logic. It does not decide whether a refund is approved or which tool an agent may call. That belongs in the application and its policy code, as we describe in when an AI agent should not act alone.

Nor should it slow anything down. A gateway running close to your application adds a small fraction to calls that already take hundreds or thousands of milliseconds. If it adds more than that, something is misconfigured.

Self-hosting changes the picture, not the principle

Teams running open models on their own GPUs need a gateway even more. It is where traffic is split between the private model and an approved hosted one, and where capacity limits keep one batch job from starving an interactive feature. Self-hosting can be cheaper at steady, high volume, and is sometimes required for data reasons; the gateway is what lets you mix both without rewriting your applications.

The same idea, one level up

ClearLoad, a workload engine we built, routes each task to a person or to an AI model by its complexity, urgency and cost, and learns from the outcome. A model gateway does the same for model calls. Both rest on one idea: the expensive resource should handle only the work that needs it, and you should be able to see who did what.

Where to start

If your system calls providers directly today, the first step is small. Put a proxy in front of the calls you already make, move the keys into it, and turn on request logging. Within a week you will know your cost per feature and per kind of request, which is usually enough to find the first saving. Routing, fallbacks and budgets follow from there.

We add gateways to existing systems as part of our LLMOps, evaluation and guardrails work, and build them in from the first day on new projects. If your AI bill or your provider risk keeps someone awake, tell us about it.

Next step

Bring us the workflow that's stuck.

Tell us the decision you want automated, or the AI system that isn't holding up. The people who would build it will read it and reply.

What happens next

  1. 01Tell us the problemA short note or a 30-minute call: the workflow, the data and what better would look like.
  2. 02Hear back within 48 hoursFrom the people who would build it, not a sales filter.
  3. 03A one-week discoveryWe find the decision, measure today's baseline and draft the evaluation set. You get a clear answer either way.