AI Agent已经从研究演示发展为生产系统。预计到2026年,超过60%的企业AI应用将包含Agent组件。但从零开始构建Agent - 管理工具循环、状态、内存、错误处理和多Agent协调 - 是非常复杂的。这就是框架发挥作用的地方。
2026年有四个框架占据主导地位: LangGraph、CrewAI、OpenAI Agents SDK 和 Claude Agent SDK。每个框架对同一个问题采取了根本不同的方法: 赋予LLM推理、规划、使用工具和协作的能力。
总览
| 方面 | LangGraph | CrewAI | OpenAI Agents SDK | Claude Agent SDK |
|---|---|---|---|---|
| 开发方 | LangChain | CrewAI Inc. | OpenAI | Anthropic |
| 架构 | 基于图 | 基于角色 | 基于交接 | 自主循环 |
| 理念 | 最大控制力 | 团队协作 | 最少抽象 | 给Agent一台电脑 |
| 语言 | Python, TypeScript | Python | Python | Python, TypeScript |
| 模型支持 | 任意 (OpenAI, Claude, 本地) | 任意 | 任意 (不顾其名称) | 仅限Claude |
| GitHub stars | ~29k | ~40k | ~21k | ~6k |
| 最适合 | 复杂的有状态工作流 | 多Agent专业化 | 路由和分流 | 编码和文件密集型任务 |
LangGraph: 图构建器
LangGraph将Agent工作流建模为有向循环图。你定义节点(执行工作的函数)和边(节点之间的转换,可以是条件性的)。状态在图中流动,并通过检查点实现持久化。
这是最显式、最可控的框架 - 每一步都由你自己连接。
核心概念
- StateGraph: 带类型化状态的图定义
- Nodes: 转换状态的Python函数
- Edges: 节点之间的连接,可以是条件性的
- Checkpointing: 用于长时间运行工作流的内置持久化
代码示例
from langgraph.graph import StateGraph, MessagesState, START, END from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4o") def call_agent(state: MessagesState): response = llm.invoke(state["messages"]) return {"messages": [response]} def should_continue(state: MessagesState): last = state["messages"][-1] if last.tool_calls: return "tools" return END def call_tools(state: MessagesState): # Execute tool calls and return results results = [] for tool_call in state["messages"][-1].tool_calls: result = execute_tool(tool_call) results.append(result) return {"messages": results} graph = StateGraph(MessagesState) graph.add_node("agent", call_agent) graph.add_node("tools", call_tools) graph.add_edge(START, "agent") graph.add_conditional_edges("agent", should_continue, {"tools": "tools", END: END}) graph.add_edge("tools", "agent") app = graph.compile() result = app.invoke({"messages": [{"role": "user", "content": "What's the weather?"}]})
优势
- 对每个步骤和转换进行细粒度控制
- 内置检查点和human-in-the-loop
- TypeScript完全支持
- 兼容任何LLM提供商
- 最适合具有条件分支和循环的复杂工作流
劣势
- 学习曲线陡峭 - 需要理解图论概念
- 对于简单用例过于冗长 - 基本Agent需要比其他框架更多的模板代码
- 没有LangSmith的情况下调试图流程可能很困难
定价
开源 (MIT)。LangSmith (托管可观测性平台) 提供生产监控的付费层级。
CrewAI: 团队组装器
CrewAI采用人类隐喻: 你组建一个由专业Agent组成的团队,每个Agent都有角色、目标和背景故事。Agent使用工具在任务上协作,由流程 (顺序、层级或共识) 进行协调。
可以把它想象成雇佣一个团队,每个成员都有特定的职位和专长。
核心概念
- Agent: 具有角色、目标、背景故事和工具的角色
- Task: 具有描述、预期输出和指定Agent的任务分配
- Crew: 协同工作的Agent组
- Process: 执行策略 (顺序、层级、共识)
- Flow: 用于连接多个团队的事件驱动编排层
代码示例
from crewai import Agent, Task, Crew, Process researcher = Agent( role="Senior Research Analyst", goal="Find comprehensive data about the given topic", backstory="You have 10 years of experience in technology research. " "You are thorough and always verify facts from multiple sources.", tools=[web_search_tool], verbose=True, ) writer = Agent( role="Technical Writer", goal="Create clear, engaging technical content", backstory="You write for a developer audience. " "Your articles are practical and include code examples.", tools=[file_tool], verbose=True, ) research_task = Task( description="Research the latest developments in WebAssembly in 2026. " "Focus on WASI, Component Model, and production use cases.", expected_output="A structured research document with key findings and sources.", agent=researcher, ) writing_task = Task( description="Write a blog post based on the research. " "Include code examples and Mermaid diagrams.", expected_output="A complete blog post in Markdown format.", agent=writer, context=[research_task], # Writer receives researcher's output ) crew = Crew( agents=[researcher, writer], tasks=[research_task, writing_task], process=Process.sequential, verbose=True, ) result = crew.kickoff() print(result.raw)
优势
- 直观的基于角色的抽象 - 易于理解
- 100+内置工具集成
- Agent间共享内存 (短期、长期、实体)
- 最大的社区 (~40k GitHub stars)
- 具有"管理者"Agent的层级流程,可以委派和验证
劣势
- 不如LangGraph那样细粒度的控制 - 你定义的是角色,不是精确的执行路径
- 当Agent意见不一致时,层级流程可能不可预测
- 多Agent对话的调试比单Agent流程更困难
定价
开源核心 (免费)。CrewAI Platform: $99/月 (Teams) 到 $120k/年 (Enterprise)。基于活跃团队数和月执行次数定价。
OpenAI Agents SDK: 路由器
OpenAI Agents SDK (Swarm的精神继承者) 专注于交接 - Agent将对话转移给其他专业Agent。这是最精简的框架: Agent、工具、交接和护栏。仅此而已。
核心概念
- Agent: 模型 + 指令 + 工具 + 交接
- Handoff: 向另一个Agent的转移 (建模为LLM可以调用的工具)
- Guardrail: 与Agent并行运行的输入/输出验证
- Runner: 执行Agent循环
- Tracing: 所有LLM调用、工具调用和交接的内置可观测性
代码示例
from agents import Agent, Runner, handoff, InputGuardrail, GuardrailFunctionOutput from pydantic import BaseModel class SafetyCheck(BaseModel): is_safe: bool reason: str async def content_safety(ctx, agent, input_text): result = await Runner.run( Agent(name="Safety", instructions="Check if input is safe. No PII."), input_text, context=ctx, ) output = SafetyCheck.model_validate_json(result.final_output) return GuardrailFunctionOutput( output_info=output, tripwire_triggered=not output.is_safe ) billing_agent = Agent( name="Billing Agent", instructions="You handle billing inquiries. Be precise with numbers.", tools=[lookup_invoice, process_payment], ) refund_agent = Agent( name="Refund Agent", instructions="You process refund requests. Always verify the order first.", tools=[lookup_order, issue_refund], ) triage_agent = Agent( name="Triage Agent", instructions="Route the customer to the right specialist. " "Ask clarifying questions if needed.", handoffs=[billing_agent, refund_agent], input_guardrails=[InputGuardrail(guardrail_function=content_safety)], ) result = await Runner.run(triage_agent, "I need a refund for order #4521") print(result.final_output) # The triage agent routes to refund_agent, which processes the refund
优势
- 简洁的交接模式 - 对路由/分流工作流来说很自然
- 护栏与执行并行运行 (快速失败,非阻塞)
- 内置调试用的追踪仪表板
- 尽管名称如此,也支持非OpenAI模型
- 最少抽象 - 易于理解和扩展
劣势
- 状态管理不如LangGraph成熟
- 没有内置持久化或检查点
- 第三方工具生态系统较小
- 以交接为中心的设计可能不适合所有架构
定价
开源 (MIT)。按使用的模型按token付费。
Claude Agent SDK: 开发者
Claude Agent SDK采取了不同的方法: 不是定义工作流或角色,而是给Agent一组工具,让它自己找出如何完成任务。它使用与Claude Code相同的自主循环 - 读取、执行、验证、迭代。
核心概念
- query(): 启动Agent循环的主入口
- 内置工具: Read, Write, Edit, Bash, Glob, Grep, WebSearch, WebFetch
- 通过MCP自定义工具: 将工具定义为进程内MCP服务器
- 子Agent: 父Agent可以委派的专业Agent
- Sessions: 在多次交互中保持上下文
代码示例
import { tool, createSdkMcpServer, query } from "@anthropic-ai/claude-agent-sdk"; import { z } from "zod"; const searchDocs = tool( "search_docs", "Search the internal documentation for relevant information", { query: z.string().describe("Search query") }, async ({ query }) => { const results = await vectorStore.similaritySearch(query, 5); return { content: [{ type: "text", text: results.map(r => r.pageContent).join("\n\n") }], }; } ); const docsServer = createSdkMcpServer({ name: "docs", version: "1.0.0", tools: [searchDocs], }); for await (const message of query({ prompt: "Find how authentication works in our system and write a summary", options: { mcpServers: { docs: docsServer }, allowedTools: ["Read", "Glob", "Grep", "mcp__docs__search_docs"], }, })) { if (message.type === "result" && message.subtype === "success") { console.log(message.result); } }
优势
- 一流的MCP集成 - 连接任何MCP服务器生态系统
- 文件操作、终端和网络访问的内置工具
- 大型代码库的自动上下文压缩
- 复杂任务的子Agent并行处理
- 与Claude Code相同的引擎 - 在真实开发工作流中经过实战检验
劣势
- 仅限Claude模型 - 不支持多提供商
- 较新的框架,社区较小
- 即使是Python SDK也需要Node.js运行时
- 与LangGraph相比,工作流控制不那么显式
定价
开源。标准Claude API token费率。Managed Agents (托管版): 除token费用外,每会话小时$0.08。
何时选择哪个
选择LangGraph如果:
- 你需要精确控制工作流的每一步
- 你的用例涉及复杂的条件逻辑和循环
- 你需要内置的持久化和human-in-the-loop检查点
- 你需要在同一工作流中使用多个LLM提供商
选择CrewAI如果:
- 你想要直观的基于角色的抽象
- 你的任务涉及多个具有不同专长的Agent
- 你需要Agent之间协作并传递上下文
- 你重视最大的社区和最多的内置集成
选择OpenAI Agents SDK如果:
- 你的主要模式是将对话路由给专家
- 你需要并行验证输入/输出的护栏
- 你想要最简单的抽象和最少的模板代码
- 内置的追踪和可观测性很重要
选择Claude Agent SDK如果:
- 你的Agent需要读写和执行代码
- 你想要一流的MCP服务器集成
- 你需要能够迭代和自我修正的自主Agent
- 你已经在使用Claude并想要最深度的集成
可以组合使用框架吗?
可以。一个常见的模式是使用一个框架进行编排,另一个用于单独的Agent:
- LangGraph 用于整体工作流图
- CrewAI 用于需要多Agent协作的特定节点
- Claude Agent SDK 用于通过MCP处理编码相关的子任务
- OpenAI Agents SDK 用于面向客户的分流和路由
这些框架并不是互相排斥的。选择适合系统各个部分的框架即可。
结论
每个框架都有明确的方向:
- LangGraph 优化控制 - 你决定每一次转换
- CrewAI 优化协作 - Agent作为团队工作
- OpenAI Agents SDK 优化简洁 - 最少抽象,干净的交接
- Claude Agent SDK 优化自主性 - 给它工具,让它工作
正确的选择取决于你的工作流、团队和现有技术栈。选择与你主要用例匹配的框架,深入学习它,当遇到其他框架擅长的场景时再引入它们。