Architecture · Knowledge
RAG Architecture: Grounding Language Models In Your Own Data
The short answer
Retrieval-augmented generation (RAG) answers questions from your own documents instead of the model's memory. An ingestion pipeline parses, chunks and indexes your sources; at question time, a retriever finds the most relevant passages, a reranker orders them, and the model writes an answer using only those passages, with citations. It keeps answers current and checkable without retraining a model.
By Maitreya Kulkarni · Updated · 11 min read
01 · The architecture
How Is A RAG System Wired?
As two pipelines that meet at the index. Ingestion turns your sources into searchable chunks ahead of time. At question time, the query skips straight to retrieval, the best passages are reranked, and the model answers from them. Evaluation results feed back into how documents are chunked.
NX-AR-04 · Architecture
Retrieval-augmented generation
- Outside world
- Data & integration
- AI agent
- System of record
- L1
Sources
(Outside world)Documents and PDFs
. Hands work to: Parse and chunk.Wikis and tickets
. Hands work to: Parse and chunk.Databases
. Hands work to: Parse and chunk.User question
At query time
. Hands work to: Hybrid retrieval.
Passes down: documents - L2
Ingestion
(Data & integration)Parse and chunk
Structure-aware, with permissions
. Hands work to: Vector index, Keyword index.
Passes down: chunks with metadata - L3
Index
(Data & integration)Vector index
Embeddings, pgvector
. Hands work to: Hybrid retrieval.Keyword index
BM25 full-text
. Hands work to: Hybrid retrieval.
Passes down: candidate passages - L4
Retrieve
(AI agent)Hybrid retrieval
Both indexes, permission filter
. Hands work to: Grounded generation.Reranker
Orders passages by relevance
. Hands work to: Grounded generation.
Passes down: the best passages - L5
Generate
(AI agent)Grounded generation
Answers only from passages
. Hands work to: Answer with citations, Evaluation.
Passes down: an answer - L6
Output
(System of record)Answer with citations
Evaluation
Faithfulness, recall
. Hands work to: Parse and chunk.
02 · Why RAG
Why Use RAG Instead Of Fine-Tuning Or A Long Prompt?
Because most business questions need facts that change, that must be cited, and that only some users may see. RAG handles all three. Fine-tuning changes behaviour rather than adding facts, and pasting everything into a long prompt gets slow, expensive and less accurate as the corpus grows.
| Item | RAG | Fine-tuning | Long context |
|---|---|---|---|
| Best for | RAGFacts, documents, Q&A | Fine-tuningBehaviour, format, style | Long contextA few documents at once |
| Freshness | RAGUpdate the index | Fine-tuningRetrain | Long contextRe-send every time |
| Citations | RAGNatural | Fine-tuningHard | Long contextPossible |
| Permissions | RAGFilter per user | Fine-tuningNone | Long contextManual |
| Cost at scale | RAGLow | Fine-tuningTraining plus hosting | Long contextHigh per request |
03 · Chunking
How Should Documents Be Chunked?
Along their structure, not at a fixed character count. Good chunks are self-contained passages that keep headings, tables and references intact, carry metadata about where they came from, and are small enough to be precise but large enough to make sense on their own.
Structure-aware splitting
Split at headings, sections and list boundaries; keep tables whole.
Context in every chunk
Prefix each chunk with its document title and section, or a generated summary, so it is findable out of context.
Metadata
Source, date, author and access rights travel with every chunk for filtering and citation.
Measure, then tune
Chunk size and overlap are tuned against retrieval metrics on real questions, not guessed.
04 · Retrieval
How Does Retrieval Find The Right Passages?
By combining meaning and wording. Vector search finds passages with similar meaning; keyword search finds exact terms such as product codes and names. Results from both are merged, filtered by permissions, and reranked by a model that reads each passage against the question.
Embeddings
A model turns text into vectors so that similar meanings sit close together.
Hybrid search
Vector and keyword results are fused, because each misses what the other catches.
Reranking
A cross-encoder scores each candidate against the question, sharply improving the top results.
Query rewriting
Vague or conversational questions are rewritten, or split into sub-questions, before searching.
05 · Grounding
How Do You Stop A RAG System From Making Things Up?
Instruct, constrain and measure. The model is told to answer only from the passages and to say when they don't contain the answer; every claim carries a citation; and faithfulness is measured on a test set so regressions are caught.
Answer only from context
Clear instructions and a refusal path when retrieval comes back empty.
Citations
Each statement links to the passage it came from, so a reader can check it.
Evaluation
Faithfulness, answer relevance and context recall scored on real questions.
Advanced patterns
Agentic RAG lets the model search repeatedly; graph RAG adds entity relationships for questions that span documents.
The ecosystem
pgvector
Supabase
OpenAI embeddings
Claude
LangChain
Hugging Face
Sources and further reading
- [1]Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020)
- [2]Anthropic, Introducing Contextual Retrieval (2024)
- [3]Es et al., RAGAS: Automated Evaluation of Retrieval Augmented Generation (2023)
- [4]pgvector: open-source vector similarity search for Postgres
- [5]Microsoft, GraphRAG


