What is LangChain? Why It Matters, Loaders, LLM Connectors & a Tutorial
Building with large language models quickly gets repetitive: you write the same glue code to call a model, swap providers, load documents, format prompts, and parse outputs. LangChain is the framework that standardizes all of that — so you can focus on your application instead of the plumbing.
What is LangChain?
LangChain is an open-source framework for building applications powered by large language models. It gives you a consistent set of building blocks — model connectors, prompt templates, document loaders, text splitters, vector stores, and chains — that snap together. The biggest idea: a single, unified interface across LLM providers, so the same code can run on Google Gemini, Anthropic Claude, OpenAI, or a local model with a one-line change.
Why use LangChain?
You can call an LLM's API directly, and for a single prompt that's fine. LangChain earns its place once your app needs more than one moving part:
- Provider-agnostic — swap between Gemini, Claude, and others without rewriting your app, so you avoid vendor lock-in.
- Batteries included — ready-made document loaders, text splitters, embeddings, and vector-store integrations for building RAG.
- Composable — its LangChain Expression Language (LCEL) lets you pipe components together with a simple | operator.
- A huge ecosystem — hundreds of integrations (databases, APIs, tools) plus LangGraph for agents and LangSmith for observability.
Connecting to an LLM
Each provider has its own LangChain package. Install the core library plus the connectors you need:
pip install langchain langchain-core langchain-community
pip install langchain-google-genai langchain-anthropic
pip install pypdf # used later, for loading PDFs
# Set your API keys as environment variables
export GOOGLE_API_KEY="your-google-api-key"
export ANTHROPIC_API_KEY="your-anthropic-api-key"Google Gemini — ChatGoogleGenerativeAI
The connector for Gemini lives in langchain-google-genai. Create the model, then call .invoke() with your prompt:
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
response = llm.invoke("Explain what LangChain is in one sentence.")
print(response.content)Anthropic Claude — ChatAnthropic
The connector for Claude lives in langchain-anthropic. Note that the latest Claude Opus models manage their own sampling, so we don't pass a temperature — we just set the model and a max_tokens limit:
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-opus-4-8", max_tokens=1024)
response = llm.invoke("Explain what LangChain is in one sentence.")
print(response.content)Here's the key payoff: both objects expose the exact same .invoke() interface. To switch your whole application from Gemini to Claude, you change one line — the line that creates llm. Everything downstream (prompts, chains, parsers) stays identical. That's the provider-agnostic promise in action.
Loading your data: document loaders
Most real applications need your own data — PDFs, spreadsheets, web pages. LangChain's document loaders turn files into a standard Document object (text plus metadata) that the rest of the pipeline understands. Loading a PDF takes three lines:
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader("report.pdf")
docs = loader.load() # one Document per page
print(f"Loaded {len(docs)} pages")
print(docs[0].page_content[:200]) # preview the first pageThere are loaders for almost any source — and they all return the same Document shape, so the code after them never changes:
- PyPDFLoader — PDF files (one Document per page)
- TextLoader — plain .txt files
- CSVLoader — each CSV row becomes a Document
- Docx2txtLoader — Microsoft Word .docx files
- WebBaseLoader — fetch and parse a web page by URL
- DirectoryLoader — load every matching file in a folder at once
Putting it together: a tiny chain
Now combine a prompt template, your model, and your loaded data into one chain using LCEL — the | operator pipes the output of each step into the next:
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(
"Summarize the following text in 3 bullet points:\n\n{text}"
)
chain = prompt | llm # pipe the prompt into the model (LCEL)
result = chain.invoke({"text": docs[0].page_content})
print(result.content)The prompt fills in {text}, passes the formatted message to the model, and returns the response. Because llm is just a variable, this exact chain runs on Gemini or Claude depending only on which connector you assigned earlier — no other change needed.
When should you use LangChain?
Reach for LangChain when you want provider flexibility, need to load and process your own documents (RAG), or are composing multiple steps — prompt, model, retrieval, parsing. For a single one-off API call with no data and no provider-switching, the raw provider SDK is simpler. LangChain shines as soon as your application has more than one moving part.
Want to go from these basics to building real RAG and agentic AI systems with LangChain and LangGraph — hands-on? That's exactly what I teach in my AI/ML bootcamps and corporate workshops.
Get in touchApa itu LangChain? (Ringkasan Bahasa Indonesia)
LangChain adalah framework open-source untuk membangun aplikasi berbasis large language model (LLM). Ia menyediakan komponen siap pakai — konektor model, template prompt, document loader, text splitter, dan vector store — yang bisa dirangkai. Ide utamanya: satu antarmuka yang seragam untuk berbagai provider, sehingga kode yang sama bisa berjalan di Google Gemini, Anthropic Claude, atau model lain hanya dengan mengubah satu baris.
Mengapa pakai LangChain?
- Tidak terikat satu provider — berpindah antara Gemini, Claude, dan lainnya tanpa menulis ulang aplikasi.
- Lengkap — document loader, text splitter, embeddings, dan integrasi vector store untuk membangun RAG.
- Mudah dirangkai — LangChain Expression Language (LCEL) menyambung komponen dengan operator | .
- Ekosistem besar — ratusan integrasi, plus LangGraph untuk agent dan LangSmith untuk observability.
Gunakan LangChain ketika Anda butuh fleksibilitas provider, perlu memuat dan memproses dokumen sendiri (RAG), atau merangkai beberapa langkah sekaligus. Untuk satu panggilan API sederhana, SDK provider langsung sudah cukup.
Ingin belajar membangun sistem RAG dan agentic AI nyata dengan LangChain dan LangGraph secara hands-on? Itulah yang saya ajarkan di bootcamp dan corporate workshop AI/ML saya.
Get in touch