Why RAG over fine-tuning?
Fine-tuning an LLM on your data is expensive, time-consuming, and goes stale as soon as your data changes. RAG, by contrast, is dynamic: your knowledge base is updated continuously, and the model always queries the most recent data with every request.
That's why, for 90% of enterprise use cases (internal documentation, customer support, contract analysis), RAG is the right answer.
Target architecture
Our reference stack for a production-ready RAG system:
Ingestion & chunking
Load documents (PDF, Word, web, APIs), split them into coherent, overlapping chunks, then clean and normalize the text.
Embedding & vector storage
Vectorize with text-embedding-3-large (OpenAI) or a local model. Store the vectors in Postgres with the pgvector extension for native vector queries.
Hybrid retrieval
Combine semantic search (cosine similarity) with BM25 full-text search to maximize the precision of the retrieved context.
Generation & evaluation
Build the prompt with the retrieved context, call the LLM, and automatically evaluate responses using RAGAS or LangSmith.
The code that matters
from langchain_postgres import PGVector
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
vectorstore = PGVector(
embeddings=embeddings,
collection_name="knowledge_base",
connection="postgresql+psycopg://user:pass@localhost/ragdb",
use_jsonb=True,
)from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
retriever = vectorstore.as_retriever(
search_type="mmr",
search_kwargs={"k": 6, "fetch_k": 20}
)
prompt = ChatPromptTemplate.from_template("""
Answer the question based solely on the provided context.
If you can't find the answer, say so clearly.
Context: {context}
Question: {input}
""")
chain = create_retrieval_chain(
retriever,
create_stuff_documents_chain(llm, prompt)
)Classic mistakes to avoid
Chunks that are too large or too small
A 2000-token chunk buries relevant information in noise. A 50-token chunk loses the context needed for understanding. Our sweet spot: 400-600 tokens with an 80-token overlap.
Skipping the evaluation phase
A RAG pipeline without evaluation metrics is flying blind. We systematically use RAGAS to measure response faithfulness, retrieval relevance, and the absence of hallucinations.
Pro tip: Enable retrieval logging from day one. It's your best debugging tool: you'll see immediately whether the problem lies in retrieval or in generation.
Production checklist
Before going to production, check that you have: incremental indexing (no full re-index on every update), batch embedding to cut API costs, caching for frequent embeddings, P95 latency monitoring, and automated regression tests on a golden dataset.
This pipeline, its evaluation and its move to production make up an entire module of the AI Engineering Bootcamp.
Want an agent that answers from your own documents? Our playbook on training an AI agent for your business goes from audit to production, with a scoring grid and a 6 to 8 week timeline. Comparing courses? Our guide to choosing a Python bootcamp for data science and machine learning gives you the curriculum checklist, the cost worksheet and the questions to ask.