Welcome to LangChain
Relevant source files
The following files were used as context for generating this wiki page:
LangChain is a framework for developing applications powered by Large Language Models (LLMs). It is designed to facilitate the creation of complex applications that go beyond simple LLM API calls by enabling the composition of LLMs with other sources of computation and knowledge. The core idea is to "chain" together interoperable components to build robust, stateful, and observable AI applications.
The framework provides standard, extensible interfaces and integrations for a wide range of components, including model providers, vector stores, and other tools. This modularity allows developers to easily swap components and adapt to the evolving AI landscape, future-proofing their architectural decisions. LangChain is not just a single library but an ecosystem of tools, including LangSmith for observability and LangGraph for building controllable agent workflows.
Sources: README.md:17-21, libs/langchain/README.md:21-25, libs/core/README.md:12-16
Core Philosophy and Architecture
LangChain's architecture is built on the principle of composability. While LLMs are powerful, their true potential is unlocked when they are combined with external data sources, APIs, and other computational tools. LangChain provides the foundational abstractions and components to build these sophisticated applications.
Sources: libs/langchain/README.md:21-25
LangChain Ecosystem
The LangChain project is structured as a monorepo containing several key packages that work together. The core of the ecosystem is langchain-core, which provides the fundamental abstractions.
This diagram illustrates the hierarchical organization of the LangChain framework.
This diagram is an interpretation of the architecture described in the documentation.
Sources: libs/core/README.md:52-54
langchain-core: Contains the base abstractions, the LangChain Expression Language (LCEL), and theRunnableinterface. It has minimal third-party dependencies.langchain: Includes the cognitive architecture of LangChain, such as chains, agents, and tools that form the core of an application's logic.langchain-community: Provides third-party integrations with various model providers, databases, and other tools.- Partner Packages: Standalone packages for specific integrations, like
langchain-fireworks. LangGraph: A library for building stateful, multi-actor applications with LLMs, extending the LCEL with cycles and more complex flows.LangSmith: A developer platform for debugging, testing, evaluating, and monitoring LLM applications.
Sources: libs/core/README.md:10-16, README.md:31-48
Key Capabilities
LangChain is designed to assist with five main areas of LLM application development, in increasing order of complexity.
This flowchart shows the main functional areas of LangChain.
Sources: libs/langchain/README.md:53-79
| Capability | Description |
|---|---|
| Models and Prompts | Provides a generic interface for all LLMs, prompt management, and optimization utilities. |
| Chains | Enables sequences of calls, going beyond a single LLM interaction to include other utilities or tools. |
| Retrieval Augmented Generation (RAG) | A specific type of chain that fetches data from an external source to use in the generation step. |
| Agents | Involves an LLM making decisions about which actions to take, executing them, observing the results, and repeating the cycle. |
| Evaluation | Assists in evaluating generative models using LLMs themselves, which is useful as traditional metrics are often insufficient. |
Sources: libs/langchain/README.md:53-79
Common applications built with these capabilities include:
- Question Answering with RAG
- Extracting structured output from text
- Chatbots
Sources: libs/langchain/README.md:27-42
The Runnable Interface and LCEL
The core of LangChain's composition is the Runnable interface. Most components in langchain-core implement this interface, which provides a standard way to invoke, batch, and stream them.
Sources: libs/core/README.md:20-25, libs/core/langchain_core/runnables/base.py:100-104
Runnable Interface
The Runnable class is an abstract base class that defines the standard interface for all composable components in LangChain.
This class diagram shows the Runnable interface and some of its key implementations.
Sources: libs/core/langchain_core/runnables/base.py:100,1402,1768,2450
Key methods of a Runnable:
| Method | Description |
|---|---|
invoke() / ainvoke() | Transforms a single input into an output. |
batch() / abatch() | Efficiently transforms multiple inputs into outputs, typically in parallel. |
stream() / astream() | Streams the output for a single input as it's generated. |
astream_log() | Streams output and intermediate results as RunLogPatch objects for tracing. |
astream_events() | Streams StreamEvent objects for real-time progress information. |
Sources: libs/core/langchain_core/runnables/base.py:110-116, libs/core/langchain_core/runnables/base.py:410-580
LangChain Expression Language (LCEL)
LCEL is a declarative syntax for composing Runnable objects into sequences or parallel pipelines. It uses the | (pipe) operator to chain components together. LCEL automatically handles optimizations like parallel execution, streaming, and async support.
A simple LCEL chain looks like this:
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
model = ChatOpenAI()
output_parser = StrOutputParser()
chain = prompt | model | output_parser
chain.invoke({"topic": "bears"})
This declarative style simplifies building complex chains and ensures they benefit from the Runnable interface's features.
Sources: libs/core/README.md:43-50, libs/core/langchain_core/runnables/base.py:146-160
This diagram shows a typical sequence of operations when a RunnableSequence is invoked.
This sequence diagram illustrates the flow of an invoke call through a simple LCEL chain.
Sources: libs/core/langchain_core/runnables/base.py:1402-1498
Getting Started
Installation
To get started, install the core LangChain package:
pip install langchain
This will install langchain-core, langchain-community, and langchain itself. Partner packages may need to be installed separately.
Sources: README.md:23-25, libs/langchain/README.md:18-20
LangChain CLI
The langchain-cli is a command-line tool for creating, managing, and serving LangChain applications.
| Command | Description |
|---|---|
langchain app new <name> | Creates a new LangServe application. |
langchain app add <package> | Adds a template package to the current application. |
langchain app remove <path> | Removes a package from the current application. |
langchain serve | Starts the LangServe app, making it available via a FastAPI server. |
Sources: libs/cli/DOCS.md:14-80
Development and Security
Contribution Guidelines
LangChain is an open-source project and welcomes contributions. Key development principles include:
- Stable Public Interfaces: Avoid breaking changes to public APIs.
- Code Quality: All Python code must include type hints and follow style guidelines.
- Testing: New features and bug fixes must be covered by unit tests (
pytest). - Documentation: Public functions require Google-style docstrings.
- Commit Standards: Use the Conventional Commits format for PR titles (e.g.,
feat(core): ...).
Sources: CLAUDE.md:5-100
Security Best Practices
When building applications that interact with external resources (filesystems, APIs, databases), it is critical to follow security best practices.
- Limit Permissions: Grant the principle of least privilege. Use read-only credentials and sandbox environments (e.g., containers) where possible.
- Anticipate Potential Misuse: Assume that an LLM with access to credentials may use them in any way the permissions allow, including destructive actions.
- Defense in Depth: Combine multiple security layers, such as read-only permissions and sandboxing, rather than relying on a single defense.
Failure to adhere to these practices can lead to data corruption, unauthorized access to confidential information, or compromised system availability.
Sources: SECURITY.md:5-30