Skip to content

RAG: how AI uses company documents

DYNAMINDS Labs team · · 6 min

AI Workers

In brief

RAG (retrieval-augmented generation) is a way of working with a language model in which the system first retrieves passages from company documents that match the question, and only then does the model write an answer based on them. The model does not need to know the documents from training: it receives them at the moment of the question, together with an instruction to stick to their content and name the source. As a result the answer rests on the current procedure, price list or contract, and access permissions to documents can be enforced at retrieval.

How RAG works step by step

RAG has two parts: preparing documents and handling a question. The method's name was introduced by Lewis and co-authors in a 2020 paper, and today it is a common way of combining language models with company knowledge.

Preparing documents (indexing):

  1. The system pulls documents from sources: procedures, instructions, contracts, product sheets, answers to past inquiries.
  2. It splits them into passages, for example paragraphs or sections, and stores metadata with each: source, version, date, department, access level.
  3. It turns each passage into a vector of numbers (an embedding) that captures its meaning, and stores it in an index, often a vector database.

Handling a question:

  1. The question is also turned into a vector, and the system retrieves the passages closest in meaning. This is often combined with keyword search, because catalog numbers and proper names are found less well by vector search.
  2. The selected passages go to the model together with the question and an instruction: "answer only on the basis of the passages, name the source, and if the answer is not there, say so".
  3. The model writes an answer that refers to the document and its version.

We write more about vector databases themselves in the article Vector databases: what they are for.

When to use RAG and when to query a system

RAG is for text, not for the numbers that live in transactional systems. This distinction saves many errors.

QuestionRight sourceMethod
How to proceed with a complaint about damage in transport?complaints procedureRAG
What payment terms does the supplier contract have?contract in the repositoryRAG
How many units of an item are in stock?ERPERP query
What is the price for the customer this week?price list in the ERPERP query
Does the invoice match the order?ERP and KSeFdata comparison, no RAG

Stock levels in a week-old PDF are out of date before they reach the index. An AI Worker that answers about availability should query the ERP, not search documents. A well-designed AI Worker combines both ways: it takes the rule from the procedure and current data from the system.

Permissions: a filter in retrieval, not in the model

Access to documents is enforced at the retrieval stage, not through an instruction to the model. A command like "do not show salespeople board contracts" is not a safeguard, because the model can ignore it or be talked into ignoring it.

A safe setup looks like this:

  • every passage in the index has a permissions label copied from the source system, for example from folder permissions,
  • retrieval returns only passages the asking person, or the AI Worker acting in a given process, has the right to see,
  • a permissions change in the source system reaches the index at the next update, and a deleted document disappears from the index.

This is the same principle that DYNAMINDS Labs applies to AI Workers: they see only what you grant them permissions to in the system, and revoking permissions closes their access.

OWASP, in its Top 10 for LLM Applications, describes vector and embedding weaknesses separately, including data leakage between users who share one index. In a company with several entities or departments with different permissions this is a real risk that has to be checked in tests, not assumed away.

Quality: what breaks RAG and how to measure it

The most common cause of bad RAG answers is retrieval, not the model. If an outdated version of a procedure reaches the model, the model will summarize it correctly and give a wrong answer.

Typical sources of errors:

  1. Multiple versions of a document. The index contains a 2023 and a 2025 procedure, and retrieval returns the older one. Version metadata and removing withdrawn versions from the index help.
  2. Bad chunking. A price list table cut in the middle of a row loses its meaning. Tables and lists are better split by structure than every fixed number of characters.
  3. No answer in the documents. A model that has not found an answer should say so. If it does not, it fills in from general knowledge, which we write about in the article Hallucinations of language models: how to limit them in processes.
  4. Commands hidden in documents. A document from a customer may contain text posing as an instruction to the model. OWASP describes this as the indirect variant of prompt injection.

Quality is measured with a set of control questions: several dozen questions with a correct answer and the indicated document, prepared by people who know the process. After every change to the index or the model, the set is run again and you compare how many answers pointed to the right source. This is the same way of thinking as regression testing in software.

Example: answering questions from a tender inquiry

RAG shows its value well where an answer has to be assembled from many documents. The B2B sales department of a telecom operator receives an inquiry with several dozen questions: about service parameters, fault repair time, certificates, data center safeguards.

The flow with RAG looks like this:

  1. The AI Worker splits the inquiry into single questions and for each retrieves passages from service descriptions, SLA templates, the security policy and earlier approved answers.
  2. For each question it prepares a draft answer with a reference to the document and its version.
  3. Questions it found no answer for in the documents it marks separately, instead of making up content.
  4. The salesperson and a technical owner review the draft, correct it and approve it.

The team does not start from a blank page and sees where every sentence comes from. Questions without a source also point to gaps in the company's documentation, which are worth filling before the next inquiry arrives.

Questions and answers

Does RAG mean the model learns from company documents?

No. In RAG the model does not change its parameters, it only reads selected passages at the moment of the question. The documents stay in the company's index and can be removed or changed without retraining the model. This is a different technique from fine-tuning, in which a model is trained on data.

Does RAG require moving documents to the cloud?

No. The index, vector database and model can run on a company server or in a data center in Poland. Where data is processed is decided by the deployment architecture, not by the RAG method itself. We describe the details in the article Company data and language models: deployment security.

How often should the document index be updated?

As often as the documents that matter for the process change. Procedures that change once a quarter can be indexed nightly. Documents from customers, such as tender inquiries, are indexed on arrival. What matters most is removing withdrawn documents quickly.

How do you know an answer comes from a document and not from the model's imagination?

From the citation and the link to the source. A well-built system shows the passage on which it based the answer, and a control rule checks that the quote actually appears in the document. An answer without a source should go to a human or be marked as uncertain.

What it looks like in a project

Start with one task. Describe it, and DYNAMINDS Labs will prepare a free diagnosis.

Meet DYNAMINDS Labs

Read more: AI Workers