sequential 还是 hierarchical?allow_delegation 开给谁?
本课目录
Process decides who assigns the work; allow_delegation decides who may hand it off — and limits decide when it stops.
本课唯一目标:读完后,你能讲清楚三件事——
sequential与hierarchical的区别:谁决定哪个 Agent 做哪个 Task;allow_delegation=True到底给了 Agent 什么,什么时候该开;- 为什么会出现“委派循环”(delegation loop),以及靠什么让它收尾:经理 True / 专家 False、清晰的完成标准、
max_iter。
1. 两根不同的旋钮
先把两个经常被混在一起的设置分开:
| 旋钮 | 挂在哪 | 回答的问题 | 默认值 |
|---|---|---|---|
Process(process) | Crew | Task 按什么策略分配、推进? | 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 | |
|---|---|---|
| 谁分配 Task | 你:在每个 Task 上写 agent | manager:按 Agent 能力分派,Task 不预先分配 |
| 推进顺序 | tasks 列表顺序;输出自动接力,可用 context 精确指定 | 仍按逻辑顺序推进,但由经理监督(Workflow in Action) |
| 谁判定“做完了” | Agent 产出符合 expected_output 的结果即进入下一个 | 经理审核输出(reviews outputs)、评估是否完成(assesses task completion) |
| 必填额外配置 | 无 | manager_llm 或 manager_agent 二选一 |
| 适合 | 步骤与分工都确定的流水线 | 需要“先拆解、再分派、再验收”的开放型复杂任务 |
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 装上两个工具:
# 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)
- Delegate work to coworker(委派工作):把一块活交给有对应专长的同事;
- Ask question to coworker(向同事提问):只问一个具体问题,拿回信息后自己继续做。
所以“协作”在 CrewAI 里不是魔法:它就是工具调用。Agent 决定调用工具时,参数里写明交给哪个 coworker、带什么 context。没开 allow_delegation,这两个工具就不存在——官方排错第一条正是“Agents Not Collaborating → 确认已开启 delegation”(Issue: Agents Not Collaborating)。
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)关(可选)。
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,专家 False | Hierarchical Collaboration |
5. 委派循环与收尾条件
5.1 循环是怎么来的
官方排错 Issue: Delegation Loops 的症状描述:“Agents delegate back and forth indefinitely”——互相踢皮球,没完没了。 原因很直白:如果 A 和 B 都有“委派工作”工具,A 可以把活交给 B,B 又可以交回 A……没有谁被迫自己动手。
# ✅ 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 |
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)。
6. 讲给自己听(1 分钟)
不看上文,试着回答:sequential 下谁决定 Task 归属?hierarchical 必须多给什么?allow_delegation=True 自动加了哪两个工具?
为什么“所有人都 True”容易踢皮球?三道闸分别是什么?说得出来,就达成了本课目标。然后做小测。
7. 小测(检索练习)
把 Crew 设为 hierarchical 时,官方要求必须额外提供什么?
- 一个 manager_llm 或者一个 manager_agent
- 一个 planning_llm 并且把 planning 打开
- 给每个 Task 都事先写好执行的 agent 名
- 给 crew 里的每个 Agent 都开启委派能力
manager_llm) or a custom manager agent (manager_agent) must be specified”。hierarchical 下 Task 恰恰不预先分配。见 Processes · Process Implementations。allow_delegation=True 时,CrewAI 自动给 Agent 增加了什么?
- 一个自动创建、负责分派与审核的经理角色
- 读取所有前序 Task 输出的 context 权限
- 委派工作与向同事提问这两个协作类工具
- 在 Flow 里监听其他 Crew 结束事件的能力
Delegate work to coworker(task, context, coworker) 与 Ask question to coworker(question, context, coworker)。见 How Agent Collaboration Works。关于 allow_delegation 与 Process 的关系,哪句符合官方文档?
- 只有 hierarchical 才能用,sequential 下会被忽略
- 与 Process 无关,sequential 示例里也照样开启
- 选了 hierarchical 后所有 Agent 会被自动开启
- 它默认就是 True,只有经理角色才需要手动关闭
allow_delegation 是 Agent 属性,默认 False;官方 Collaboration in Action 示例就是 Process.sequential + 三个开启委派的 Agent。默认值见 Agent Attributes。症状:“Agents delegate back and forth indefinitely”。官方给的解法是?
- 所有 Agent 都开委派,让大家彼此多商量几轮
- 改成 hierarchical 并删掉 manager_llm 配置
- 把 max_rpm 调高,让来回往返跑得更快一些
- 经理开委派、专家关委派,专家不能再转派
manager 为 allow_delegation=True,specialist 为 False # No re-delegation。见 Issue: Delegation Loops。Agent 的 max_iter 是什么意思?
- 迭代上限,到点必须交出当前的最佳答案(默认 20)
- 每分钟最多请求次数,用来避免触发限流(默认 20)
- 出错后最多重试的次数,超过就中止任务(默认 20)
- 单个任务最长执行的秒数,超时就直接停(默认 20)
max_rpm、max_retry_limit(默认 2)、max_execution_time。见 Agent Attributes。固定流水线:研究 → 写作 → 编辑,每一步由谁做都已确定。最贴合官方模式的配置是?
- hierarchical,让经理在每一步都重新挑人分派
- sequential,但给每位专家都开启委派与提问
- sequential,按列表接力并用 context 来传递
- hierarchical,并且让所有专家也都允许委派
agent + context=[上一个 Task];sequential 下“the output of one task serving as context for the next”(Sequential Process)。延伸阅读与下一步
- 一手资料(本课):CrewAI Docs · Collaboration——尤其 How Agent Collaboration Works、Hierarchical Collaboration、Troubleshooting 三节。
- 辅助:Processes · Hierarchical Process · Agents · Attributes。
- 上一课:第 2 课 · 朗读 Quickstart 的数据流。
- 下一课:第 4 课 · Unified Memory:scope 树、写时选层、前缀召回。
- 速查表:术语表 / Glossary(本课新增 allow_delegation、manager_llm / manager_agent、max_iter 等条目)。
- 可选加深:Custom Manager Agent(自定义经理;本课未展开)。
有不清楚的地方随时问我(你的老师)。