It is getting more and more common to have projects that use AI. We used to make free solutions and pay a little bit from our own pocket for hosting, but when it comes to AI, either you need a bottomless pocket or you need to find another solution. This is exactly the challenge I had with FreeChessCoach, and I solved it in a very interesting way that I want to share with you.
If you want to keep your service free and still let people use AI, you basically have 2 options:
- Bring your own key (BYOK), where people bring a key (for example OpenAI or OpenRouter) and pay for their own usage.
- Use a locally run LLM (Ollama, LM Studio). This seems impossible at first, but hear me out, I found the solution.
“The user’s localhost cannot be reached from your server, that is common knowledge!”
Well, not quite. And I figured it out by accident while I was doing something totally different.
Here is the short story: I was trying to use a free chess engine API (chess-api.com) from my backend, and I found out it is rate limited per user (IP). That means my backend counts as one single user, while many people are actually using the service. So I was thinking, what if the user calls it with their own IP and passes the result to the backend? That is possible with a tunnel. When I got that working, a new question came up: hmm, can we reach localhost the same way? And you get it.
Here is how it is done.
The browser is the bridge
My server cannot reach your machine, but your browser tab can, and your tab is already talking to my server. The tab simply passes the call along:
- Request
- Queued work
- Event
- Data access
All 9 components, as text
- Your browser tab tunnel handler · just a pipe
The only thing that can reach both sides: it is connected to the server, and it runs on your machine.
- Gets a request with a requestId over the WebSocket, runs a fetch, sends the answer back with the same requestId.
- For a stream, every SSE data: line goes back as its own frame.
- Shows a clear error when CORS or a stopped server blocks the call, because the browser reports both as the same bare TypeError.
- Local LLM LM Studio :1234 · Ollama :11434
A model on your own computer, with an OpenAI-compatible /chat/completions endpoint.
- Must allow the site's origin: "Enable CORS" in LM Studio, OLLAMA_ORIGINS in Ollama.
- Answers one request at a time.
- The setup test reads the loaded model's context window and warns below 16k tokens.
- chess-api.com engine API · rate limited per IP
Where the idea started: from my server, every player looked like one user.
- Called from the player's tab, so the rate limit is theirs, not the server's.
- The tab only fetches this one allowed host.
- Coach agent AI SDK · same coach for cloud and local
Calls doStream or doGenerate like with any provider. It does not know its model is a tab on a laptop.
- Local model adapter createLocalModel
Wraps the tunnel as a normal AI SDK model.
- Thinking off by default; a server that rejects the thinking fields gets requests without them from then on.
- Lighter prompt for small models: one-line tool cues and plain moves instead of the annotated game.
- Timeouts: 10 minutes for a whole answer, 3 minutes of silence in a stream.
- Call queue LlmCallQueue · one call per user
A local server answers one request at a time, so the API queues them per user.
- The coach streaming to you goes before background work.
- No queued call burns its timeout while it waits.
- The local server keeps one prompt prefix in its KV cache instead of switching.
- Tunnel registry /api/tunnel · per-user sockets
Knows every open tab of a signed-in user and sends work to the one used most recently.
- Background tabs get throttled, so work follows the tab you are using. The others show "Active on another tab".
- Sockets are bound to the authenticated user, never an ID the client sends.
- Background calls summaries · worker jobs
Model calls nobody is waiting for. They join the same queue, behind the coach.
- Worker calls come over an internal relay to the API process, where the queue lives.
- Engine pipeline positions to analyse
The first user of the tunnel: engine work done in the browser or from the player's IP.
Click (or tap) a box to see what it does, or use the buttons above the map to walk through a request step by step.
Every signed-in tab keeps one WebSocket open to my server. When the coach needs the model, the server sends a message down that socket with an id and the question. The tab calls the local server with a normal fetch to /chat/completions and sends the answer back with the same id. No agent to install, no port forwarding, nothing.
The nice thing is that the tab is only a pipe. All the logic stays on the server. On the server side, the local model is wrapped as a normal AI SDK model, so the coach does not even know the model is running on someone’s laptop. Same coach for cloud and local.
The idea in code
Forget my codebase for a moment, the idea fits in two small pieces of code. This is simplified, with no error handling, just to show how it works.
In the browser: keep a WebSocket open to your server. When a question comes in, ask the local model and send the answer back.
// Runs in the user's browser
const socket = new WebSocket('wss://your-app.com/tunnel');
socket.onmessage = async (event) => {
const { id, messages } = JSON.parse(event.data);
// The browser CAN reach localhost (Ollama in this example)
const response = await fetch('http://localhost:11434/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: 'llama3.2', messages }),
});
const answer = await response.json();
socket.send(JSON.stringify({ id, answer }));
};
On the server: when you need the model, send the question down that user’s socket and wait for the answer with the same id.
// Runs on your server (Node.js with the ws package)
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
const pending = new Map(); // id -> the function waiting for that answer
wss.on('connection', (socket) => {
socket.on('message', (data) => {
const { id, answer } = JSON.parse(data);
pending.get(id)?.(answer);
pending.delete(id);
});
});
function askLocalModel(socket, messages) {
const id = crypto.randomUUID();
return new Promise((resolve) => {
pending.set(id, resolve);
socket.send(JSON.stringify({ id, messages }));
});
}
// Anywhere in your backend:
const answer = await askLocalModel(userSocket, [{ role: 'user', content: 'Explain this move' }]);
That is the whole trick. For your backend, askLocalModel is just an async function that returns an answer. It does not care that the model is running on someone’s laptop.
The id is the important part: the server can have several questions on the way, and the id is how it knows which answer belongs to which question. Streaming is the same idea: the browser sends every piece of the answer with the same id, and a done at the end.
In a real app you also need to find the right socket for the logged-in user, add timeouts, and show a good error when the local server is not running. If you want to see how I did all of that, FreeChessCoach is open source on GitHub. Start with tunnel-llm-handlers.ts for the browser side and unified-tunnel-registry.ts for the server side.
The things that bit me
The happy path fits in the code above. The rest is where the real work is.
CORS. The local server has to allow your site’s origin, otherwise the browser blocks the call. In LM Studio that is “Enable CORS”, in Ollama it is OLLAMA_ORIGINS. What makes it annoying is that a CORS block, a refused connection and a stopped server all look exactly the same to the browser, one bare TypeError. So the error message does not guess, it lists the likely causes and tells you what to switch on.
Which tab answers? People open the app in three tabs. A tab in the background can be throttled or frozen by the browser and never answer, so the tunnel follows the tab you used most recently. The other tabs show “Active on another tab” with a button to take over.
One call at a time. Your laptop is not a GPU cluster. LM Studio and Ollama answer one request at a time anyway. So the server queues calls per user, and the coach talking to you goes before background work like session summaries.
Small models are small. A quantized model on a laptop is not Claude or GPT, so a local setup gets a lighter prompt. Tool descriptions are cut to one line, which removes about a third of the system prompt, and the annotated game (Nxd5?? (lost ~277cp, best Nb4; ...)) becomes plain moves with a simple word like “mistake”. The detailed version was actually confusing small models about basic facts, like who won the game. Thinking is off by default, and the setup test warns you if the model has less than 16k tokens of context.
Where it stops
To be honest, it is not magic. The tab must be open, your computer must be on, and the coaching is only as good as the model you run. But that is a fair trade: no key, no bill, and your conversations never go to an AI provider.
Final thought
We are used to thinking the server is where the power is and the browser is just a screen. Here it is the other way around: the server knows what to ask, and the browser knows where the model lives.
If your project needs AI and you want to keep it free, do not bring the model to the server. Let the browser carry the question to the model.
