Private AI Gateway
An AI gateway for small businesses that decides, request by request, whether data may leave the company. When personal data is involved, the answer comes from a model on the company's own server.
- CSR Online
- Designed, built and run by Adrián Vázquez, on his own
In production
Since November 2026
524 functional checks
- 96% of questions with the right document ranked first
- 45/45 answers correct and cited
- 5/5 isolation tests between companies
- 80 ms p95 search latency
Measured 12–24 November 2026, on invented test data.
- LiteLLM
- LibreChat
- Presidio + spaCy
- PostgreSQL + pgvector
- Ollama
- bge-m3
- FastAPI
- Docker
- Caddy
The situation
In a small business, contracts, payslips and customer records end up pasted into public chatbots because it's the quickest way to get an answer. The company doesn't know what has left or who got it; nobody sees what it spends on AI; everyone uses their own tool, with no central accounts or access log; and a generic chatbot doesn't know the company, so it invents deadlines, prices or terms the company doesn't offer.
The hard constraint was hardware. The private models run on CPU, with no GPU, and two generations running at once on the same CPU each drop from about 20 to 0.3 tokens per second.
The task
One platform for several companies, where their rules are enforced by the server on every request rather than by a written policy that depends on each person: before anything goes to an external model, the server looks for personal data and, if it finds any, the private model answers; spend is capped per company and per person, accounts are managed in one place, and answers come from the company's own documents, cited, or as an “I can't find it”.
With three guarantees: the server decides (nobody can remove an assistant's instructions, change their company's key or jump the queue), it fails safe, and no text goes into the logs.
How it works
One chat request
- Caddy → LibreChat
Browser → Caddy (HTTPS, the only public entry) → LibreChat, which assembles the request in a fixed order: shared rules, the company profile, the assistant's instructions, then the date and the user.
- LiteLLM
LiteLLM gateway: the company is a LiteLLM team (monthly cap) and each person an end user (models, cap and rate limit). A request over any limit stops here and reaches no model.
- PostgreSQL + pgvector
Knowledge base, only in assistants that use the documentation: hybrid search in that company's documents and, if it finds something, the request is pinned to a local model.
- Presidio + spaCy
Personal-data detector, only when an external model was asked for: it checks the whole request body, tools and their descriptions included.
- LiteLLM
One local queue with priorities, ageing and round-robin across companies and people.
- Ollama
Private model on the model server, inside the internal network, streamed.
- LiteLLM
Optional external model, only for text with no personal data, without the private part of the company profile or the employee's name.
- Ollama
From a document to a cited answer
Upload: the console checks each file's real type from its content, its size, duplicates and the quota.
Ingestion: each file is opened by a child process with no credentials and limited memory and time; scanned pages go through the local OCR, always behind the chat in the queue.
Chunks of about 350 tokens, each with its bge-m3 vector, saved in one transaction per document.
Search: vectors plus Spanish full-text (accents optional), fused with RRF, under row-level security per company.
Answer from a local model: the passages arrive as data, between random per-request markers, each with its citation ready.
Six internal networks, separated by relationship: only the proxy is published (443), only the gateway has a way out, the detector and the embeddings can only be reached by the gateway, and the chat has no route to the internet; automated tests check it on every run.
Each company sees only its own data: row-level security, one LibreChat group per company bound to its key, and anything belonging to another company returns 404. Usage is logged without the text.
How and why
D1 A detector decides where each request goes.
Before anything is sent to an external model, a Spanish-language personal-data detector (Presidio with spaCy) checks the whole conversation. Anything carrying personal data, an image or more than 200,000 characters goes to the private model, and so does everything while the detector is down.
Rejected: spaCy's English model, which tagged “Mi DNI es” (“my ID number is”) as a person's name.
D2 One queue for the local models.
Two generations at once on the same CPU each run about seventy times slower, so the model server takes one request at a time. Chat first, then OCR, then indexing; a waiting request moves up a level every minute, turns rotate across companies and people, and priority comes from each service's key, never from the client. I modelled the queue as M/G/1 with non-preemptive priorities (Cobham) and simulated it with the real scheduler.
The cost: round-robin shares turns, not waits: under saturation the chat's p95 sits above a pure first-come order (34% higher, estimated). It's the price of one large company not hogging the server.
D3 One LibreChat group per company.
Each group sends its company's key to the gateway, and a user with no group falls back to a blocked key: they can open the chat, but spend nobody's budget.
Rejected: LibreChat's native tenants, experimental and needing a domain per company.
The cost: a group change takes up to 60 s to arrive.
D4 Embeddings off the model server.
A fourth model loaded there would evict one of the chat's, with 40–70 s reloads. text-embeddings-inference computes them with bge-m3 on the platform server, and they live in the same PostgreSQL (pgvector), so the daily backup already includes them.
D5 A dedicated OCR model.
On bad scans, GLM-OCR read 59 of 65 fields at 4–11 s a page, against 46 of 65 at 15–26 s for Gemma 4, the vision model already loaded.
The cost: it can mix up the decimal comma and point on a blurry scan, so the assistant gets a warning on every scanned page.
D6 No high reasoning on the local models.
On CPU, “Deep” multiplied the time by 5 and made everyone wait, and “Normal” is already slow: a percentage problem took 10 s on “Fast” (the default) and 167 s on “Normal”, with the same correct answer.
D7 No reranker.
I measured it: it added zero points of recall in the top three and 14–20 s per question on CPU. Hybrid search already put the right document first for 96% of questions.
Also: every image is pinned by digest, and the gateway's cosign signature is verified before each upgrade and on every test run.