Building a RAG Chain End-to-End
Stitch it together: loader -> splitter -> embeddings -> vector store -> retriever -> prompt -> model.
Building a RAG Chain End-to-End is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Project Goal
Combine loaders, splitters, vector stores, and LCEL into a complete production-shaped RAG chain.
Step 1: Load and Chunk
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
loader = PyPDFLoader('handbook.pdf')
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
chunks = splitter.split_documents(docs)
print(f'{len(chunks)} chunks loaded')Step 2: Embed and Store
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
embeddings = OpenAIEmbeddings(model='text-embedding-3-small')
store = Chroma.from_documents(
chunks,
embeddings,
persist_directory='./handbook_db'
)Step 3: Set Up Retriever
retriever = store.as_retriever(
search_type='similarity',
search_kwargs={'k': 4}
)Step 4: Define the Prompt
from langchain.prompts import ChatPromptTemplate
rag_prompt = ChatPromptTemplate.from_template('''
You are a helpful assistant. Use the context below to answer.
If the answer is not in the context, say you do not know.
Cite sources like [1], [2].
Context:
{context}
Question: {question}
Answer:
''')Step 5: Format Documents Helper
def format_docs(docs):
return '\n\n'.join(
f'[{i+1}] (source: {d.metadata.get("source", "?")}, page {d.metadata.get("page", "?")})\n{d.page_content}'
for i, d in enumerate(docs)
)
from types import SimpleNamespace
docs = [
SimpleNamespace(metadata={'source': 'handbook.pdf', 'page': 3}, page_content='Vacation policy details.'),
SimpleNamespace(metadata={'source': 'handbook.pdf', 'page': 5}, page_content='Sick leave details.'),
]
print(format_docs(docs))
Step 6: Assemble the LCEL Chain
from langchain_core.runnables import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model='gpt-4o-mini', temperature=0)
rag_chain = (
{'context': retriever | format_docs, 'question': RunnablePassthrough()}
| rag_prompt
| model
| StrOutputParser()
)Step 7: Invoke
answer = rag_chain.invoke('What is our refund policy?')
print(answer)Step 8: Stream the Answer
for chunk in rag_chain.stream('What is our refund policy?'):
print(chunk, end='', flush=True)Step 9: Return Sources Separately
Sometimes you want both the answer AND the retrieved docs in the output:
from langchain_core.runnables import RunnableParallel
rag_with_sources = RunnableParallel(
context=retriever,
answer=rag_chain
)
result = rag_with_sources.invoke('What is our refund policy?')
print(result['answer'])
for doc in result['context']:
print(doc.metadata)Step 10: Conversational RAG
Add history-aware retrieval — rewrite follow-up questions to standalone form before retrieval:
from langchain.chains import create_history_aware_retriever
# 'And what about XL sizes?' -> 'What is the refund policy for XL sizes?'Step 11: Eval the Chain
LangSmith integration captures every chain invocation. You can build datasets from real usage and run evals.
Production Considerations
- Pin model versions
- Set max_tokens
- Add token usage tracking
- Implement retries with fallback model
- Cache embeddings (already deterministic in OpenAI)
- Add Langfuse/LangSmith for traces
format_docs Purpose
Why use a format_docs helper between the retriever and the prompt?
Recap
You built a complete RAG chain with LCEL. This skeleton scales to production with the additions above.
Frequently asked questions
Is the “Building a RAG Chain End-to-End” lesson free?
Yes — the full text of “Building a RAG Chain End-to-End” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Building a RAG Chain End-to-End”?
Stitch it together: loader -> splitter -> embeddings -> vector store -> retriever -> prompt -> model. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a RAG Chain End-to-End” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- LangChain Architecture: Models, Prompts, Chains
- Loaders, Splitters and Vector Stores
- LCEL (LangChain Expression Language)
- Building a RAG Chain End-to-End