Eslam Hamed
Technical Lead & AI Solutions Architect
Building enterprise AI systems, cloud platforms, and production-ready software
Case study ·
An AI analyst that tells you how far to trust each number
A conversational analyst over PostgreSQL, MySQL and SQL Server, in Arabic and English. The model plans and explains; a database or plain code computes every number, a second model audits each query, and a person can certify an answer into a receipt.
The client wanted a conversational assistant that behaves like a senior data analyst: an executive asks in plain language and gets a useful answer, not a dashboard. The conversation was never the hard part. The hard part was making a fluent model safe in front of people who act on its numbers, and keeping it safe once it met databases nobody had prepared for it.
Context
The audience is senior, and the system is headed for operational data that people run a business on. One invented figure in front of a board ends the engagement. The data must also stay in the country: the brief was a sovereign deployment, on a model the client hosts itself. So the design could not count on a frontier model being careful, and could not trust any model with arithmetic.
The constraint
Language models are fluent with numbers and unreliable at producing them. Catching a fabricated figure after the fact is a bet that the reviewer is never wrong. So the first rule is structural. The model plans, queries and explains. It never calculates. A database engine or plain, tested code produces every figure; the model says what the figures mean.
First answer: an engine with receipts
The first version enforced that rule in the strictest way available. The model could reach only a registry of typed capabilities: hand-built, parameterised queries, run under the database's own row-level security. A deterministic engine computed each figure and minted a receipt for it: a content-hashed, tamper-evident record of what was computed, over what, and when. The model wrote the sentence, and a mechanical check compared every numeral in it with the engine's figures before anyone saw it.
It worked, and it had a ceiling. Every new question needed a new hand-built capability, and every new database its own set. A product that answers only the questions its engineer anticipated is a dashboard with better manners.
Second answer: any database, and a ladder of trust
The second version keeps the rule and removes the ceiling. Connect a PostgreSQL, MySQL or SQL Server database with a read-only credential, and minutes later the assistant answers questions nobody modelled in advance. It stays safe because an answer is never one model's unchecked work, and never claims more certainty than it has.
- The model states its intent, then drafts one query. The intent is in business words, apart from the SQL, so there is something independent to check the SQL against.
- An auditor checks the query against the intent. A second model, by default from another vendor, accepts the draft or rejects it with a reason. A rejected draft is not run. After a second rejection the assistant asks, or says it cannot answer; it does not serve a figure the control turned down twice.
- A gate admits the query on its parsed form. Each dialect has its own gate, built on a real SQL parser: exactly one read-only SELECT, only tables pinned when the database was connected, a row cap, and no value in the result that the data did not produce.
- Code works out the facts. Totals, shares, the highest and lowest, the period still running, placeholder dates, a projection with its range: all computed beside the rows and handed to the model. An early version got every number right and still called the wrong month the lowest, so comparisons come from code too.
- The answer names its read and wears its label. It must cite the read behind its figures, and that read's rows are what the page charts. Everything on this path is labelled exploration: a best effort over a source nobody has certified.
Exploration is the first rung of a ladder. The second is certification: any answer's query can be saved as a verified metric. The model drafts candidates, a person reads the full SQL and certifies it, and from then on the engine re-runs that exact frozen text and mints a receipt on every run. A verified metric is named with what it is fixed to, "orders placed in fiscal year 2026" and never just "orders placed", and a question that adds a filter drops back to exploration and says so. At answer time the model is no longer in the loop.
What an answer looks like
The company in these screens is not real. Tall Order Farms is an invented vertical-farming business with three small databases, one on each engine the product reads. The interface is real, every query shown passed the product's own gates, and every chart, forecast and note came from its own code.
Asked what to expect in December, the model does not guess. Code projects the figure from the last three complete months, takes the range from how far the last twelve spread, and leaves the running month out. The chart draws the forecast only because the answer states the figure code projected.
The auditor earns its seat
Ask for "fiscal year 2026" and a plausible query filters the order date to calendar 2026. It runs, it returns a number, and the number is wrong: 4,642 orders where the right answer is 5,262. Here the auditor rejects that draft before it touches the database, and says why. The second draft passes.
Honest about what the data cannot say
A count over a condition silently ignores the rows where that column is empty. When a large share is empty, code counts those rows with its own fixed query, and the answer must say how many it could not check.
An unusual figure is explained by what the data shows, never by a guessed cause. A spike gets its day-by-day breakdown from code. Asked why, the assistant reports which farm, which crop, and that no reason was recorded. It does not offer a campaign, a season or a data import, because the data shows none.
Charts come from code too
The model never chooses a chart. The server reads the result's shape: which column is a period, which numbers genuinely add up to a whole, whether the last period is still running. Only then does the page offer a line, columns, a donut or a stack. An average is never stacked, and a top five is never drawn as a share of everything. "Why this answer?" lists what the cited read touched.
From an answer to a receipt
Verified metrics are where the first version's guarantee returns, now for any database. The drafts are the model's, the decision is a person's, and the number is the engine's.
Three engines, one way of asking
PostgreSQL, MySQL and SQL Server each get their own gate, reader and traps: the SQL Server gate refuses a two-part table name, because on that engine the connection is not a boundary. Connecting a database is a paste and a wait: a read-only credential, a scan that reports its progress table by table, and a first answer minutes later. Access comes from the client's MCP gateway, so their IT team owns one door.
Arabic first, inside someone else's product
The same assistant embeds in a customer's own web application. It signs people in with that application's token, takes its colour, opens in Arabic and right to left, and scopes every read to the signed-in person's company. Inside a customer's page no SQL travels to the browser at all. Column names, chart labels and saved questions follow the reader's language; figures stay in Western digits, so a number reads the same in the sentence and in the chart.
Running it
The system runs on the client's Kubernetes platform, on its 37th release since late October. In November the self-hosted model's server changed its behaviour: every request that enforced the final answer's JSON schema ran to the token limit, so each turn took close to five minutes and failed. I reproduced it from inside the running pod, found that the same format given in words came back valid in five seconds, and shipped that as a per-model setting the same day. Then came the thing that should have caught it: a health check that runs one real turn through the live model every fifteen minutes and raises an alert on two misses.
Decisions worth reading
One hundred and thirty architecture decisions are recorded in the repository. Beyond the ones above, these carry the most weight:
- A wrong fact is worse than none. A note written by code speaks only when the rows prove it. Before the standout-day note shipped, it ran over 32 date columns in the test databases: 29 notes, each recomputed by a separately written query, all correct, and every query shape that should stay silent did.
- Somebody must be able to win an honest disagreement. The auditor sees one turn; the analyst sees the conversation. So the analyst may formally disagree, and then the read runs with the objection attached: shown, not buried. An auditor outage fails open, visibly and only once.
- Two tiers of answer, never blurred. A verified answer wears its receipt, an exploratory one says it has none, and a mixed answer never claims to be verified.
- Receipts hold the figure and its hashes, never the underlying rows. A number can be re-checked later without the evidence store becoming a second copy of the data.
- The model is configuration. Any compatible endpoint can take either seat, a model whose schema enforcement fails gets the format in words, and the live one is checked with a real answer, not a ping.
Proving it works
The integration gate is CI on real engines. The last full run passed 8,085 tests against PostgreSQL 16, MySQL 8.4 and SQL Server 2022, including a parity suite that reads the same data through all three readers. Golden conversations pin whole turns. Changes to how facts are computed get an independent cold review and a fuzz; one such release was fuzzed over 200,000 generated reads.
For the first dataset I also built Data Invoker, a synthetic-data engine in which an invented persona causes every row, so the tool knows the truth. It ships an answer key with planted patterns and deliberate decoys, and a scoring contract that rewards a correct description over a wrong cause. "The analyst found the real signal" became a score.
How it was built
One person, held to team discipline. Every feature starts as a design note with testable acceptance criteria and lands through a pull request: 397 merged so far. A second model reviews code changes cold, from the diff alone; I own direction and the merge; tests own correctness. The repository holds about 104,000 lines of Python under more test code than product code. The interface's look began as a design prototype from a teammate on the client's side; wiring it to a real system, and everything behind it, is mine.
Result
A working analyst over three database engines, in two languages, embedded in a customer's product and running on the client's own platform: on test data today, and built for the day the data is real. It answers questions nobody modelled in advance, and tells the reader which answers are proven, which are a best effort, and what the data cannot say. Figures are from the repository on 18 November 2026.
What it demonstrates
How to make a language-model product safe for an audience that will check. Keep the model where it is good, planning and explaining, and out of where it is not, arithmetic and certainty. Put a second pair of eyes and a parser between the model and the database. Let code state the facts. And give people a way to turn a good answer into a guaranteed one.