← All articles

RAG Explained: How to Make AI Answer From Your Own Company Documents

· 4 min read

RAG Explained: How to Make AI Answer From Your Own Company Documents

This guide is for managers, developers and founders who want an AI assistant that answers from their own documents: policies, contracts, product manuals or past tickets. It explains what RAG is, how it works step by step, how well it can work according to published numbers, and when you should not build it at all.

What RAG means

RAG stands for retrieval-augmented generation. Before the model answers, a search step finds the most relevant pieces of your documents and puts them into the prompt. The model then writes its answer using that material. Anthropic describes it as retrieving relevant information from a knowledge base to improve the model's responses.

The idea comes from a 2020 research paper, "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks", by Patrick Lewis and colleagues from Facebook AI Research, University College London and New York University, accepted at NeurIPS 2020. It paired a language model with a searchable index of Wikipedia. The same pattern now powers most "chat with your documents" products.

Why do it? A model does not know your private documents, and it cannot be trusted to remember details from training. Putting the right passage in front of it lets it answer from facts you control, and lets you show the source.

The steps, in order

1. Collect the documents: PDFs, Word files, web pages, help articles. 2. Split them into chunks, short passages of a few paragraphs each. 3. Turn each chunk into an embedding, a list of numbers that captures its meaning, and store them in a vector database. Many teams also keep a keyword index. 4. When a user asks a question, search for the best-matching chunks. 5. Send the question and those chunks to the model, and tell it to answer only from them and to say when the answer is not there. 6. Show the answer with links to the source passages.

Most failures happen at steps 2 and 4: chunks that lose their meaning, and searches that miss the right chunk.

The problem with plain chunking

Anthropic's Contextual Retrieval write-up explains the weak point. When a document is cut into chunks, each chunk loses its surroundings. A chunk saying "revenue grew 3% over the previous quarter" does not say which company or which quarter. Search then cannot tell it apart from similar passages. In Anthropic's words, traditional RAG solutions remove context when encoding information.

Anthropic's fix is to add a short explanation of where the chunk comes from before it is indexed. It also combined embeddings with BM25, a classic keyword-matching method, and added a reranking step.

Column chart of failed retrievals in top-20 results: standard RAG 5.7 percent, contextual embeddings 3.7, plus contextual BM25 2.9, plus reranking 1.9
Failed retrievals fell from 5.7% to 1.9% in Anthropic's tests.

In Anthropic's tests, measured on the top 20 retrieved chunks, the failure rate was 5.7% for standard retrieval. Contextual embeddings cut it to 3.7% (35% fewer failures). Adding contextual BM25 brought it to 2.9% (49% fewer). Adding reranking brought it to 1.9% (67% fewer). These are Anthropic's own test results on its own datasets, so measure your own documents rather than assuming the same gains. Anthropic estimated the one-time cost of creating the extra context at about $1.02 per million document tokens, under its stated assumptions.

When you do not need RAG

Anthropic gives a plain rule: if your whole knowledge base is smaller than 200,000 tokens, about 500 pages, you can put all of it in the prompt and skip RAG. It adds that prompt caching makes this faster and cheaper, with latency cut by more than half and costs by up to 90%. A small company with one 80-page policy manual may not need a vector database at all.

RAG is also the wrong tool for questions that need totals across thousands of records. Use a database query for those.

Mistakes to avoid

- **Indexing everything.** Old, duplicate or contradictory documents produce old, contradictory answers. Clean first. - **Ignoring permissions.** If HR files and public FAQs share one index, any user can ask the bot about salaries. Filter results by who is asking. - **No "I don't know" rule.** Tell the model to refuse when the retrieved text does not contain the answer. - **Never testing.** Write 30 to 50 real questions with correct answers and re-run them after every change. - **Trusting retrieved text blindly.** A document can contain hidden instructions. Read our guide to [prompt injection](https://encoderbytes.com/Blog/prompt-injection-explained-security-hole-in-every-ai-app).

A Pakistan and Gulf angle

Many businesses here keep policies in English, Urdu and scanned PDFs. Test your pipeline on scanned pages and Urdu text before promising anything, since text extraction and search quality can drop on both. Check where your documents are sent and stored if they contain customer data, and what your contracts and local rules require.

Where to go next

For the wider picture of prompting, RAG and fine-tuning, see our guide to [LLMs for business](https://encoderbytes.com/Blog/llms-for-business-prompting-rag-fine-tuning-explained). This article is general information, not legal or compliance advice.

Sources

Anthropic, Introducing Contextual Retrieval: https://www.anthropic.com/news/contextual-retrieval

Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (arXiv): https://arxiv.org/abs/2005.11401

Frequently asked questions

What is RAG in AI?

Retrieval-augmented generation finds the most relevant passages from your documents and gives them to the AI model with the question, so it answers from your material instead of memory.

Do I need RAG or can I paste documents into the prompt?

Anthropic says that if your knowledge base is under about 200,000 tokens, roughly 500 pages, you can include it all in the prompt and skip RAG. Larger collections need retrieval.

How accurate is RAG?

It depends on your documents and setup. In Anthropic's tests, adding contextual embeddings, keyword search and reranking cut failed retrievals from 5.7% to 1.9%, so test on your own questions.

Can RAG stop AI from making things up?

It reduces the problem but does not remove it. Tell the model to answer only from the retrieved text, show sources, and test with real questions.

Contact

Let's Discuss Your Project

Tell us what you are building. Every conversation is covered by a mutual NDA.

What happens next

  1. 1Our team contacts you within one business day
  2. 2An initial discussion to understand your requirements
  3. 3A scope assessment and proposal

We reply from info@encoderbytes.com.