Getting Started with LangChain: Core Concepts and Hands-On Guide for LLM and Agent Development
Getting Started with LangChain: Core C…
A beginner's guide to LangChain covering core concepts of LLMs, Agents, and hands-on development.
This article systematically introduces LangChain as the mainstream framework for LLM application development. It covers the unified interface design based on the Adapter Pattern for multi-provider support, clarifies the distinction between LLMs (foundational capability layer) and Agents (application layer based on the ReAct framework), and walks through environment setup, coding best practices, advanced topics like LangGraph, and related career paths.
Introduction: Why Learn LangChain?
In the current AI application development landscape, LangChain has become the de facto standard framework. According to industry observations, over 90% of companies developing LLM-based applications use the Python + LangChain/LangGraph tech stack. Whether you're a newcomer to the field or a developer looking to transition into AI, mastering LangChain is an essential skill.
This article is based on LangChain version 1.2 and systematically covers the core concepts, differences, and practical usage of Models and Agents, helping you build a clear technical understanding.
The Origin of LangChain: LangChain was created by Harrison Chase in October 2022, originally to solve the "glue code" problem in LLM application development. Before LangChain, developers had to manually handle prompt management, context window limitations, multi-step reasoning chains, and other complex issues. LangChain standardized these common patterns by introducing abstractions like Chain, Memory, and Tool. With the emergence of powerful models like GPT-4, LangChain further evolved into LangGraph (for complex multi-step workflows) and LangSmith (for debugging and monitoring), forming a complete development ecosystem.

LLMs vs. Agents: Clarifying the Concepts
What's the Relationship Between Them?
Many newcomers to LLMs share a common confusion: LLMs and Agents sound like two completely different things — one is a "model" and the other is an "intelligent agent." How are they related?
In simple terms:
- LLM (Model) is the foundational capability layer, responsible for understanding and generating text — essentially a powerful "brain"
- Agent is the application layer built on top of the LLM, giving the model the "ability to act" — it can invoke tools, access external data, and perform multi-step reasoning
Here's an analogy: an LLM is like a highly knowledgeable scholar who can only "talk the talk," while an Agent equips that scholar with a computer, phone, search engine, and other tools, enabling them to actually "get things done."
The core mechanism behind Agents originates from the ReAct (Reasoning + Acting) framework proposed by Google in 2022. ReAct enables LLMs to alternate between "Reasoning" and "Acting" steps when answering questions: the model first analyzes the current state, decides which tool to call, receives the tool's output, and then continues reasoning until it reaches a final answer. This "Think → Act → Observe" loop is the theoretical foundation of modern Agents. LangChain's AgentExecutor is the engineering implementation of this mechanism — it coordinates interactions between the LLM and tools, manages intermediate states, and handles exceptions.
As for the recently popular DeepAgent, it essentially adds some automated management capabilities on top of the Agent paradigm, with no fundamental change to the core principles. Once you truly understand how Agents work, you'll find DeepAgent very easy to pick up.
Tech Stack Choice: Why Python?
While languages like Java and TypeScript also have corresponding LLM development frameworks, Python holds an absolute dominant position in real-world enterprise applications. Here's why:
- Mature ecosystem: Python has accumulated the richest collection of libraries and tools in the AI/ML space
- Timely updates: The LLM field evolves extremely fast, and the Python ecosystem provides the quickest support for the latest features
- Active community: It's much easier to find solutions when you encounter problems

Other languages aren't incapable of LLM development, but they often suffer from lag — the latest model capabilities and API features may take some time before they're fully supported in those languages' frameworks.
Core Features of the LangChain Framework
Unified Interface: One Codebase for Multiple Models
One of LangChain's most fundamental design principles is the Unified Interface for models.
In real-world development, you may need to integrate with multiple model providers:
- DeepSeek
- OpenAI
- Anthropic (Claude)
- Alibaba Tongyi Qianwen
- Tencent Hunyuan
- Zhipu AI

Without a unified interface, you'd need to write a dedicated set of integration code for each provider, resulting in extremely high maintenance costs. LangChain's unified interface design means: you only need to write one standardized set of code, and switching the underlying model provider requires only a configuration change.
Behind this design is the classic Adapter Pattern from software engineering. All chat models inherit from the BaseChatModel base class and implement unified methods like invoke(), stream(), and batch(). This means that whether the underlying service is OpenAI's REST API, Anthropic's message format, or a locally deployed Ollama model, the interface seen by the business logic layer is completely consistent. LangChain 1.x further separated core abstractions into the langchain-core package, making the interfaces more stable and unaffected by higher-level feature iterations.
The benefits of this design are obvious:
- Lower migration costs: Switching from DeepSeek to OpenAI might require changing just one line of configuration
- Easy comparison testing: You can quickly compare results across different models, and even implement automatic model routing based on task type
- Higher development efficiency: Developers focus on business logic rather than API differences between providers
Environment Setup: Preparing the .env File
Before you start coding, you need to prepare a .env file in your project root directory to store API keys and connection URLs for each model provider:
# DeepSeek
DEEPSEEK_API_KEY=your_api_key_here
DEEPSEEK_BASE_URL=https://api.deepseek.com
# OpenAI
OPENAI_API_KEY=your_api_key_here
OPENAI_BASE_URL=https://api.openai.com/v1
# Anthropic
ANTHROPIC_API_KEY=your_api_key_here
# More providers...

The two most critical configuration items for each provider are:
- API_KEY: Authentication key, which you need to obtain by registering on each provider's developer platform
- BASE_URL: The base address of the API service, which varies by provider
For DeepSeek, for example, after logging into the DeepSeek developer platform, you can find the corresponding API Key and Base URL on the API management page.
Security Best Practices: The use of
.envfiles follows the "separate config from code" principle in the "12-Factor App" methodology. In LLM application development, a leaked API Key can cause serious financial losses (since LLM API calls are billed per token). Beyond adding.envto.gitignore, in production environments you should consider using professional secret management services (such as AWS Secrets Manager or HashiCorp Vault). Python'spython-dotenvlibrary loads variables from the.envfile intoos.environat runtime, allowing frameworks like LangChain to read them automatically without explicitly passing keys in code.
Hands-On: Using LangChain's Unified Interface
Basic Invocation Pattern
LangChain's unified interface makes model invocation very concise. Here's a typical calling pattern (pseudocode illustration):
from langchain_openai import ChatOpenAI
from langchain_community.chat_models import ChatDeepSeek
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Using the DeepSeek model
model_deepseek = ChatDeepSeek(model="deepseek-chat")
# Using the OpenAI model
model_openai = ChatOpenAI(model="gpt-4")
# The invocation method is exactly the same
response = model_deepseek.invoke("Please introduce the LangChain framework")
Notice that in the code above, regardless of which model provider you use, the invocation method (.invoke()) is identical. This is the core advantage of LangChain's unified interface.
Coding Best Practices
Here are a few things worth noting in real project development:
- Use English for variable names: Although Python supports Chinese variable names, always use English naming in production projects
- Never hardcode sensitive information: API Keys and other sensitive data must be stored in
.envfiles, and.envshould be added to.gitignore - Implement proper error handling: Network calls can fail, so you need retry and fallback mechanisms
Advanced Topics: LangGraph and Complex Agent Orchestration
Once you've mastered the basics of LangChain, the next important learning milestone is LangGraph. LangGraph is a framework within the LangChain ecosystem specifically designed for building stateful, multi-step Agent workflows, modeling the Agent's execution process as a Directed Graph.
Unlike simple linear Chains, LangGraph supports:
- Conditional branching: Deciding which path to take next based on model output
- Looping execution: Allowing Agents to retry repeatedly until they reach their goal
- Parallel processing: Running multiple subtasks simultaneously to improve efficiency
- Human-in-the-loop: Pausing at critical nodes to wait for human confirmation
This makes LangGraph particularly well-suited for building systems that require collaboration among multiple specialized Agents. Cutting-edge approaches like DeepAgent are built on similar multi-Agent collaboration architectures, achieving more complex autonomous task completion through automated task decomposition and Agent scheduling — which further underscores why understanding the fundamental principles of Agents is so important.
Career Paths After Mastering LangChain
After mastering LangChain + Agent development skills, consider the following career directions:
- AI Application Developer: Building enterprise-grade AI applications with LangChain — one of the most in-demand roles today
- AI Agent Developer: Focusing on the design and development of intelligent agents, including multi-Agent collaboration and tool chain orchestration
- LLM Integration Architect: Responsible for model selection, integration, and optimization within enterprises
- AI Product Manager: Understanding technical boundaries to design sound AI product strategies
Conclusion
As the mainstream framework for LLM application development, LangChain's core value lies in lowering the barrier to AI application development. Through its unified interface design (an engineering application of the Adapter Pattern), developers can focus on business logic rather than underlying API differences. Through the Agent mechanism based on the ReAct framework, LLMs evolve from "being able to talk" to "being able to act." LangGraph further extends Agent capabilities to complex multi-step, multi-role collaboration scenarios.
For those looking to get started with LLM development, the recommended learning path is:
- First master Python fundamentals and LangChain's core concepts
- Understand the differences and connections between Models and Agents (with a focus on the ReAct loop mechanism)
- Gradually dive deeper into advanced features like LangGraph through hands-on projects
- Keep an eye on cutting-edge directions like DeepAgent to stay technically sharp
The pace of technological iteration in the LLM era is unprecedented, but the fundamentals never change — a solid foundation is always the best weapon against change.
Related articles
TutorialsCursor + Codex Dual-IDE Collaboration: A Practical Methodology for Open-Source Project Customization
A complete methodology for open-source project customization based on real-world experience, detailing the Cursor+Codex dual-IDE workflow, seven-stage process, MVP validation, and AI source code reading techniques.
TutorialsCursor Multi-Agent in Practice: Building a Full-Stack Next.js Blog in 50 Minutes
Build a full-stack blog in 50 minutes using Cursor IDE's multi-Agent mode with Next.js, Clerk auth, and Supabase. Learn the 4-phase AI Agent workflow and key integration pitfalls.
TutorialsBuilding an AI Software Factory from Scratch: A Cursor Engineer's Hands-On Experience with Multi-Agent Collaboration
Cursor engineer Eric shares practical insights on building an AI software factory: automation levels, guardrail design, parallel Agent management, and scaling to 1000+ Agents for 24/7 development.