云计算百科
云计算领域专业知识百科平台

智能体元年来了:程序员必懂的 6 大 Agent 工作流设计模式(附实战案例)

1. 引言:从 Prompt Engineering 到 Agentic Workflow

2.0 时代的 AI 开发,核心不再是写出“完美的提示词”,而是构建“完美的流程”。吴恩达教授曾提到:“即使是较弱的模型,在优秀的工作流设计下,其表现也能超过缺乏流程的最强模型。”

对于程序员而言,Agent 模式意味着我们将 AI 视为一个异步的、有状态的函数执行器。今天,我们将深度拆解 6 大核心模式,并重点实战最具生命力的“多智能体协作”模式。


2. 程序员必读:Agent 工作流的 6 大设计模式

为了方便理解,我们将这些模式总结为下表:

模式名称核心逻辑适用场景复杂度
Reflection (反思) 生成 -> 评价 -> 修正 代码生成、文案润色 ⭐⭐
**Tool Use (工具) ** 识别需求 -> 调用外部 API 搜索、执行数学计算 ⭐⭐
Planning (规划) 分解任务 -> 逐步执行 复杂项目管理、旅行规划 ⭐⭐⭐
Multi-agent (协作) 角色分工 -> 消息总线 -> 共识 软件开发全流程、市场调研 ⭐⭐⭐⭐⭐
Dynamic Control (动态) 根据中间结果调整分支路径 自动化故障排查 ⭐⭐⭐⭐
Memory Management (记忆) 短期缓存 + 向量数据库长期检索 个人助理、长文本分析 ⭐⭐⭐

3. 核心模式深度剖析:多智能体协作 (Multi-agent Collaboration)

多智能体协作的本质是**“分治法”**。

在 LangGraph 框架下,每个 Agent 被抽象为一个节点 (Node),它们之间的交互则是边 (Edge)。这解决了传统 LangChain Chain 模式下难以处理的“循环迭代”和“状态回溯”问题。

3.1 为什么选择 LangGraph?

传统的线性工作流(Chain)像是一根筋的流水线,而 LangGraph 允许我们构建有环图(Cyclic Graph)。

  • 状态持久化:能够保存中间对话状态。
  • 人机协作模式:支持在特定节点插入“人工审批”。
  • 细粒度控制:精准控制哪个 Agent 在什么时候发言。

4. 实战演示:基于 LangGraph 构建“技术文章创作团队”

我们将模拟一个小型工作室:一个 Researcher(研究员) 负责搜集资料,一个 Writer(作家) 负责撰写,一个 Reviewer(审核员) 负责找茬反馈。

4.1 架构设计(Mermaid 渲染)

#mermaid-svg-l2VOf8poD32sG2bn{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-l2VOf8poD32sG2bn .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-l2VOf8poD32sG2bn .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-l2VOf8poD32sG2bn .error-icon{fill:#552222;}#mermaid-svg-l2VOf8poD32sG2bn .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-l2VOf8poD32sG2bn .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-l2VOf8poD32sG2bn .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-l2VOf8poD32sG2bn .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-l2VOf8poD32sG2bn .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-l2VOf8poD32sG2bn .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-l2VOf8poD32sG2bn .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-l2VOf8poD32sG2bn .marker{fill:#333333;stroke:#333333;}#mermaid-svg-l2VOf8poD32sG2bn .marker.cross{stroke:#333333;}#mermaid-svg-l2VOf8poD32sG2bn svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-l2VOf8poD32sG2bn p{margin:0;}#mermaid-svg-l2VOf8poD32sG2bn .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-l2VOf8poD32sG2bn .cluster-label text{fill:#333;}#mermaid-svg-l2VOf8poD32sG2bn .cluster-label span{color:#333;}#mermaid-svg-l2VOf8poD32sG2bn .cluster-label span p{background-color:transparent;}#mermaid-svg-l2VOf8poD32sG2bn .label text,#mermaid-svg-l2VOf8poD32sG2bn span{fill:#333;color:#333;}#mermaid-svg-l2VOf8poD32sG2bn .node rect,#mermaid-svg-l2VOf8poD32sG2bn .node circle,#mermaid-svg-l2VOf8poD32sG2bn .node ellipse,#mermaid-svg-l2VOf8poD32sG2bn .node polygon,#mermaid-svg-l2VOf8poD32sG2bn .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-l2VOf8poD32sG2bn .rough-node .label text,#mermaid-svg-l2VOf8poD32sG2bn .node .label text,#mermaid-svg-l2VOf8poD32sG2bn .image-shape .label,#mermaid-svg-l2VOf8poD32sG2bn .icon-shape .label{text-anchor:middle;}#mermaid-svg-l2VOf8poD32sG2bn .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-l2VOf8poD32sG2bn .rough-node .label,#mermaid-svg-l2VOf8poD32sG2bn .node .label,#mermaid-svg-l2VOf8poD32sG2bn .image-shape .label,#mermaid-svg-l2VOf8poD32sG2bn .icon-shape .label{text-align:center;}#mermaid-svg-l2VOf8poD32sG2bn .node.clickable{cursor:pointer;}#mermaid-svg-l2VOf8poD32sG2bn .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-l2VOf8poD32sG2bn .arrowheadPath{fill:#333333;}#mermaid-svg-l2VOf8poD32sG2bn .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-l2VOf8poD32sG2bn .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-l2VOf8poD32sG2bn .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-l2VOf8poD32sG2bn .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-l2VOf8poD32sG2bn .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-l2VOf8poD32sG2bn .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-l2VOf8poD32sG2bn .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-l2VOf8poD32sG2bn .cluster text{fill:#333;}#mermaid-svg-l2VOf8poD32sG2bn .cluster span{color:#333;}#mermaid-svg-l2VOf8poD32sG2bn 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-l2VOf8poD32sG2bn .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-l2VOf8poD32sG2bn rect.text{fill:none;stroke-width:0;}#mermaid-svg-l2VOf8poD32sG2bn .icon-shape,#mermaid-svg-l2VOf8poD32sG2bn .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-l2VOf8poD32sG2bn .icon-shape p,#mermaid-svg-l2VOf8poD32sG2bn .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-l2VOf8poD32sG2bn .icon-shape rect,#mermaid-svg-l2VOf8poD32sG2bn .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-l2VOf8poD32sG2bn .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-l2VOf8poD32sG2bn .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-l2VOf8poD32sG2bn :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

反馈修改建议

审核通过

开始

Researcher Agent

Writer Agent

Reviewer Agent

发布

4.2 核心代码实现 (Python + LangGraph)

import operator
from typing import Annotated, List, TypedDict
from langgraph.graph import StateGraph, END

# 1. 定义状态对象:这是 Agent 之间共享的“黑板”
class AgentState(TypedDict):
topic: str
content: str
feedback: str
revision_count: int

# 2. 定义 Researcher 节点
def researcher_node(state: AgentState):
topic = state['topic']
# 模拟调用搜索工具
research_notes = f"关于 {topic} 的最新技术趋势是…"
return {"content": research_notes}

# 3. 定义 Writer 节点
def writer_node(state: AgentState):
content = state['content']
feedback = state.get('feedback', "")
# 根据反馈修改
draft = f"基于资料: {content}。针对反馈: {feedback} 创作的博文。"
return {"content": draft, "revision_count": state.get('revision_count', 0) + 1}

# 4. 定义 Reviewer 节点(控制流核心)
def reviewer_node(state: AgentState):
if len(state['content']) > 100:
return "accept"
return "reject"

# 5. 构建工作流图
workflow = StateGraph(AgentState)

workflow.add_node("researcher", researcher_node)
workflow.add_node("writer", writer_node)

workflow.set_entry_point("researcher")
workflow.add_edge("researcher", "writer")

# 增加逻辑分支:如果 Reviewer 拒绝,则回跳到 Writer
workflow.add_conditional_edges(
"writer",
reviewer_node,
{
"accept": END,
"reject": "writer"
}
)

app = workflow.compile()


5. 资深开发者进阶:大流量下的 Agent 性能与成本控制

在生产环境下,Agent 工作流会面临两个严峻挑战:Token 消耗和响应延迟。

5.1 状态裁剪 (State Management)

随着 Agent 往复对话,上下文会迅速膨胀。

  • 技巧:在 LangGraph 的 State 中,只保留最新的 轮对话,或者对历史背景进行摘要。

5.2 并行化执行 (Parallelism)

如果多个 Agent 之间没有依赖关系(例如同时搜集 3 个维度的信息),应使用 Annotated[List, operator.add] 实现节点的并行扇出(Fan-out)与扇入(Fan-in)。


6. 常见问题解答 (FAQ)

Q: Agent 陷入死循环(Looping)怎么办?
A: 必须设置 max_iterations。在 LangGraph 中,可以通过在 State 中维护一个计数器,当达到 5 次迭代仍未通过审核时,强制终止并报错或人工介入。

Q: 为什么不用 AutoGPT 这种全自动工具?
A: 全自动意味着不可控。对于程序员来说,基于图结构(Graph-based)的确定性流控才是商业化落地的首选,它在“灵活性”与“确定性”之间取得了平衡。


7. 总结:拥抱 Agentic 原生开发

智能体元年不是 AI 代替程序员,而是程序员升级为 “AI 架构师”。通过设计合理的工作流(Workflow),我们能让大模型从“会说话的百科全书”变成“能干活的数字化员工”。

在多智能体协作中,你认为最难的部分是“Prompt 的精准度”还是“Agent 之间的通信协议设计”?你目前在尝试将 Agent 应用在哪个具体的业务场景?
在评论区留下你的思考,我们将抽取 3 位深度评论送出最新的 Agent 开发思维导图一份! 🚀

赞(0)
未经允许不得转载:网硕互联帮助中心 » 智能体元年来了:程序员必懂的 6 大 Agent 工作流设计模式(附实战案例)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!