
Dan Patiño
AI Strategy & Innovation at Coderhouse
Artificial Intelligence
A RAG (retrieval-augmented generation) application answers questions by first retrieving relevant passages from your own documents and then asking a large language model (LLM) to write an answer grounded in those passages. Building your first one takes five steps: prepare and chunk your documents, turn the chunks into embeddings, store them in a vector database, retrieve the best matches for each question, and generate an answer with the retrieved context in the prompt. The hard part is not getting a demo to run; it is making retrieval reliable and measuring whether the answers are actually correct.
This tutorial walks through each step, the decisions that matter, and how to tell whether your RAG app is good enough to show anyone.
Why RAG matters
LLMs only know what was in their training data, and they can produce confident answers that are wrong. RAG addresses both problems by giving the model fresh, specific context at question time. The approach was formalized in the research paper Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, and it is now the standard pattern behind internal knowledge assistants, support bots, and document search tools. If hallucinations are new to you, start with why LLMs make things up.
What you need before you start
Comfort with Python and installing packages
An API key for an LLM provider such as OpenAI or Anthropic
A small, real document set, such as 20 to 50 pages of product docs, policies, or course notes
A list of 15 to 20 questions you know the correct answers to, which you will use for testing
Step-by-step: build your first RAG app
Step 1: Load and chunk your documents
Split documents into passages small enough to be specific but large enough to keep meaning, often a few hundred tokens with some overlap. Keep metadata such as the source file and section title so you can cite where an answer came from. Chunking choices affect quality more than most beginners expect; this guide to chunking explains the trade-offs.
Step 2: Create embeddings
An embedding model turns each chunk into a vector, a list of numbers that captures meaning, so similar text ends up close together. Provider documentation such as the OpenAI embeddings guide shows how to call these models. For the concept behind it, read what embeddings are and why RAG depends on them.
Step 3: Store vectors in a vector database
For a first project, a local vector store such as Chroma is enough. Store each vector with its chunk text and metadata. Managed options like Pinecone make sense later when you need scale or a hosted service.
Step 4: Retrieve the best matches
When a question arrives, embed it with the same model and fetch the top few most similar chunks. Start with three to five. Then improve retrieval with two common upgrades:
Hybrid search, which combines vector similarity with keyword matching so exact terms like product codes are not missed. See how hybrid search works.
Reranking, where a second model reorders candidates by relevance before they reach the LLM.
Anthropic's write-up on contextual retrieval is a useful read on why adding context to chunks and combining retrieval methods reduces failed lookups.
Step 5: Generate a grounded answer
Build a prompt that includes the retrieved chunks and clear instructions: answer only from the provided context, cite the source, and say "I don't know" when the context does not contain the answer. That last instruction prevents a large share of made-up answers.
Step 6: Wrap it in a simple interface
A small FastAPI endpoint or a basic web page is enough to let others try it. Log every question, the retrieved chunks, and the answer. Those logs are how you debug.
How to know if your RAG app works
A RAG demo that answers three questions well can still fail half the time on real use. Test it the way an engineer would:
Check retrieval separately. For each test question, did the correct chunk appear in the retrieved set? If not, fix chunking, embeddings, or search before touching the prompt.
Check answer quality. Is the answer correct, supported by the retrieved text, and honest when information is missing?
Track cost and latency. Bigger contexts and more chunks cost more and respond slower.
Break it on purpose. Ask questions outside your documents, use typos, and ask about near-duplicate topics.
For metrics and methods, see how to measure retrieval quality in RAG.
Common beginner mistakes
Tuning the prompt when the real problem is that retrieval never found the right chunk
Using huge chunks that bury the relevant sentence
Embedding questions and documents with different models
Testing only with questions you wrote while reading the documents, which makes retrieval look better than it is
Skipping source citations, which makes answers impossible to verify
Frequently asked questions
Do I need a framework like LangChain to build RAG?
No. You can build a first RAG app with an LLM SDK, an embedding model, and a vector store. Frameworks like LangChain or LangGraph help once you add tools, memory, or multi-step workflows.
Is RAG better than fine-tuning?
They solve different problems. RAG adds up-to-date knowledge at question time; fine-tuning changes how a model behaves or writes. Most teams start with RAG because it is cheaper to update.
What should a good RAG course teach?
Look for chunking, embeddings, vector search, hybrid search and reranking, evaluation with test sets, and deployment, plus a project reviewed by an instructor.
Build RAG systems with live instruction
If you want to go from tutorial to production-style RAG with feedback from practitioners, Coderhouse offers two live online courses with one class per week:
AI Engineering Course: 8 weeks for developers comfortable with Python, covering embeddings, chunking, vector retrieval, RAG with hybrid search and reranking, evaluation, agents, and deployment, ending with a capstone.
Data Analytics Course: 11 weeks of SQL, Python, visualization, statistics, experimentation, and AI-assisted analysis.
I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.