CrewAI 学习空间 · 第 3 课 · 约 12 分钟 · 概念为主;附官方短片段体感(非作业) · 一手资料:官方 Collaboration(辅以 Processes、Hierarchical Process、Agents;2026-10-12 抓取,docs v1.15.27) · ← 第 2 课 · 第 4 课 →

sequential 还是 hierarchical?allow_delegation 开给谁?

Process decides who assigns the work; allow_delegation decides who may hand it off — and limits decide when it stops.

本课唯一目标:读完后,你能讲清楚三件事——

  1. sequential 与 hierarchical 的区别:谁决定哪个 Agent 做哪个 Task;
  2. allow_delegation=True 到底给了 Agent 什么,什么时候该开;
  3. 为什么会出现“委派循环”(delegation loop),以及靠什么让它收尾:经理 True / 专家 False、清晰的完成标准、max_iter。

第 1 课在表格里提过两种 Process;第 2 课的 Quickstart 只有一个 Task、process: "sequential"。本课把“排班”和“转派”这两根旋钮拆开讲。

1. 两根不同的旋钮

先把两个经常被混在一起的设置分开:

旋钮挂在哪回答的问题默认值
Process(process)CrewTask 按什么策略分配、推进?sequential(Crew Attributes)
Allow Delegation(allow_delegation)Agent这个 Agent 能不能把活转给 / 去问同事?False(Agent Attributes)

Processes 把 Process 比作人类团队里的项目管理(project management):它决定任务如何被分发、执行。 而 Collaboration 说的是另一件事:Agent 之间通过委派任务和提问互相借用专长。

2. Process:sequential vs hierarchical

sequential 与 hierarchical 对照图 左:sequential,三个 Task 按列表顺序执行,每个 Task 预先绑定一个 Agent,上一个输出作为下一个的上下文。右:hierarchical,一个 manager Agent 在顶部,向 researcher 和 writer 分派任务,并审核它们的输出。 sequential(默认) 谁做什么:写在 Task 的 agent 字段里 Task 1 · research agent: researcher 输出 → 上下文 Task 2 · write agent: writer 输出 → 上下文 Task 3 · edit agent: editor 顺序 = tasks 列表顺序 hierarchical 谁做什么:manager 运行时决定 manager manager_llm | manager_agent 规划 · 分派 · 审核 researcher 专家 writer 专家 Task 不预先分配;经理判断是否完成 必须提供 manager_llm 或 manager_agent
图 1 左:Sequential Process——按列表顺序,上一个输出作为下一个的上下文。右:Hierarchical Process——经理负责 planning、delegation、validation。
sequentialhierarchical
谁分配 Task你:在每个 Task 上写 agentmanager:按 Agent 能力分派,Task 不预先分配
推进顺序tasks 列表顺序;输出自动接力,可用 context 精确指定仍按逻辑顺序推进,但由经理监督(Workflow in Action)
谁判定“做完了”Agent 产出符合 expected_output 的结果即进入下一个经理审核输出(reviews outputs)、评估是否完成(assesses task completion)
必填额外配置无manager_llm 或 manager_agent 二选一
适合步骤与分工都确定的流水线需要“先拆解、再分派、再验收”的开放型复杂任务

“适合”一行是教学归纳;官方对 hierarchical 的说法是“emulates a corporate hierarchy”,并在 Hierarchical Collaboration 中写 “For complex projects, use a hierarchical process with a manager agent”。

官方 给 Crew 指定 Process — 摘自 Processes · Assigning Processes to a Crew。
from crewai import Crew, Process

crew = Crew(
    agents=my_agents,
    tasks=my_tasks,
    process=Process.sequential
)

# hierarchical:必须提供 manager_llm 或 manager_agent
crew = Crew(
    agents=my_agents,
    tasks=my_tasks,
    process=Process.hierarchical,
    manager_llm="gpt-4o"
    # or
    # manager_agent=my_manager_agent
)

JSON-first 写法同理:在 crew.jsonc 里写 "process": "hierarchical",并给出 manager_llm 或 manager_agent;manager_agent 可以引用一个不在顶层 agents 列表里的 agents/<name>.jsonc (Crews · JSONC Configuration)。 Process 本身是枚举(Enum),只有这两个值(Process Class)。

3. allow_delegation:多出来的两个工具

Collaboration · How Agent Collaboration Works:当 allow_delegation=True,CrewAI 会自动给这个 Agent 装上两个工具:

官方 自动获得的两个协作工具 — 摘自 Delegate Work Tool / Ask Question Tool。
# Agent automatically gets this tool:
# Delegate work to coworker(task: str, context: str, coworker: str)

# Agent automatically gets this tool:
# Ask question to coworker(question: str, context: str, coworker: str)

所以“协作”在 CrewAI 里不是魔法:它就是工具调用。Agent 决定调用工具时,参数里写明交给哪个 coworker、带什么 context。没开 allow_delegation,这两个工具就不存在——官方排错第一条正是“Agents Not Collaborating → 确认已开启 delegation”(Issue: Agents Not Collaborating)。

官方 JSON-first 写法:放在 Agent 文件的 settings 里 — 摘自 Agents · JSONC Configuration(节选)。
// agents/researcher.jsonc
{
  "role": "{topic} Senior Data Researcher",
  ...
  "settings": {
    "verbose": true,
    "allow_delegation": false,
    "max_iter": 20
  }
}

4. 什么时候开?经理 True,专家 False

官方最佳实践 Strategic Delegation Enabling 只有两行原则: 给协调者和通才(coordinators and generalists)开;给专注的专家(focused specialists)关(可选)。

官方 层级协作里的典型分工 — 摘自 Hierarchical Collaboration(省略 backstory / verbose)。
manager = Agent(
    role="Project Manager",
    goal="Coordinate team efforts and ensure project success",
    allow_delegation=True,
)
researcher = Agent(
    role="Researcher",
    goal="Provide accurate research and analysis",
    allow_delegation=False,  # Specialists focus on their expertise
)
writer = Agent(
    role="Writer",
    goal="Create compelling content",
    allow_delegation=False,
)
情形建议依据
固定流水线:研究 → 写作 → 编辑,每步谁做已确定sequential + context;delegation 可全关Pattern 1 只靠 context 传递
一个 Task 需要多人专长(如写作要查资料)给牵头 Agent 开 True,被问的专家关Pattern 2:“Lead agent, but can delegate”
开放复杂任务,需要拆解、分派、验收hierarchical;经理 True,专家 FalseHierarchical Collaboration

5. 委派循环与收尾条件

5.1 循环是怎么来的

官方排错 Issue: Delegation Loops 的症状描述:“Agents delegate back and forth indefinitely”——互相踢皮球,没完没了。 原因很直白:如果 A 和 B 都有“委派工作”工具,A 可以把活交给 B,B 又可以交回 A……没有谁被迫自己动手。

委派循环与官方解法 左:A 与 B 都允许委派,箭头来回无限往返。右:经理允许委派,专家 A、B 不允许委派,只有从经理指向专家的单向箭头。 ✗ 都是 True:可能来回踢 A True B True delegate back and forth indefinitely ✓ 经理 True,专家 False Manager True Specialist A False Specialist B False
图 2 左:双方都能委派,就可能无限往返。右:官方解法——清晰层级,专家不再转派(No re-delegation)。
官方 解法:清晰的层级与职责 — 摘自 Issue: Delegation Loops。
# ✅ Solution: Clear hierarchy and responsibilities
manager = Agent(role="Manager", allow_delegation=True)
specialist1 = Agent(role="Specialist A", allow_delegation=False)  # No re-delegation
specialist2 = Agent(role="Specialist B", allow_delegation=False)

5.2 收尾条件:三道闸

“让它停下来”可以从三个层面看,从软到硬:

闸怎么起作用出处
① 结构闸:经理 True、专家 False 专家手里根本没有“委派工作”工具,只能自己产出 → 环路在结构上被切断 Delegation Loops
② 语言闸:goal / 完成标准写清楚 expected_output 是“任务完成长什么样”的描述;description 里写明受众、长度、交付物、什么情况下才去委派,Agent 就知道何时该交卷 Task Attributes · Too Much Back-and-Forth · Clear Task Descriptions
③ 硬上限:max_iter(及 max_execution_time) max_iter:达到最大迭代次数前必须给出它的最佳答案,默认 20;max_execution_time:任务执行最长秒数(默认不设) Agent Attributes · Execution Control
官方 “语言闸”的写法示例:给足上下文,并写明何时委派 — 摘自 Issue: Too Much Back-and-Forth。
Task(
    description="""Write a technical blog post about machine learning.

    Context: Target audience is software developers with basic ML knowledge.
    Length: 1200 words
    Include: code examples, practical applications, best practices

    If you need specific technical details, delegate research to the researcher.""",
    ...
)

注意这段 description 的结构:受众 + 长度 + 必含内容给出了“做完”的边界;最后一句把委派限定在“需要具体技术细节时”且只交给 researcher——这就是在用自然语言收窄委派的范围。 官方还建议把协作规矩写进 backstory(如 “Only escalate blocking issues to the Project Manager”,见 Custom Collaboration Rules)。

提醒:max_iter 是兜底,不是设计手段——到了上限会强制交出“目前最好的答案”,质量未必达标。先用 ①②,再用 ③ 防跑飞。(教学说明)

6. 讲给自己听(1 分钟)

不看上文,试着回答:sequential 下谁决定 Task 归属?hierarchical 必须多给什么?allow_delegation=True 自动加了哪两个工具? 为什么“所有人都 True”容易踢皮球?三道闸分别是什么?说得出来,就达成了本课目标。然后做小测。

7. 小测(检索练习)

点击选项即判定,每题只能作答一次;选项顺序随机。“重做”可清空重来。一手资料:Collaboration · Processes。

把 Crew 设为 hierarchical 时,官方要求必须额外提供什么?

  1. 一个 manager_llm 或者一个 manager_agent
  2. 一个 planning_llm 并且把 planning 打开
  3. 给每个 Task 都事先写好执行的 agent 名
  4. 给 crew 里的每个 Agent 都开启委派能力
“A manager language model (manager_llm) or a custom manager agent (manager_agent) must be specified”。hierarchical 下 Task 恰恰不预先分配。见 Processes · Process Implementations。

allow_delegation=True 时,CrewAI 自动给 Agent 增加了什么?

  1. 一个自动创建、负责分派与审核的经理角色
  2. 读取所有前序 Task 输出的 context 权限
  3. 委派工作与向同事提问这两个协作类工具
  4. 在 Flow 里监听其他 Crew 结束事件的能力
两个工具:Delegate work to coworker(task, context, coworker) 与 Ask question to coworker(question, context, coworker)。见 How Agent Collaboration Works。

关于 allow_delegation 与 Process 的关系,哪句符合官方文档?

  1. 只有 hierarchical 才能用,sequential 下会被忽略
  2. 与 Process 无关,sequential 示例里也照样开启
  3. 选了 hierarchical 后所有 Agent 会被自动开启
  4. 它默认就是 True,只有经理角色才需要手动关闭
allow_delegation 是 Agent 属性,默认 False;官方 Collaboration in Action 示例就是 Process.sequential + 三个开启委派的 Agent。默认值见 Agent Attributes。

症状:“Agents delegate back and forth indefinitely”。官方给的解法是?

  1. 所有 Agent 都开委派,让大家彼此多商量几轮
  2. 改成 hierarchical 并删掉 manager_llm 配置
  3. 把 max_rpm 调高,让来回往返跑得更快一些
  4. 经理开委派、专家关委派,专家不能再转派
“Clear hierarchy and responsibilities”:manager 为 allow_delegation=True,specialist 为 False # No re-delegation。见 Issue: Delegation Loops。

Agent 的 max_iter 是什么意思?

  1. 迭代上限,到点必须交出当前的最佳答案(默认 20)
  2. 每分钟最多请求次数,用来避免触发限流(默认 20)
  3. 出错后最多重试的次数,超过就中止任务(默认 20)
  4. 单个任务最长执行的秒数,超时就直接停(默认 20)
“Maximum iterations before the agent must provide its best answer. Default is 20.” 其余分别是 max_rpm、max_retry_limit(默认 2)、max_execution_time。见 Agent Attributes。

固定流水线:研究 → 写作 → 编辑,每一步由谁做都已确定。最贴合官方模式的配置是?

  1. hierarchical,让经理在每一步都重新挑人分派
  2. sequential,但给每位专家都开启委派与提问
  3. sequential,按列表接力并用 context 来传递
  4. hierarchical,并且让所有专家也都允许委派
官方 Pattern 1: Research → Write → Edit 只靠每个 Task 预先指定 agent + context=[上一个 Task];sequential 下“the output of one task serving as context for the next”(Sequential Process)。

延伸阅读与下一步

有不清楚的地方随时问我(你的老师)。