Leanpub Header

Skip to main content

The Art and Science of RAG

Understanding, Designing, and Deploying Retrieval-Augmented Generation Systems

The Art and Science of RAG
This book is 100% completeLast updated on 2026-09-28

Unlock the full potential of large language models with RAG. This book takes you from core concepts to production-ready systems, exploring everything from embeddings and search to evaluation and security. With clear explanations, runnable code and practical insights, you'll learn how to build reliable AI systems grounded in the knowledge that matters.

Minimum price

$25.00

$35.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
242
Pages
About

About

About the Book

Large language models are powerful, but they hallucinate, their knowledge is frozen in time, and they cannot natively access private or proprietary information. Retrieval-Augmented Generation (RAG) addresses all of these problems by combining external knowledge retrieval with language model generation. This book takes you from the foundational concepts behind RAG through every design decision that matters in production: embeddings, chunking, indexing, hybrid search, reranking, context construction, generation control, evaluation, security, and system architecture. You will understand not only what each component does, but why it exists, how it works internally, the trade-offs between competing approaches, and when each technique is appropriate. Through complete runnable code examples and practical guidance, this book functions both as a first-principles textbook and a reference you will use when building real systems.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Understanding, Designing, and Deploying Retrieval-Augmented Generation Systems

Chapter 1: Why RAG? The Case for Retrieval-Augmented Generation

  1. The Knowledge Problem with Language Models
  2. A Simple Example: What Goes Wrong Without Retrieval
  3. The Core Idea of RAG in One Sentence
  4. What This Book Will Teach You

Chapter 2: Language Model Limitations and the Need for External Knowledge

  1. How Pretrained Language Models Store Knowledge
  2. Hallucinations: Causes and Manifestations
  3. The Training Cutoff Problem
  4. Parameter-Efficient Knowledge Updating
  5. Why Finetuning Alone Is Not Enough
  6. The Scalability Argument for Retrieval

Chapter 3: Information Retrieval Foundations

  1. What Is Information Retrieval
  2. The Vector Space Model and TF-IDF
  3. The BM25 Algorithm
  4. Inverted Indexes and Boolean Retrieval
  5. Relevance Metrics: Precision, Recall, NDCG
  6. The History That Led Us to RAG

Chapter 4: Embeddings and Vector Representations

  1. From Words to Vectors: Word Embeddings to Sentence Embeddings
  2. What an Embedding Actually Represents
  3. Architecture of Modern Embedding Models
  4. Cosine Similarity and Vector Distance Metrics
  5. Evaluating Embedding Quality
  6. Choosing an Embedding Model

Chapter 5: Similarity Search and Vector Databases

  1. The Nearest Neighbor Problem at Scale
  2. Brute-Force Search and Its Limits
  3. Approximate Nearest Neighbor Algorithms: HNSW, IVF, PQ
  4. Vector Database Architecture and Design
  5. Choosing a Vector Database
  6. Vector Index Maintenance

Chapter 6: Document Ingestion and Preprocessing

  1. The Ingestion Pipeline End-to-End
  2. Document Format Parsing: PDFs, HTML, Office Documents
  3. Text Cleaning and Normalization
  4. Handling Special Content: Tables, Code, Images, Math
  5. Metadata Extraction and Structuring
  6. Building a Robust Ingestion System

Chapter 7: Chunking Strategies

  1. Why Chunking Matters
  2. Fixed-Size and Sliding-Window Chunking
  3. Semantic and Recursive Chunking
  4. Chunking for Code, Legal Documents, and Technical Manuals
  5. Parent-Child and Hierarchical Chunking
  6. Measuring and Tuning Chunk Quality

Chapter 8: Index Construction

  1. Embedding and Storing Chunks
  2. Building Vector Indexes at Scale
  3. Combining Dense and Sparse Indexes
  4. Metadata Indexing and Filtering
  5. Incremental Indexing and Data Freshness
  6. Index Versioning and Rollbacks

Chapter 9: Basic Retrieval

  1. The Retrieval Operation
  2. Dense Retrieval Mechanics
  3. Sparse Retrieval Mechanics
  4. Combining Dense and Sparse: Hybrid Search
  5. Metadata Filtering and Faceted Search
  6. Retrieval Performance Tuning

Chapter 10: Query Transformation and Expansion

  1. The Query Transformation Problem
  2. Query Rewriting for Clarity and Completeness
  3. Multi-Query Retrieval
  4. Hypothetical Document Embeddings (HyDE)
  5. Query Decomposition for Complex Questions
  6. When Query Transformation Hurts

Chapter 11: Reranking

  1. Why Reranking Is Necessary
  2. Cross-Encoder Architecture
  3. Reranker Training and Fine-Tuning
  4. Late-Interaction and ColBERT Models
  5. Practical Reranking: Latency vs. Quality Trade-Offs
  6. Choosing and Configuring a Reranker

Chapter 12: Context Construction and Prompt Design

  1. From Chunks to Context
  2. Formatting Retrieved Context for the LLM
  3. Context Compression and Summarization
  4. Token Budget Management
  5. Prompt Design for Grounded Generation
  6. Structuring Multi-Chunk Contexts

Chapter 13: The Generation Step

  1. Guiding the LLM with Retrieved Context
  2. Temperature, Top-P, and Generation Control
  3. Enforcing Faithfulness to Context
  4. Handling Conflicting or Insufficient Evidence
  5. Streaming Responses and Partial Answers
  6. Generation Failure Modes

Chapter 14: Building a Naïve RAG Pipeline

  1. Designing the Naïve RAG Architecture
  2. Project Structure and Dependencies
  3. Ingestion and Indexing Code
  4. Retrieval Implementation
  5. Generation Integration
  6. End-to-End Execution and Testing

Chapter 15: Advanced RAG Patterns

  1. Hybrid Search with Reranking: The Standard Advanced Pattern
  2. Contextual Compression Retrieval
  3. Self-Correction and Self-RAG
  4. Adaptive Retrieval Strategies
  5. Implementing Advanced Patterns

Chapter 16: Modular RAG Architectures

  1. The Case for Modularity
  2. Toolformer-Inspired Approaches
  3. Modular Design Patterns
  4. Routing and Orchestrating Modules
  5. Implementing a Modular RAG System

Chapter 17: Hierarchical and Parent-Child Retrieval

  1. The Granularity Problem
  2. Parent-Child Retrieval
  3. Multi-Level Hierarchical Indexes
  4. Small-to-Big Retrieval
  5. Implementing Hierarchical Retrieval

Chapter 18: Multi-Hop and Agentic RAG

  1. Single-Hop vs. Multi-Hop Reasoning
  2. Iterative Retrieval Strategies
  3. Agentic RAG: Reasoning, Planning, Acting
  4. Search and Retriever Tools for Agents
  5. Controlling Agentic RAG Complexity
  6. Implementing Multi-Hop Retrieval

Chapter 19: GraphRAG and Knowledge Graphs

  1. Why Graphs for RAG
  2. Knowledge Graph Construction from Documents
  3. Graph-Based Retrieval
  4. GraphRAG Architecture Patterns
  5. Implementing GraphRAG

Chapter 20: Conversational RAG and Multimodal RAG

  1. Conversational RAG: Maintaining Context Across Turns
  2. Conversational Query Understanding
  3. Conversation History in the Prompt
  4. Multimodal Embeddings and Retrieval
  5. Image, Audio, and Video in RAG
  6. Implementing Conversational RAG

Chapter 21: Long-Context Approaches and Structured Data

  1. Long-Context LLMs: RAG Competitors or Complements
  2. Attention at Scale
  3. When Long Context Is Preferable to RAG
  4. Hybrid: RAG with Long-Context Models
  5. Retrieval from Structured and Tabular Data
  6. SQL Retrieval and Text-to-SQL
  7. Retrieval from Structured and Tabular Data

Chapter 22: RAG Evaluation: Measuring What Matters

  1. What to Evaluate: A Complete Taxonomy
  2. Retrieval Quality Metrics: Recall, Precision, MRR, NDCG
  3. Answer Quality and Faithfulness Metrics
  4. Hallucination Detection
  5. Automated Evaluation: RAGAS, TruLens, and Custom Metrics
  6. Human Evaluation and Benchmark Design

Chapter 23: Debugging RAG Systems

  1. Classifying RAG Failures
  2. Retrieval Failures: Diagnosing and Fixing
  3. Generation Failures: Diagnosing and Fixing
  4. End-to-End Debugging Workflow
  5. Logging, Tracing, and Observability
  6. Continuous Improvement Loops

Chapter 24: Security and Access Control for RAG

  1. Prompt Injection: Direct Attacks
  2. Prompt Injection: Indirect Attacks Through Retrieved Documents
  3. Authorization-Aware Retrieval
  4. Data Leakage
  5. Tenant Isolation in Multi-Tenant Systems
  6. Malicious Documents and Content Poisoning
  7. Red-Teaming and Security Testing

Chapter 25: Production Architecture and Deployment

  1. Small-Scale Architecture: Single-Service Design
  2. Enterprise Architecture: Modular and Scalable Design
  3. Multi-Tenant Architecture
  4. Deployment Options: Cloud, On-Premise, and Hybrid
  5. Scalability Patterns
  6. High Availability and Fault Tolerance

Chapter 26: Production Operations and Optimization

  1. Monitoring and Observability
  2. Latency Optimization
  3. Cost Optimization
  4. Data Freshness and Incremental Indexing
  5. Versioning, Rollback, and Continuous Deployment

Chapter 27: Real-World Case Studies and Patterns

  1. Case Study 1: Customer Support Bot for SaaS Company
  2. Case Study 2: Enterprise Knowledge Management System
  3. Case Study 3: Financial Research and Analysis
  4. Case Study 4: Healthcare Information System
  5. Case Study 5: Developer Documentation Assistant
  6. Case Study 6: E-Commerce Product Information System
  7. Lessons Across Case Studies

Chapter 28: Conclusion: The RAG Landscape and Future Directions

  1. The Core Insight: Why RAG Endures
  2. Key Principles for Effective RAG
  3. The RAG Technology Landscape
  4. Future Directions: Where RAG Is Heading
  5. Getting Started: Practical Guidance
  6. The Promise of RAG

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub