Guides · 5 min read · Updated

AI API Gateway: Features, Benefits & Setup Checklist

A guide to AI API gateways: authentication, routing, usage metering, limits, observability, and security considerations.

Illustration for the guide: AI API Gateway: Features, Benefits & Setup Checklist
In this guideWhat an AI API gateway does, the eight features that matter, how streaming and billing interact, whether to build or buy, and a checklist for setting one up.

What a gateway is

An AI API gateway is a service that sits between your applications and the model providers. Applications send their requests to the gateway. The gateway decides who is allowed to send what, forwards the request to the right provider, and records what happened. To the application it looks like a normal model API. To the people running it, it is the one place where access, cost and reliability are managed.

Ordinary API gateways do similar work for web services, but AI traffic has needs they were not built for: responses that stream for minutes, billing that depends on token counts inside the response, and requests whose cost is unknown until they finish.

Why teams add one

The eight features that matter

1. Authentication

The gateway issues its own keys and holds the provider credentials itself. Keys should be stored hashed, shown once, and easy to disable or delete. A leaked gateway key can be revoked in seconds without touching anything else.

2. Routing

The gateway maps a model name to a provider and endpoint. Good routing also supports the formats your tools use: OpenAI Chat Completions, OpenAI Responses and Claude Messages.

3. Metering

Every response is read for its usage report: input tokens, output tokens and, where they exist, cached and reasoning tokens. This is the basis of all cost reporting, so it has to be exact, including on streamed responses.

4. Limits

Requests per minute per key, a spend cap per key, and a balance or budget per account. Limits turn a possible disaster into an error message.

5. Billing

If you charge for usage, internally or to customers, the gateway needs a ledger: a balance per account, a price per model, and a record of every charge.

6. Observability

A per-request log with time, model, key, status, tokens, cost, duration and a request ID. When someone asks "why did this cost so much?" or "why did this fail?", this is where the answer is.

7. Error handling

Providers fail in different ways and with different messages. A gateway should turn these into a small, stable set of errors that clients can act on, and should never charge for a request that failed.

8. Security

Encrypted connections, no logging of secrets, and a clear rule on whether request content is stored at all. Most gateways should store usage data and not prompts.

Streaming changes everything

Most AI responses are streamed word by word. A gateway has to pass the stream through without delaying it, and at the same time read it for the usage report, which arrives at the very end. That leads to three cases worth planning for:

Buffering a whole response to make this easier is not an option: it turns a live stream into a long wait and breaks interactive tools.

Holding and settling

With prepaid balances there is a second problem: the cost of a request is unknown when it starts. If the gateway only charged at the end, a customer with one cent could start a hundred expensive requests at once.

The standard answer is a hold. When a request arrives, the gateway estimates its cost from the size of the input and the maximum output, and sets that amount aside. When the request finishes, it charges the exact cost and releases the rest. If the request fails, the whole hold is released. Holds that are left behind after a crash need to be released on restart.

Build, self-host or buy

OptionGood whenCost
Build your ownYou have unusual requirements and engineers to maintain itWeeks to build, and ongoing work on billing and streaming edge cases
Self-host an open-source gatewayYou must keep traffic inside your own networkServers, upgrades and your own provider accounts and bills
Use a hosted gatewayYou want one key and one bill todayA dependency on the service, and your requests pass through it

A hosted gateway that also resells capacity, as CheapTokens does, adds one more benefit: you don't need accounts with the model providers at all, and prices can be below list.

Setup checklist

  1. List your models and formats. Which models, and do your tools need Chat Completions, Responses or Claude Messages?
  2. Create one key per app or environment. Production, staging and each developer tool get their own.
  3. Set a spend limit on each key. Especially anything that runs unattended.
  4. Point clients at the gateway. Change the base URL and key. The setup wizard has the exact settings for common tools.
  5. Send test requests. Streamed and non-streamed, one per model you plan to use.
  6. Check the log. Confirm each request shows the model, tokens and cost you expect.
  7. Test the failures. A wrong key, an unknown model and an empty balance should each give a clear error.
  8. Plan a fallback. For critical paths, know what you switch to if the gateway is unavailable.

How CheapTokens covers this

CheapTokens is a hosted gateway with these pieces built in: a personal API key for each account, Chat Completions, Responses and Claude Messages endpoints, exact per-request metering, prepaid balances with hold-and-settle billing, a full request history with request IDs, and no charge for failed requests. Request and response content is not stored. The docs cover the endpoints, errors and limits.

Create a CheapTokens account ↗

Related guides