LangChain Introduction
LangChain is a Python framework for building large language model (LLM) applications.
LangChain provides a unified interface to connect various AI models and supports building intelligent Agents that can automatically call tools, retrieve knowledge, and remember context.

What is LangChain?
Simply put, LangChain solves a core problem:Enabling large language models to interact with the external world.。
Native LLMs can only generate text based on training data. But in real-world applications, we need AI to query databases, call APIs, search documents, and send emails.
LangChain provides a set of standardized components to chain these capabilities together.

| Component | Role | Core Function | Common Uses |
|---|---|---|---|
| Models | Connect large language models |
|
|
| Prompts (Prompt Templates) | Manage prompt templates |
|
|
| Document Loader | Read external document data |
|
|
| Text Splitter | Split long text |
|
|
| Memory | Implement contextual memory |
|
|
| Retriever | Retrieve relevant knowledge content |
|
|
| Tools | Call external tools and APIs |
|
|
| Output Parser | Parse model output results |
|
|
| Chains | Combine multiple components to form workflows |
|
|
From a technical perspective, LangChain is a modular LLM application development framework that includes three layers:
| Layer | Description | Package name |
|---|---|---|
| Core abstraction layer | Defines basic interfaces for models, tools, messages, etc. | langchain-core |
| User interface layer | Provides high-level APIs such as init_chat_model, create_agent | langchain |
| Integration layer | Connects third-party services such as OpenAI, Anthropic, Ollama | langchain-openai, etc. |
Why Choose LangChain?
If you only need to call a model API once, a direct HTTP request is sufficient. But when you need to build a complete AI application, LangChain offers the following advantages:
| Capability | Description | Applicable scenarios |
|---|---|---|
| Unified model interface | Switch between OpenAI / Anthropic / DeepSeek and other models with one set of code | Multi-model comparison testing, cost optimization |
| Agent architecture | The model automatically decides when to call tools, forming a thought-action loop | Automation tasks, intelligent customer service |
| Middleware system | Insert custom logic before and after model calls (retry, caching, filtering) | Reliability assurance in production environments |
| Structured output | Have the model return JSON in a specified format for easy program parsing | Data extraction, form filling |
| Memory and persistence | Built-in conversation memory and cross-session storage capabilities | Multi-turn conversations, user preference memory |
| Rich ecosystem | Hundreds of third-party integrations covering mainstream models and tools | Quickly integrate various services |
What Can LangChain Do?
The following are the most typical application scenarios for LangChain:
Intelligent Chatbots
A chat assistant with multi-turn conversation memory that can call external tools (check weather, check orders, send emails).
RAG Knowledge Base Q&A
Vectorize and store private documents (PDFs, web pages, databases), allowing the model to answer questions based on these documents with cited sources.
Agent Automation Assistant
The model autonomously plans task steps, calls different tools as needed, and completes complex multi-step operations, such as "Help me organize last week's sales data and generate a report."
Data Extraction and Analysis
Extract structured information from unstructured text (such as extracting key fields from scanned contract documents), or have the model generate data analysis conclusions.
Relationship Between LangChain and LangGraph
Many beginners are confused about the difference between these two libraries. Simply put:
| Comparison dimension | LangChain | LangGraph |
|---|---|---|
| Positioning | High-level Agent framework, ready to use out of the box | Low-level workflow engine, fine-grained control |
| Ease of learning | Low, create an Agent in 10 lines of code | Medium to high, requires understanding the concept of graphs |
| Applicable scenarios | Standard Agent applications, rapid prototyping | Complex multi-step workflows, multi-Agent collaboration |
| Relationship | LangChain's create_agent() is built on top of LangGraph | |
This tutorial focuses on LangChain. If you are just starting out, beginning with LangChain is the right choice. When you need finer process control, then dive deeper into LangGraph.
Preparation
Before you start learning, you only need to have the following foundation:
- Basic Python syntax (functions, classes, type annotations)
- Able to install Python packages with pip
- Familiar with basic command-line operations
- Have a large model API Key (OpenAI, Anthropic, etc. all acceptable)
Other ExtensionsThe code examples in this tutorial have been tested in a Python 3.10+ environment.