Day 10:LangGraph - Agent 的图执行引擎

Day 10:LangGraph - Agent 的图执行引擎

🤖 系列:Java工程师转AI Agent 3个月学习计划

👤 作者:宸丶一 | 28岁Java程序员,规划狂魔,周一继续肝

🎯 今日目标: LangGraph、Agent、工作流、状态机、图执行引擎

💬 个人格言: 代码改不改变世界我不知道,但先让我准时下班。


📖 前言

在 Day 9 的学习中,我们深入理解了 LangChain Agent 的源码,掌握了 Agent 的三个核心状态:AgentAction、AgentStep、AgentFinish,以及 ReAct(推理+行动)的执行模式。

今天,我们进入 Day 10 的学习:LangGraph - Agent 的图执行引擎

如果说 LangChain Agent 是"开箱即用的智能体",那么 LangGraph 就是"自己搭积木"的底层框架。它让我们能够设计任意复杂的 Agent 工作流,从简单的单工具调用,到复杂的多 Agent 协作,都可以轻松实现。


🎯 学习目标

  1. 理解 LangGraph 的核心概念:State、Node、Edge、Graph
  2. 用 LangGraph 构建简单的 Agent 工作流
  3. 对比 LangChain Agent 和 LangGraph 的差异
  4. 完成思考题和练习

📚 核心概念

1. State(状态)

定义: Agent 的当前状态,保存对话历史、工具结果等信息

Java 对标: Context 对象或 HttpServletRequest

示例:

python 复制代码
class AgentState(TypedDict):
    messages: list      # 对话历史(类似 List<Message>)
    tool_calls: list    # 工具调用列表
    tool_results: list  # 工具结果列表

理解: State 就像 Agent 的"记忆",保存了当前对话的所有信息。每次执行节点时,都可以读取和更新 State。

类图展示:
#mermaid-svg-EEGHC6uUgruppKzG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-EEGHC6uUgruppKzG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EEGHC6uUgruppKzG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EEGHC6uUgruppKzG .error-icon{fill:#552222;}#mermaid-svg-EEGHC6uUgruppKzG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EEGHC6uUgruppKzG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EEGHC6uUgruppKzG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EEGHC6uUgruppKzG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EEGHC6uUgruppKzG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EEGHC6uUgruppKzG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EEGHC6uUgruppKzG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EEGHC6uUgruppKzG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EEGHC6uUgruppKzG .marker.cross{stroke:#333333;}#mermaid-svg-EEGHC6uUgruppKzG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EEGHC6uUgruppKzG p{margin:0;}#mermaid-svg-EEGHC6uUgruppKzG g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-EEGHC6uUgruppKzG g.classGroup text .title{font-weight:bolder;}#mermaid-svg-EEGHC6uUgruppKzG .cluster-label text{fill:#333;}#mermaid-svg-EEGHC6uUgruppKzG .cluster-label span{color:#333;}#mermaid-svg-EEGHC6uUgruppKzG .cluster-label span p{background-color:transparent;}#mermaid-svg-EEGHC6uUgruppKzG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EEGHC6uUgruppKzG .cluster text{fill:#333;}#mermaid-svg-EEGHC6uUgruppKzG .cluster span{color:#333;}#mermaid-svg-EEGHC6uUgruppKzG .nodeLabel,#mermaid-svg-EEGHC6uUgruppKzG .edgeLabel{color:#131300;}#mermaid-svg-EEGHC6uUgruppKzG .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-EEGHC6uUgruppKzG .label text{fill:#131300;}#mermaid-svg-EEGHC6uUgruppKzG .labelBkg{background:#ECECFF;}#mermaid-svg-EEGHC6uUgruppKzG .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-EEGHC6uUgruppKzG .classTitle{font-weight:bolder;}#mermaid-svg-EEGHC6uUgruppKzG .node rect,#mermaid-svg-EEGHC6uUgruppKzG .node circle,#mermaid-svg-EEGHC6uUgruppKzG .node ellipse,#mermaid-svg-EEGHC6uUgruppKzG .node polygon,#mermaid-svg-EEGHC6uUgruppKzG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EEGHC6uUgruppKzG .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG g.clickable{cursor:pointer;}#mermaid-svg-EEGHC6uUgruppKzG g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-EEGHC6uUgruppKzG g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-EEGHC6uUgruppKzG .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-EEGHC6uUgruppKzG .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-EEGHC6uUgruppKzG .dashed-line{stroke-dasharray:3;}#mermaid-svg-EEGHC6uUgruppKzG .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-EEGHC6uUgruppKzG #compositionStart,#mermaid-svg-EEGHC6uUgruppKzG .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #compositionEnd,#mermaid-svg-EEGHC6uUgruppKzG .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #dependencyStart,#mermaid-svg-EEGHC6uUgruppKzG .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #dependencyStart,#mermaid-svg-EEGHC6uUgruppKzG .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #extensionStart,#mermaid-svg-EEGHC6uUgruppKzG .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #extensionEnd,#mermaid-svg-EEGHC6uUgruppKzG .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #aggregationStart,#mermaid-svg-EEGHC6uUgruppKzG .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #aggregationEnd,#mermaid-svg-EEGHC6uUgruppKzG .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #lollipopStart,#mermaid-svg-EEGHC6uUgruppKzG .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG #lollipopEnd,#mermaid-svg-EEGHC6uUgruppKzG .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-EEGHC6uUgruppKzG .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-EEGHC6uUgruppKzG .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EEGHC6uUgruppKzG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EEGHC6uUgruppKzG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EEGHC6uUgruppKzG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 包含
包含
包含
1
1
1
*
*
*
AgentState
+list messages
+list tool_calls
+list tool_results
Message
+str role
+str content
ToolCall
+str name
+dict args
+str id
ToolResult
+str tool_call_id
+str name
+str content


2. Node(节点)

定义: 执行具体任务的单元(调用 LLM、执行工具等)

Java 对标: Service 方法

示例:

python 复制代码
def llm_node(state: AgentState) -> AgentState:
    """LLM 节点:调用大模型进行思考"""
    messages = state["messages"]
    response = llm.invoke(messages)
    return {**state, "messages": messages + [response]}

理解: Node 就像一个个"工人",负责执行具体的任务。每个 Node 都接收 State 作为输入,执行任务后返回更新后的 State。

节点类型图:
#mermaid-svg-h6NAmGhB5BB4dtJX{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-h6NAmGhB5BB4dtJX .error-icon{fill:#552222;}#mermaid-svg-h6NAmGhB5BB4dtJX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-h6NAmGhB5BB4dtJX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-h6NAmGhB5BB4dtJX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-h6NAmGhB5BB4dtJX .marker.cross{stroke:#333333;}#mermaid-svg-h6NAmGhB5BB4dtJX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-h6NAmGhB5BB4dtJX p{margin:0;}#mermaid-svg-h6NAmGhB5BB4dtJX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX .cluster-label text{fill:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX .cluster-label span{color:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX .cluster-label span p{background-color:transparent;}#mermaid-svg-h6NAmGhB5BB4dtJX .label text,#mermaid-svg-h6NAmGhB5BB4dtJX span{fill:#333;color:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX .node rect,#mermaid-svg-h6NAmGhB5BB4dtJX .node circle,#mermaid-svg-h6NAmGhB5BB4dtJX .node ellipse,#mermaid-svg-h6NAmGhB5BB4dtJX .node polygon,#mermaid-svg-h6NAmGhB5BB4dtJX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-h6NAmGhB5BB4dtJX .rough-node .label text,#mermaid-svg-h6NAmGhB5BB4dtJX .node .label text,#mermaid-svg-h6NAmGhB5BB4dtJX .image-shape .label,#mermaid-svg-h6NAmGhB5BB4dtJX .icon-shape .label{text-anchor:middle;}#mermaid-svg-h6NAmGhB5BB4dtJX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-h6NAmGhB5BB4dtJX .rough-node .label,#mermaid-svg-h6NAmGhB5BB4dtJX .node .label,#mermaid-svg-h6NAmGhB5BB4dtJX .image-shape .label,#mermaid-svg-h6NAmGhB5BB4dtJX .icon-shape .label{text-align:center;}#mermaid-svg-h6NAmGhB5BB4dtJX .node.clickable{cursor:pointer;}#mermaid-svg-h6NAmGhB5BB4dtJX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-h6NAmGhB5BB4dtJX .arrowheadPath{fill:#333333;}#mermaid-svg-h6NAmGhB5BB4dtJX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-h6NAmGhB5BB4dtJX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-h6NAmGhB5BB4dtJX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-h6NAmGhB5BB4dtJX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-h6NAmGhB5BB4dtJX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-h6NAmGhB5BB4dtJX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-h6NAmGhB5BB4dtJX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-h6NAmGhB5BB4dtJX .cluster text{fill:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX .cluster span{color:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-h6NAmGhB5BB4dtJX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-h6NAmGhB5BB4dtJX rect.text{fill:none;stroke-width:0;}#mermaid-svg-h6NAmGhB5BB4dtJX .icon-shape,#mermaid-svg-h6NAmGhB5BB4dtJX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-h6NAmGhB5BB4dtJX .icon-shape p,#mermaid-svg-h6NAmGhB5BB4dtJX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-h6NAmGhB5BB4dtJX .icon-shape .label rect,#mermaid-svg-h6NAmGhB5BB4dtJX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-h6NAmGhB5BB4dtJX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-h6NAmGhB5BB4dtJX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-h6NAmGhB5BB4dtJX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Node 类型
LLM 节点
工具节点
回复节点
判断节点
调用大模型思考
执行具体工具
生成最终回复
条件判断路由


3. Edge(边)

定义: 连接节点的条件分支

Java 对标: if-else 逻辑

示例:

python 复制代码
def should_use_tool(state: AgentState) -> str:
    """条件边:判断是否需要调用工具"""
    if state.get("tool_calls"):
        return "tool_node"
    return "respond_node"

理解: Edge 就像"交通指挥员",根据条件决定下一步执行哪个 Node。可以是固定的(add_edge),也可以是条件的(add_conditional_edges)。

边类型图:
#mermaid-svg-XM1Fwdqo3uZ18cPj{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-XM1Fwdqo3uZ18cPj .error-icon{fill:#552222;}#mermaid-svg-XM1Fwdqo3uZ18cPj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-XM1Fwdqo3uZ18cPj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .marker.cross{stroke:#333333;}#mermaid-svg-XM1Fwdqo3uZ18cPj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-XM1Fwdqo3uZ18cPj p{margin:0;}#mermaid-svg-XM1Fwdqo3uZ18cPj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .cluster-label text{fill:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .cluster-label span{color:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .cluster-label span p{background-color:transparent;}#mermaid-svg-XM1Fwdqo3uZ18cPj .label text,#mermaid-svg-XM1Fwdqo3uZ18cPj span{fill:#333;color:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .node rect,#mermaid-svg-XM1Fwdqo3uZ18cPj .node circle,#mermaid-svg-XM1Fwdqo3uZ18cPj .node ellipse,#mermaid-svg-XM1Fwdqo3uZ18cPj .node polygon,#mermaid-svg-XM1Fwdqo3uZ18cPj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .rough-node .label text,#mermaid-svg-XM1Fwdqo3uZ18cPj .node .label text,#mermaid-svg-XM1Fwdqo3uZ18cPj .image-shape .label,#mermaid-svg-XM1Fwdqo3uZ18cPj .icon-shape .label{text-anchor:middle;}#mermaid-svg-XM1Fwdqo3uZ18cPj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .rough-node .label,#mermaid-svg-XM1Fwdqo3uZ18cPj .node .label,#mermaid-svg-XM1Fwdqo3uZ18cPj .image-shape .label,#mermaid-svg-XM1Fwdqo3uZ18cPj .icon-shape .label{text-align:center;}#mermaid-svg-XM1Fwdqo3uZ18cPj .node.clickable{cursor:pointer;}#mermaid-svg-XM1Fwdqo3uZ18cPj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .arrowheadPath{fill:#333333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XM1Fwdqo3uZ18cPj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-XM1Fwdqo3uZ18cPj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XM1Fwdqo3uZ18cPj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-XM1Fwdqo3uZ18cPj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .cluster text{fill:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj .cluster span{color:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-XM1Fwdqo3uZ18cPj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-XM1Fwdqo3uZ18cPj rect.text{fill:none;stroke-width:0;}#mermaid-svg-XM1Fwdqo3uZ18cPj .icon-shape,#mermaid-svg-XM1Fwdqo3uZ18cPj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XM1Fwdqo3uZ18cPj .icon-shape p,#mermaid-svg-XM1Fwdqo3uZ18cPj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-XM1Fwdqo3uZ18cPj .icon-shape .label rect,#mermaid-svg-XM1Fwdqo3uZ18cPj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XM1Fwdqo3uZ18cPj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-XM1Fwdqo3uZ18cPj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-XM1Fwdqo3uZ18cPj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Edge 类型
普通边 add_edge
条件边 add_conditional_edges
固定路由: A → B
条件路由: A → B 或 A → C


4. Graph(图)

定义: 由 State、Node、Edge 组成的完整工作流

Java 对标: 状态机

示例:

python 复制代码
graph = StateGraph(AgentState)
graph.add_node("llm", llm_node)
graph.add_node("tool", tool_node)
graph.add_node("respond", respond_node)
graph.set_entry_point("llm")
graph.add_conditional_edges("llm", should_use_tool, {...})
graph.add_edge("tool", "respond")
graph.add_edge("respond", END)
app = graph.compile()

理解: Graph 就像一张"流程图",定义了 Agent 的执行顺序和逻辑。通过编译(compile),把流程图变成可执行的程序。

Graph 组成图:
#mermaid-svg-0B71pVhA26E49S5i{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-0B71pVhA26E49S5i .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0B71pVhA26E49S5i .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0B71pVhA26E49S5i .error-icon{fill:#552222;}#mermaid-svg-0B71pVhA26E49S5i .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0B71pVhA26E49S5i .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0B71pVhA26E49S5i .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0B71pVhA26E49S5i .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0B71pVhA26E49S5i .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0B71pVhA26E49S5i .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0B71pVhA26E49S5i .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0B71pVhA26E49S5i .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0B71pVhA26E49S5i .marker.cross{stroke:#333333;}#mermaid-svg-0B71pVhA26E49S5i svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0B71pVhA26E49S5i p{margin:0;}#mermaid-svg-0B71pVhA26E49S5i .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-0B71pVhA26E49S5i .cluster-label text{fill:#333;}#mermaid-svg-0B71pVhA26E49S5i .cluster-label span{color:#333;}#mermaid-svg-0B71pVhA26E49S5i .cluster-label span p{background-color:transparent;}#mermaid-svg-0B71pVhA26E49S5i .label text,#mermaid-svg-0B71pVhA26E49S5i span{fill:#333;color:#333;}#mermaid-svg-0B71pVhA26E49S5i .node rect,#mermaid-svg-0B71pVhA26E49S5i .node circle,#mermaid-svg-0B71pVhA26E49S5i .node ellipse,#mermaid-svg-0B71pVhA26E49S5i .node polygon,#mermaid-svg-0B71pVhA26E49S5i .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-0B71pVhA26E49S5i .rough-node .label text,#mermaid-svg-0B71pVhA26E49S5i .node .label text,#mermaid-svg-0B71pVhA26E49S5i .image-shape .label,#mermaid-svg-0B71pVhA26E49S5i .icon-shape .label{text-anchor:middle;}#mermaid-svg-0B71pVhA26E49S5i .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-0B71pVhA26E49S5i .rough-node .label,#mermaid-svg-0B71pVhA26E49S5i .node .label,#mermaid-svg-0B71pVhA26E49S5i .image-shape .label,#mermaid-svg-0B71pVhA26E49S5i .icon-shape .label{text-align:center;}#mermaid-svg-0B71pVhA26E49S5i .node.clickable{cursor:pointer;}#mermaid-svg-0B71pVhA26E49S5i .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-0B71pVhA26E49S5i .arrowheadPath{fill:#333333;}#mermaid-svg-0B71pVhA26E49S5i .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-0B71pVhA26E49S5i .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-0B71pVhA26E49S5i .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0B71pVhA26E49S5i .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-0B71pVhA26E49S5i .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0B71pVhA26E49S5i .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-0B71pVhA26E49S5i .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-0B71pVhA26E49S5i .cluster text{fill:#333;}#mermaid-svg-0B71pVhA26E49S5i .cluster span{color:#333;}#mermaid-svg-0B71pVhA26E49S5i div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-0B71pVhA26E49S5i .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-0B71pVhA26E49S5i rect.text{fill:none;stroke-width:0;}#mermaid-svg-0B71pVhA26E49S5i .icon-shape,#mermaid-svg-0B71pVhA26E49S5i .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0B71pVhA26E49S5i .icon-shape p,#mermaid-svg-0B71pVhA26E49S5i .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-0B71pVhA26E49S5i .icon-shape .label rect,#mermaid-svg-0B71pVhA26E49S5i .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0B71pVhA26E49S5i .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-0B71pVhA26E49S5i .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-0B71pVhA26E49S5i :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Graph 组成
State 状态
Node 节点
Edge 边
保存当前信息
执行具体任务
连接节点
完整的工作流


🔄 工作流设计

简单模式

流程图:
#mermaid-svg-E0mLV7Vyaxyl9mxK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-E0mLV7Vyaxyl9mxK .error-icon{fill:#552222;}#mermaid-svg-E0mLV7Vyaxyl9mxK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-E0mLV7Vyaxyl9mxK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .marker.cross{stroke:#333333;}#mermaid-svg-E0mLV7Vyaxyl9mxK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-E0mLV7Vyaxyl9mxK p{margin:0;}#mermaid-svg-E0mLV7Vyaxyl9mxK .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .cluster-label text{fill:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .cluster-label span{color:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .cluster-label span p{background-color:transparent;}#mermaid-svg-E0mLV7Vyaxyl9mxK .label text,#mermaid-svg-E0mLV7Vyaxyl9mxK span{fill:#333;color:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .node rect,#mermaid-svg-E0mLV7Vyaxyl9mxK .node circle,#mermaid-svg-E0mLV7Vyaxyl9mxK .node ellipse,#mermaid-svg-E0mLV7Vyaxyl9mxK .node polygon,#mermaid-svg-E0mLV7Vyaxyl9mxK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .rough-node .label text,#mermaid-svg-E0mLV7Vyaxyl9mxK .node .label text,#mermaid-svg-E0mLV7Vyaxyl9mxK .image-shape .label,#mermaid-svg-E0mLV7Vyaxyl9mxK .icon-shape .label{text-anchor:middle;}#mermaid-svg-E0mLV7Vyaxyl9mxK .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .rough-node .label,#mermaid-svg-E0mLV7Vyaxyl9mxK .node .label,#mermaid-svg-E0mLV7Vyaxyl9mxK .image-shape .label,#mermaid-svg-E0mLV7Vyaxyl9mxK .icon-shape .label{text-align:center;}#mermaid-svg-E0mLV7Vyaxyl9mxK .node.clickable{cursor:pointer;}#mermaid-svg-E0mLV7Vyaxyl9mxK .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .arrowheadPath{fill:#333333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-E0mLV7Vyaxyl9mxK .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-E0mLV7Vyaxyl9mxK .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-E0mLV7Vyaxyl9mxK .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-E0mLV7Vyaxyl9mxK .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .cluster text{fill:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK .cluster span{color:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-E0mLV7Vyaxyl9mxK .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-E0mLV7Vyaxyl9mxK rect.text{fill:none;stroke-width:0;}#mermaid-svg-E0mLV7Vyaxyl9mxK .icon-shape,#mermaid-svg-E0mLV7Vyaxyl9mxK .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-E0mLV7Vyaxyl9mxK .icon-shape p,#mermaid-svg-E0mLV7Vyaxyl9mxK .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-E0mLV7Vyaxyl9mxK .icon-shape .label rect,#mermaid-svg-E0mLV7Vyaxyl9mxK .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-E0mLV7Vyaxyl9mxK .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-E0mLV7Vyaxyl9mxK .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-E0mLV7Vyaxyl9mxK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 需要工具
不需要
用户输入
llm_node: LLM 思考
should_use_tool: 条件判断
tool_node: 执行工具
respond_node: 生成回复
END

执行流程:

  1. 用户输入 → LLM 思考
  2. 判断是否需要工具
  3. 需要工具 → 执行工具 → 生成回复
  4. 不需要工具 → 直接生成回复
  5. 结束

ReAct 模式(带循环)

流程图:
#mermaid-svg-Sh5kXmxDJgdkCS3g{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Sh5kXmxDJgdkCS3g .error-icon{fill:#552222;}#mermaid-svg-Sh5kXmxDJgdkCS3g .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Sh5kXmxDJgdkCS3g .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .marker.cross{stroke:#333333;}#mermaid-svg-Sh5kXmxDJgdkCS3g svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Sh5kXmxDJgdkCS3g p{margin:0;}#mermaid-svg-Sh5kXmxDJgdkCS3g .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .cluster-label text{fill:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .cluster-label span{color:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .cluster-label span p{background-color:transparent;}#mermaid-svg-Sh5kXmxDJgdkCS3g .label text,#mermaid-svg-Sh5kXmxDJgdkCS3g span{fill:#333;color:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .node rect,#mermaid-svg-Sh5kXmxDJgdkCS3g .node circle,#mermaid-svg-Sh5kXmxDJgdkCS3g .node ellipse,#mermaid-svg-Sh5kXmxDJgdkCS3g .node polygon,#mermaid-svg-Sh5kXmxDJgdkCS3g .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .rough-node .label text,#mermaid-svg-Sh5kXmxDJgdkCS3g .node .label text,#mermaid-svg-Sh5kXmxDJgdkCS3g .image-shape .label,#mermaid-svg-Sh5kXmxDJgdkCS3g .icon-shape .label{text-anchor:middle;}#mermaid-svg-Sh5kXmxDJgdkCS3g .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .rough-node .label,#mermaid-svg-Sh5kXmxDJgdkCS3g .node .label,#mermaid-svg-Sh5kXmxDJgdkCS3g .image-shape .label,#mermaid-svg-Sh5kXmxDJgdkCS3g .icon-shape .label{text-align:center;}#mermaid-svg-Sh5kXmxDJgdkCS3g .node.clickable{cursor:pointer;}#mermaid-svg-Sh5kXmxDJgdkCS3g .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .arrowheadPath{fill:#333333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Sh5kXmxDJgdkCS3g .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Sh5kXmxDJgdkCS3g .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Sh5kXmxDJgdkCS3g .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Sh5kXmxDJgdkCS3g .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .cluster text{fill:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g .cluster span{color:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Sh5kXmxDJgdkCS3g .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Sh5kXmxDJgdkCS3g rect.text{fill:none;stroke-width:0;}#mermaid-svg-Sh5kXmxDJgdkCS3g .icon-shape,#mermaid-svg-Sh5kXmxDJgdkCS3g .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Sh5kXmxDJgdkCS3g .icon-shape p,#mermaid-svg-Sh5kXmxDJgdkCS3g .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Sh5kXmxDJgdkCS3g .icon-shape .label rect,#mermaid-svg-Sh5kXmxDJgdkCS3g .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Sh5kXmxDJgdkCS3g .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Sh5kXmxDJgdkCS3g .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Sh5kXmxDJgdkCS3g :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 需要
不需要
用户输入
LLM 思考
是否需要工具?
执行工具
直接回复
观察工具结果
END

关键区别: ReAct 模式有循环,工具执行完会回到 LLM 再思考,可以处理更复杂的任务。

执行流程:

  1. 用户输入 → LLM 思考
  2. 判断是否需要工具
  3. 需要工具 → 执行工具 → 观察结果 → 回到 LLM 再思考
  4. 不需要工具 → 直接回复
  5. 任务完成 → 结束

多 Agent 协作模式

流程图:
#mermaid-svg-t1Sa1ESRlgVyfDlO{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-t1Sa1ESRlgVyfDlO .error-icon{fill:#552222;}#mermaid-svg-t1Sa1ESRlgVyfDlO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-t1Sa1ESRlgVyfDlO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .marker.cross{stroke:#333333;}#mermaid-svg-t1Sa1ESRlgVyfDlO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-t1Sa1ESRlgVyfDlO p{margin:0;}#mermaid-svg-t1Sa1ESRlgVyfDlO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .cluster-label text{fill:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .cluster-label span{color:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .cluster-label span p{background-color:transparent;}#mermaid-svg-t1Sa1ESRlgVyfDlO .label text,#mermaid-svg-t1Sa1ESRlgVyfDlO span{fill:#333;color:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .node rect,#mermaid-svg-t1Sa1ESRlgVyfDlO .node circle,#mermaid-svg-t1Sa1ESRlgVyfDlO .node ellipse,#mermaid-svg-t1Sa1ESRlgVyfDlO .node polygon,#mermaid-svg-t1Sa1ESRlgVyfDlO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .rough-node .label text,#mermaid-svg-t1Sa1ESRlgVyfDlO .node .label text,#mermaid-svg-t1Sa1ESRlgVyfDlO .image-shape .label,#mermaid-svg-t1Sa1ESRlgVyfDlO .icon-shape .label{text-anchor:middle;}#mermaid-svg-t1Sa1ESRlgVyfDlO .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .rough-node .label,#mermaid-svg-t1Sa1ESRlgVyfDlO .node .label,#mermaid-svg-t1Sa1ESRlgVyfDlO .image-shape .label,#mermaid-svg-t1Sa1ESRlgVyfDlO .icon-shape .label{text-align:center;}#mermaid-svg-t1Sa1ESRlgVyfDlO .node.clickable{cursor:pointer;}#mermaid-svg-t1Sa1ESRlgVyfDlO .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .arrowheadPath{fill:#333333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-t1Sa1ESRlgVyfDlO .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-t1Sa1ESRlgVyfDlO .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-t1Sa1ESRlgVyfDlO .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-t1Sa1ESRlgVyfDlO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .cluster text{fill:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO .cluster span{color:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-t1Sa1ESRlgVyfDlO .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-t1Sa1ESRlgVyfDlO rect.text{fill:none;stroke-width:0;}#mermaid-svg-t1Sa1ESRlgVyfDlO .icon-shape,#mermaid-svg-t1Sa1ESRlgVyfDlO .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-t1Sa1ESRlgVyfDlO .icon-shape p,#mermaid-svg-t1Sa1ESRlgVyfDlO .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-t1Sa1ESRlgVyfDlO .icon-shape .label rect,#mermaid-svg-t1Sa1ESRlgVyfDlO .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-t1Sa1ESRlgVyfDlO .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-t1Sa1ESRlgVyfDlO .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-t1Sa1ESRlgVyfDlO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 不满意
满意
用户输入
规划 Agent: 拆解任务
执行 Agent: 执行任务
检查 Agent: 检查结果
结果满意?
返回结果
END

特点:

  • 多个 Agent 协作完成任务
  • 支持循环和反馈
  • 适合复杂的业务场景

🆚 LangChain Agent vs LangGraph

对比表格

特性 LangChain Agent LangGraph
抽象层级 高层抽象,开箱即用 底层框架,自己设计
状态管理 隐式(框架管理) 显式(自己定义)
流程控制 固定(ReAct 循环) 自定义(任意图结构)
代码复杂度 简单 更复杂
灵活性 有限 非常灵活
适用场景 简单场景 复杂场景

关系图

#mermaid-svg-xngnGZjhwiwi5cZk{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-xngnGZjhwiwi5cZk .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-xngnGZjhwiwi5cZk .error-icon{fill:#552222;}#mermaid-svg-xngnGZjhwiwi5cZk .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-xngnGZjhwiwi5cZk .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-xngnGZjhwiwi5cZk .marker{fill:#333333;stroke:#333333;}#mermaid-svg-xngnGZjhwiwi5cZk .marker.cross{stroke:#333333;}#mermaid-svg-xngnGZjhwiwi5cZk svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-xngnGZjhwiwi5cZk p{margin:0;}#mermaid-svg-xngnGZjhwiwi5cZk .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-xngnGZjhwiwi5cZk .cluster-label text{fill:#333;}#mermaid-svg-xngnGZjhwiwi5cZk .cluster-label span{color:#333;}#mermaid-svg-xngnGZjhwiwi5cZk .cluster-label span p{background-color:transparent;}#mermaid-svg-xngnGZjhwiwi5cZk .label text,#mermaid-svg-xngnGZjhwiwi5cZk span{fill:#333;color:#333;}#mermaid-svg-xngnGZjhwiwi5cZk .node rect,#mermaid-svg-xngnGZjhwiwi5cZk .node circle,#mermaid-svg-xngnGZjhwiwi5cZk .node ellipse,#mermaid-svg-xngnGZjhwiwi5cZk .node polygon,#mermaid-svg-xngnGZjhwiwi5cZk .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-xngnGZjhwiwi5cZk .rough-node .label text,#mermaid-svg-xngnGZjhwiwi5cZk .node .label text,#mermaid-svg-xngnGZjhwiwi5cZk .image-shape .label,#mermaid-svg-xngnGZjhwiwi5cZk .icon-shape .label{text-anchor:middle;}#mermaid-svg-xngnGZjhwiwi5cZk .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-xngnGZjhwiwi5cZk .rough-node .label,#mermaid-svg-xngnGZjhwiwi5cZk .node .label,#mermaid-svg-xngnGZjhwiwi5cZk .image-shape .label,#mermaid-svg-xngnGZjhwiwi5cZk .icon-shape .label{text-align:center;}#mermaid-svg-xngnGZjhwiwi5cZk .node.clickable{cursor:pointer;}#mermaid-svg-xngnGZjhwiwi5cZk .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-xngnGZjhwiwi5cZk .arrowheadPath{fill:#333333;}#mermaid-svg-xngnGZjhwiwi5cZk .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-xngnGZjhwiwi5cZk .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-xngnGZjhwiwi5cZk .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-xngnGZjhwiwi5cZk .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-xngnGZjhwiwi5cZk .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-xngnGZjhwiwi5cZk .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-xngnGZjhwiwi5cZk .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-xngnGZjhwiwi5cZk .cluster text{fill:#333;}#mermaid-svg-xngnGZjhwiwi5cZk .cluster span{color:#333;}#mermaid-svg-xngnGZjhwiwi5cZk div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-xngnGZjhwiwi5cZk .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-xngnGZjhwiwi5cZk rect.text{fill:none;stroke-width:0;}#mermaid-svg-xngnGZjhwiwi5cZk .icon-shape,#mermaid-svg-xngnGZjhwiwi5cZk .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-xngnGZjhwiwi5cZk .icon-shape p,#mermaid-svg-xngnGZjhwiwi5cZk .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-xngnGZjhwiwi5cZk .icon-shape .label rect,#mermaid-svg-xngnGZjhwiwi5cZk .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-xngnGZjhwiwi5cZk .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-xngnGZjhwiwi5cZk .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-xngnGZjhwiwi5cZk :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} LangChain Agent
高层封装: 开箱即用
底层框架: LangGraph
简单场景: 单工具调用
复杂场景: 多Agent协作
快速开发: 学习成本低
灵活设计: 学习成本高

简单说:

  • LangChain Agent = 成品手机(买来就能用)
  • LangGraph = DIY 手机(自己组装,更灵活)

关系: LangChain Agent 的底层其实就是 LangGraph,只是封装了一个通用的流程图供大家使用。


💻 代码示例

示例 1:模拟版(不用真实 LLM)

执行流程图:
#mermaid-svg-9LcARcxvDbFBPuxa{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9LcARcxvDbFBPuxa .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9LcARcxvDbFBPuxa .error-icon{fill:#552222;}#mermaid-svg-9LcARcxvDbFBPuxa .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9LcARcxvDbFBPuxa .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9LcARcxvDbFBPuxa .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9LcARcxvDbFBPuxa .marker.cross{stroke:#333333;}#mermaid-svg-9LcARcxvDbFBPuxa svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9LcARcxvDbFBPuxa p{margin:0;}#mermaid-svg-9LcARcxvDbFBPuxa .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-9LcARcxvDbFBPuxa .cluster-label text{fill:#333;}#mermaid-svg-9LcARcxvDbFBPuxa .cluster-label span{color:#333;}#mermaid-svg-9LcARcxvDbFBPuxa .cluster-label span p{background-color:transparent;}#mermaid-svg-9LcARcxvDbFBPuxa .label text,#mermaid-svg-9LcARcxvDbFBPuxa span{fill:#333;color:#333;}#mermaid-svg-9LcARcxvDbFBPuxa .node rect,#mermaid-svg-9LcARcxvDbFBPuxa .node circle,#mermaid-svg-9LcARcxvDbFBPuxa .node ellipse,#mermaid-svg-9LcARcxvDbFBPuxa .node polygon,#mermaid-svg-9LcARcxvDbFBPuxa .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9LcARcxvDbFBPuxa .rough-node .label text,#mermaid-svg-9LcARcxvDbFBPuxa .node .label text,#mermaid-svg-9LcARcxvDbFBPuxa .image-shape .label,#mermaid-svg-9LcARcxvDbFBPuxa .icon-shape .label{text-anchor:middle;}#mermaid-svg-9LcARcxvDbFBPuxa .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-9LcARcxvDbFBPuxa .rough-node .label,#mermaid-svg-9LcARcxvDbFBPuxa .node .label,#mermaid-svg-9LcARcxvDbFBPuxa .image-shape .label,#mermaid-svg-9LcARcxvDbFBPuxa .icon-shape .label{text-align:center;}#mermaid-svg-9LcARcxvDbFBPuxa .node.clickable{cursor:pointer;}#mermaid-svg-9LcARcxvDbFBPuxa .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-9LcARcxvDbFBPuxa .arrowheadPath{fill:#333333;}#mermaid-svg-9LcARcxvDbFBPuxa .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9LcARcxvDbFBPuxa .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9LcARcxvDbFBPuxa .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9LcARcxvDbFBPuxa .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-9LcARcxvDbFBPuxa .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9LcARcxvDbFBPuxa .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-9LcARcxvDbFBPuxa .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9LcARcxvDbFBPuxa .cluster text{fill:#333;}#mermaid-svg-9LcARcxvDbFBPuxa .cluster span{color:#333;}#mermaid-svg-9LcARcxvDbFBPuxa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9LcARcxvDbFBPuxa .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-9LcARcxvDbFBPuxa rect.text{fill:none;stroke-width:0;}#mermaid-svg-9LcARcxvDbFBPuxa .icon-shape,#mermaid-svg-9LcARcxvDbFBPuxa .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9LcARcxvDbFBPuxa .icon-shape p,#mermaid-svg-9LcARcxvDbFBPuxa .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-9LcARcxvDbFBPuxa .icon-shape .label rect,#mermaid-svg-9LcARcxvDbFBPuxa .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9LcARcxvDbFBPuxa .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-9LcARcxvDbFBPuxa .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-9LcARcxvDbFBPuxa :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 需要工具
用户输入: 北京天气怎么样?
think_node: 识别到天气关键词
should_use_tool: current_tool = get_weather
tool_node: 执行天气查询
respond_node: 生成回复
最终回复: 北京今天晴,25度,微风

代码:

python 复制代码
from typing import TypedDict
from langgraph.graph import StateGraph, END

# 定义 State
class AgentState(TypedDict):
    messages: list
    current_tool: str
    tool_result: str

# 定义 Node
def think_node(state: AgentState) -> AgentState:
    messages = state["messages"]
    last_message = messages[-1]["content"]
    
    if "天气" in last_message:
        return {**state, "current_tool": "get_weather"}
    elif "计算" in last_message:
        return {**state, "current_tool": "calculator"}
    else:
        return {**state, "current_tool": "none"}

def tool_node(state: AgentState) -> AgentState:
    current_tool = state["current_tool"]
    if current_tool == "get_weather":
        result = "北京今天晴,25度,微风"
    elif current_tool == "calculator":
        result = "计算结果: 126"
    else:
        result = "未知工具"
    return {**state, "tool_result": result}

def respond_node(state: AgentState) -> AgentState:
    tool_result = state["tool_result"]
    response = f"根据查询结果,{tool_result}"
    return {**state, "messages": state["messages"] + [{"role": "assistant", "content": response}]}

def should_use_tool(state: AgentState) -> str:
    if state.get("current_tool") and state["current_tool"] != "none":
        return "tool_node"
    return "respond_node"

# 创建 Graph
graph = StateGraph(AgentState)
graph.add_node("think", think_node)
graph.add_node("tool", tool_node)
graph.add_node("respond", respond_node)
graph.set_entry_point("think")
graph.add_conditional_edges("think", should_use_tool, {
    "tool_node": "tool",
    "respond_node": "respond"
})
graph.add_edge("tool", "respond")
graph.add_edge("respond", END)

# 编译并运行
app = graph.compile()
result = app.invoke({
    "messages": [{"role": "user", "content": "北京天气怎么样?"}],
    "current_tool": "",
    "tool_result": ""
})
print(result["messages"][-1]["content"])

示例 2:真实版(使用 LLM)

执行流程图:
#mermaid-svg-YLpKSotriMRrX4hG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-YLpKSotriMRrX4hG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-YLpKSotriMRrX4hG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-YLpKSotriMRrX4hG .error-icon{fill:#552222;}#mermaid-svg-YLpKSotriMRrX4hG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YLpKSotriMRrX4hG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-YLpKSotriMRrX4hG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YLpKSotriMRrX4hG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YLpKSotriMRrX4hG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-YLpKSotriMRrX4hG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YLpKSotriMRrX4hG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YLpKSotriMRrX4hG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YLpKSotriMRrX4hG .marker.cross{stroke:#333333;}#mermaid-svg-YLpKSotriMRrX4hG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YLpKSotriMRrX4hG p{margin:0;}#mermaid-svg-YLpKSotriMRrX4hG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-YLpKSotriMRrX4hG .cluster-label text{fill:#333;}#mermaid-svg-YLpKSotriMRrX4hG .cluster-label span{color:#333;}#mermaid-svg-YLpKSotriMRrX4hG .cluster-label span p{background-color:transparent;}#mermaid-svg-YLpKSotriMRrX4hG .label text,#mermaid-svg-YLpKSotriMRrX4hG span{fill:#333;color:#333;}#mermaid-svg-YLpKSotriMRrX4hG .node rect,#mermaid-svg-YLpKSotriMRrX4hG .node circle,#mermaid-svg-YLpKSotriMRrX4hG .node ellipse,#mermaid-svg-YLpKSotriMRrX4hG .node polygon,#mermaid-svg-YLpKSotriMRrX4hG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-YLpKSotriMRrX4hG .rough-node .label text,#mermaid-svg-YLpKSotriMRrX4hG .node .label text,#mermaid-svg-YLpKSotriMRrX4hG .image-shape .label,#mermaid-svg-YLpKSotriMRrX4hG .icon-shape .label{text-anchor:middle;}#mermaid-svg-YLpKSotriMRrX4hG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-YLpKSotriMRrX4hG .rough-node .label,#mermaid-svg-YLpKSotriMRrX4hG .node .label,#mermaid-svg-YLpKSotriMRrX4hG .image-shape .label,#mermaid-svg-YLpKSotriMRrX4hG .icon-shape .label{text-align:center;}#mermaid-svg-YLpKSotriMRrX4hG .node.clickable{cursor:pointer;}#mermaid-svg-YLpKSotriMRrX4hG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-YLpKSotriMRrX4hG .arrowheadPath{fill:#333333;}#mermaid-svg-YLpKSotriMRrX4hG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-YLpKSotriMRrX4hG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-YLpKSotriMRrX4hG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YLpKSotriMRrX4hG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-YLpKSotriMRrX4hG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YLpKSotriMRrX4hG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-YLpKSotriMRrX4hG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-YLpKSotriMRrX4hG .cluster text{fill:#333;}#mermaid-svg-YLpKSotriMRrX4hG .cluster span{color:#333;}#mermaid-svg-YLpKSotriMRrX4hG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-YLpKSotriMRrX4hG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-YLpKSotriMRrX4hG rect.text{fill:none;stroke-width:0;}#mermaid-svg-YLpKSotriMRrX4hG .icon-shape,#mermaid-svg-YLpKSotriMRrX4hG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YLpKSotriMRrX4hG .icon-shape p,#mermaid-svg-YLpKSotriMRrX4hG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-YLpKSotriMRrX4hG .icon-shape .label rect,#mermaid-svg-YLpKSotriMRrX4hG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YLpKSotriMRrX4hG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-YLpKSotriMRrX4hG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-YLpKSotriMRrX4hG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 需要工具
用户输入: 北京天气怎么样?
llm_node: 调用 MiMo 大模型
should_use_tool: 检测到 tool_calls
tool_node: 执行 get_weather
respond_node: 生成最终回复
最终回复: 北京今天天气晴朗...

代码:

python 复制代码
from typing import TypedDict
from langgraph.graph import StateGraph, END
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI

# 定义工具
@tool
def get_weather(location: str) -> str:
    """获取指定城市的天气信息"""
    weather_data = {"北京": "晴天,25度,微风"}
    return weather_data.get(location, f"{location}的天气数据暂无")

# 定义 State
class AgentState(TypedDict):
    messages: list
    tool_calls: list
    tool_results: list

# 定义 Node
def llm_node(state: AgentState) -> AgentState:
    llm = ChatOpenAI(model="mimo-v2-flash", base_url="api.xiaomimimo.com/v1")
    llm_with_tools = llm.bind_tools([get_weather])
    response = llm_with_tools.invoke(state["messages"])
    tool_calls = response.tool_calls if hasattr(response, 'tool_calls') else []
    return {
        "messages": state["messages"] + [response],
        "tool_calls": tool_calls,
        "tool_results": []
    }

def tool_node(state: AgentState) -> AgentState:
    tool_calls = state["tool_calls"]
    tool_results = []
    for tool_call in tool_calls:
        result = get_weather.invoke(tool_call["args"])
        tool_results.append({"content": result})
    return {
        "messages": state["messages"],
        "tool_calls": [],
        "tool_results": tool_results
    }

def respond_node(state: AgentState) -> AgentState:
    messages = state["messages"]
    for tool_result in state["tool_results"]:
        messages.append({"role": "tool", "content": tool_result["content"]})
    llm = ChatOpenAI(model="mimo-v2-flash", base_url="api.xiaomimimo.com/v1")
    response = llm.invoke(messages)
    return {
        "messages": messages + [response],
        "tool_calls": [],
        "tool_results": []
    }

def should_use_tool(state: AgentState) -> str:
    if state.get("tool_calls"):
        return "tool_node"
    return "end"

# 创建 Graph
graph = StateGraph(AgentState)
graph.add_node("llm", llm_node)
graph.add_node("tool", tool_node)
graph.add_node("respond", respond_node)
graph.set_entry_point("llm")
graph.add_conditional_edges("llm", should_use_tool, {
    "tool_node": "tool",
    "end": END
})
graph.add_edge("tool", "respond")
graph.add_edge("respond", END)

# 编译并运行
app = graph.compile()
result = app.invoke({
    "messages": [{"role": "user", "content": "北京天气怎么样?"}],
    "tool_calls": [],
    "tool_results": []
})
print(result["messages"][-1].content)

🎓 学习收获

1. 核心概念理解

概念关系图:
#mermaid-svg-ybQYR1jsRBb5hUvz{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ybQYR1jsRBb5hUvz .error-icon{fill:#552222;}#mermaid-svg-ybQYR1jsRBb5hUvz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ybQYR1jsRBb5hUvz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ybQYR1jsRBb5hUvz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ybQYR1jsRBb5hUvz .marker.cross{stroke:#333333;}#mermaid-svg-ybQYR1jsRBb5hUvz svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ybQYR1jsRBb5hUvz p{margin:0;}#mermaid-svg-ybQYR1jsRBb5hUvz .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz .cluster-label text{fill:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz .cluster-label span{color:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz .cluster-label span p{background-color:transparent;}#mermaid-svg-ybQYR1jsRBb5hUvz .label text,#mermaid-svg-ybQYR1jsRBb5hUvz span{fill:#333;color:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz .node rect,#mermaid-svg-ybQYR1jsRBb5hUvz .node circle,#mermaid-svg-ybQYR1jsRBb5hUvz .node ellipse,#mermaid-svg-ybQYR1jsRBb5hUvz .node polygon,#mermaid-svg-ybQYR1jsRBb5hUvz .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ybQYR1jsRBb5hUvz .rough-node .label text,#mermaid-svg-ybQYR1jsRBb5hUvz .node .label text,#mermaid-svg-ybQYR1jsRBb5hUvz .image-shape .label,#mermaid-svg-ybQYR1jsRBb5hUvz .icon-shape .label{text-anchor:middle;}#mermaid-svg-ybQYR1jsRBb5hUvz .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ybQYR1jsRBb5hUvz .rough-node .label,#mermaid-svg-ybQYR1jsRBb5hUvz .node .label,#mermaid-svg-ybQYR1jsRBb5hUvz .image-shape .label,#mermaid-svg-ybQYR1jsRBb5hUvz .icon-shape .label{text-align:center;}#mermaid-svg-ybQYR1jsRBb5hUvz .node.clickable{cursor:pointer;}#mermaid-svg-ybQYR1jsRBb5hUvz .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ybQYR1jsRBb5hUvz .arrowheadPath{fill:#333333;}#mermaid-svg-ybQYR1jsRBb5hUvz .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ybQYR1jsRBb5hUvz .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ybQYR1jsRBb5hUvz .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ybQYR1jsRBb5hUvz .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ybQYR1jsRBb5hUvz .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ybQYR1jsRBb5hUvz .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ybQYR1jsRBb5hUvz .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ybQYR1jsRBb5hUvz .cluster text{fill:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz .cluster span{color:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ybQYR1jsRBb5hUvz .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ybQYR1jsRBb5hUvz rect.text{fill:none;stroke-width:0;}#mermaid-svg-ybQYR1jsRBb5hUvz .icon-shape,#mermaid-svg-ybQYR1jsRBb5hUvz .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ybQYR1jsRBb5hUvz .icon-shape p,#mermaid-svg-ybQYR1jsRBb5hUvz .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ybQYR1jsRBb5hUvz .icon-shape .label rect,#mermaid-svg-ybQYR1jsRBb5hUvz .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ybQYR1jsRBb5hUvz .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ybQYR1jsRBb5hUvz .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ybQYR1jsRBb5hUvz :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} LangGraph 核心概念
State 状态
Node 节点
Edge 边
Graph 图
Agent 的记忆
Agent 的工人
Agent 的指挥员
Agent 的流程图
完整的工作流

2. 工作流设计

工作流类型对比:
#mermaid-svg-q7VkJo3fd3jtIqaY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-q7VkJo3fd3jtIqaY .error-icon{fill:#552222;}#mermaid-svg-q7VkJo3fd3jtIqaY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-q7VkJo3fd3jtIqaY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-q7VkJo3fd3jtIqaY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-q7VkJo3fd3jtIqaY .marker.cross{stroke:#333333;}#mermaid-svg-q7VkJo3fd3jtIqaY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-q7VkJo3fd3jtIqaY p{margin:0;}#mermaid-svg-q7VkJo3fd3jtIqaY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY .cluster-label text{fill:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY .cluster-label span{color:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY .cluster-label span p{background-color:transparent;}#mermaid-svg-q7VkJo3fd3jtIqaY .label text,#mermaid-svg-q7VkJo3fd3jtIqaY span{fill:#333;color:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY .node rect,#mermaid-svg-q7VkJo3fd3jtIqaY .node circle,#mermaid-svg-q7VkJo3fd3jtIqaY .node ellipse,#mermaid-svg-q7VkJo3fd3jtIqaY .node polygon,#mermaid-svg-q7VkJo3fd3jtIqaY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-q7VkJo3fd3jtIqaY .rough-node .label text,#mermaid-svg-q7VkJo3fd3jtIqaY .node .label text,#mermaid-svg-q7VkJo3fd3jtIqaY .image-shape .label,#mermaid-svg-q7VkJo3fd3jtIqaY .icon-shape .label{text-anchor:middle;}#mermaid-svg-q7VkJo3fd3jtIqaY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-q7VkJo3fd3jtIqaY .rough-node .label,#mermaid-svg-q7VkJo3fd3jtIqaY .node .label,#mermaid-svg-q7VkJo3fd3jtIqaY .image-shape .label,#mermaid-svg-q7VkJo3fd3jtIqaY .icon-shape .label{text-align:center;}#mermaid-svg-q7VkJo3fd3jtIqaY .node.clickable{cursor:pointer;}#mermaid-svg-q7VkJo3fd3jtIqaY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-q7VkJo3fd3jtIqaY .arrowheadPath{fill:#333333;}#mermaid-svg-q7VkJo3fd3jtIqaY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-q7VkJo3fd3jtIqaY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-q7VkJo3fd3jtIqaY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-q7VkJo3fd3jtIqaY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-q7VkJo3fd3jtIqaY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-q7VkJo3fd3jtIqaY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-q7VkJo3fd3jtIqaY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-q7VkJo3fd3jtIqaY .cluster text{fill:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY .cluster span{color:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-q7VkJo3fd3jtIqaY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-q7VkJo3fd3jtIqaY rect.text{fill:none;stroke-width:0;}#mermaid-svg-q7VkJo3fd3jtIqaY .icon-shape,#mermaid-svg-q7VkJo3fd3jtIqaY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-q7VkJo3fd3jtIqaY .icon-shape p,#mermaid-svg-q7VkJo3fd3jtIqaY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-q7VkJo3fd3jtIqaY .icon-shape .label rect,#mermaid-svg-q7VkJo3fd3jtIqaY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-q7VkJo3fd3jtIqaY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-q7VkJo3fd3jtIqaY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-q7VkJo3fd3jtIqaY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 工作流类型
简单模式
ReAct 模式
多 Agent 模式
线性流程
适合简单场景
带循环
适合复杂场景
多 Agent 协作
适合超复杂场景

3. 对比分析

LangChain Agent vs LangGraph:
#mermaid-svg-fT67gnbIzuFJhVXv{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-fT67gnbIzuFJhVXv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-fT67gnbIzuFJhVXv .error-icon{fill:#552222;}#mermaid-svg-fT67gnbIzuFJhVXv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fT67gnbIzuFJhVXv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fT67gnbIzuFJhVXv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fT67gnbIzuFJhVXv .marker.cross{stroke:#333333;}#mermaid-svg-fT67gnbIzuFJhVXv svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fT67gnbIzuFJhVXv p{margin:0;}#mermaid-svg-fT67gnbIzuFJhVXv .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-fT67gnbIzuFJhVXv .cluster-label text{fill:#333;}#mermaid-svg-fT67gnbIzuFJhVXv .cluster-label span{color:#333;}#mermaid-svg-fT67gnbIzuFJhVXv .cluster-label span p{background-color:transparent;}#mermaid-svg-fT67gnbIzuFJhVXv .label text,#mermaid-svg-fT67gnbIzuFJhVXv span{fill:#333;color:#333;}#mermaid-svg-fT67gnbIzuFJhVXv .node rect,#mermaid-svg-fT67gnbIzuFJhVXv .node circle,#mermaid-svg-fT67gnbIzuFJhVXv .node ellipse,#mermaid-svg-fT67gnbIzuFJhVXv .node polygon,#mermaid-svg-fT67gnbIzuFJhVXv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-fT67gnbIzuFJhVXv .rough-node .label text,#mermaid-svg-fT67gnbIzuFJhVXv .node .label text,#mermaid-svg-fT67gnbIzuFJhVXv .image-shape .label,#mermaid-svg-fT67gnbIzuFJhVXv .icon-shape .label{text-anchor:middle;}#mermaid-svg-fT67gnbIzuFJhVXv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-fT67gnbIzuFJhVXv .rough-node .label,#mermaid-svg-fT67gnbIzuFJhVXv .node .label,#mermaid-svg-fT67gnbIzuFJhVXv .image-shape .label,#mermaid-svg-fT67gnbIzuFJhVXv .icon-shape .label{text-align:center;}#mermaid-svg-fT67gnbIzuFJhVXv .node.clickable{cursor:pointer;}#mermaid-svg-fT67gnbIzuFJhVXv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-fT67gnbIzuFJhVXv .arrowheadPath{fill:#333333;}#mermaid-svg-fT67gnbIzuFJhVXv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-fT67gnbIzuFJhVXv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-fT67gnbIzuFJhVXv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fT67gnbIzuFJhVXv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-fT67gnbIzuFJhVXv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fT67gnbIzuFJhVXv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-fT67gnbIzuFJhVXv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-fT67gnbIzuFJhVXv .cluster text{fill:#333;}#mermaid-svg-fT67gnbIzuFJhVXv .cluster span{color:#333;}#mermaid-svg-fT67gnbIzuFJhVXv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-fT67gnbIzuFJhVXv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-fT67gnbIzuFJhVXv rect.text{fill:none;stroke-width:0;}#mermaid-svg-fT67gnbIzuFJhVXv .icon-shape,#mermaid-svg-fT67gnbIzuFJhVXv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fT67gnbIzuFJhVXv .icon-shape p,#mermaid-svg-fT67gnbIzuFJhVXv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-fT67gnbIzuFJhVXv .icon-shape .label rect,#mermaid-svg-fT67gnbIzuFJhVXv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fT67gnbIzuFJhVXv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-fT67gnbIzuFJhVXv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-fT67gnbIzuFJhVXv :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} LangChain Agent
开箱即用
简单场景
学习成本低
LangGraph
自己设计
复杂场景
学习成本高
共同点
都是 Agent 框架
都支持工具调用
都基于 LangChain


❓ 思考题

第一部分:基础概念

  1. LangGraph 的三个核心概念是什么?
  2. 用 Java 思维对标:State、Node、Edge、Graph 分别类似什么?
  3. LangGraph 的执行流程是什么?

第二部分:对比分析

  1. 对比 LangChain Agent 和 LangGraph
  2. LangGraph 的优势是什么?
  3. 什么时候用 LangChain Agent,什么时候用 LangGraph?

第三部分:设计应用

假设你要设计一个"客服 Agent",它能:

  1. 理解用户问题
  2. 查询订单状态
  3. 处理退款申请
  4. 转接人工客服

请用 LangGraph 的 State、Node、Edge 描述这个工作流。

参考答案流程图:
#mermaid-svg-v6yEenbgMtEjnBqP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-v6yEenbgMtEjnBqP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-v6yEenbgMtEjnBqP .error-icon{fill:#552222;}#mermaid-svg-v6yEenbgMtEjnBqP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-v6yEenbgMtEjnBqP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-v6yEenbgMtEjnBqP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-v6yEenbgMtEjnBqP .marker.cross{stroke:#333333;}#mermaid-svg-v6yEenbgMtEjnBqP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-v6yEenbgMtEjnBqP p{margin:0;}#mermaid-svg-v6yEenbgMtEjnBqP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-v6yEenbgMtEjnBqP .cluster-label text{fill:#333;}#mermaid-svg-v6yEenbgMtEjnBqP .cluster-label span{color:#333;}#mermaid-svg-v6yEenbgMtEjnBqP .cluster-label span p{background-color:transparent;}#mermaid-svg-v6yEenbgMtEjnBqP .label text,#mermaid-svg-v6yEenbgMtEjnBqP span{fill:#333;color:#333;}#mermaid-svg-v6yEenbgMtEjnBqP .node rect,#mermaid-svg-v6yEenbgMtEjnBqP .node circle,#mermaid-svg-v6yEenbgMtEjnBqP .node ellipse,#mermaid-svg-v6yEenbgMtEjnBqP .node polygon,#mermaid-svg-v6yEenbgMtEjnBqP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-v6yEenbgMtEjnBqP .rough-node .label text,#mermaid-svg-v6yEenbgMtEjnBqP .node .label text,#mermaid-svg-v6yEenbgMtEjnBqP .image-shape .label,#mermaid-svg-v6yEenbgMtEjnBqP .icon-shape .label{text-anchor:middle;}#mermaid-svg-v6yEenbgMtEjnBqP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-v6yEenbgMtEjnBqP .rough-node .label,#mermaid-svg-v6yEenbgMtEjnBqP .node .label,#mermaid-svg-v6yEenbgMtEjnBqP .image-shape .label,#mermaid-svg-v6yEenbgMtEjnBqP .icon-shape .label{text-align:center;}#mermaid-svg-v6yEenbgMtEjnBqP .node.clickable{cursor:pointer;}#mermaid-svg-v6yEenbgMtEjnBqP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-v6yEenbgMtEjnBqP .arrowheadPath{fill:#333333;}#mermaid-svg-v6yEenbgMtEjnBqP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-v6yEenbgMtEjnBqP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-v6yEenbgMtEjnBqP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-v6yEenbgMtEjnBqP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-v6yEenbgMtEjnBqP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-v6yEenbgMtEjnBqP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-v6yEenbgMtEjnBqP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-v6yEenbgMtEjnBqP .cluster text{fill:#333;}#mermaid-svg-v6yEenbgMtEjnBqP .cluster span{color:#333;}#mermaid-svg-v6yEenbgMtEjnBqP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-v6yEenbgMtEjnBqP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-v6yEenbgMtEjnBqP rect.text{fill:none;stroke-width:0;}#mermaid-svg-v6yEenbgMtEjnBqP .icon-shape,#mermaid-svg-v6yEenbgMtEjnBqP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-v6yEenbgMtEjnBqP .icon-shape p,#mermaid-svg-v6yEenbgMtEjnBqP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-v6yEenbgMtEjnBqP .icon-shape .label rect,#mermaid-svg-v6yEenbgMtEjnBqP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-v6yEenbgMtEjnBqP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-v6yEenbgMtEjnBqP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-v6yEenbgMtEjnBqP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 查询订单
退款申请
转接人工
其他
用户输入
意图识别节点
判断意图
订单查询节点
退款处理节点
转接人工节点
通用回复节点
生成回复
END


📝 总结

Day 10 的学习让我们从"会用 Agent"升级到"会构建 Agent 工作流"。

核心收获:

  1. LangGraph 的核心概念:State、Node、Edge、Graph
  2. 工作流设计:从简单到复杂,从线性到循环
  3. 对比分析:LangChain Agent vs LangGraph 的使用场景

学习路径图:
#mermaid-svg-oeZ8e10QssuoDEYJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-oeZ8e10QssuoDEYJ .error-icon{fill:#552222;}#mermaid-svg-oeZ8e10QssuoDEYJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-oeZ8e10QssuoDEYJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-oeZ8e10QssuoDEYJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-oeZ8e10QssuoDEYJ .marker.cross{stroke:#333333;}#mermaid-svg-oeZ8e10QssuoDEYJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-oeZ8e10QssuoDEYJ p{margin:0;}#mermaid-svg-oeZ8e10QssuoDEYJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ .cluster-label text{fill:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ .cluster-label span{color:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ .cluster-label span p{background-color:transparent;}#mermaid-svg-oeZ8e10QssuoDEYJ .label text,#mermaid-svg-oeZ8e10QssuoDEYJ span{fill:#333;color:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ .node rect,#mermaid-svg-oeZ8e10QssuoDEYJ .node circle,#mermaid-svg-oeZ8e10QssuoDEYJ .node ellipse,#mermaid-svg-oeZ8e10QssuoDEYJ .node polygon,#mermaid-svg-oeZ8e10QssuoDEYJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-oeZ8e10QssuoDEYJ .rough-node .label text,#mermaid-svg-oeZ8e10QssuoDEYJ .node .label text,#mermaid-svg-oeZ8e10QssuoDEYJ .image-shape .label,#mermaid-svg-oeZ8e10QssuoDEYJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-oeZ8e10QssuoDEYJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-oeZ8e10QssuoDEYJ .rough-node .label,#mermaid-svg-oeZ8e10QssuoDEYJ .node .label,#mermaid-svg-oeZ8e10QssuoDEYJ .image-shape .label,#mermaid-svg-oeZ8e10QssuoDEYJ .icon-shape .label{text-align:center;}#mermaid-svg-oeZ8e10QssuoDEYJ .node.clickable{cursor:pointer;}#mermaid-svg-oeZ8e10QssuoDEYJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-oeZ8e10QssuoDEYJ .arrowheadPath{fill:#333333;}#mermaid-svg-oeZ8e10QssuoDEYJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-oeZ8e10QssuoDEYJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-oeZ8e10QssuoDEYJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-oeZ8e10QssuoDEYJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-oeZ8e10QssuoDEYJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-oeZ8e10QssuoDEYJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-oeZ8e10QssuoDEYJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-oeZ8e10QssuoDEYJ .cluster text{fill:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ .cluster span{color:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-oeZ8e10QssuoDEYJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-oeZ8e10QssuoDEYJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-oeZ8e10QssuoDEYJ .icon-shape,#mermaid-svg-oeZ8e10QssuoDEYJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-oeZ8e10QssuoDEYJ .icon-shape p,#mermaid-svg-oeZ8e10QssuoDEYJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-oeZ8e10QssuoDEYJ .icon-shape .label rect,#mermaid-svg-oeZ8e10QssuoDEYJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-oeZ8e10QssuoDEYJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-oeZ8e10QssuoDEYJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-oeZ8e10QssuoDEYJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Day 1-8
Python 基础 + API 调用
Day 9: Agent 源码解析
Day 10: LangGraph
Day 11: 多 Agent 协作
Day 12: 人机协作
Day 13: 持久化

下一步:

  • Day 11:多 Agent 协作
  • Day 12:人机协作
  • Day 13:持久化

📚 参考资料

  1. LangGraph 官方文档
  2. LangChain Agent 源码
  3. ReAct 论文

系列文章:



📅 Day 9 完成! 继续加油,明天见!

💬 个人格言: 代码改不改变世界我不知道,但先让我准时下班。

相关推荐
hikktn1 小时前
Excel 导出 OOM 预防实战:30 万行从堆溢出到 50MB 的演进
java·excel·easyexcel
风味蘑菇干1 小时前
WTomcat服务器
java·服务器
weixin_307779131 小时前
Python写入Shell文件使用Linux系统的换行符
linux·开发语言·python·自动化
ylscode1 小时前
GreatXML BitLocker绕过漏洞深度解析:Windows Defender离线扫描如何被改造成本地提权后门
windows·安全
燕-孑2 小时前
tomcat详解(基础到高级生产)
java·tomcat
zmzb01032 小时前
Python课后习题训练记录Day130
开发语言·python
码不停蹄的玄黓2 小时前
Spring Bean 生命周期
java·后端·spring
西安邮电大学2 小时前
分治算法详细讲解
java·后端·其他·算法·面试
摇滚侠2 小时前
Mybatis 入门到项目实战 搭建 MyBatis 框架 01-14
java·tomcat·mybatis