Home/AI/ML Notes/Basics of Agents/Retrieval-Augmented Generation (RAG)
⌂ Main Page
Basics of Agents

Retrieval-Augmented Generation (RAG)

Best practices, benefits, RAG vs. fine-tuning, RAG flavors, embedding quantization, common errors, and agentic RAG.

On this page

RAG Best Practices

RAG grounds LLM in external knowledge - answering Qs by retrieving relevant context from your own docs => sends it to LLM to generate accurate answers. This prevents hallucinations or incorrect info, provides references. Deploying requires extensive experimentation to tune / optimize many params:

RAG Benefits

RAG vs. Fine-Tuning

Fine-tuning increases accu by 6%. RAG additionally increases accu by 5% more. Choice of approach depends on specific app, nature and size of data, available resources for model development. Use either or both:

  1. Fine-tuning = model adaptation - changing the LLM’s behavior (structure=weights), vocabulary, writing style, customizing model's tone or jargon for a niche application, refine safety and helpfulness - knowledge mostly unchanged (Google example);

  2. RAG relies on updated external data to generate outputs grounded to custom knowledge while the LLM’s vocab and writing style are unchanged. RAG is for knowledge, fine-tune is inefficient for this.

  3. if your app needs both custom knowledge & LLM adaptation - use a hybrid approach (RAG + fine-tuning),

  4. if you don't need either, prompt engineering is the way to go.

Types of RAG: 1) GraphRAG (RAG w/knowledge graphs (KGs) for augmenting context w/structured domain-specific knowledge; automated KG construction (triple extraction); best for multi-hop questions (facts across docs) & global/corpus-level Qs (‘main themes across all reports’) where chunk-level vector RAG fails, 2) LightRAG - simple & fast, graph-enhanced, near-GraphRAG quality at a fraction of cost/latency: extracts entity–relation graph from docs; dual-level retrieval (low-level: specific entities/relations; high-level: broader themes) = graph traversal + vector search; supports incremental index updates (no full rebuild): Article + GitHub, 3) RAFT trains special Q&A model w/CoT responses - robust in ignoring irrelevant distractor docs, 4) SELF-RAG - self-reflection using fine-tuned LLM to predict if retrieval is needed & then evaluate relevance of retrieved info, 5) Corrective RAG: uses lightweight T5 model to assess quality of retrieved docs (classify as Correct, Ambig. - supplement w/web search, Incorrect - replace w/web) - more flexible, easy to implement than Self-RAG. 6) RAT = RAG+COT: CoT reasonretrieve CoT steps → refineanswer.

Embedding Quantization & Truncation: substantial cost and latency reductions in retrieval and similarity search + fewer bits for storage. Example: [12, 1, -100, 0.3,] => [1,1,0,1,] (0 if negative):

Embedding Truncation: w/min. performance loss, faster retrieval, clustering, etc. Train model on domain. Matryoshka Representation Learning (MRL) - leading dims carry most information; truncate e.g. 1024→256 dims for ~4x cheaper search w/small accuracy loss; optionally rescore top candidates w/full-dim vectors.

Errors in RAG Systems

Chunking

Retrieval

Generation

Agentic RAG System - using AI agents to optimize information retrieval and synthesis.

Agentic RAG systems

AI Agents perform tasks autonomously. Benefits: efficiency & cost reduction, scalability & adaptability, enhanced decision making (increasing workloads, respond quickly to market changes).