LangChain LLM Framework 9 min read

LangChain Guide 2025: Build LLM Apps & AI Agents

LangChain is the most widely used framework for building LLM applications in Python and JavaScript. It standardizes how you chain models, tools, memory, and data retrievers into production-ready AI apps and agents.

What Is LangChain?

LangChain (released 2022) is an open-source framework that provides:

  • Unified interfaces to 50+ LLM providers (OpenAI, Anthropic, Groq, HuggingFace, Ollama, etc.)
  • Abstractions for building chains — sequences of LLM calls, tools, and parsers
  • Memory systems for persisting conversation history across turns
  • Document loaders, text splitters, and vector store integrations for RAG
  • Agent executors that let LLMs decide which tools to call
  • LangGraph for stateful, graph-based agentic workflows
  • LangSmith for tracing, evaluation, and observability

LangChain's GitHub repo has 90k+ stars. It's used by teams at Stripe, Notion, Replit, and thousands of startups shipping AI features.

Installation

# Core LangChain package
pip install langchain

# With OpenAI provider
pip install langchain langchain-openai

# With Anthropic provider
pip install langchain langchain-anthropic

# JavaScript / TypeScript
npm install langchain @langchain/openai

LangChain split into modular packages in 2024. You install provider-specific packages (langchain-openai, langchain-anthropic) separately from the core framework. This reduces dependency bloat.

LCEL — LangChain Expression Language

LCEL is LangChain's modern way to compose chains using the | pipe operator. It replaces legacy classes and gives you streaming, async, and batching for free:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Define components
prompt = ChatPromptTemplate.from_template(
    "Summarize this in one sentence: {text}"
)
model = ChatOpenAI(model="gpt-4o-mini")
parser = StrOutputParser()

# Compose with LCEL pipe syntax
chain = prompt | model | parser

# Invoke
result = chain.invoke({"text": "LangChain is a framework..."})

# Stream
for chunk in chain.stream({"text": "LangChain is a framework..."}):
    print(chunk, end="", flush=True)

# Batch
results = chain.batch([
    {"text": "First document..."},
    {"text": "Second document..."},
])

Core Components

ChatModels / LLMs

Unified interface to call any provider. ChatOpenAI, ChatAnthropic, ChatGroq, ChatOllama all share the same .invoke() / .stream() interface.

PromptTemplates

Parameterized prompts with variable injection. ChatPromptTemplate.from_messages() for multi-turn conversations. FewShotPromptTemplate for in-context examples. Shareable via LangChain Hub.

Memory

Persist conversation history across turns. ConversationBufferMemory (full history), ConversationSummaryMemory (compressed), VectorStoreRetrieverMemory (semantic recall).

Tools & Agents

Give LLMs the ability to search the web, run Python, query databases, or call APIs. Built-in tools: TavilySearch, PythonREPL, SQLDatabase. Define custom tools with the @tool decorator.

Document Loaders & Retrievers (RAG)

Load PDFs, web pages, Notion databases, GitHub repos. Split into chunks. Embed and store in Pinecone, Chroma, pgvector, or FAISS. Retrieve relevant chunks at query time. This is LangChain's most common production pattern.

LangGraph — Agentic Workflows

LangGraph (separate package: pip install langgraph) models agent behavior as a directed graph. Each node is a Python function; edges define what runs next — including conditional branching based on LLM decisions:

from langgraph.graph import StateGraph, END
from typing import TypedDict

class AgentState(TypedDict):
    messages: list
    next_action: str

# Build graph
graph = StateGraph(AgentState)
graph.add_node("agent", call_model)
graph.add_node("tools", call_tools)
graph.add_conditional_edges(
    "agent",
    should_continue,          # returns "tools" or "end"
    {"tools": "tools", "end": END}
)
graph.add_edge("tools", "agent")
graph.set_entry_point("agent")

app = graph.compile()

LangGraph is what powers many agentic coding tools under the hood. It's the recommended approach for multi-step, stateful agents in production.

LangSmith — Observability & Evaluation

LangSmith is the tracing and evaluation platform for LangChain applications. Enable it with two environment variables:

export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=ls__...

# Now every chain.invoke() auto-traces to LangSmith
chain.invoke({"text": "..."})

LangSmith shows you latency per step, token usage, prompt versions, and lets you run evals against test datasets. Free tier: 5k traces/month. Developer: $39/mo. Essential for debugging complex chains in production.

Supported Models

LangChain has integrations for 50+ providers. Most popular:

Provider Package Key Models
OpenAI langchain-openai GPT-4o, o3, o4-mini
Anthropic langchain-anthropic Claude 3.5/3.7 Sonnet, Claude Opus 4
Groq langchain-groq Llama 3.3 70B, Mixtral (ultra-fast inference)
HuggingFace langchain-huggingface Any HF Inference API model
Ollama (local) langchain-ollama Llama 3, Mistral, Phi-3 (runs locally)
Google langchain-google-genai Gemini 2.0 Flash, Gemini 2.5 Pro

LangChain vs LlamaIndex vs Haystack

Factor LangChain LlamaIndex Haystack
Primary strength General agents, tool use, broad integrations RAG, document indexing, retrieval Production NLP pipelines, search
Agent framework LangGraph (mature) AgentWorkflow Agent Pipeline
Observability LangSmith (best-in-class) LlamaTrace Third-party
Learning curve Medium (abstractions evolved) Low for RAG, higher for agents Low for pipelines
Best for Complex agents, chatbots, multi-tool apps Doc Q&A, knowledge bases, RAG systems Search, QA, enterprise NLP

Monitor Your AI Stack

LangChain apps depend on OpenAI, Anthropic, and other APIs. Track uptime for every provider in your stack from one dashboard — get instant alerts when any dependency degrades.

Monitor AI API Status Free →