Infrastructure · 2026 · Jasa
One gateway for every AI call
The site, the English tutor and the email assistant all call models. I didn't want keys scattered around, so there's one entry point, and I can see every call.
Why add this layer
With more apps come more problems: keys scattered around, several places to change for a new model, and no clear view of who used what. My answer is to point every app at one OpenAI-compatible address, api.xmc.hk, and leave everything else to the gateway.
Small and sufficient
The stack is simple: FastAPI, httpx and one SQLite file. A personal project makes few calls, so nothing heavier is needed. The care goes into these boundaries:
- One key per app, each with a daily quota. If one app misbehaves, stop its key without affecting the rest.
- The database stores only SHA-256 hashes, never plain keys. Even if the database leaks, no usable key leaks with it.
- API and admin entrances are separate. Two processes on two ports, and the admin panel sits behind an email code.
The upstream will not tell you the usage
Only after finishing the dashboard did I find that my upstream does not return usage. Adding include_usage to a streaming request changes nothing. So I estimate: about one token per CJK character, and one per four other characters. Estimates are flagged separately and shown as ≈, never mixed with real numbers.
Try it. This uses the same formula as the gateway:
A small trap
During the switch, one service kept getting 403 even though its key was right. Cloudflare in front of the gateway was blocking Python's default Python-urllib User-Agent, so the requests never reached the gateway. Giving them their own User-Agent fixed it. Next time I see a 403, I'll first check who sent it.
