Introduction to LangChain
LangChain is a framework for building applications powered by large language models (LLMs). It provides composable building blocks — models, prompts, chains, memory, agents, and tools — that let you go from a raw LLM call to a production-grade AI application.
Why LangChain?
Calling an LLM API directly works for simple use cases, but real applications need more: multi-step reasoning, memory of past conversations, access to external data, and the ability to take actions. LangChain provides standardised abstractions for all of these so you can focus on your application logic, not the glue code.
| Problem | LangChain solution |
|---|---|
| LLM outputs are unstructured | Output parsers & structured output |
| Context window limits | Memory + RAG (retrieval-augmented generation) |
| LLM can't act on the world | Agents + Tools (search, code execution, APIs) |
| Hard to switch LLM providers | Unified model interface (OpenAI, Anthropic, Ollama, …) |
| Complex multi-step pipelines | Chains & LCEL (composable pipe syntax) |
Core components
Models
- LLMs & Chat Models
- Embeddings
- OpenAI, Anthropic, Ollama…
Prompts
- PromptTemplate
- ChatPromptTemplate
- Few-shot examples
Chains / LCEL
- Sequential steps
- | pipe operator
- Parallel branches
Memory
- Conversation history
- Summarisation
- Buffer window
Agents & Tools
- ReAct pattern
- Web search, code, APIs
- Custom tools
RAG
- Document loaders
- Text splitters
- Vector stores
Installation
# Core package
pip install langchain
# LLM provider integrations
pip install langchain-openai # OpenAI / Azure OpenAI
pip install langchain-anthropic # Anthropic Claude
pip install langchain-community # Ollama, HuggingFace, 100+ integrations
# Vector stores and RAG
pip install langchain-chroma # Chroma vector DB
pip install faiss-cpu # FAISS vector search
# Useful utilities
pip install python-dotenv # load .env secrets
pip install tiktoken # token counting for OpenAI
Your first LangChain app
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
# 1. Instantiate a model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
# 2. Send a message
messages = [
SystemMessage(content="You are a helpful coding assistant."),
HumanMessage(content="Explain what a Docker container is in 2 sentences."),
]
response = llm.invoke(messages)
print(response.content)
Environment setup
# .env file (never commit this!)
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
# Load in Python
from dotenv import load_dotenv
load_dotenv() # reads .env automatically
import os
key = os.getenv("OPENAI_API_KEY")
LangChain ecosystem
| Package | Purpose |
|---|---|
langchain-core | Base abstractions: Runnable, BaseMessage, PromptTemplate |
langchain | Chains, agents, memory — the main framework |
langchain-openai | ChatOpenAI, OpenAIEmbeddings |
langchain-anthropic | ChatAnthropic (Claude models) |
langchain-community | 100+ community integrations (Ollama, HuggingFace, loaders) |
langchain-chroma | Chroma vector store integration |
langgraph | Graph-based stateful multi-agent workflows |
langserve | Deploy chains as REST APIs (FastAPI-based) |
langsmith | Tracing, debugging, evaluation for LLM apps |
LangChain Expression Language (LCEL) is the modern way to compose chains using the
| pipe operator. It supports streaming, async, batching, and tracing out of the box. All examples in this tutorial use LCEL.