← Blog

How to Build an API Gateway for LLMs

2026-10-29 · 5 min read · SubToAPI Team

An API gateway for LLMs is a thin service that sits between your applications and the model providers you use (Claude, GPT, Gemini, local models). It handles the things you don't want scattered across every client: authentication, rate limiting, request routing, streaming, logging, and cost tracking. Instead of each app talking directly to a provider's SDK with a raw API key embedded in it, every app talks to your gateway, and the gateway talks to the providers.

The short answer to "how do I build one" is: start with a reverse proxy that issues its own API keys, forwards requests to the right provider endpoint, streams responses back unchanged, and logs token usage per key. You can build this yourself in an afternoon for a single provider, or it can grow into a much larger project once you add multi-tenant billing, failover between providers, and tool-use support. Below is the architecture and the code to get a working version running.

Why you need a gateway instead of direct SDK calls

If you only have one app and one developer, calling the Claude API directly is fine. The problems start when you have more than one of any of these:

A gateway solves all of this by giving you one place to issue scoped keys, enforce limits, and collect metadata, instead of duplicating that logic in every client.

Core components of an LLM gateway

1. Key issuance and auth

Your gateway should issue its own application-level keys (not the raw provider key) and map each one to a provider credential, a rate limit, and usage counters. A simple scheme:

sub_live_<random>  →  { provider: "claude", model: "claude-3-5-sonnet", limits: {...} }

Clients authenticate with Authorization: Bearer <your-key>, and the gateway swaps in the real provider credential server-side. The provider key never leaves your infrastructure.

2. Request routing

A route like POST /v1/messages on your gateway maps to the provider's actual endpoint. This is also where you decide which model/provider to call — useful if you want to route by cost, latency, or failover to a backup provider when one is down.

3. Streaming passthrough

LLM responses are often streamed as server-sent events. Your gateway needs to pipe the stream through without buffering the whole response in memory, or you lose the point of streaming.

4. Logging and usage metadata

Every request/response pair should produce a log entry with token counts, latency, model used, and the calling key. This is what lets you build usage dashboards, billing, and rate limits later.

5. Rate limiting and quotas

Per-key limits (requests/minute, tokens/day) protect you from a single bad client burning your whole budget, and they're table stakes if you're exposing the gateway to multiple teams or customers.

A minimal gateway in Node.js

Here's a stripped-down example using Express that proxies to a Claude-compatible endpoint, checks a simple in-memory rate limit, and streams the response back:

import express from "express";
import fetch from "node-fetch";

const app = express();
app.use(express.json());

const keyStore = {
  "gw_abc123": { provider: "claude", limit: 100, used: 0 }
};

app.post("/v1/messages", async (req, res) => {
  const authHeader = req.headers.authorization || "";
  const key = authHeader.replace("Bearer ", "");
  const entry = keyStore[key];

  if (!entry) return res.status(401).json({ error: "invalid_key" });
  if (entry.used >= entry.limit) {
    return res.status(429).json({ error: "rate_limit_exceeded" });
  }
  entry.used += 1;

  const providerRes = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "content-type": "application/json",
      "x-api-key": process.env.ANTHROPIC_API_KEY,
      "anthropic-version": "2023-06-01"
    },
    body: JSON.stringify(req.body)
  });

  res.status(providerRes.status);
  providerRes.body.pipe(res); // passthrough for streaming
});

app.listen(8787, () => console.log("gateway listening on 8787"));

This covers the basics, but a production gateway needs persistent storage for keys and usage (not an in-memory object), proper rate-limit windows, retry/backoff on provider errors, audit logs, and probably multi-model routing. That's a lot of infrastructure to maintain just to make Claude calls safe and observable.

When to build vs. use a managed gateway

Building your own gateway makes sense if you need custom routing logic across many providers or have specific compliance requirements. For most teams that just need to turn their Claude access into a secure, metered HTTPS API without maintaining proxy infrastructure, a managed option is faster.

SubToAPI does exactly this: it gives you sub_live_... application keys, streaming support, tool-use passthrough, per-key usage metadata, and team seats, all on top of your existing Claude access. You get the gateway described above — auth, routing, streaming, logging — without running or patching it yourself. Check the quickstart to see the request format, or the streaming and tools docs if those are the pieces you need. Plans start with a free trial at signup, and pricing details are on the pricing page.

Whether you self-host or use a managed layer, the goal is the same: one well-defined boundary between your apps and the model provider, with keys, limits, and logs that actually tell you what's happening.

questions

Do I need a gateway if I only have one app calling Claude? Not strictly — direct SDK calls work fine for a single app with one developer. A gateway becomes valuable once you have multiple apps, team members, or customers who need separate keys, limits, or usage visibility.

Can a gateway support streaming responses? Yes, but it needs to pipe the stream through without buffering the full response, otherwise clients lose the benefit of streaming and may hit timeouts on long responses.

What's the difference between an API gateway and a load balancer for LLMs? A load balancer mainly distributes requests across endpoints to manage capacity and failover. A gateway does that plus auth, key issuance, usage tracking, and request transformation — it's a superset focused on access control and observability, not just traffic distribution.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →