LangChain
LangChain is one of the most popular open-source frameworks for building language-model-based applications — from a simple chatbot to complex agents with several tools.
What Is LangChain?
LangChain's main goal is to standardize and simplify connecting the different pieces of an AI application to one another: the language model, prompts, external data sources, memory, and tools. Instead of writing bespoke code for every integration, LangChain provides uniform interfaces that work with dozens of different model and tool providers.
Core Components
- Models — a uniform interface for calling different language models
- Prompts — reusable, parameterized prompt templates
- Chains — composing several steps (prompt → model → output processing) into one callable unit
- Retrievers — a uniform interface for connecting to vector databases, for implementing RAG
- Tools / Agents — defining tools the model can call and the decision logic for using them
LCEL: The Chain Composition Language
The LangChain Expression Language (LCEL) is a declarative way to compose components with the |
(pipe) operator — similar to piping in a Unix command line. Each component takes an input, processes it, and
passes the output to the next component.
Code Sample
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_anthropic import ChatAnthropic
prompt = ChatPromptTemplate.from_template(
"Summarize the price and stock status of {product}."
)
model = ChatAnthropic(model="claude-sonnet")
chain = prompt | model | StrOutputParser()
result = chain.invoke({"product": "Model X wireless headphones"})
print(result)
Streaming & Callbacks
Waiting for the model's entire response to be generated isn't a great experience for interactive UIs —
which is why every LCEL component implements a stream() method alongside invoke(),
returning tokens one by one as they're produced (exactly the "live typing" experience of most chatbots today).
In addition, Callbacks let you hook into a chain's intermediate events (a model call starting, each token arriving, a tool being called, an error occurring) — without changing the chain's core logic. This mechanism is the foundation behind tools like LangSmith for logging and tracing.
The Related Ecosystem
LangSmith is an observability and debugging tool for tracing chain execution. For building more complex agents with finer control over the execution flow (especially loops and conditional branches), the LangChain team built the LangGraph framework on top of these same components.
When Is LangChain a Good Fit?
For applications with a relatively linear flow (prompt → model → processing, or a standard RAG pipeline), LangChain dramatically speeds up development. For complex agentic behavior with multi-branch decisions and persistent state, LangGraph is usually the better choice.
FAQ
Is LangChain only for Python?
No, its JavaScript/TypeScript version is also fully maintained.