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
- Keys spread everywhere. Provider keys end up in laptops, CI systems and scripts. A gateway keeps the real keys in one place and hands out its own.
- Nobody knows who spent what. One shared provider key gives one shared bill. A gateway attributes every request to a key, a team or a customer.
- No limits. A looping agent can spend a month's budget overnight. A gateway can cap spend and request rates.
- Several providers. Each has its own format and account. A gateway gives one interface for all of them.
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:
- The normal case. The stream ends with a usage report and the request is billed exactly.
- The client disconnects early. No usage report arrives. The gateway has to estimate, and should mark the request as estimated.
- The provider fails mid-stream. The response started as a success and then carried an error. The gateway has to notice that, record a failure and charge nothing.
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
| Option | Good when | Cost |
|---|---|---|
| Build your own | You have unusual requirements and engineers to maintain it | Weeks to build, and ongoing work on billing and streaming edge cases |
| Self-host an open-source gateway | You must keep traffic inside your own network | Servers, upgrades and your own provider accounts and bills |
| Use a hosted gateway | You want one key and one bill today | A 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
- List your models and formats. Which models, and do your tools need Chat Completions, Responses or Claude Messages?
- Create one key per app or environment. Production, staging and each developer tool get their own.
- Set a spend limit on each key. Especially anything that runs unattended.
- Point clients at the gateway. Change the base URL and key. The setup wizard has the exact settings for common tools.
- Send test requests. Streamed and non-streamed, one per model you plan to use.
- Check the log. Confirm each request shows the model, tokens and cost you expect.
- Test the failures. A wrong key, an unknown model and an empty balance should each give a clear error.
- 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 ↗