langchain
**LangChain** is the **open-source Python and JavaScript framework for building LLM-powered applications that provides standard abstractions for prompts, chains, agents, memory, and retrieval** — widely adopted for rapid prototyping of RAG systems, conversational AI agents, and document processing pipelines by providing pre-built components that connect LLMs to external data sources and tools.
**What Is LangChain?**
- **Definition**: A framework that provides composable abstractions for LLM application development — Prompt Templates for structured prompts, Chains for sequential operations, Agents for tool-using LLMs, Memory for conversation history, and Document Loaders/Retrievers for RAG — plus integrations with 100+ LLM providers, vector databases, and tools.
- **LCEL (LangChain Expression Language)**: LangChain's modern composition syntax uses the pipe operator to chain components: retriever | prompt | llm | parser — building chains by connecting components left to right.
- **Integrations**: LangChain provides pre-built integrations with OpenAI, Anthropic, Hugging Face, Ollama, Chroma, Pinecone, Weaviate, FAISS, and dozens more — one import gives you a standardized interface to any LLM or vector store.
- **LangSmith**: Companion observability platform for tracing, debugging, and evaluating LangChain applications — visualizes each step of chain execution with inputs, outputs, latency, and token usage.
- **Status**: LangChain is the most downloaded LLM framework package on PyPI — extremely popular for prototyping, though teams sometimes move to simpler direct API code for production.
**Why LangChain Matters for AI/ML**
- **RAG Prototype Speed**: Building a RAG system from scratch (chunking, embedding, storing, retrieving, prompting) takes days; LangChain provides all components pre-built — prototype to working demo in hours.
- **Agent Frameworks**: LangChain's agent executors implement ReAct and tool-calling patterns — connecting an LLM to web search, code execution, database queries, and custom functions with standard interfaces.
- **LLM Provider Switching**: LangChain's ChatModel abstraction works identically with OpenAI, Anthropic, and local models — swap providers by changing one class import, all downstream code unchanged.
- **Document Processing**: LangChain's document loaders handle PDF, Word, HTML, Notion, Confluence, GitHub, and 50+ other formats — standardizing document ingestion for RAG pipelines.
- **Evaluation**: LangChain + LangSmith provides evaluation frameworks for RAG quality — measuring retrieval relevance, answer faithfulness, and context precision at scale.
**Core LangChain Patterns**
**Basic RAG Chain (LCEL)**:
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
llm = ChatOpenAI(model="gpt-4o")
embeddings = OpenAIEmbeddings()
vectorstore = Chroma(embedding_function=embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
prompt = ChatPromptTemplate.from_template("""
Answer based on context: {context}
Question: {question}
""")
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
response = rag_chain.invoke("What is RAG?")
**Tool-Using Agent**:
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.tools import tool
@tool
def search_database(query: str) -> str:
"""Search the product database for information."""
return db.query(query)
@tool
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return weather_api.get(city)
llm = ChatOpenAI(model="gpt-4o")
agent = create_tool_calling_agent(llm, tools=[search_database, get_weather], prompt=prompt)
executor = AgentExecutor(agent=agent, tools=[search_database, get_weather], verbose=True)
result = executor.invoke({"input": "What is the weather in NYC and what products do we sell?"})
**Conversation Memory**:
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationChain
memory = ConversationBufferWindowMemory(k=10) # Keep last 10 exchanges
chain = ConversationChain(llm=llm, memory=memory)
response = chain.predict(input="Tell me about RAG")
**LangChain vs Alternatives**
| Framework | Abstractions | Integrations | Production | Learning Curve |
|-----------|-------------|-------------|------------|----------------|
| LangChain | Many | 100+ | Medium | High |
| LlamaIndex | RAG-focused | 50+ | High | Medium |
| DSPy | Optimization | LLM-only | High | High |
| Direct API | None | Manual | High | Low |
LangChain is **the comprehensive LLM application framework that accelerates prototyping through pre-built abstractions** — by providing standard components for every layer of an LLM application stack with hundreds of integrations, LangChain enables rapid development of RAG systems, agents, and document pipelines, making it the default starting point for LLM application development despite the tendency to migrate toward simpler, more direct code in production.