LangChain LangGraph
Module 1 Module 2 Module 3 Module 4 Module 5 Module 6
Module 1 · Basic Introduction
1 1-1 Simple Graph
2 1-2 Chain
3 1-3 Router
4 1-4 Agent
5 1-5 Agent Memory
6 1-6 Deployment
SUM Module Summary
HomeLangGraphModule 1 · Basic Getting StartedModule Summary
LANGGRAPH MODULE 1 · SUMMARY

Introduction to LangGraph Basics
Module Summary

From the first graph to production deployment, Module 1 builds the complete basic cognitive system of LangGraph.

6Sub-course
4core concepts
1ReAct loop
Scalability
Module learning path
Simple Graph
L1 · State · Node
Edge · compile
Chain
L2 · Messages
bind_tools · LLM
Router
L3 · ToolNode
tools_condition
Agent
L4 · ReAct Loop
multi-step reasoning
Memory
L5 · Checkpointer
thread_id
Deployment
L6 · Studio
langgraph.json
📌 Module knowledge context
Three elements of the graph (L1)
  • State: Data shared between nodes
  • Node: Processing logic unit
  • Edge: Control execution flow
Messages and Tools (L2+L3)
  • MessagesStateManage conversations
  • bind_toolsRegistration tool
  • ToolNodeAutomatic execution
Agent and memory (L4+L5)
  • ReAct loop: Reasoning → Action → Observation
  • Checkpointer: Persistence
  • thread_id:session isolation
Detailed explanation of 6 sub-courses
1

Simple Graph

StateGraph · Node · Edge · State

The minimum operating unit of LangGraph: a directed graph composed of State, Node, and Edge. This is the basic skeleton for understanding all subsequent courses.

  • State(TypedDict): A "common whiteboard" shared by nodes in the graph. All nodes read and write the same data.
  • Node(function): Ordinary Python function that receives State and returns updated dictionary
  • Edge: Ordinary edges (fixed direction) and conditional edges (dynamically determined at runtime)
  • compile + invoke: The compiled graph is an executable object, and the initial State is passed in to start execution.
  • START / END:Virtual entry and exit nodes of the graph
Core insights:LangGraph uses "graph" to replace if/else - when AI needs to call tools in a loop, execute in parallel, or intervene manually, the graph structure makes the process visual, testable, and maintainable.
In-depth explanation →
2

Chain

MessagesState · bind_tools · add_messages

Connect the real LLM to the picture and introduce the message dialogue system. This is a critical step from "demo diagram" to "real AI application".

  • HumanMessage / AIMessage: LangChain’s standard message format and unified conversation history structure
  • MessagesState:built-inmessages: Annotated[list, add_messages], out-of-the-box conversation state
  • llm.invoke(messages): Call LLM in Node, input message list, and output AIMessage
  • bind_tools: Inject tool definitions into LLM so the model knows what can be called
  • add_messages Reducer: Ensure that messages are appended rather than overwritten, and the complete conversation history is retained
Core insights:MessagesStateis the standard starting point for LangGraph conversational Agent, which comes pre-installed withadd_messagesreducer, which correctly accumulates conversation history without manual configuration.
In-depth explanation →
3

Router

ToolNode · tools_condition · Conditional routing

Let the graph automatically decide whether to "call the tool" or "end directly" based on the output of LLM, which is the core routing mechanism for building tools to call Agents.

  • ToolNode: LangGraph built-in node, automatically parses tool call requests in AIMessage and executes them
  • tools_condition: Built-in conditional function to detect whether LLM outputs a tool call → route to ToolNode or END
  • add_conditional_edges: Bind conditional functions to graph edges to implement dynamic routing
  • @tool decorator: Declare ordinary Python functions as LangChain tools and automatically generate JSON Schema
Core insights:ToolNode + tools_conditionIt is the "standard suite" of LangGraph tool calls - the combination of the two can complete tool routing with 4 lines of code, without the need to manually parse the tool call results.
In-depth explanation →
4

Agent

ReAct architecture · Tool loop · Multi-step reasoning

Add a back edge to Router to form a real Agent loop: LLM reasoning → call tools → observe results → reason again until the problem is solved.

  • ReAct mode: Reasoning + Acting alternate iteration, which is the mainstream paradigm of modern LLM Agent
  • edge looptools → agentThe back edge makes the graph form a loop, supporting unlimited tool call iterations.
  • Tool call sequence: Each loop generates AIMessage (including tool_call) + ToolMessage (tool result)
  • automatic termination: LLM no longer outputs tool calls whentools_conditionRoute to END
Core insights:Agent = Router + a back edge. This difference in the back edge changes the graph from "single decision-making" to "continuous reasoning loop" - this is the core capability improvement of LangGraph graph structure compared to LangChain chain call.
In-depth explanation →
5

Agent Memory

Checkpointer · MemorySaver · thread_id

Add "memory" to the Agent so that it can remember the previous content in multiple rounds of dialogue and maintain the state between different steps of the same run.

  • Checkpointer:LangGraph’s state persistence mechanism automatically saves State snapshots after each node is executed.
  • MemorySaver: Memory-level checkpointer, valid within the process, preferred for development and debugging
  • thread_id: The unique identifier of the conversation session. The same thread_id shares history, and different IDs are isolated from each other.
  • compile(checkpointer=): The only entrance to mount checkpointer, which can be passed in during compilation.
Core insights:MemorySaverOnly the entry-level version - it stores the state in memory and is lost when the process restarts. Module 2 will learn to upgrade it toSqliteSaver(Persistent to disk), this is the memory solution available for production.
In-depth explanation →
6

Deployment

LangGraph Studio · langgraph.json · API

Deploy locally developed graphs as production services. LangGraph Studio provides visual debugging, and LangGraph Cloud provides one-click API deployment.

  • langgraph.json: Deployment configuration file, declaring the entrance, dependencies, and environment variables of the graph
  • LangGraph Studio: Local visual IDE, view graph structure, state snapshot, node execution process in real time
  • LangGraph CLIlanggraph devStart the local service,langgraph buildPackaging Docker images
  • RemoteGraph: Connect the remotely deployed map through URL and call it like a local map
Core insights:The biggest value of Studio isdebugInstead of deployment - it allows you to intuitively see every step of the state changes in the graph during development, which is 10 times more efficient than printing logs. The deployment itself requires only one configuration file.
In-depth explanation →
Horizontal comparison of core concepts

Module 1 Knowledge Points Quick Checklist

Core APIs, usage scenarios and key instructions of 6 courses

Concept/APICoursescore roleKey usage
StateGraphL1Builder for diagrams, accepting the Schema classStateGraph(MyState)
add_node / add_edgeL1Add nodes and edges to the graphbuilder.add_node("n", fn)
compile / invokeL1Compilation graph/execution graphgraph.invoke({"key": val})
MessagesStateL2Built-in conversation status, including add_messagesclass S(MessagesState): ...
bind_toolsL2Inject tool list into LLMllm.bind_tools([tool1, tool2])
ToolNodeL3Tools to automate LLM requestsToolNode([tool1, tool2])
tools_conditionL3Detect whether there are tool calls and routing decisionsadd_conditional_edges("agent", tools_condition)
ReAct back edgeL4The cyclic edge of tools→agent implements iterative reasoningadd_edge("tools", "agent")
MemorySaverL5Memory-level checkpointer, saves State snapshotcompile(checkpointer=MemorySaver())
thread_idL5Identify conversation sessions, share history with the same ID{"configurable": {"thread_id": "x"}}
langgraph.jsonL6Deployment configuration, declaration graph entry and dependencies{"graphs": {"agent": "./graph.py:graph"}}
langgraph devL6Start the Studio visual local debugging serviceCLI commands
Module core harvest
🧩

Graphs = the language of process control

LangGraph uses Node + Edge to describe the AI ​​execution process, which is more intuitive than if/else and more flexible than LangChain Chain - the graph naturally supports branches, loops, and parallelism.

💬

MessagesState is the cornerstone of conversation

MessagesState + add_messagesreducer is the starting point for all conversational agents. Understanding how it appends messages is a prerequisite for writing a correct Chatbot.

🔧

ToolNode encapsulates tool call details

ToolNode + tools_conditionAutomatically handles parsing and execution of tool calls, eliminating the need for manual extractiontool_callsfields, significantly reducing boilerplate code.

🔁

An edge creates ReAct Agent

Agent = Router + add_edge("tools", "agent"). This back edge makes the graph form a loop, allowing LLM to continue reasoning until the autonomous judgment task is completed.

🧠

thread_id is the key to memory

every timeinvokePass in the samethread_id, the graph will continue from where it ended last time. Different users use different IDs, and their status is completely isolated.

🚀

Studio makes debugging visual

LangGraph Studio can display graph structure, each step state, and tool call details in real time. It is more efficient than any print statement when developing complex Agents.

Return to Module 1 Home Page
Comprehensive practical projects
Project

Smart travel assistant with tool recall and persistent memory
Travel Planning Agent

A travel planning assistant capable of multi-step reasoning, automatic tool calling, and cross-wheel memory of user preferences. It connects all 6 core knowledge points of Module 1 into a complete runnable project.

L1 StateGraph L2 MessagesState L3 ToolNode L4 ReAct Loop L5 MemorySaver L6 Studio deployment
project capabilities
Check city weather
Search attraction information
Estimate travel budget
Multi-step reasoning planning itinerary
Remember user preferences across wheels
Studio visual debugging
Graph structure · ReAct Agent + checkpointer
L4 · ReAct execution loop
START
initial message
agent node
L2 · LLM Reasoning
tools_condition L3 · Routing Determination
tools node
L3 · ToolNode execution
Back to the edge L4
END
When no tool is called
💾 MemorySaver checkpointer · L5 · Snapshot per step → thread_id isolate session
Code block 1 / 5
Tool definition - L1 + L2 + L3
# ── 依赖导入 ───────────────────────────────────────────
from typing import Annotated
from typing_extensions import TypedDict
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, MessagesState, START, END
from langgraph.prebuilt import ToolNode, tools_condition
from langgraph.checkpoint.memory import MemorySaver

# ════════════════════════════════════════════════════
# 【L3】工具定义:@tool 装饰器自动生成 JSON Schema
#   LLM 通过 Schema 了解每个工具的参数和用途
#   ToolNode 根据 LLM 输出的 tool_call 自动匹配并执行
# ════════════════════════════════════════════════════
@tool
def get_weather(city: str) -> str:
    """获取指定城市的当前天气信息。"""
    # 实际项目中调用真实天气 API,此处模拟返回
    weather_data = {
        "北京": "晴,26°C,微风",
        "上海": "多云,22°C,东南风3级",
        "成都": "阴,18°C,无风",
    }
    return weather_data.get(city, f"{city}:天气数据暂不可用")

@tool
def search_attractions(city: str, category: str = "all") -> str:
    """搜索城市的热门景点。category 可为 'nature'(自然)、'culture'(文化)或 'all'(全部)。"""
    attractions = {
        "北京": ["故宫", "长城", "颐和园", "天坛"],
        "上海": ["外滩", "豫园", "东方明珠", "田子坊"],
        "成都": ["大熊猫基地", "宽窄巷子", "武侯祠", "锦里"],
    }
    spots = attractions.get(city, [])
    return f"{city}热门景点:{', '.join(spots)}" if spots else f"暂无{city}景点数据"

@tool
def estimate_budget(city: str, days: int, style: str = "standard") -> str:
    """估算旅行预算。style 可为 'budget'(经济)、'standard'(标准)、'luxury'(豪华)。"""
    per_day = {"budget": 300, "standard": 600, "luxury": 1500}
    daily_cost = per_day.get(style, 600)
    total = daily_cost * days
    return f"{city} {days}天{style}旅行预计费用:¥{total}(每天约¥{daily_cost})"

# 工具列表:注册所有可用工具
tools = [get_weather, search_attractions, estimate_budget]
L3 · The role of @tool decorator

@toolDid two things: ① Read the type annotation and docstring of the function, and automatically generate a JSON Schema that LLM can understand; ② Pack the ordinary function intoBaseToolobject, forToolNodeandbind_toolsuse.The docstring of the function is the tool description, the clearer it is written, the more accurately LLM can judge when to call the tool.

Code block 2 / 5
State definition and Agent node - L1 + L2
# ════════════════════════════════════════════════════
# 【L1 + L2】State 定义
#   · 继承 MessagesState(L2):内置 messages 字段 +
#     add_messages Reducer,开箱即用
#   · 扩展自定义字段(L1):TypedDict 风格添加额外状态
# ════════════════════════════════════════════════════
class TravelState(MessagesState):
    # MessagesState 已内置:messages: Annotated[list[AnyMessage], add_messages]
    # 下面是扩展字段
    destination: str         # 用户确定的目的地(默认覆盖策略)
    travel_days: int          # 旅行天数

# ════════════════════════════════════════════════════
# 【L2】LLM 初始化 + bind_tools
#   · bind_tools 将工具的 JSON Schema 注入 LLM 的系统提示
#   · LLM 输出 tool_call 时,内容是 JSON 格式的参数
# ════════════════════════════════════════════════════
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
llm_with_tools = llm.bind_tools(tools)  # 【L2】核心:让 LLM 知道有哪些工具

# ════════════════════════════════════════════════════
# 【L2 + L4】Agent 节点:ReAct 循环的推理端
#   · 读取完整消息历史,带入工具上下文,调用 LLM
#   · 返回 {"messages": [新的 AIMessage]}
#   · add_messages Reducer 自动追加,不覆盖历史
# ════════════════════════════════════════════════════
def agent(state: TravelState):
    system_prompt = SystemMessage(content=(
        "你是一个专业的旅行规划助手。你可以查询天气、搜索景点、估算预算。"
        "请根据用户需求,综合使用工具制定完整的旅行建议。"
    ))
    # state["messages"] 包含完整的对话历史(由 add_messages Reducer 维护)
    response = llm_with_tools.invoke([system_prompt] + state["messages"])
    return {"messages": [response]}  # 返回字典,Reducer 自动追加
L2 · Inherited advantages of MessagesState

inheritMessagesStatethan defined from zeroTypedDictomittedAnnotated[list, add_messages]configuration. At the same time, it supports free expansion fields——destinationandtravel_daysWith the default overwrite policy, every write from a node replaces the old value.

L2 · The underlying mechanism of bind_tools

llm.bind_tools(tools)Append the tool's JSON Schema to the API call on every requesttoolsin the parameters. After LLM sees the tool list, it can output a specialAIMessage,Thattool_callsThe fields contain the tool name and parameters - this is the triggerToolNodeexecution signal.

Code Block 3 / 5
Construction of diagram: ReAct structure - L1 + L3 + L4
# ════════════════════════════════════════════════════
# 【L1】StateGraph:图的构建器
# 【L3】ToolNode:自动执行工具调用的内置节点
# ════════════════════════════════════════════════════
tool_node = ToolNode(tools)  # 【L3】传入工具列表,自动匹配 tool_call 并执行

builder = StateGraph(TravelState)   # 【L1】创建图,传入 State Schema

# ── 注册节点 ────────────────────────────────────────
builder.add_node("agent", agent)       # 【L1】LLM 推理节点
builder.add_node("tools", tool_node)  # 【L3】工具执行节点

# ── 连接边 ──────────────────────────────────────────
builder.add_edge(START, "agent")       # 【L1】入口 → agent

# 【L3】条件边:LLM 有工具调用 → tools,否则 → END
builder.add_conditional_edges(
    "agent",
    tools_condition,   # 内置路由函数,检测 tool_calls 是否存在
)

# 【L4】关键!这条回边让图形成环,实现 ReAct 循环
# tools 执行完工具后,把结果(ToolMessage)追加进 messages,
# 然后重新路由到 agent,让 LLM 看到工具结果继续推理
builder.add_edge("tools", "agent")

# ── 【L5】挂载 MemorySaver,实现跨轮对话记忆 ──────
memory = MemorySaver()
graph = builder.compile(checkpointer=memory)  # 【L5】编译时注入 Checkpointer
L4 · How a back edge creates a ReAct loop

Noadd_edge("tools", "agent"), the graph ends after calling the tool once. After adding this return edge:Tool results (ToolMessage) are appended to messages → the agent node re-reads the complete message history (including tool results) → LLM infers again based on the new information → may continue to call the tool or answer directly. This loop continues until LLM no longer outputs tool calls,tools_conditionAt this point routing to END.

Code block 4 / 5
Run and Multi-Run Memory – L5
# ════════════════════════════════════════════════════
# 【L5】thread_id:会话标识
#   · 相同 thread_id → 共享同一对话历史
#   · 不同 thread_id → 完全独立,互不干扰
# ════════════════════════════════════════════════════
config = {"configurable": {"thread_id": "user_alice_trip_001"}}

# ── 第 1 轮:询问北京旅行建议 ─────────────────────
result_1 = graph.invoke(
    {"messages": [HumanMessage(content="我想去北京旅行3天,帮我规划一下")]},
    config=config,
)
# Agent 会自动调用:get_weather("北京") → search_attractions("北京") →
# estimate_budget("北京", 3, "standard"),然后综合给出回答

# ── 第 2 轮:追问(自动记得第 1 轮内容)────────────
result_2 = graph.invoke(
    {"messages": [HumanMessage(content="如果改成豪华出行,费用大概是多少?")]},
    config=config,  # 同一 thread_id → 记得"北京3天"的上下文
)
# LLM 知道上下文是北京3天,直接调用 estimate_budget("北京", 3, "luxury")

# ── 多用户隔离示例 ──────────────────────────────────
alice_config = {"configurable": {"thread_id": "alice_001"}}
bob_config   = {"configurable": {"thread_id": "bob_001"}}
# alice 和 bob 的对话历史完全隔离,互不影响

# ── 查看完整消息历史 ────────────────────────────────
for msg in result_2["messages"]:
    role = msg.__class__.__name__.replace("Message", "")
    print(f"[{role}] {msg.content[:80]}")
L5 · How MemorySaver works

Each time a node is executed, LangGraph serializes the current State and uses(thread_id, checkpoint_id)Store key in MemorySaver (memory dictionary). next timeinvokePass in the samethread_idWhen , LangGraph first loads the latest snapshot as the initial State, and then executes the new node.This is why round 2 can "remember" what round 1 was about.

L5 · Why does the agent know that the second round is "Beijing 3 days"?

messagesField consists ofadd_messagesreducer maintenance, it will convert each round of HumanMessage, AIMessage, ToolMessageAppend allEnter the history list. At the beginning of round 2, the agent node obtainedstate["messages"]Contains all messages from Round 1 - LLM sees the complete context and naturally knows that the destination is Beijing.

Code Blocks 5 / 5
Deployment configuration - L6
# ── langgraph.json(项目根目录)──────────────────────
# 【L6】LangGraph Studio / Cloud 的部署配置文件
# · "graphs":声明图的名称 → 对应的 Python 模块路径
# · "dependencies":项目依赖,打包时自动安装
# · "env":环境变量文件路径

# langgraph.json
{
  "graphs": {
    "travel_agent": "./travel_agent.py:graph"
    //                    ↑ 文件路径        ↑ 变量名
  },
  "dependencies": ["."],
  "env": ".env"
}

# ── 本地启动 LangGraph Studio ────────────────────────
# 【L6】在项目根目录运行,会自动打开 Studio 界面
# $ langgraph dev
#   → 启动本地 API 服务(默认 http://127.0.0.1:2024)
#   → 自动打开 Studio 可视化界面
#   → 可在 Studio 里直接输入消息测试图的运行

# ── Studio 的调试能力 ────────────────────────────────
# 【L6】每次执行可以看到:
#   · 图结构可视化(节点 + 边,含 ReAct 循环)
#   · 每个节点执行前后的 State 快照对比
#   · 工具调用的输入参数和返回结果
#   · 每条消息的完整内容(AIMessage 含 tool_calls)
#   · 可以在任意步骤"时间旅行"(查看历史快照)

# ── 云端部署(可选)─────────────────────────────────
# $ langgraph build -t my-travel-agent  # 构建 Docker 镜像
# $ langgraph up                        # 启动生产服务
L6 · Minimal configuration of langgraph.json

langgraph.jsonIt is the only entrance for Studio and Cloud to identify projects."graphs"The key declares the mapping relationship "graph name → variable in Python file" - as long astravel_agent.pyThere is a man namedgraphcompiled graph object, Studio can find and visualize it.langgraph devThe command starts the FastAPI service in the background and exposes the standard LangGraph API through which Studio interacts with graphs.

Overview of correspondence between knowledge points

Exact mapping of project parts to 6 sub-courses

Project compositionCorresponding coursesCore APIProblem solved
TravelState(MessagesState)L1 + L2class S(MessagesState)Define data contracts + built-in conversation message management
@tool tool definitionL2 + L3@toolDecoratorAutomatically generate JSON Schema for LLM to understand and call
llm.bind_tools(tools)L2bind_tools([...])Inject tool information into LLM so it knows what to tune
agent node functionL2 + L4llm_with_tools.invoke(messages)Read the complete message history and LLM inference determines the next step
ToolNode(tools)L3ToolNode([...])Automatically parse tool_calls, execute the corresponding tool, and return the result
tools_condition conditional edgeL3add_conditional_edges("agent", tools_condition)Detect whether there is a tool call and route to tools or END
add_edge("tools", "agent")L4add_edge("tools", "agent")Forming a ReAct loop - tool results are fed back into LLM inference
MemorySaver checkpointerL5compile(checkpointer=MemorySaver())State snapshot of each step, supporting multi-step dialogue memory across rounds
thread_id configurationL5{"configurable": {"thread_id": "x"}}Identification session, same ID recovery history, different ID isolation
langgraph.json configurationL6{"graphs": {"name": "./file.py:var"}}Declare the deployment entry, Studio/Cloud identifies the project
Return to Module 1 Home Page