Skip to content
🔗

LangChain

LangChain Inc.

An open-source framework for building LLM applications, providing a complete toolchain and component library.

By EZ4Code Team
Visit Official Site

Overview

LangChain is a leading framework for building LLM applications, providing rich abstractions and tools. Its core philosophy is to decompose LLM applications into composable components: models, prompts, chains, agents, memory, tools, etc. LangChain supports 100+ LLM providers and thousands of integrations, making it the de facto standard for LLM application development. The LangChain ecosystem includes LangSmith (monitoring), LangServe (deployment), LangGraph (stateful agents), and more.

Installation

LangChain is installed via pip, with different packages installed as needed. The core package provides basic functionality, and the community package provides third-party integrations. Using a virtual environment to manage dependencies is recommended.

# Install core packages
pip install langchain
pip install langchain-core
pip install langchain-community

# Install specific integrations
pip install langchain-openai
pip install langchain-anthropic
pip install langchain-google-genai

# Install all dependencies
pip install langchain[all]

LLMs

LangChain supports multiple LLM interfaces, including OpenAI, Anthropic, Google, local models, and more. Through a unified interface, you can easily switch between different models. It supports synchronous and asynchronous calls, streaming output, batch processing, and more.

from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic

# OpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)
response = llm.invoke("Explain quantum computing")

# Anthropic
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")

# Streaming output
for chunk in llm.stream("Write a poem"):
    print(chunk.content, end="")

Prompts

LangChain provides a powerful prompt template system, supporting variable interpolation, few-shot examples, partial formatting, and more. PromptTemplate is used for text models, and ChatPromptTemplate is used for chat models. Loading prompt templates from files is supported.

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {role} expert."),
    ("human", "Please explain {topic}."),
])

chain = prompt | llm
response = chain.invoke({"role": "AI", "topic": "deep learning"})

Chains

Chain is the core concept of LangChain, combining multiple components in sequence. LCEL (LangChain Expression Language) uses the pipe symbol | to combine components, supporting streaming, batch processing, async, and more. Chains can be nested and reused.

from langchain_core.output_parsers import StrOutputParser

# LCEL chain
chain = prompt | llm | StrOutputParser()
result = chain.invoke({"role": "AI", "topic": "RAG"})

# Complex chain
from langchain_core.runnables import RunnablePassthrough

rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

Agents

Agent is an LLM application capable of autonomous decision-making and tool invocation. LangChain supports multiple agent types, such as ReAct, OpenAI Functions, Tool Calling, and more. Agents can dynamically select tools and execution paths based on input. LangGraph provides more powerful stateful agent building capabilities.

from langchain.agents import create_tool_calling_agent, AgentExecutor

tools = [search_tool, calculator_tool]
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)

result = executor.invoke({"input": "What is the size of the AI market in 2024?"})

Memory

Memory allows applications to maintain context. LangChain provides multiple memory types: ConversationBufferMemory (full history), ConversationSummaryMemory (summary), ConversationBufferWindowMemory (window), and more. Persistent storage is supported.

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

chain = prompt | llm | StrOutputParser()
# Use memory in the chain
chain_with_memory = (
    RunnablePassthrough.assign(
        history=lambda x: memory.load_memory_variables({})["chat_history"]
    )
    | prompt
    | llm
)

Vector Stores

Vector stores are the core of RAG applications. LangChain supports 50+ vector databases, including Chroma, Pinecone, Weaviate, FAISS, and more. It provides a unified interface for document storage and similarity search.

from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
    documents=docs,
    embedding=embeddings,
    persist_directory="./chroma_db"
)

retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
docs = retriever.invoke("query question")

RAG

RAG (Retrieval-Augmented Generation) is an important application scenario for LangChain. By retrieving relevant documents, it enhances the LLM's ability to answer. LangChain provides a complete RAG toolchain, including document loading, splitting, embedding, retrieval, generation, and more.

from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter

# Load and split documents
loader = WebBaseLoader("https://example.com")
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
splits = splitter.split_documents(docs)

# Create vector store
vectorstore = Chroma.from_documents(splits, embeddings)
retriever = vectorstore.as_retriever()

# RAG chain
rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
)

Tools

Tools extend the capabilities of LLMs, enabling them to perform operations. LangChain provides a rich set of built-in tools, such as search, calculation, API calls, and more. Custom tool development is supported using the @tool decorator or the BaseTool class.

from langchain_core.tools import tool

@tool
def search_web(query: str) -> str:
    """Search the web for information."""
    # Implement search logic
    return search_results

@tool
def calculate(expression: str) -> str:
    """Calculate a math expression."""
    return str(eval(expression))

tools = [search_web, calculate]

Callbacks

Callbacks are used to monitor and log the execution of LLM applications. You can track token usage, execution time, errors, and more. LangSmith provides powerful monitoring and debugging capabilities, supporting visual analysis of chain execution flows.

from langchain_core.callbacks import BaseCallbackHandler

class MyHandler(BaseCallbackHandler):
    def on_llm_start(self, serialized, prompts, **kwargs):
        print(f"LLM start: {serialized}")
    
    def on_llm_end(self, response, **kwargs):
        print(f"LLM end: {response}")

chain.invoke("question", config={"callbacks": [MyHandler()]})

Deployment

LangChain applications can be deployed as REST APIs via LangServe. LangServe provides REST interfaces, a Playground, streaming support, and more. Deployment to cloud platforms such as AWS, GCP, Vercel, and more is also supported. LangSmith provides production monitoring capabilities.

# Deploy with LangServe
from langserve import add_routes
from fastapi import FastAPI

app = FastAPI()
add_routes(app, chain, path="/chat")

# Run: uvicorn server:app --reload
# Access: http://localhost:8000/chat/playground

LangServe gives every chain a REST API plus a Playground UI for free.

Configuration

LangChain is configured in Python code with provider-specific packages. Install langchain-openai, langchain-anthropic, etc. and set API keys as environment variables. LCEL chains compose with the pipe | operator. LangSmith (set LANGCHAIN_API_KEY) provides tracing and monitoring. Vector store and embedding choices are made per application.

# .env
OPENAI_API_KEY=your-key
ANTHROPIC_API_KEY=your-key
LANGCHAIN_API_KEY=your-key      # for LangSmith tracing
LANGCHAIN_TRACING_V2=true

# Python: pick a model
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

# LCEL chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

chain = prompt | llm | StrOutputParser()

# Vector store
from langchain_chroma import Chroma
vectorstore = Chroma(embedding_function=embeddings,
                     persist_directory="./chroma_db")

Enable LangSmith tracing early (LANGCHAIN_TRACING_V2=true)—it makes chain debugging dramatically easier.

FAQ

Common questions cover model switching, cost, vector store choice, LangSmith vs LangServe, and LCEL. LangChain's unified interface lets you swap models by changing one line. LangSmith is for monitoring/tracing; LangServe is for deploying chains as APIs.

Q: How do I switch models?
A: Change the model class (ChatOpenAI -> ChatAnthropic) and the env var;
   LCEL chains work unchanged.

Q: How do I cut costs?
A: Use gpt-4o-mini for simple tasks, cache responses, and trim context
   before passing to the LLM.

Q: Which vector store should I use?
A: Chroma for local dev, Pinecone/Weaviate for production, FAISS for
   in-memory experiments.

Q: LangSmith vs LangServe?
A: LangSmith monitors and traces LLM calls; LangServe deploys chains as
   REST APIs. They are complementary.

Q: What is LCEL?
A: LangChain Expression Language—compose chains with the | pipe operator,
   getting streaming, async, and batch for free.

Prefer LCEL (the | pipe syntax) over legacy chains; it gives streaming, async, and batch support for free.

More AI Guides