AI Agent 开发框架对比:LangGraph vs CrewAI vs AutoGen

作者:Maurice | 灵阙学院

选型背景

2025-2026 年,AI Agent 开发框架从百花齐放走向收敛。对于技术团队来说,选择合适的框架直接影响开发效率和产品质量。本文对比三个主流开源框架的架构设计、适用场景和工程实践。

框架概览

维度 LangGraph CrewAI AutoGen
维护方 LangChain (Harrison Chase) CrewAI Inc (Joao Moura) Microsoft Research
首发时间 2024 Q1 2023 Q4 2023 Q3
核心抽象 状态图(StateGraph) 角色+任务(Crew) 对话代理(ConversableAgent)
编排模式 显式图编排 声明式角色分配 对话驱动
模型绑定 任意(通过 LangChain) 任意 任意
生产就绪度 中高
GitHub Stars 10K+ 25K+ 35K+

LangGraph:显式图编排

设计哲学

LangGraph 的核心是状态图(StateGraph)——将 Agent 的工作流程建模为有限状态机。每个节点是一个处理步骤,边定义了条件转移逻辑。

核心概念

State(状态):贯穿整个图的共享数据结构

from typing import TypedDict, Annotated

class AgentState(TypedDict):
    messages: Annotated[list, add_messages]
    current_step: str
    findings: list[str]
    final_report: str

Node(节点):处理状态的函数

async def research_node(state: AgentState) -> dict:
    """研究节点:搜索相关信息"""
    query = state["messages"][-1].content
    results = await search_tool(query)
    return {"findings": results, "current_step": "analyze"}

Edge(边):条件路由

def route_decision(state: AgentState) -> str:
    if len(state["findings"]) > 5:
        return "synthesize"
    return "research_more"

Graph(图):组合节点和边

graph = StateGraph(AgentState)
graph.add_node("research", research_node)
graph.add_node("analyze", analyze_node)
graph.add_node("synthesize", synthesize_node)

graph.add_edge("research", "analyze")
graph.add_conditional_edges("analyze", route_decision)
graph.set_entry_point("research")

优势

  1. 完全可控:每个状态转移都是显式的,没有"黑箱"
  2. 可观测:通过 LangSmith 追踪每个节点的输入输出
  3. 持久化:内置 Checkpointing,支持断点续传
  4. 人机协作:原生支持 Human-in-the-Loop 节点
  5. 流式输出:支持节点级别的流式结果返回

局限

  1. 学习曲线陡:状态图概念对非图论背景的开发者不直观
  2. 代码量大:简单任务也需要定义 State/Node/Edge
  3. 过度工程:对于简单的线性工作流,图编排是过度设计

适用场景

  • 复杂多分支工作流(如审计流程、合规检查)
  • 需要 Human-in-the-Loop 的关键决策点
  • 需要断点续传的长时任务
  • 对可观测性要求高的生产系统

CrewAI:角色化协作

设计哲学

CrewAI 的核心是角色扮演——将 Agent 团队建模为一组有明确分工的角色(Agent),每个角色有背景故事、目标和可用工具,共同完成任务(Task)。

核心概念

Agent(角色)

from crewai import Agent

researcher = Agent(
    role="AI Research Analyst",
    goal="Find the most relevant and recent information on the topic",
    backstory="You are a senior research analyst with 10 years of experience...",
    tools=[search_tool, web_scraper],
    llm="gpt-4",
    verbose=True,
)

writer = Agent(
    role="Technical Writer",
    goal="Transform research findings into clear, engaging articles",
    backstory="You are an award-winning technical writer...",
    tools=[],
    llm="claude-3-sonnet",
)

Task(任务)

from crewai import Task

research_task = Task(
    description="Research the latest trends in AI Agent frameworks...",
    expected_output="A comprehensive research report with sources",
    agent=researcher,
)

writing_task = Task(
    description="Write a 2000-word article based on the research...",
    expected_output="A polished article in Markdown format",
    agent=writer,
    context=[research_task],  # 依赖研究任务的输出
)

Crew(团队)

from crewai import Crew, Process

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,  # 或 Process.hierarchical
    verbose=True,
)

result = crew.kickoff()

优势

  1. 直觉化:角色/任务/团队的概念贴近人类协作方式
  2. 快速上手:10 行代码就能跑起来一个多 Agent 系统
  3. 灵活委派:支持 hierarchical 模式,由 Manager Agent 动态分配
  4. 社区活跃:GitHub Stars 最高,社区生态丰富

局限

  1. 控制粒度粗:无法精细控制 Agent 之间的交互逻辑
  2. 调试困难:Agent 之间的对话是"黑箱",出错难以定位
  3. 稳定性波动:Agent 的输出质量依赖 LLM 的表现,难以保证一致
  4. 记忆管理弱:跨任务的记忆传递机制不够成熟

适用场景

  • 内容创作流水线(研究 → 写作 → 审校)
  • 数据分析团队(采集 → 清洗 → 分析 → 可视化)
  • 快速原型验证(MVP 阶段)
  • 非关键路径的自动化任务

AutoGen:对话驱动

设计哲学

AutoGen 的核心是对话式协作——所有 Agent 通过对话交互完成任务。每个 Agent 既可以生成消息,也可以接收和处理消息。

核心概念

from autogen import AssistantAgent, UserProxyAgent

# 助手 Agent(LLM 驱动)
assistant = AssistantAgent(
    name="assistant",
    llm_config={"model": "gpt-4"},
    system_message="You are a helpful AI assistant...",
)

# 用户代理 Agent(可执行代码)
user_proxy = UserProxyAgent(
    name="user_proxy",
    human_input_mode="TERMINATE",  # 何时需要人工介入
    code_execution_config={"work_dir": "workspace"},
)

# 启动对话
user_proxy.initiate_chat(
    assistant,
    message="Write a Python script that analyzes sales data...",
)

GroupChat(群聊模式)

from autogen import GroupChat, GroupChatManager

analyst = AssistantAgent(name="analyst", ...)
coder = AssistantAgent(name="coder", ...)
reviewer = AssistantAgent(name="reviewer", ...)

group_chat = GroupChat(
    agents=[user_proxy, analyst, coder, reviewer],
    messages=[],
    max_round=20,
)

manager = GroupChatManager(groupchat=group_chat, llm_config=llm_config)
user_proxy.initiate_chat(manager, message="Analyze Q4 sales data...")

优势

  1. 代码执行:原生支持 Agent 生成并执行代码
  2. 灵活对话:Agent 可以自由对话、提问、质疑
  3. 人工介入:多种 human_input_mode 控制人工参与度
  4. 微软生态:与 Azure AI、Office 365 集成便利

局限

  1. 对话失控:Agent 之间可能陷入无意义的"客套对话"
  2. 成本不可控:对话轮次难以预测,token 消耗波动大
  3. 确定性低:相同输入不一定产生相同的对话路径
  4. 生产化难度:从对话 Demo 到生产系统的差距较大

适用场景

  • 代码生成与调试(Agent 写代码 + 执行 + 修bug)
  • 研究讨论(多视角分析同一问题)
  • 教育辅导(Tutor Agent + Student Agent)
  • 需要代码执行能力的自动化任务

选型决策矩阵

需求 推荐框架 原因
复杂工作流 + 生产稳定 LangGraph 显式控制 + 持久化
快速原型 + 多角色 CrewAI 直觉化 API + 快速上手
代码生成 + 执行 AutoGen 原生代码执行能力
合规审计流程 LangGraph HITL + 可审计
内容生产流水线 CrewAI 角色分工自然
数据分析探索 AutoGen 代码执行 + 交互式
长时运行任务 LangGraph Checkpointing

混合使用建议

在实际项目中,三个框架不是互斥的:

  1. LangGraph 做编排:作为顶层工作流引擎
  2. CrewAI 做子任务:在 LangGraph 的某个节点内运行一个 CrewAI Crew
  3. AutoGen 做代码任务:需要代码执行的节点使用 AutoGen
LangGraph Workflow
├── Node 1: 需求分析 (LangGraph)
├── Node 2: 多角色研究 (CrewAI Crew)
├── Node 3: 数据分析 (AutoGen + Code Execution)
├── Node 4: Human Review (LangGraph HITL)
└── Node 5: 报告生成 (CrewAI Crew)

新兴竞品

除了三大框架,还有值得关注的新选手:

  • Google ADK:Google 官方 Agent 开发框架,深度集成 Gemini
  • Claude Agent SDK:Anthropic 官方,Claude 原生支持
  • Pydantic AI:Pydantic 团队出品,类型安全优先
  • smolagents(HuggingFace):轻量级,适合开源模型

Maurice | maurice_wen@proton.me