LangChain Tutorial: Build LLM Applications from Scratch (2026)
LangChain emerged as the go-to framework for building applications on top of large language models. It abstracts prompting, chaining, and tool integration into composable components.
By 2026, LangChain supports hundreds of LLM providers, vector stores, and tool integrations. LangSmith adds observability for production LLM applications.
Setting Up LangChain
LangChain is provider-agnostic. You can use OpenAI, Anthropic, Google, or local models via Ollama. The ChatModel interface abstracts provider differences.
Start with a local model through Ollama for development to avoid API costs.
pip install langchain langchain-openai\nfrom langchain_openai import ChatOpenAI\nllm = ChatOpenAI(model="gpt-4", temperature=0)
Chains and Prompt Templates
Chains are sequences of LLM calls. Prompt templates parameterize your prompts with dynamic inputs. LangChain supports few-shot and chat prompt templates.
I built a support triage system using separate chains for intent classification, entity extraction, and response generation.
from langchain.prompts import ChatPromptTemplate\nprompt = ChatPromptTemplate.from_messages([("system", "You are helpful."), ("human", "{q}")])\nchain = prompt | llm
Retrieval Augmented Generation
RAG retrieves relevant documents from a vector store and injects them into the prompt context. This gives the LLM up-to-date knowledge without fine-tuning.
LangChain provides document loaders, text splitters, embedding models, vector stores, and retrieval chains.
from langchain_community.vectorstores import Chroma\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter\ndocs = RecursiveCharacterTextSplitter(chunk_size=1000).split_documents(raw_docs)\nvectorstore = Chroma.from_documents(docs, embeddings)
Conversational Agents with Tools
Agents are LLMs that can use tools like search engines, Python code, or APIs. LangChain provides ReAct and OpenAI Functions agent types.
I built a data analyst agent with tools for SQL queries, matplotlib charts, and documentation access.
from langchain.agents import AgentExecutor, create_react_agent\nagent = create_react_agent(llm, tools, prompt)\nagent_executor = AgentExecutor(agent=agent, tools=tools)
Memory and Conversation History
LLMs are stateless. LangChain provides memory components like ConversationBufferMemory and ConversationSummaryMemory.
For customer support chatbots, ConversationSummaryMemory compresses long conversations into summaries.
from langchain.memory import ConversationBufferMemory\nconversation = ConversationChain(llm=llm, memory=ConversationBufferMemory())\nconversation.predict(input="Hi!")
Evaluation with LangSmith
LangSmith provides tracing, monitoring, and evaluation. You can view every LLM call and tool invocation in a trace viewer.
I set up a regression suite testing 200 conversations after every deployment.
import os\nos.environ["LANGCHAIN_TRACING_V2"] = "true"\nos.environ["LANGCHAIN_API_KEY"] = "your-key"
Frequently Asked Questions
Difference between LangChain and LlamaIndex?
LangChain focuses on chains and agents. LlamaIndex specializes in data indexing and retrieval. Many projects use both.
Do I need an API key?
Yes for proprietary LLMs, but you can use local models through Ollama for free.
How to handle rate limits?
LangChain provides built-in callbacks for rate limiting and retry logic with exponential backoff.
Can LangChain be used in production?
Yes, with LangSmith for monitoring, Pydantic parsers, and async support for high throughput.
Originally published on Ayodhyyya. Last updated June 1, 2026.