Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
AI Integration

When the Answer Lives in a Database, Not a Document

Last updated:

Documents and databases need different handling

Retrieval works for prose. It does not work for “how many orders over five hundred pounds shipped late last month”, because that answer does not exist in any document.

For those, the model's job is to write the query, and the database's job is to produce the number.

The safe pattern

  1. The question is translated into a query against a documented schema
  2. The query is validated — read-only, permitted tables, row limits
  3. It runs against a reporting replica, never production
  4. The result is returned as data, and the model explains it
  5. The query is shown, so the user can check the logic
Showing the query is what makes this trustworthy. A number with no visible derivation is a number nobody should act on.

What to restrict

  • Read-only credentials, always
  • An allow-list of tables and views, not the whole schema
  • Row and time limits, to prevent an accidental full scan
  • Permission filters applied in the view, not in the generated query
  • No writes, ever, from generated queries

Views are your friend

Expose curated views rather than raw tables: correctly joined, permission-filtered, with sensible column names. Query quality improves dramatically and the risk surface shrinks.

A dozen well-named views usually beats access to two hundred raw tables, for both accuracy and safety.

Where it works and where it does not

Question typeFit
Counts, sums and simple filtersStrong
Trends over timeStrong
Joins across two or three tablesGood with views
Complex financial calculationsUse a defined report instead
Anything a regulator will readUse a defined report instead

Frequently asked questions

Is this safe against our production database?

Only against a read replica with restricted credentials. Never against production with write access, regardless of the safeguards in front of it.

How accurate is it?

Good on well-named schemas with curated views, poor on sprawling legacy ones. The schema quality decides it more than the model does.

What about ambiguous questions?

It should ask — “do you mean order date or ship date?” Guessing produces confident answers to the wrong question.

Should this replace our reporting?

No. It handles ad hoc questions well. Defined reports remain the right answer for anything recurring or consequential.

Keep reading

Ad hoc data questions queueing on one analyst?

A safe query layer over curated views takes the routine ones off their desk. Tell us what your data looks like.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

AI IntegrationEnterprise AIAI Agents