The Complete RAG Systems Guide: From Zero to Production

For developers with backend engineering experience who are learning AI application engineering. The goal is to build a complete map of RAG in your mind: what every stage does, why it exists, and which choices are available.


First, the Most Basic Question: What Problem Does RAG Solve?

LLMs have three fundamental weaknesses: their knowledge stops at the training cutoff date, they cannot access your private data, and they confidently fabricate answers about things they do not know—hallucinations.

RAG, or Retrieval-Augmented Generation, takes a direct approach: do not make the model answer from memory; let it answer with reference material in front of it. It is like an open-book exam. The model still needs comprehension and reasoning, but it does not have to memorize all knowledge.

The flow is: a user asks a question → retrieve the most relevant passages from a knowledge base → place those passages in the LLM prompt as context → have the LLM answer from that context.

Facebook AI Research introduced the approach in a 2020 paper. By 2026, it had become a standard architecture for knowledge-intensive AI applications.


Panoramic Map

RAG System
│
├── 1. Ingestion Pipeline — offline processing
│   ├── 1.1 Document Loading & Preprocessing
│   ├── 1.2 Chunking
│   ├── 1.3 Contextual Enrichment [optional]
│   ├── 1.4 Embedding
│   └── 1.5 Indexing & Storage
│
├── 2. Query Pipeline — online processing
│   ├── 2.1 Query Understanding & Transformation
│   ├── 2.2 Retrieval
│   ├── 2.3 Reranking
│   └── 2.4 Context Compression & Assembly
│
├── 3. Generation
│   ├── 3.1 Prompt Construction
│   ├── 3.2 Answer Generation
│   └── 3.3 Citation & Attribution
│
├── 4. Evaluation
│   ├── 4.1 Retrieval-quality evaluation
│   ├── 4.2 Generation-quality evaluation
│   ├── 4.3 End-to-end evaluation
│   └── 4.4 Citation-quality evaluation
│
└── 5. Operations — specific to production
    ├── 5.1 Observability
    ├── 5.2 Index Refresh
    ├── 5.3 Cost optimization
    └── 5.4 Security and compliance

The following sections expand each layer.


1. Ingestion Pipeline

This is the offline stage. It turns raw documents into a searchable vector index. Eighty percent of RAG failures can be traced back to this layer.

1.1 Document Loading and Preprocessing

What it does: converts raw documents in formats such as PDF, Word, HTML, Markdown, databases, and JSON returned by APIs into clean, structured text.

Why it matters: garbage in, garbage out. OCR errors in PDFs, navigation noise in HTML, and misaligned table extraction contaminate every subsequent stage if they are not corrected here.

Key design decisions:

  • Choosing a document parser. Basic libraries such as PyPDF and python-docx are sufficient for simple text. Complex documents—scanned PDFs, multi-column layouts, and nested tables—require specialist tools such as Unstructured.io, LlamaParse, Docling, or Azure Document Intelligence.
  • Extracting metadata. At this stage, extract and preserve filenames, page numbers, heading hierarchy, creation and modification times, and document types. This metadata is essential for later retrieval filtering and result presentation.
  • Normalizing preprocessing. Standardize encoding as UTF-8, remove formatting noise such as headers, footers, and watermark text, and handle special characters.

In Java terms, this is the Extract + Transform portion of an ETL pipeline.

1.2 Chunking

What it does: splits long documents into pieces suitable for embedding and retrieval.

Why it matters: as covered earlier in detail, the central trade-off is that chunks that are too large make retrieval imprecise, while chunks that are too small lose context.

Recommended defaults, based on 2026 benchmarks:

  • Strategy: Recursive Character Splitting
  • Chunk size: 512 tokens
  • Overlap: 50–100 tokens, or 10–20%
  • Use different strategies for different document types: split Markdown by headings, code by functions, and tables by rows

Key principle: begin with a simple strategy and use evaluation data to tune parameters. Do not start by pursuing semantic chunking.

What it does: supplements each chunk with its original context to compensate for context lost during chunking.

Main methods:

  • Contextual Chunking, the Anthropic method: use an LLM to generate a short contextual prefix for each chunk, describing its position and background in the full document. Anthropic reported a 49% reduction in retrieval failures.
  • Append a document summary: add the document’s global summary to each chunk’s metadata.
  • Prefix the heading hierarchy: prepend the chunk’s heading path, such as “Chapter 3 > Authentication Module > JWT Implementation,” to its text.

This step has a cost—one LLM call per chunk—but can significantly improve retrieval quality.

1.4 Embedding

What it does: converts a text chunk into a high-dimensional vector, usually 768–3,072 dimensions, so semantically similar texts lie closer together in vector space.

Key design decisions:

  • Model selection. Mainstream options in 2026 include:
  • Commercial APIs: Voyage AI’s voyage-3-large leads the MTEB leaderboard and supports 32K context; OpenAI’s text-embedding-3-large and text-embedding-3-small are the most widely adopted choices.
  • Open-source self-hosting: BGE-M3 is a strong multilingual option; E5-large-v2 suits English workloads and has 10 ms latency on CPU.
  • Your planned BGE-M3 is an appropriate choice for a multilingual Chinese, Japanese, and English workload.
  • The dimension-cost trade-off. More dimensions improve precision but increase storage cost and slow queries. Many models support Matryoshka embeddings, which can be truncated to fewer dimensions with little loss of accuracy.
  • Important: the embedding model’s maximum input length is a hard limit, not a target. A 512-token chunk often performs better in a model with an 8,192-token limit than filling the whole window.

1.5 Indexing and Storage

What it does: stores vectors together with their text and metadata in a database that supports fast retrieval.

Key design decisions:

  • Choosing a vector database:
  • pgvector, your planned choice: a PostgreSQL extension suited to teams that already run PG infrastructure. Its advantages are familiar operations and the ability to colocate vectors with business data; at very large scale, above 10 million vectors, it is slower than specialized databases.
  • Specialized vector databases: Pinecone is fully managed; Weaviate and Qdrant are open source; Chroma is lightweight and suitable for prototypes.
  • In-memory databases: Redis supports both vector search and caching, reducing the number of components.
  • Index types: HNSW is the most common and queries quickly but uses substantial memory. IVFFlat suits large datasets and requires a trade-off between accuracy and speed.
  • Store the original text and metadata as well. After a vector match, the original text must be retrieved and sent to the LLM, so it must be stored with the vector or remain available through a linked query.

2. Query Pipeline

This is the online flow after the user asks a question. Every step influences the quality of the context ultimately retrieved.

2.1 Query Understanding and Transformation

What it does: improves the user’s query before retrieval so it is better suited to vector search.

Why it matters: raw user questions are often ambiguous, conversational, or overly complex. Embedding them directly usually produces weak retrieval.

Main methods:

  • Query Rewriting: use an LLM to rewrite a conversational question into retrieval-friendly language. For example, “How do I use this?” becomes “Usage and parameter reference for the XXX API.”
  • Query Expansion: generate several semantically related query variants, retrieve with each, and merge the results to improve recall.
  • Query Decomposition: divide a complex question into subquestions. “Compare A and B in performance and cost” becomes separate retrievals for A’s performance, B’s performance, A’s cost, and B’s cost, followed by consolidation.
  • HyDE, or Hypothetical Document Embedding: first have an LLM generate a hypothetical answer, then use that answer rather than the original query for vector retrieval. The rationale is that a hypothetical answer is closer in semantic space to a real answer document than the question itself.

Practical recommendation: do not add query transformation in the first version. Retrieve directly with the raw query. If evaluation shows low context recall, add rewriting and expansion incrementally.

2.2 Retrieval

What it does: finds chunks in the vector database that are most relevant to the query.

Three main strategies:

Dense Retrieval: embed the query and find nearest neighbors in vector space, usually with cosine similarity. It is good at semantics: “how to verify user identity” can match a document about authentication. It is weaker at exact terms, so a specific error code may be found more reliably by traditional search.

Sparse Retrieval: traditional keyword-based retrieval such as BM25. It excels at exact matches—error codes, product names, and specialist terms—but does not understand synonyms and semantics.

Hybrid Retrieval ⭐ recommended: run Dense and Sparse retrieval together, then merge results with Reciprocal Rank Fusion, or RRF. The 2026 consensus is that hybrid retrieval almost always outperforms either method alone, improving recall by 1–9%.

How many results to retrieve: a common starting point is top 20 candidates, then use reranking to reduce them to the top 5 sent to the LLM. Tune the exact values through evaluation.

2.3 Reranking

What it does: scores retrieved candidate chunks a second time so that the truly relevant ones move to the front.

Why it is needed: cosine similarity from vector retrieval is a coarse relevance measure. Two chunks may have similar cosine scores while differing greatly in actual relevance. A reranker uses a more precise model, usually a cross-encoder, to assess each chunk’s relevance to the query.

How it works:

Retrieval returns 20 candidate chunks
→ Reranker scores each (query, chunk) pair
→ Sort by score
→ Send the top 5 to the LLM

Mainstream choices: Cohere Rerank, a commercial API priced at $1 per 1,000 searches; BGE-Reranker, which is open source; and ColBERT, a highly accurate late-interaction model that is more complex to deploy.

Practical effect: reranking usually improves final answer quality significantly because it ensures that the LLM sees context that is truly relevant, not merely closest in vector space.

2.4 Context Compression and Assembly

What it does: performs the final optimization before sending chunks to the LLM: deduplication, compression, and ordering.

Core operations:

  • Deduplication: merge or remove highly repetitive chunks, such as overlapping regions.
  • MMR, or Maximal Marginal Relevance: maximize diversity while preserving relevance, avoiding five top chunks that all say the same thing.
  • Context ordering: research shows that LLMs attend most strongly to information at the beginning and end of context, while middle content is more easily overlooked—the lost-in-the-middle effect. Place the most relevant chunks at the beginning and end.
  • Length control: keep the total context within the LLM’s context window while reserving enough room for the system prompt, conversation history, and output.

3. Generation

Once relevant context has been retrieved, the LLM generates an answer from it.

3.1 Prompt Construction

What it does: assembles the system prompt, retrieved context, and user question into the final LLM prompt.

A typical prompt structure:

[System Prompt]
You are an assistant that answers questions from the supplied reference material.
Answer only from the references below. If they do not contain relevant information, say explicitly, "I could not find relevant information in the supplied material."
Cite specific sources in the answer.

[Retrieved Context]
--- Reference 1 (source: xxx.pdf, page 3) ---
<content of chunk 1>

--- Reference 2 (source: yyy.md, section: Authentication) ---
<content of chunk 2>

...

[User Query]
The user's original question

Key principles:

  • Explicitly instruct the model to answer only from the provided context to reduce hallucinations.
  • Explicitly tell it to say when it does not know. That is better than fabricating an answer.
  • Require source citations to improve verifiability.
  • Keep prompt templates in dedicated files rather than scattering them through the code.

3.2 Answer Generation

What it does: calls an LLM to generate the final answer.

Key design decisions:

  • Model choice: depends on quality requirements and budget. GPT-5.4 and Claude Opus 4.6 suit high-quality workloads; GPT-5.4 Mini, Claude Sonnet 4.6, and MiniMax M2.7 suit cost-sensitive workloads.
  • Temperature: RAG normally uses a low temperature of 0–0.3 because the model should remain faithful to the context rather than be creative.
  • Streaming: production systems almost always stream output to reduce perceived Time to First Token, or TTFT.
  • Fallback strategy: LLM APIs may time out or fail, so implement retries and degradation, such as switching to a faster model if the primary model times out.

3.3 Citation and Attribution

What it does: indicates which location in which source document supports each factual claim.

Why it matters: verifiability is one of RAG’s central advantages over a plain LLM. Users can follow a citation to the original text and confirm the answer. This is especially important to enterprise customers.

Implementation:

  • Assign every context chunk an identifier such as [1] or [2] in the prompt, and require the model to cite those identifiers.
  • After generation, map citation identifiers back to the exact original location: filename, page number, and paragraph.
  • In the frontend, provide links that jump to the source.

4. Evaluation

Most tutorials ignore this stage, but every production system needs it. Without evaluation, you do not know whether the system is good, and after changing a parameter you do not know whether it improved or regressed.

4.1 Retrieval-Quality Evaluation

This evaluates whether the retrieval layer found the correct chunks.

MetricMeaning
Context RecallOf the chunks that should have been retrieved, how many were actually retrieved; higher is better
Context PrecisionOf the chunks retrieved, how many were genuinely relevant; higher is better
MRR, Mean Reciprocal RankThe average rank of the first correct chunk

Low Context Recall → chunks may be too small or a strategy may cut away key information, or top-K may be too low. Low Context Precision → chunks may be too large and include irrelevant content, or reranking may be missing.

4.2 Generation-Quality Evaluation

This evaluates the quality of the LLM’s answer.

MetricMeaning
FaithfulnessWhether the answer remains faithful to the retrieved context instead of hallucinating
Answer CorrectnessWhether the answer matches the expected answer
Answer RelevancyWhether the answer is relevant to the user’s question

Low Faithfulness → the model is inventing information outside the context; adjust the prompt or lower temperature. Low Answer Correctness with high Faithfulness → the retrieval layer supplied the wrong context.

4.3 End-to-End Evaluation

End-to-end evaluation measures overall quality from the user’s question to the final answer without separating retrieval from generation.

The most effective method is to prepare a set of (question, ground_truth_answer) pairs, run the entire pipeline, and compare system output with the ground truth.

4.4 Citation-Quality Evaluation

This is where the previously discussed citation topic fits: evaluate whether generated citations point accurately to the original text.

Evaluation tools: Ragas and DeepEval were among the most widely used RAG evaluation frameworks in 2026 and cover all the metrics above. Both are already included in your learning plan.

Evaluation-Driven Development workflow:

  1. Prepare 50–100 evaluation examples.
  2. Run a baseline with default settings.
  3. Record every metric.
  4. Change one parameter at a time and compare the metrics.
  5. Iterate.

5. Operations

This is where prototypes become production systems.

5.1 Observability

Metrics you must monitor:

  • Latency at each stage: embedding, retrieval, and LLM generation
  • Token consumption and cost per request
  • Number of chunks returned and distribution of relevance scores
  • Faithfulness score of LLM answers, which can be evaluated automatically with an LLM-as-judge
  • Error rate: API timeouts, embedding failures, and empty retrieval

Tooling: Langfuse, which you plan to use, was one of the most widely used LLM observability platforms in 2026 and traces the complete path of each request through the pipeline.

5.2 Index Refresh

Documents change. A RAG system needs a strategy for keeping the index synchronized with source documents.

  • Full rebuild: suitable for small collections below 10,000 documents; periodically repeat all chunking and embedding.
  • Incremental update: process only new and modified documents. Track document versions through file hashes or modification times.
  • Refresh frequency: daily for dynamic content such as product catalogs and support knowledge bases; weekly or manually for static documents such as technical manuals and contracts.

5.3 Cost Optimization

RAG cost mainly comes from three sources:

  • Embedding API calls: a one-time ingestion cost plus recurring cost during index refreshes
  • LLM API calls: required for every user query and usually the largest recurring cost
  • Vector-database storage and queries: proportional to data volume and QPS

Optimization methods:

  • Semantic Cache: return a previous result for a similar query instead of calling the LLM again, substantially reducing LLM cost.
  • Model Routing: use a fast, inexpensive small model for simple questions and a capable large model for complex ones.
  • Reduce embedding dimensions: use Matryoshka embeddings to lower dimensions and reduce storage and query cost.

5.4 Security and Compliance

  • Prompt-injection defense: a user may embed malicious instructions in a query to make the LLM reveal context or act unexpectedly. Input filtering and output review are required.
  • Data isolation: in a multitenant system, tenant A’s query must never retrieve tenant B’s documents. Enforce this with metadata filtering, adding a tenant_id condition at query time.
  • PII handling: if documents contain personal information, redact it during ingestion or filter it from returned results.
  • Data sovereignty: if data cannot leave its jurisdiction, use a self-hosted embedding model and vector database rather than a cloud API.

6. Advanced Architecture Patterns

The preceding sections describe the standard RAG architecture. When evaluation metrics for that architecture reach a plateau, consider the following advanced patterns.

Agentic RAG

Let the LLM decide whether to retrieve, what to retrieve, and how many times to retrieve. Instead of a fixed “retrieve once → generate” flow, use an agent loop. The model may retrieve once, find that information is insufficient, rewrite the query, and retrieve a second time, continuing until it has enough context to answer.

GraphRAG

Add a knowledge graph alongside vector retrieval. Vector search is strong at finding semantically similar text but weak at questions requiring reasoning across documents, such as “Were company A’s CEO and company B’s CTO classmates?” A knowledge graph stores entities and relationships, supplementing this structured reasoning.

Multi-Modal RAG

Retrieve not only text but also images, tables, and charts. If a user asks, “What was the Q3 revenue trend?”, the system can retrieve relevant tables and charts and send them together to a multimodal LLM.

Self-RAG

The model evaluates itself during generation. It first produces part of an answer, then asks, “Do I need more retrieval to support this statement?” If so, it triggers another retrieval. This deeply integrates RAG with reasoning.


How to Build Your First RAG Project

Given your learning plan and technology choices, I recommend this path:

Step 1: Minimal Viable RAG

Run the complete pipeline with the simplest configuration:

  • Markdown/PDF documents → Recursive Splitting at 512 tokens → BGE-M3 embedding → pgvector storage
  • User query → vector retrieval top 5 → Claude/GPT answer generation
  • Run one baseline evaluation with Ragas

Step 2: Add Core Optimizations

Use evaluation results to add components incrementally:

  • Hybrid retrieval: vector + BM25
  • Reranking with BGE-Reranker or Cohere
  • Query rewriting
  • Contextual Chunking

Run evaluation after adding each component and quantify the improvement.

Step 3: Productionize

  • Add Langfuse observability
  • Implement incremental index updates
  • Add a semantic cache
  • Deploy to GCP Cloud Run

This path aligns with the goal in your v4 learning plan to make a Production RAG System your first portfolio project.


References

  • Lewis et al., “Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks”, Facebook AI Research, 2020 — the original RAG paper
  • NVIDIA Technical Blog: Finding the Best Chunking Strategy for Accurate AI Responses, 2025
  • Anthropic: Contextual Retrieval Paper, 2024
  • Vectara / FloTorch Benchmark: 50 academic papers, 7 strategies, February 2026
  • Redis Blog: RAG at Scale — How to Build Production AI Systems in 2026
  • PremAI: Building Production RAG — Architecture, Chunking, Evaluation & Monitoring, 2026
  • Towards AI: 9 RAG Architectures Every AI Developer Must Know, 2026
  • ZTABS: RAG Architecture Explained — Complete Guide, 2026