*{box-sizing:border-box;margin:0;padding:0;}
body{font-family:’Segoe UI’,sans-serif;color:#1e293b;line-height:1.7;background:#f8fafc;}
.container{max-width:820px;margin:0 auto;padding:24px 16px;}
h1{font-size:2rem;font-weight:800;color:#0D1B2A;line-height:1.25;margin-bottom:18px;}
h2{font-size:1.45rem;font-weight:700;color:#1D4ED8;margin:36px 0 14px;}
h3{font-size:1.1rem;font-weight:700;color:#0D1B2A;margin:20px 0 8px;}
p{margin-bottom:14px;font-size:1rem;}
ul,ol{padding-left:22px;margin-bottom:16px;}
li{margin-bottom:8px;font-size:1rem;}
table{width:100%;border-collapse:collapse;margin:20px 0;font-size:0.93rem;}
th{background:#1D4ED8;color:#fff;padding:10px 12px;text-align:left;}
td{padding:9px 12px;border-bottom:1px solid #e2e8f0;}
tr:nth-child(even) td{background:#f1f5f9;}
pre{background:#1e293b;color:#e2e8f0;padding:20px;border-radius:8px;overflow-x:auto;font-size:0.88rem;line-height:1.6;white-space:pre-wrap;margin:16px 0;}
.takeaway{background:#EEF2FF;border-left:4px solid #4F46E5;border-radius:0 8px 8px 0;padding:16px 20px;margin:18px 0;}
.takeaway strong{color:#4F46E5;display:block;margin-bottom:4px;}
.tl-dr{background:#f0fdf4;border:1px solid #86efac;border-radius:8px;padding:18px 22px;margin:20px 0;}
.tl-dr h3{color:#16a34a;margin-bottom:10px;}
.gai-table-wrap{overflow-x:auto;margin:20px 0;}
.gai-table-wrap table{margin:0;}
@media(max-width:600px){h1{font-size:1.5rem;}h2{font-size:1.2rem;}.gai-table-wrap{font-size:13px;}}
LangChain Tutorial 2026: Build Your First AI App with Python (Step-by-Step)
Direct Answer: LangChain is the most popular LLM orchestration framework in 2026 with 80,000+ GitHub stars, and this tutorial walks you through building a complete document Q&A chatbot with Python — from installation to a working app. LangChain gives you the building blocks — chains, agents, tools, memory, retrievers, and output parsers — to connect LLMs to real-world data and build production AI applications. Whether you want to build RAG chatbots, document summarisers, data extraction pipelines, or autonomous AI agents, LangChain is where you start. AI developers with LangChain skills earn ₹10-25 LPA in India. Prerequisites: basic Python and an API key. Time to complete: under 2 hours.
TL;DR — LangChain Tutorial Python 2026
- What is LangChain: An open-source Python framework (80K+ GitHub stars) for building applications powered by LLMs — chains, agents, tools, memory, and retrieval in one toolkit.
- LCEL (LangChain Expression Language): The new composable pipe syntax (
prompt | model | parser) that replaced legacy chain classes. All new LangChain code uses LCEL. - This tutorial builds: A document Q&A chatbot that loads your PDF, chunks it, stores embeddings in ChromaDB, and answers questions grounded in your document.
- LLM integrations: OpenAI, Anthropic Claude, Google Gemini, Hugging Face, Ollama (local) — swap models with one line of code.
- Use cases: RAG chatbots, document Q&A, summarisation, data extraction, autonomous agents, customer support bots.
- Deployment: Streamlit (quick UI), FastAPI (API backend), LangServe (production serving).
- Career impact: LangChain developers earn ₹10-25 LPA in India. Indian companies across customer support, legal tech, and edtech are hiring.
- Prerequisites: Python basics + an OpenAI (or any LLM provider) API key. No ML background needed.
What Is LangChain? The Core Concept
LangChain is an open-source Python framework that provides the plumbing between LLMs and the real world. An LLM by itself can generate text — but it cannot read your company’s documents, query your database, call external APIs, remember previous conversations, or take actions based on its reasoning. LangChain fills every one of these gaps. It gives you standardised, composable components that handle document loading, text splitting, embedding, vector storage, retrieval, prompt management, output parsing, memory, tool use, and agent orchestration — so you focus on your application logic, not on wiring together API calls.
Think of it this way: if an LLM is an engine, LangChain is the entire vehicle — chassis, steering, transmission, and dashboard. The engine is powerful, but useless without the vehicle around it. LangChain turns a raw LLM into a functional application that can interact with data, hold conversations, use tools, and produce structured output.
In 2026, LangChain has consolidated its position as the default LLM orchestration framework. With 80,000+ GitHub stars, 3,000+ contributors, and integrations with every major LLM provider (OpenAI, Anthropic, Google, Hugging Face, Ollama), it is the framework that Indian companies reach for first when building AI applications. The introduction of LangChain Expression Language (LCEL) — a composable pipe syntax that replaced the older, more verbose chain classes — has made the framework significantly more elegant and maintainable.
LangChain is not an LLM. It is the framework that makes LLMs useful for real applications. Without LangChain (or a similar framework), building a production AI app means writing hundreds of lines of boilerplate for document processing, embedding, retrieval, prompt management, and output parsing. LangChain reduces this to composable, reusable components. If you want to build AI apps with Python in 2026, LangChain is the first framework you learn — everything else (LlamaIndex, CrewAI, AutoGen) assumes you understand the concepts LangChain teaches.
Step-by-Step: Build a Document Q&A Chatbot with LangChain
This tutorial builds a complete document Q&A chatbot. You will load a PDF document, split it into chunks, generate embeddings, store them in a vector database (ChromaDB), and build a retrieval chain that answers questions grounded in your document. By the end, you will have a working AI app and understand every core LangChain concept.
Step 1: Set Up Your Environment
Install LangChain and its dependencies. You need Python 3.9 or higher.
# Create a virtual environment python -m venv langchain-env source langchain-env/bin/activate # Windows: langchain-envScriptsactivate # Install core packages pip install langchain langchain-openai langchain-community pip install chromadb pypdf tiktoken
Set your API key as an environment variable. This tutorial uses OpenAI, but you can swap in Anthropic Claude, Google Gemini, or a local model via Ollama with a single line change.
import os os.environ["OPENAI_API_KEY"] = "your-api-key-here"
Step 2: Load and Split Your Document
LangChain provides document loaders for PDFs, Word files, CSVs, web pages, Notion, and 100+ other sources. Here we load a PDF and split it into chunks optimised for retrieval.
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Load the PDF
loader = PyPDFLoader("your-document.pdf")
pages = loader.load()
# Split into chunks (400 tokens, 50-token overlap)
splitter = RecursiveCharacterTextSplitter(
chunk_size=400,
chunk_overlap=50,
separators=["nn", "n", ". ", " ", ""]
)
chunks = splitter.split_documents(pages)
print(f"Document split into {len(chunks)} chunks")
The RecursiveCharacterTextSplitter is LangChain’s smartest splitter. It tries to split on paragraph breaks first, then sentences, then words — preserving semantic coherence within each chunk. The 400-token chunk size with 50-token overlap is the sweet spot for most RAG applications: large enough to carry context, small enough to be precise.
Step 3: Create Embeddings and Store in ChromaDB
Convert your document chunks into vector embeddings and store them in ChromaDB — a lightweight, free vector database that runs locally.
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
# Create embeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
# Store in ChromaDB
vectorstore = Chroma.from_documents(
documents=chunks,
embedding=embeddings,
persist_directory="./chroma_db"
)
# Create a retriever
retriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 4} # Return top 4 relevant chunks
)
This step does three things: converts each text chunk into a 1,536-dimensional vector using OpenAI’s embedding model, stores the vectors in a local ChromaDB database, and creates a retriever that will find the 4 most relevant chunks for any query. The entire process takes seconds for a typical document.
Step 4: Build the RAG Chain with LCEL
This is where LangChain Expression Language (LCEL) shines. LCEL uses the pipe operator (|) to compose components into a chain — readable, debuggable, and production-ready.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
# Initialise the LLM
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# Define the prompt template
prompt = ChatPromptTemplate.from_template("""
Answer the question based only on the following context.
If you cannot find the answer in the context, say "I don't have
enough information to answer this question."
Context: {context}
Question: {question}
Answer:
""")
# Format retrieved documents into a single string
def format_docs(docs):
return "nn".join(doc.page_content for doc in docs)
# Build the LCEL chain
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Ask a question
response = rag_chain.invoke("What are the key findings in this document?")
print(response)
Read this chain left to right: the user’s question goes to the retriever, which finds relevant chunks and formats them as context. The context and question fill the prompt template. The prompt goes to the LLM. The LLM’s response goes through the output parser, which extracts the string. Four components, one pipe chain, zero boilerplate. This is modern LangChain.
Step 5: Add Conversation Memory
A Q&A bot that forgets the previous question is frustrating. LangChain’s memory components maintain conversation history so the bot can handle follow-up questions.
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
# Set up memory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True,
output_key="answer"
)
# Build conversational chain
qa_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=memory,
return_source_documents=True
)
# Multi-turn conversation
result1 = qa_chain.invoke({"question": "What is the main topic?"})
print(result1["answer"])
result2 = qa_chain.invoke({"question": "Can you elaborate on that?"})
print(result2["answer"]) # Understands "that" from context
The ConversationBufferMemory stores the full conversation history. For longer conversations, use ConversationSummaryMemory (summarises older messages to save tokens) or ConversationBufferWindowMemory (keeps only the last N exchanges). The right memory strategy depends on your use case and token budget.
Step 6: Deploy with Streamlit
Turn your chatbot into a web application with Streamlit in under 20 lines.
# app.py
import streamlit as st
from your_chain import rag_chain # Import your chain from Step 4
st.title("Document Q&A Chatbot")
st.write("Ask questions about your uploaded document.")
if "messages" not in st.session_state:
st.session_state.messages = []
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
if question := st.chat_input("Ask a question..."):
st.session_state.messages.append({"role": "user", "content": question})
st.chat_message("user").write(question)
response = rag_chain.invoke(question)
st.session_state.messages.append({"role": "assistant", "content": response})
st.chat_message("assistant").write(response)
# Run: streamlit run app.py
For production APIs, use FastAPI or LangServe (LangChain’s built-in serving framework) instead of Streamlit. LangServe generates a REST API endpoint from any LCEL chain with automatic input/output validation, streaming support, and a built-in playground UI.
LangChain Use Cases: What You Can Build
The document Q&A chatbot above is one pattern. LangChain supports a broad range of AI application architectures. Here are the use cases driving adoption in India.
- RAG chatbots: The most common use case. Connect an LLM to your company’s knowledge base — product docs, policies, FAQs — and let users ask questions in natural language. Indian e-commerce, banking, and SaaS companies are deploying these at scale for customer support, reducing L1 ticket volume by 60-80%.
- Document summarisation: Load long documents (legal contracts, research papers, financial reports) and generate concise summaries using LangChain’s map-reduce and refine summarisation chains. Legal-tech startups in India use this to process thousands of case laws daily.
- Data extraction: Extract structured data from unstructured text. Parse invoices, resumes, medical records, or any document into clean JSON using LangChain’s output parsers. A single chain replaces months of regex-based extraction code.
- Autonomous AI agents: Build agents that reason about a task, decide which tools to use, execute multi-step plans, and handle errors. LangChain agents can browse the web, query databases, execute code, send emails, and chain multiple actions together. This is the frontier of LLM application development in 2026.
- Conversational AI: Build chatbots with personality, memory, and context awareness. LangChain’s memory modules let you build bots that remember user preferences, track conversation threads, and provide personalised responses across sessions.
LangChain Core Components
| Component | What It Does | Key Classes | When to Use |
|---|---|---|---|
| Models | Interface to LLMs (chat and completion) | ChatOpenAI, ChatAnthropic, ChatGoogleGenerativeAI | Every LangChain app — this is the LLM layer |
| Prompts | Template and manage prompts with variables | ChatPromptTemplate, PromptTemplate, FewShotPromptTemplate | Structuring inputs to the LLM with dynamic data |
| Output Parsers | Parse LLM output into structured formats | StrOutputParser, JsonOutputParser, PydanticOutputParser | When you need JSON, lists, or typed objects from LLM output |
| Retrievers | Fetch relevant documents from a data source | VectorStoreRetriever, MultiQueryRetriever, ContextualCompression | RAG applications — connecting LLMs to your data |
| Memory | Store and recall conversation history | ConversationBufferMemory, ConversationSummaryMemory | Multi-turn chatbots that need context from earlier messages |
| Agents | LLMs that reason, plan, and use tools autonomously | create_react_agent, create_tool_calling_agent | Complex tasks requiring multi-step reasoning and tool use |
| Tools | Functions the agent can call (search, calculate, API calls) | Tool, @tool decorator, built-in tools (search, Wikipedia) | Giving agents capabilities beyond text generation |
| Chains (LCEL) | Compose components into pipelines using pipe syntax | RunnableSequence, RunnablePassthrough, RunnableParallel | Every LangChain app — LCEL is the standard composition pattern |
| Vector Stores | Store and search document embeddings | Chroma, Pinecone, Weaviate, Qdrant, FAISS | RAG — storing and retrieving document chunks by similarity |
| Document Loaders | Load data from 100+ sources | PyPDFLoader, CSVLoader, WebBaseLoader, NotionLoader | Ingesting documents into your RAG pipeline |
Source: LangChain official documentation (2026). Component names reflect the latest LCEL-based API.
The power of LangChain is not in any single component — it is in how they compose together. A retriever fetches relevant documents, a prompt template structures them with the user’s question, a model generates the answer, and an output parser formats the result. LCEL lets you express this entire pipeline as
retriever | prompt | model | parser — one line of code that is readable, testable, and production-ready. Master LCEL and you master modern LangChain development.
Case Study: Indian Edtech Builds Course Recommendation Bot with LangChain
The Problem
An edtech startup in Chennai (80 employees, 15,000+ active students) offered 200+ courses across data analytics, AI/ML, cybersecurity, and digital marketing. Students struggled to find the right course for their background and career goals. The counselling team handled 300+ WhatsApp queries per day, with an average response time of 4-6 hours. 40% of queries were repetitive (“Which course is best for a BCA fresher?”, “What is the fee for the data analytics programme?”).
The Solution
They built a LangChain-powered recommendation chatbot in 3 weeks. The RAG pipeline used LangChain with ChromaDB for course catalogue search, OpenAI GPT-4o-mini as the LLM, and conversation memory to track student preferences across a multi-turn dialogue. Course syllabi, fee structures, placement data, and eligibility criteria were chunked and embedded in ChromaDB. The bot asked clarifying questions (educational background, work experience, budget, career goal), retrieved relevant courses, and provided personalised recommendations with fee details and placement statistics.
Result
The bot handled 65% of incoming queries without human intervention. Average first-response time dropped from 4-6 hours to under 10 seconds. Counsellor workload reduced by 50%, allowing them to focus on high-intent leads. Course discovery improved — students were now finding and enrolling in courses they would not have discovered through manual browsing. Monthly infrastructure cost: ₹8,000 (OpenAI API + hosting). Build cost: 3 weeks of one developer’s time using LangChain.
Common Mistakes When Learning LangChain
- Mistake: Using legacy chain classes instead of LCEL.
Fix: LangChain has undergone a major API shift. The oldLLMChain,SequentialChain, andSimpleSequentialChainclasses are deprecated. All new LangChain code should use LCEL (the pipe|syntax). If you are following a tutorial that importsLLMChain, it is outdated. Useprompt | model | parserinstead. - Mistake: Not pinning your LangChain version.
Fix: LangChain releases frequently and breaking changes are common. Always pin your version inrequirements.txt(e.g.,langchain==0.3.x). Upgrade deliberately, not accidentally. A working app can break overnight if you runpip install --upgrade langchainwithout reviewing the changelog. - Mistake: Overcomplicating your first project with agents.
Fix: Agents are powerful but complex. Start with a simple RAG chain (the tutorial above). Get retrieval working reliably before adding agents, tools, and multi-step reasoning. Most production LangChain apps are chains, not agents. Agents add value only when the task genuinely requires autonomous tool selection and multi-step planning. - Mistake: Ignoring LangSmith for debugging.
Fix: When your chain produces wrong answers, you need to see exactly what happened at each step — what was retrieved, what prompt was sent, what the LLM returned. LangSmith (LangChain’s debugging and monitoring platform) provides full trace visibility for every chain invocation. Set it up from day one. Debugging LangChain without LangSmith is like debugging code without a debugger — technically possible, practically painful. - Mistake: Using chunk sizes that are too large or too small.
Fix: Chunk size is the single most impactful variable in RAG quality. Chunks that are too large (2,000+ tokens) dilute relevant information. Chunks that are too small (50-100 tokens) lose context. Start with 300-500 tokens and 50-100 token overlap. Test retrieval quality on 50+ representative queries before finalising.
Frequently Asked Questions
What is LangChain and why should I learn it in 2026?
LangChain is an open-source Python framework for building applications powered by large language models (LLMs). It provides standardised components for prompt management, document loading, embedding, vector storage, retrieval, memory, agents, and tool use. With 80,000+ GitHub stars, it is the most widely adopted LLM orchestration framework. You should learn it because every production AI application in 2026 — from RAG chatbots to autonomous agents — uses LangChain or a similar framework. LangChain developers earn ₹10-25 LPA in India, and demand far exceeds supply.
What are the prerequisites for this LangChain tutorial?
You need basic Python knowledge (variables, functions, loops, pip installs) and an API key from any LLM provider (OpenAI, Anthropic, Google, or a free local model via Ollama). You do not need a machine learning background, linear algebra knowledge, or GPU hardware. If you can write a Python function and install a pip package, you have everything you need to complete this tutorial and build your first LangChain application.
What is LCEL (LangChain Expression Language)?
LCEL is LangChain’s composable syntax for building chains using the pipe operator (|). Instead of creating chain objects with verbose class initialisation, you write prompt | model | parser — a pipeline that reads left to right. LCEL replaced the legacy chain classes (LLMChain, SequentialChain) and is now the standard way to build LangChain applications. LCEL chains support streaming, async execution, batch processing, and built-in fallback handling. All new LangChain tutorials and documentation use LCEL.
Which LLM should I use with LangChain?
For learning and prototyping: OpenAI GPT-4o-mini (cheapest quality-to-cost ratio, $0.15 per million input tokens). For production applications requiring high accuracy: GPT-4o, Anthropic Claude, or Google Gemini. For local development without API costs: Ollama with Llama 3, Mistral, or Phi-3 running on your machine. LangChain abstracts the LLM layer — you can swap models by changing one line of code (ChatOpenAI to ChatAnthropic to ChatGoogleGenerativeAI). Start with GPT-4o-mini, upgrade when needed.
How is LangChain different from LlamaIndex?
LangChain is a general-purpose LLM orchestration framework — it handles chains, agents, tools, memory, and retrieval across many use cases. LlamaIndex is purpose-built for data retrieval and RAG. If your application is primarily about connecting an LLM to data sources for question answering, LlamaIndex may be simpler. If your application involves agents, multi-step workflows, tool use, or complex chains beyond basic retrieval, LangChain provides more flexibility. Many production systems use both: LlamaIndex for the data ingestion layer and LangChain for the orchestration layer.
Can I build LangChain apps without OpenAI?
Yes. LangChain integrates with 50+ LLM providers. You can use Anthropic Claude (ChatAnthropic), Google Gemini (ChatGoogleGenerativeAI), Hugging Face models, Ollama for local models (Llama 3, Mistral, Phi-3), AWS Bedrock, Azure OpenAI, and many others. For embeddings, you can use Cohere, Hugging Face sentence-transformers, or Ollama embeddings instead of OpenAI. LangChain is provider-agnostic by design — the abstraction layer means your application code stays the same regardless of the underlying LLM.
What is the salary for LangChain developers in India in 2026?
AI developers with LangChain and LLM application skills earn ₹10-25 LPA in India. Entry-level (0-1 year, can build RAG apps and simple chains): ₹6-12 LPA. Mid-level (2-4 years, production LangChain deployments, agent architectures): ₹12-20 LPA. Senior (5+ years, LLM architecture, hybrid RAG + fine-tuning, team leadership): ₹20-35 LPA. The highest demand is in Bengaluru, Hyderabad, and Chennai. Indian companies in customer support automation, legal tech, edtech, and enterprise SaaS are actively hiring LangChain developers. The skill is in the top 5 most-searched AI skills on Indian job portals.
How do I deploy a LangChain app to production?
Three primary options. Streamlit: fastest path to a shareable web app — ideal for demos, internal tools, and MVPs. Deploy on Streamlit Cloud (free tier available). FastAPI: build a REST API backend that any frontend can call. Deploy on any cloud provider (AWS, GCP, Azure, Railway, Render). LangServe: LangChain’s built-in production serving framework that auto-generates API endpoints from LCEL chains with streaming, input validation, and a playground UI. For production deployments, pair your serving layer with LangSmith for monitoring, tracing, and debugging in production.
Your Next Step: Build AI Applications with LangChain
You have just built a document Q&A chatbot with LangChain — from raw PDF to working conversational AI in six steps. This is not a toy project. This is the exact architecture that Indian companies are deploying in production for customer support, internal knowledge management, legal document search, and course recommendation. The only difference between this tutorial and a production system is scale, error handling, and monitoring — all of which LangChain and LangSmith handle.
The career data is clear: LangChain developers earn ₹10-25 LPA in India, and demand is accelerating. Every company building AI applications needs engineers who can architect RAG pipelines, build agents, integrate vector databases, and deploy LangChain apps to production. The framework is the easy part — what separates job-ready developers from tutorial-followers is hands-on experience building real applications with real data, real edge cases, and real deployment constraints.
If you want to go beyond tutorials and build production-grade LLM applications — RAG pipelines, autonomous agents, fine-tuning workflows, and deployment — you need structured, project-based training with mentorship and placement support. The gap between “I completed a tutorial” and “I shipped a production AI app” is the gap that companies pay ₹10-25 LPA to fill.