LangChain is an open-source framework for building AI applications by chaining together large language models (LLMs), prompts, and tools into modular workflows. It simplifies complex AI pipelines—from basic question-answering to advanced retrieval-augmented generation (RAG) and agentic systems.
1. Installation
Python
# Create a virtual environment (recommended)
python -m venv langchain-env
source langchain-env/bin/activate # Linux/macOS
# OR
langchain-env\Scripts\activate # Windows
# Install LangChain (core package)
pip install langchain==0.2.0
# Install optional dependencies (e.g., for OpenAI, ChromaDB)
pip install langchain-openai chromadb tiktoken
Expected Output:
Successfully installed langchain-0.2.0 ...
JavaScript/TypeScript
# Initialize a Node.js project (if starting fresh)
npm init -y
# Install LangChain
npm install [email protected]
# Install optional dependencies (e.g., for OpenAI)
npm install @langchain/openai
Gotchas:
- Python 3.8+ required. Check with
python --version. - For Windows, use PowerShell or WSL to avoid path issues.
- If you get
ModuleNotFoundError, ensure your virtual environment is activated.
2. Your First Chain
Create a file first_chain.py:
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Initialize the LLM (requires OPENAI_API_KEY in environment)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
# Define a prompt template
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("user", "{input}")
])
# Create the chain: prompt → LLM → output parser
chain = prompt | llm | StrOutputParser()
# Run the chain
response = chain.invoke({"input": "Explain LangChain in one sentence."})
print(response)
Run it:
export OPENAI_API_KEY="your-api-key" # Linux/macOS
# OR
set OPENAI_API_KEY="your-api-key" # Windows
python first_chain.py
Expected Output:
LangChain is a framework for building applications with large language models by chaining together modular components like prompts, LLMs, and tools.
3. RAG Pipeline Setup
Step 1: Load and Split Documents
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Load a PDF (e.g., a product manual)
loader = PyPDFLoader("example.pdf")
pages = loader.load()
# Split into chunks
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
docs = text_splitter.split_documents(pages)
Step 2: Create a Vector Store
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
# Embed and store documents
vectorstore = Chroma.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
persist_directory="./chroma_db"
)
Step 3: Build the RAG Chain
from langchain_core.runnables import RunnablePassthrough
# Retrieve relevant documents
retriever = vectorstore.as_retriever()
# Define the RAG prompt
rag_prompt = ChatPromptTemplate.from_messages([
("system", "Answer the question based on the following context:\n{context}"),
("user", "{question}")
])
# Combine retrieval and generation
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| rag_prompt
| llm
| StrOutputParser()
)
# Ask a question
response = rag_chain.invoke("What is the warranty period?")
print(response)
Expected Output:
The warranty period for this product is 2 years from the date of purchase, as stated on page 5 of the manual.
Gotchas:
- Chunk Size: Too small → poor context. Too large → noisy retrieval. Start with
chunk_size=1000. - Embedding Models: Use
HuggingFaceEmbeddingsfor free alternatives to OpenAI embeddings. - Persistence: Add
persist_directoryto save the vector store between runs.
4. Tool Use and Agents
Step 1: Define Tools
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
# Wikipedia tool
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
# Custom tool (e.g., calculator)
from langchain_core.tools import tool
@tool
def multiply(a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
tools = [wikipedia, multiply]
Step 2: Create an Agent
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
# Pull a pre-built prompt
prompt = hub.pull("hwchase17/openai-tools-agent")
# Initialize the agent
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Run the agent
response = agent_executor.invoke({
"input": "What is the population of France in 2026? Multiply it by 2."
})
print(response["output"])
Expected Output:
The agent will use the Wikipedia tool to look up the population of France and then apply the multiply tool to the result.
Gotchas:
- Tool Descriptions: Agents rely on tool descriptions to decide when to use them. Be explicit (e.g., "Use this for arithmetic operations").
- Rate Limits: Wikipedia API may throttle requests. Add error handling:
from tenacity import retry, stop_after_attempt @retry(stop=stop_after_attempt(3)) def _run(self, query: str) -> str: return super()._run(query)
5. Memory and Conversation History
In-Memory Chat History
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
# Initialize chat history
history = InMemoryChatMessageHistory()
# Add messages
history.add_user_message("What is LangChain?")
history.add_ai_message("LangChain is a framework for building LLM applications.")
# Create a chain with history
chain = prompt | llm | StrOutputParser()
chain_with_history = RunnableWithMessageHistory(
chain,
lambda session_id: history,
input_messages_key="input",
history_messages_key="chat_history",
)
# Run with history
response = chain_with_history.invoke(
{"input": "How do I get started?"},
config={"configurable": {"session_id": "abc123"}}
)
print(response)
Expected Output:
To get started with LangChain, install it using pip and follow the quickstart guide at python.langchain.com. Would you like a code example?
Persistent History (SQLite)
from langchain_community.chat_message_histories import SQLChatMessageHistory
# Store history in SQLite
history = SQLChatMessageHistory(
session_id="abc123",
connection_string="sqlite:///chat_history.db"
)
# Use in a chain (same as above)
chain_with_history = RunnableWithMessageHistory(...)
Gotchas:
- Token Limits: Long conversations may exceed LLM context windows. Use
ConversationSummaryMemoryto condense history:from langchain.memory import ConversationSummaryMemory memory = ConversationSummaryMemory(llm=llm) - Session Management: Always pass a
session_idto avoid mixing conversations.
6. LangSmith for Debugging
Step 1: Set Up LangSmith
- Sign up at LangSmith (free tier: 5,000 traces/month).
- Get your API key from the settings page.
Step 2: Configure Tracing
from langchain_core.tracers import LangChainTracer
tracer = LangChainTracer(
project_name="my-project",
api_key="your-langsmith-api-key"
)
# Add to your chain
chain = prompt | llm | StrOutputParser()
chain_with_tracing = chain.with_config({"callbacks": [tracer]})
# Run the chain
response = chain_with_tracing.invoke({"input": "Debug me!"})
print(response)
Step 3: View Traces in LangSmith
- Go to LangSmith.
- Select your project.
- Inspect:
- Input/output for each step.
- Latency and token usage.
- Errors and retries.
Example Trace:
Run ID: 123e4567-e89b-12d3-a456-426614174000
Status: Success
Latency: 1.2s
Steps:
1. PromptTemplate: "You are a helpful assistant. Answer: {input}"
2. ChatOpenAI: gpt-4o-mini (input_tokens=10, output_tokens=25)
3. StrOutputParser: "Debugging is fun!"
Gotchas:
- Cost: Free tier is generous, but monitor usage to avoid surprises.
- Sensitive Data: LangSmith logs inputs/outputs. Use
redact=Trueto mask PII:tracer = LangChainTracer(..., redact=True)
7. When to Use (and When Not To)
✅ Use LangChain When:
- Building complex workflows: Chains, agents, and RAG pipelines.
- Integrating multiple tools: APIs, databases, web search.
- Prototyping quickly: Pre-built components (e.g.,
WikipediaQueryRun). - Debugging: LangSmith for observability.
❌ Avoid LangChain When:
- Simple scripts: A single API call to OpenAI is sufficient.
- Performance-critical apps: Overhead of chains/agents may slow you down.
- Tight budgets: Free but relies on paid APIs (e.g., OpenAI, LangSmith).
- Non-Python/JS stacks: Limited support for other languages.
Alternatives:
- LlamaIndex: Better for RAG-heavy applications (e.g., document search) LangChain vs. LlamaIndex
- Haystack: More mature for enterprise NLP pipelines Haystack Comparison
What's Next?
-
Deploy a RAG app: Use FastAPI to serve your RAG chain as an API. Example: FastAPI integration guide
-
Explore LangGraph: Build <a href="/services/ai-agents">multi-agent</a> systems for complex tasks.
pip install langgraphTutorial: LangGraph Quickstart
-
Optimize costs: Replace OpenAI with local models (e.g., Ollama, <a href="/services/open-source-llm-integration">llama</a>.cpp).
pip install langchain-community
LangChain accelerates AI development but requires careful planning for enterprise use. If you're evaluating frameworks or need help deploying production-ready AI workflows, Hyperion's AI transformation <a href="/services/coaching-vs-consulting">consulting</a> can help you navigate the trade-offs and build scalable solutions.
