Back to blog posts

AI Engineering / RAG

RAG: Architecture

Updated Jul 2026 12 min read Built from handwritten notes

Mental Model

RAG is a sytem to provide extra relevant information to an LLM, so that the model can answer from your documents and not only from what it learned during training.

Why RAG exists

LLMs have two common failures:

1. Knowledge cutoff:
- Once the LLM has completed its training, it will not learn anything new.
- For example, if an LLM was trained on web development knowledge up to June 2025, it will not know features from a new React version released after June 2025.
- It also will not know your private component library and CSS styles because that data was not part of training.

2. Hallucination:
- If you ask an LLM something it does not know, it may guess. It will produce an answer, that too confidently. But the answer may be wrong, unrelated, or irrelevant.
- Since the model has to answer something, it can hallucinate instead of saying, “I do not know this.”

RAG

- RAG stands for Retrieval Augmented Generation.
- Your RAG application is not part of the LLM. You build the RAG app to provide extra relevant information to the LLM, so when it answers, it can answer from data your RAG gave, its own training, the user prompt and the retrieved context.

RAG combines two memories

1. Parametric memory

What the LLM already knows from the parameters it has already been trained on.

2. Non-parametric memory

External set of documents your model can read from when answering the question. It provides extra relevant context.

Example:

User asks: “What is the leave policy?”

Without RAG

The LLM may guess:
- “Typically 10 paid leaves” OR
- It may hallucinate: “Leave policy describes the company's rules on how an employee can take time off.”

With RAG

  1. 1. User asks a question.
  2. 2. RAG system searches company documents.
  3. 3. It finds the leave policy.
  4. 4. It sends the user prompt & the leave policy to the LLM.
  5. 5. LLM answers using this retrieved leave policy.

Basic RAG architecture

Offline Indexing + Online Retrieval

Offline

Prepare knowledge before the user asks a question

1

Ingest

Supply extra relevant information

Company Policies
Latest Papers
Support Tickets
Break docs into chunks. Helps limit the LLM context window which reduce hallucination.
2

Chunking

Split source material into useful pieces

Leave policy

Holiday Calendar

WFH Policy

Travel policy

Convert each text chunk into vectors. LLM systems compare vectors, not raw text.
3

Embedding

Represent every chunk in vector space

Leave policy

Holiday Calendar

WFH Policy

Travel policy

Store chunk embeddings in the vector database.
4

Vector DB

Indexed chunk embeddings

Online

Runs when the user asks a question

5

User Query

Embed query with the same model

“What is the leave policy?”
Query vector

Embedded query

6

Search within DB

Compare query vector with chunk vectors

Vector similarity search returns the most relevant chunks.
7

Top K Relevant Chunks

K = 5 most similar chunks

Leave policy

Benefits FAQ

8

LLM

Receives

User Query Vector
Top K Relevant Chunks

Generate grounded answer

1. Ingest

- Provide the RAG app, extra relevant information like the company documents, codebase, design principles, user feedback etc.
- These could be PDFs, Word documents, web pages, code, GitHub links, support tickets, support logs, images, videos, or other internal sources.
- e.g. HR policies, GitHub repository, or support logs.

2. Chunking

- Split documents into smaller chunks
- If company data is large, not all of it will fit in the LLM context window. So when answering, the LLM may use only part of the company data or use summarized data, which can cause hallucination.
- Chunking helps limit the LLM context window.

- For example, an HR policies document may become separate chunks for leave policy, travel policy, holiday calendar, and work-from-home policy.
- If the user asks “What is the leave policy?”, your RAG can retrieve only the relevant leave policy chunk, not the whole document.

3. Embedding

- Convert chunks to vectors.
- LLMs do not understand text directly; they understand vectors.
- The user query is also converted to an embedding. Use the same embedding model for chunks and the user query so similar meaning tokens lands close together in the same vector space.

4. Vector database

- Store embeddings in a vector database, such as Chroma DB, Pinecone, or Postgres with pgvector.
- We cannot use a regular SQL database since we are not doing text comparison or keyword search. We do vector comparison, hence we need vector DB.

Example of a chunk stored in DB:

{ "id": "employee-policy-leave-001", "text": "Employees get 20 paid leaves per year", "embedding": [0.12, -0.44, 0.9], "metadata": { "source": "employee-handbook.pdf", "section": "HR - Leave Policy", "page": 12, "chunkIndex": 1 } }

5. Retrieval

- Retrieval performs vector similarity search to compare the user query embedding vector with chunk vectors.
- It retrieves the top K chunks. These are the most relevant chunks likely to have the right data to feed the LLM.
- K is decided when designing the RAG system.

Final generation step

The LLM gets the user query vector & relevant chunks from RAG. The LLM answers using this additional context.

User query vector
+
Relevant chunks
LLM answers using additional context