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.

FlowJasa AIEmail AIGatewayAuth · Quota · LogsUpstream modelUsage dashboard
One key per app. Changing models, limiting quota and checking usage all happen in this middle layer.

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:

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:

Estimated tokens. Calculated only in your browser and sent nowhere.

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.

← All notes