Back to Blog
ai-news
1 min read
AI-powered content

Building an LLM From Scratch — Part 8: RAG Theory

Theoretical foundations of Retrieval-Augmented Generation covering vector embeddings, similarity search, document chunking strategies, and hybrid retrieval.

chatgpt, ai, llm

2026 update note

Older publish date · context add-on

Editorial note for 2026. This does not replace the historical article below.

  • Prefer current official docs for frameworks, APIs, and package names; sample code here is mostly pedagogical—check release notes when migrating.
  • Llama / RAG / Prompt ecosystems move fast; pair this post with 2026 articles and the tools directory on this site.
  • If you spot factual drift, reach out via the footer—we will refresh this note or spin up a follow-up post.

Recent picks: Observability · Graph RAG · Prompting & tools · Small-model deployment

Building an LLM From Scratch — Part 8: RAG Theory

RAG combines retrieval systems with generative models for knowledge-grounded text generation.

Vector Search

Documents are embedded into vectors using models like BGE, E5, or Instructor. ANN indexes (HNSW, IVF) enable sub-10ms search over millions of vectors, trading 5-10% recall for orders-of-magnitude speed improvement over brute-force search.

Chunking Strategies

Fixed-size: Simple and predictable. 512 tokens with 64 token overlap prevents middle-of-sentence splits.

Semantic: Split at topic boundaries using embedding similarity thresholds.

Recursive: Start with large chunks, fall back to smaller sizes when chunks exceed length limits.

Hybrid Search

Dense embeddings capture semantic similarity. BM25 captures exact keyword matches. Hybrid search combines scores with inverted weighting: alpha * dense_score + (1-alpha) * keyword_score.

Reranking

Cross-encoder rerankers evaluate query-document pairs jointly, producing more accurate relevance scores than bi-encoder embeddings. Improves top-5 relevance by 15-25%.

chatgptaillmprompt-engineeringprogramming

Related Content

Articles

Related Tools

Related Workflows