· 4 min read
One API, many models: building a bring-your-own-token LLM gateway
Why we put a single API in front of multiple LLM providers, let customers bring their own keys, and what that taught me about cost, latency and accuracy.
At IT Together we built an AI microservice platform that gave developers access to multiple large language models through one unified API. It had a bring-your-own-token architecture: customers could plug in their own provider API keys instead of paying through us.
That design came from real conversations with clients, and I think it is a pattern more teams should consider. Here is why we built it and what I learned along the way.
The problem: every team was rebuilding the same thing
Different teams and clients wanted different models. One wanted the strongest model available for complex reasoning. Another needed something cheap and fast for high-volume classification. A third had a policy that data could only go to a specific provider.
Without a shared layer, each project wired up its own provider SDK, its own retry logic, its own way of handling streaming, its own way of tracking cost. Switching models meant rewriting code. Comparing models meant building the comparison from scratch each time.
A gateway solves that once. Applications talk to one API. The gateway talks to the providers.
Why bring your own token
The "bring your own token" part was about three things clients kept raising.
Cost control. When usage runs through the customer's own provider account, they see the bill directly, set their own limits, and use any discounts or credits they already have. There is no markup to argue about.
Data privacy. Many organisations already have an agreement with a particular provider covering how their data is handled. Using their own key keeps requests under that agreement.
Model flexibility. If a better model comes out next month, a customer can switch to it without waiting for us to negotiate anything.
For us, it also removed a risk: we were not carrying every customer's usage costs on our own account.
What the gateway has to normalise
Providers look similar from a distance. Up close, they differ in lots of small ways. The gateway's job is to hide that.
- Request and response shapes. Messages, system prompts, tool calls and parameters are named and structured differently. We defined one internal format and translated at the edges.
- Streaming. Everyone streams tokens, but the event formats differ. Applications should receive one consistent stream regardless of the provider underneath.
- Errors. Rate limits, overloaded servers, content filters and bad requests all come back differently. Mapping them into a small set of clear error types makes retries and user-facing messages much simpler.
- Usage. Token counts are reported differently. Normalising them is what makes cost tracking possible.
Handle keys like the secrets they are
Accepting customers' API keys means you are holding something valuable. A few rules we treated as non-negotiable:
- Encrypt keys at rest and decrypt only at the moment of the request.
- Never write keys to logs, error messages or analytics. Check this, because it is easy to leak a key inside a logged request object.
- Scope every key to the customer and project it belongs to, so one tenant can never trigger usage on another's key.
- Let customers rotate or revoke keys themselves.
Choosing a model is a three-way trade
A big part of my role was LLM selection and optimisation across providers. Every choice came down to balancing three things: cost, latency and accuracy.
The mistake I saw most often was starting from "which model is best?" There is no best. There is best for this task, at this volume, with this response time.
What worked was making the comparison concrete:
- Build a small evaluation set from real examples of the task.
- Run it through several candidate models via the gateway, which makes this cheap because the code does not change.
- Score accuracy on what actually matters for the task.
- Record the average and worst-case latency, and the cost per thousand requests.
Often the result was surprising. A smaller, cheaper model would be good enough for most requests, and the expensive one only mattered for a hard minority. That leads naturally to routing: send easy work to the fast model, hard work to the strong one.
What I would do the same again
Putting one API in front of many models was one of the most useful pieces of infrastructure we built. It made experiments cheap, kept applications simple, and meant model decisions could be made on evidence rather than habit.
If you are building more than one LLM feature, build the gateway early. Your future self, and whoever has to switch models next quarter, will thank you.
For the other half of the story, getting good answers out of your own documents, see what building a RAG platform taught me about hallucinations.