ИТ развој

System Architecture: Build Databases That Antidote AI Hallucinations

Системска архитектура: Изградете бази на податоци што ги спречуваат халуцинациите на ВИ

AI systems do not hallucinate because they are careless. They hallucinate because they are asked to produce confident language from incomplete, ambiguous, or poorly structured context. If your application treats an LLM as a source of truth, the failure is architectural long before it is linguistic.

The practical antidote is not a longer prompt. It is a database design that makes authoritative facts easy to retrieve, hard to corrupt, and possible to cite. The model should interpret and communicate approved data; it should not quietly replace your system of record.

Start with a boundary: facts belong in the database

A useful architecture separates three things that are often mixed together: operational facts, generated text, and user-facing decisions.

  • Operational facts are orders, product specifications, account permissions, policies, prices, and timestamps.
  • Generated text is an LLM’s proposed explanation, summary, classification, or draft response.
  • Decisions are actions such as issuing a refund, changing access, or publishing content.

The first category must be governed by normal backend rules: schemas, constraints, transactions, authorization, and audit trails. Generated text is derived output. A decision may use generated text as input, but it needs explicit business validation before it changes state.

This distinction sounds obvious, yet many AI features fail by flattening it. A support assistant receives a customer question, invents an answer from a loosely assembled document dump, and presents it with the same authority as a row from the billing database. Users cannot tell the difference, and neither can the next engineer who inherits the feature.

Model the truth so it can be retrieved precisely

An AI-ready database is not necessarily a vector database. It is first a well-designed application database. Clear entities, stable identifiers, normalized ownership, and effective constraints make retrieval more reliable before semantic search enters the picture.

Suppose a system answers questions about subscription plans. Do not store the current plan rules only as a prose blob. Keep the facts queryable: plan identifier, currency, price, billing interval, entitlement, effective date, and status. Store explanatory text too, but connect it to the structured record it describes.

CREATE TABLE plan_versions (
    id BIGINT PRIMARY KEY,
    plan_code VARCHAR(50) NOT NULL,
    effective_from TIMESTAMP NOT NULL,
    effective_until TIMESTAMP NULL,
    monthly_price_cents INT NOT NULL,
    currency CHAR(3) NOT NULL,
    status VARCHAR(20) NOT NULL,
    UNIQUE (plan_code, effective_from)
);

A model can summarize “what the Pro plan includes,” but your application should retrieve the applicable version using dates and status before the model sees anything. That prevents a semantically similar but expired plan description from becoming the answer.

Make provenance a first-class field

Every piece of context sent to a model should have an origin. For documents, record source type, source identifier, version, publication state, owner, and timestamps. For structured records, include the primary key and the fields used to construct the answer.

This does two important jobs. First, it lets the UI show the user what the answer relied on. Second, it makes incidents debuggable. “The assistant gave the wrong answer” becomes a tractable question: was the source record wrong, was retrieval wrong, did prompt assembly omit a rule, or did the model contradict supplied context?

Without provenance, teams tend to diagnose AI defects as mysterious model behavior. With provenance, they can apply ordinary software engineering.

Retrieve narrowly, then constrain the model

Retrieval should be a backend responsibility, not an invitation for the model to roam through your data. Resolve identity and permissions first. Query structured facts directly where possible. Use full-text or semantic retrieval for unstructured material only after filtering by tenant, access level, document status, and relevant product area.

For example, an authenticated customer asking about their invoice should trigger a parameterized query scoped to that customer. It should not be answered by similarity search across all invoices.

$statement = $pdo->prepare(
    'SELECT id, total_cents, currency, status, issued_at
     FROM invoices
     WHERE id = :invoice_id
       AND account_id = :account_id'
);

$statement->execute([
    'invoice_id' => $invoiceId,
    'account_id' => $accountId,
]);

$invoice = $statement->fetch(PDO::FETCH_ASSOC);

The application can then give the model a compact, trustworthy context. If no row is returned, the correct behavior is not a plausible invoice explanation. It is a clear statement that the invoice was not found or is unavailable to that account.

Prompt instructions still matter, but they should reinforce architecture rather than compensate for its absence. Tell the model to use only supplied context, distinguish unknowns from facts, and return citations or source identifiers. Then validate the output format before presenting it.

Use schemas at every handoff

Free-form text is excellent for conversation and poor for system integration. When an AI feature must return data that code will consume, define a strict response shape. A model may generate a draft answer, but your service should parse and validate it before acting on it.

For a knowledge assistant, a response contract might include an answer, an uncertainty flag, and a list of source IDs. If source IDs do not belong to the retrieved context, reject the response or remove unsupported citations. If the model says it lacks enough information, preserve that outcome instead of retrying until it sounds decisive.

  • Validate required fields and data types.
  • Validate identifiers against the retrieval set.
  • Apply authorization again before exposing source details.
  • Keep generated output separate from approved records.
  • Require explicit confirmation for consequential actions.

This is especially important for tool use. A model can suggest an action and its parameters, but the backend must authorize the action, validate parameters, run it transactionally where appropriate, and return the actual result. The model should never become a privileged database client.

Version knowledge as carefully as code

Policies, documentation, contracts, and product rules change. If retrieval has no concept of version or effective date, an assistant will eventually combine old and new truth into a persuasive mistake.

Prefer immutable published versions over in-place edits for high-value knowledge. Mark the currently applicable version explicitly, retain the prior version for auditability, and ensure indexing jobs remove or deactivate superseded content. For regulated or contractual material, store the effective period and make the retrieval query date-aware.

Generated answers deserve retention too, but not as truth. Log the request scope, source versions, model response, validation result, and final user-visible answer. Avoid indiscriminate logging of sensitive prompts; apply the same data-minimization and retention policies you use elsewhere in the platform.

Design for graceful refusal

A good AI system needs a reliable way to say “I do not know.” Low-confidence retrieval, conflicting sources, missing permissions, or absent structured records should lead to a bounded response and a useful next step.

That may mean linking the relevant documentation, asking a precise clarification question, or routing the user to a human workflow. The goal is not maximum answer rate. The goal is maximum justified answer rate.

Measure the system accordingly. Track unsupported citations, empty retrievals, conflict rates, validation failures, and cases where users correct the answer. Review these alongside ordinary operational metrics such as query latency, error rates, and queue depth. Hallucination resistance is an observable property of the whole pipeline, not a personality trait of a model.

The durable architecture is deliberately unglamorous

The most dependable AI features are built from familiar parts: relational constraints, scoped queries, authorization checks, versioned documents, structured contracts, and logs that explain what happened. The LLM adds value at the edge, where language must be understood or produced. It should not sit at the center, deciding what reality is.

Build your database so the application can answer, “What do we know, who is allowed to know it, and where did it come from?” Once those answers are explicit, AI becomes far more useful—and far less likely to confidently make the rest up.

Портрет на автор на блогот

Mihajlo

Јас сум Михајло - развивач поттикнат од љубопитност, дисциплина и постојаната желба да создадам нешто значајно. Споделувам увиди, упатства и бесплатни услуги за да им помогнам на другите да ја поедностават својата работа и да растат во постојано развивачкиот свет на софтверот и вештачката интелигенција.