AI & Agents · Data Science & ML · Security & Compliance
Mosaic BI: AI Analyst Team on MoE, RLHF + RLVR and an OWASP AI Gateway
By David Gadyan · Published
An AI business-intelligence department. A manager asks "why did our revenue fall this quarter?" and a team of analyst experts, chosen by a Mixture-of-Experts router, investigates through an AI Gateway mapped to the OWASP Top 10 for LLM applications. Every number comes from governed SQL, every finding is checked by deterministic verifiers and cited to evidence, and the revenue change reconciles to a residual of 0.00. A continuous-learning loop turns reviewer preferences into RLHF data and the same verifiers into RLVR rewards for per-expert QLoRA adapters. FastAPI, LangGraph, DuckDB, Next.js, Ollama MoE models, Claude, TRL. 351 automated tests.
The problem
Managers ask questions such as "why did our revenue fall 18% this quarter?". Answering well takes a data analyst, a finance analyst, marketing and sales people and a statistician, and a few days. A chatbot over the same data answers in seconds, but it can invent a number, run a query it should not, or leak one client's data into another's answer. Mosaic BI is an AI analytics department that gives the fast answer with the discipline of the real team: numbers only from governed SQL, every finding checked, and every model call through one security gateway.
The product in use
What I built
An executive overview where every KPI is computed by SQL. Net revenue, gross profit and margin, orders, new customers and average order value, with trends, region and segment breakdowns, top products and alerts. The language model never produces a number on this page.
Ask in plain English. The manager types a question and picks a mode: auto (the router chooses the analysts), manual, review (the plan waits for approval) or deep. Suggested questions come from the approved datasets and metrics.
A team of analyst experts, routed by Mixture of Experts. The investigation runs as a LangGraph state machine: clarify, plan, execute, report. A business-role router chooses the minimum set of experts for the question and the data that exists (marketing runs only if a spend metric exists) and builds a dependency graph: the data analyst first, then finance, sales, customer and marketing in parallel, then the statistician and the executive advisor. Each expert has its own model, tools, permissions, step and token limits, and version history.
Findings that cite their evidence. Each expert runs a deterministic analysis pack whose SQL goes through the gateway and is stored as evidence, then makes one bounded model pass that must return a strict schema. Verifiers then check that every number exists in its cited evidence, that units are right and that no causal claim is made without support. Findings that fail are dropped.
A revenue bridge that adds up. Net revenue = gross sales − discounts − refunds, and gross sales = orders × average order value. The gross change is split with LMDI, which is exact with no interaction term, and the residual is always shown. On the sample company the change of −22.96% splits into average order value −17.10 pp, order volume −6.09 pp, refunds −0.68 pp and discounts +0.92 pp, residual 0.00. The statistician adds Welch and Mann-Whitney tests, two-proportion z-tests with Holm correction, a seasonality reference and a Simpson's-paradox check, and the report states that the split is an accounting decomposition, not causal proof.
Architecture design
Hover or tap a component to light its path and see what it does.
Mixture of Experts, in two layers
The platform uses MoE at two levels and never mixes them up:
- Layer A, business-role routing. A deterministic, auditable router picks which analyst roles work on a question, like a gating network over people's jobs. Independent experts run in parallel.
- Layer B, neural MoE models. Open MoE models such as DeepSeek-V2-Lite (64 routed experts) and Qwen MoE are served locally through Ollama, with Claude as an API option behind the same gateway. The MoE Lab lists each model's provider, quantisation, context, number of experts and active experts per token, benchmarks latency, tokens per second and VRAM, and shows expert-routing heatmaps only when real router telemetry is collected from the GPU host. Otherwise it says the telemetry is unavailable, and it states that neural experts are not the business roles.
RLHF and RLVR: a continuous-learning loop
The analysts are built to get better from real use without losing trust:
| Stage | What happens |
|---|---|
| Capture | Every run stores its verifier results. Reviewers rate findings, correct them, and compare two candidate answers side by side, judged on the evidence, not on how persuasive the text is. |
| RLHF data | Preference pairs need a majority vote and no factual issues; inter-rater agreement (kappa) is tracked. They train the experts with DPO. |
| RLVR rewards | The same deterministic verifiers that gate answers at runtime become the reward: SQL safety, cited evidence exists, numbers match the evidence, the decomposition reconciles, statistics are correct, no unsupported causal claims. A hard failure scores 0. GRPO optimises against these rewards. |
| Curate | Only fully verified runs with no negative feedback become SFT examples. Data is de-identified, re-scanned for personal data, de-duplicated and approved by an admin before export. |
| Train | One QLoRA adapter per expert on a shared base model, on a single RTX 4090: SFT, then GRPO with the verifier rewards, then DPO on the preference pairs. |
| Promote | A model registry keeps hashes, datasets and configs. An adapter must beat the prompt-only baseline with no safety or factuality regression, then goes to a canary share of investigations, then to everyone, with one-click rollback. |
Training Studio shows the loop and the RLVR pass rate per verifier and per role.
The training pipeline is complete and tested (110 tests on CPU; the GPU tests skip without a GPU). The first SFT, GRPO and DPO runs on the RTX 4090 are the next step, so no trained-adapter results are claimed here.
Security: an AI Gateway mapped to the OWASP Top 10 for LLM applications
Every model call, tool call and SQL query goes through one gateway that denies by default. Everything uploaded, returned by a tool or written by a model is treated as untrusted.
| OWASP LLM risk | How Mosaic BI handles it |
|---|---|
| LLM01 Prompt injection | Every context block carries a trust label and untrusted blocks are wrapped in delimiters. Injection signals (including Unicode tricks and base64) are recorded, but authorization is checked again on every tool call, so content cannot change what an agent may do. |
| LLM02 Sensitive information | Each workspace has its own DuckDB file; membership is checked on every request; restricted columns are never given to agents; row filters are added to SQL; DLP redacts model context and output. |
| LLM03 Excessive agency | Only narrow read-only tools. A tool must be registered and allowed for the role and the agent, and its arguments must match a closed schema. There are no write, admin or shell tools. |
| LLM04 Supply chain | Pinned dependencies, adapter artifacts registered with a hash, tool versions pinned in the registry. |
| LLM05 Data and model poisoning | Uploaded data is profiled and quarantined until approved; training data needs verified runs, majority-vote preferences and admin approval. |
| LLM06 Unbounded consumption | Token, step, tool-call and cost budgets per run, context caps, rate limits, SQL timeouts and row caps, sandbox resource limits. |
| LLM07 Misinformation | Numbers come only from SQL, each finding's value must exist in its evidence, causal language and units are checked, the residual is reported and statistics use Holm correction. |
| LLM08 System prompt and secret exposure | Secrets live only in HashiCorp Vault; APIs return status, never values; keys and tokens are redacted from logs and audit. |
| LLM09 Vector and embedding weaknesses | Retrieval is switched off by policy in this version; the policy requires access-control filtering before it can be enabled. |
| LLM10 Improper output handling | SQL is parsed with sqlglot against an allowlist (no DDL, DML, ATTACH, COPY, PRAGMA or table functions); the UI renders safe markdown with a strict CSP; spreadsheet exports escape formulas. |
Other controls: a SHA-256 hash-chained audit log that can be verified, fail-closed behaviour when a provider or secret is missing, and session revocation. The security suite has 30+ cases across LLM01 to LLM10, including 15 SQL attack payloads.
Testing and status
351 automated tests: 47 backend tests (an end-to-end investigation checked against a hidden ground truth, the dashboard agreeing with the investigation, the security suite, the RLHF-to-export loop), 194 verifier tests including reward-hacking cases, and 110 training-pipeline tests. The frontend passes lint, type checks and a production build. The recorded runs used the offline deterministic provider, which gives the same numbers as Claude because numbers never come from the model; only the narrative wording changes. The sample company, Meridian Goods, is synthetic: 18 months of data in nine related tables.
Related projects
NexusCall: Multi-Tenant AI Voice Call Center & CRM Platform
AI & Agents · AI ERP & SaaS · Security & Compliance
Jev AlphaTrading: Crypto Trading Research with Decision Models
AI & Agents · Finance & Quant · Data Science & ML
Private AI Gateway: Secure, Self-Hosted LLM Access for Every Team
AI & Agents · Security & Compliance · Cloud, DevOps & LLMOps