Building a Simple RAG Pipeline with LangChain

LangChain became one of the most popular frameworks for building RAG (Retrieval-Augmented Generation) pipelines — genuinely useful for quickly assembling the core components, worth understanding what it’s actually doing underneath.

from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain_anthropic import ChatAnthropic

# 1. Load and chunk documents
loader = TextLoader("company_handbook.txt")
documents = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(documents)

# 2. Embed chunks and store in a vector database
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = Chroma.from_documents(chunks, embeddings)

# 3. Build a retrieval-augmented QA chain
llm = ChatAnthropic(model="claude-sonnet-5")
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
)

# 4. Ask a question - relevant chunks are retrieved and passed to the LLM automatically
answer = qa_chain.invoke("What's our company's remote work policy?")

What LangChain Genuinely Automates

The manual version of this pipeline requires writing your own document chunking logic, embedding calls, vector storage and retrieval, and prompt assembly combining retrieved chunks with the user’s actual question — LangChain wraps all of this into a genuinely composable set of reusable components, meaningfully trading some fine-grained control for real development speed.

When to Skip the Framework

For a genuinely simple, well-understood RAG pipeline, writing the components directly yourself (rather than through a framework’s abstraction layer) is sometimes genuinely easier to debug and reason about — LangChain’s real value grows specifically as pipeline complexity grows (multiple chained retrieval steps, agentic tool use combined with retrieval), not necessarily for a single, straightforward retrieve-then-generate flow like the one shown above.