A gateway for local models: device tokens instead of ssh tunnels

An OpenAI-compatible gateway in front of mlx_lm and Ollama on a Mac mini - a token per device, a round-robin queue and the right to switch models.

My local language models run on a Mac mini with 48 GB of memory: a large one for the home bot (the mlx_lm server) and bge-m3 for semantic search (Ollama). Model servers can't check who is calling them, so they listen only on localhost.

Before, two clients reached them, both through ssh tunnels: the bot from a Raspberry Pi and the editor extension from a laptop. For two machines with an ssh key this works, but for a phone or a third-party app this path is closed.

One service in front of the model servers

Now a small gateway stands in front of the model servers: one service that listens on the network, checks the device token and runs a queue.

phone, laptop, bot  ──►  one name on the network  ──►  gateway  ──►  mlx_lm (bot model)
(each has its token)             (Caddy)                         └──►  Ollama (bge-m3)

To the outside the gateway exposes an OpenAI-compatible API at /v1: for almost any app it is enough to set the address and the key. The code lives in a home repository for now.

What the gateway can do

  • chat with streaming, text completion, embeddings, a list of models;
  • a token per device: the database holds only a fingerprint, the token can't be recovered from the database;
  • adding a device with a QR code: the token, the connection link and the QR are shown once;
  • revoking and reissuing a token;
  • device rights: which models are visible, how many requests at once, whether it may switch the model on the server;
  • a round-robin queue: one request at a time goes to a model, devices are served in turn;
  • per-device accounting: when it called, how many requests and tokens;
  • the gateway does not store the texts of requests and responses and does not write them to the log.

Adding a device

A device is added in the admin panel or from a terminal on the node itself:

npm run device:create -- --name <name> [--allow-model-switch] [--model <model>]... [--max-parallel <number>]

By default a device sees all models, can't switch them and holds up to 4 parallel requests. The command prints the connection link, and the token is shown once.

On the client side you set the API base URL (the gateway address with the /v1 path) and the API key. Whether the gateway is working is visible at /healthz. The client gets 401 if the token is revoked or wrong, and 403 if the device is not allowed the model or the model switch.

The "model switch" right

A model server keeps one large model in memory. If a device asks for another one, the server unloads the bot model and loads the requested one, which takes about 40 seconds. The bot's next request brings its model back, another 20 seconds or so, and the bot is silent all that time.

Two large models don't fit into 48 GB at the same time. The day before the rollout I tried keeping a second one alongside: GPU memory ran out, and the bot model went down for 20 minutes.

So the right is off by default. A device without it sees only the bot model and does not get in the bot's way. The right is given only where another model is really needed, and the bot itself does not need it.

What was verified at rollout

I tested with a separate device, which I then revoked:

CheckResult
A short request to the bot modelresponse in 0.5 s
Streamingfirst chunk after ~150 ms
bge-m3 embeddingsa vector of length 1024
A request for another model without the switch right403, the bot model is not unloaded
A request after the token is revoked401
Gateway logno tokens and no request texts

What is not finished

There is no backup of the database, and it is postponed deliberately. Losing gateway.sqlite means that all devices will have to be issued again.

The "Backends" page judges a server by its list of models and can show "available" when generation does not actually work. Then I have to look at the log of the model server itself. This is a defect.

Old clients still go through tunnels past the gateway, and their requests do not get into the queue.

For a single laptop a tunnel is simpler. The gateway is needed when there are more than two clients or one of them has no ssh.

Tags: Architecture, Local models, Self-hosting

LinkedIn X Telegram

Read next

Comments