design-task
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCrewAI Task Design Guide
CrewAI任务设计指南
How to write effective tasks that produce reliable, high-quality output from your agents.
如何编写能让Agent生成可靠、高质量输出的有效任务。
The 80/20 Rule
80/20原则
Spend 80% of your effort on task design, 20% on agent design. The task is the most important lever you have. A well-designed task with a mediocre agent will outperform a poorly designed task with an excellent agent.
**将80%的精力投入到任务设计中,20%投入到Agent设计中。**任务是你手中最重要的抓手。一个设计精良的任务搭配普通的Agent,表现会优于设计糟糕的任务搭配优秀的Agent。
1. Anatomy of an Effective Task
1. 有效任务的核心构成
Every task needs two things: a description (what to do and how) and an expected_output (what the result looks like).
每个任务都需要两个要素:描述(description)(做什么以及怎么做)和预期输出(expected_output)(结果的呈现形式)。
Description — The Instructions
描述——执行指令
A good description includes:
- What to do — the core action
- How to do it — specific steps or approach
- Context — why this matters, what it feeds into
- Constraints — scope limits, things to avoid
- Inputs — what data or context is available
yaml
research_task:
description: >
Conduct thorough research about {topic} for the year {current_year}.
Your research should:
1. Identify the top 5 key trends and breakthroughs
2. For each trend, find at least 2 credible sources
3. Note any controversies or competing viewpoints
4. Assess potential industry impact (high/medium/low)
Focus on developments from the last 6 months.
Do NOT include speculation or unverified claims.
The output will feed into a report for {target_audience}.
expected_output: >
A structured research brief with 5 sections, one per trend.
Each section includes: trend name, 2-3 paragraph summary,
source citations, impact assessment (high/medium/low),
and a confidence level for your findings.
agent: researcher一份优质的描述应包含:
- 内容——核心动作
- 方式——具体步骤或方法
- 上下文——任务的重要性、产出的用途
- 约束条件——范围限制、需要规避的内容
- 输入——可用的数据或上下文信息
yaml
research_task:
description: >
Conduct thorough research about {topic} for the year {current_year}.
Your research should:
1. Identify the top 5 key trends and breakthroughs
2. For each trend, find at least 2 credible sources
3. Note any controversies or competing viewpoints
4. Assess potential industry impact (high/medium/low)
Focus on developments from the last 6 months.
Do NOT include speculation or unverified claims.
The output will feed into a report for {target_audience}.
expected_output: >
A structured research brief with 5 sections, one per trend.
Each section includes: trend name, 2-3 paragraph summary,
source citations, impact assessment (high/medium/low),
and a confidence level for your findings.
agent: researcherExpected Output — The Success Criteria
预期输出——成功标准
The tells the agent what "done" looks like. Be specific about:
expected_output- Format — bullet points, paragraphs, JSON, table
- Structure — sections, headings, order
- Length — approximate word count or number of items
- Quality markers — citations required, confidence levels, specific fields
| Bad Expected Output | Good Expected Output |
|---|---|
| |
| |
| |
expected_output- 格式——项目符号、段落、JSON、表格
- 结构——章节、标题、顺序
- 篇幅——大致字数或条目数量
- 质量指标——是否需要引用、置信度、特定字段
| 糟糕的预期输出 | 优质的预期输出 |
|---|---|
| |
| |
| |
2. The Single Purpose Principle
2. 单一目标原则
One task = one objective. Never combine multiple operations into a single task.
一个任务=一个明确目标。切勿在单个任务中合并多项操作。
Bad: "God Task"
反面示例:“全能任务”
yaml
undefinedyaml
undefinedDON'T do this — too many objectives in one task
请勿这样做——单个任务包含过多目标
research_and_write_task:
description: >
Research {topic}, analyze the findings, write a blog post,
and proofread it for grammar errors.
expected_output: >
A polished blog post about {topic}.
undefinedresearch_and_write_task:
description: >
Research {topic}, analyze the findings, write a blog post,
and proofread it for grammar errors.
expected_output: >
A polished blog post about {topic}.
undefinedGood: Focused Tasks
正面示例:聚焦型任务
yaml
research_task:
description: >
Research {topic} and identify the top 5 key developments.
expected_output: >
A research brief with 5 sections covering key trends.
agent: researcher
writing_task:
description: >
Using the research findings, write a technical blog post about {topic}.
expected_output: >
A 1000-1500 word blog post with introduction, main sections,
and conclusion. Include code examples where relevant.
agent: writer
editing_task:
description: >
Review and edit the blog post for grammar, clarity, and consistency.
expected_output: >
The final edited blog post with all corrections applied.
Include a brief editor's note listing what was changed.
agent: editorEach task has one clear objective. The sequential flow passes context automatically.
yaml
research_task:
description: >
Research {topic} and identify the top 5 key developments.
expected_output: >
A research brief with 5 sections covering key trends.
agent: researcher
writing_task:
description: >
Using the research findings, write a technical blog post about {topic}.
expected_output: >
A 1000-1500 word blog post with introduction, main sections,
and conclusion. Include code examples where relevant.
agent: writer
editing_task:
description: >
Review and edit the blog post for grammar, clarity, and consistency.
expected_output: >
The final edited blog post with all corrections applied.
Include a brief editor's note listing what was changed.
agent: editor每个任务都有一个清晰的目标。任务的顺序流转会自动传递上下文信息。
3. Task Configuration Reference
3. 任务配置参考
Essential Parameters
核心参数
python
Task(
description="...", # Required: what to do
expected_output="...", # Required: what the result looks like
agent=researcher, # Optional for hierarchical process; required for sequential
)python
Task(
description="...", # 必填:任务内容
expected_output="...", # 必填:结果呈现形式
agent=researcher, # 分层流程可选;顺序流程必填
)Task Dependencies with context
context基于context
的任务依赖
contextpython
analysis_task = Task(
description="Analyze the research findings...",
expected_output="...",
agent=analyst,
context=[research_task], # Receives research_task's output as context
)In sequential process: Each task auto-receives all prior task outputs. Use only when you need non-linear dependencies.
contextIn hierarchical process: is how you create explicit data flow between tasks.
contextpython
analysis_task = Task(
description="Analyze the research findings...",
expected_output="...",
agent=analyst,
context=[research_task], # 接收research_task的输出作为上下文
)**在顺序流程中:**每个任务会自动接收所有前置任务的输出。仅当需要非线性依赖时才使用。
context在分层流程中:是创建任务间明确数据流的方式。
contextStructured Output
结构化输出
Use or when downstream code needs to parse the result:
output_pydanticoutput_jsonpython
from pydantic import BaseModel
class ResearchReport(BaseModel):
trends: list[str]
confidence: float
sources: list[str]
research_task = Task(
description="...",
expected_output="A structured report with trends, confidence score, and sources.",
agent=researcher,
output_pydantic=ResearchReport, # Agent's output is parsed into this model
)Important: is always a string description — never a class name. The Pydantic model goes in , and the text tells the agent what fields to include.
expected_outputoutput_pydanticexpected_outputAccess structured output:
python
result = crew.kickoff(inputs={...})
last_task_output = result.pydantic # Pydantic model from the last task
all_outputs = result.tasks_output # List of all TaskOutput objects
first_task = all_outputs[0].pydantic # Pydantic from a specific task当下游代码需要解析任务结果时,使用或:
output_pydanticoutput_jsonpython
from pydantic import BaseModel
class ResearchReport(BaseModel):
trends: list[str]
confidence: float
sources: list[str]
research_task = Task(
description="...",
expected_output="A structured report with trends, confidence score, and sources.",
agent=researcher,
output_pydantic=ResearchReport, # Agent的输出会被解析为该模型
)重要提示:始终是字符串描述——绝不能是类名。Pydantic模型应配置在中,文本需告知Agent需要包含哪些字段。
expected_outputoutput_pydanticexpected_output访问结构化输出:
python
result = crew.kickoff(inputs={...})
last_task_output = result.pydantic # 最后一个任务的Pydantic模型
all_outputs = result.tasks_output # 所有TaskOutput对象的列表
first_task = all_outputs[0].pydantic # 特定任务的Pydantic模型File Output
文件输出
python
Task(
...,
output_file="output/report.md", # Save output to file
create_directory=True, # Create directory if missing (default: True)
)File output and structured output can be combined — the file gets the raw text, and gets the parsed model.
output_pydanticpython
Task(
...,
output_file="output/report.md", # 将输出保存到文件
create_directory=True, # 若目录不存在则创建(默认:True)
)文件输出与结构化输出可结合使用——文件保存原始文本,保存解析后的模型。
output_pydanticAsync Execution
异步执行
python
Task(
...,
async_execution=True, # Run without blocking the next task
)Use for tasks that can run in parallel. The crew continues to the next task while this one executes. Use on downstream tasks to wait for async results.
contextpython
Task(
...,
async_execution=True, # 非阻塞执行,不等待当前任务完成即可启动下一个任务
)适用于可并行执行的任务。Crew会在当前任务执行时继续运行下一个任务。若下游任务需要等待异步任务结果,可使用。
contextHuman Review
人工审核
python
Task(
...,
human_input=True, # Pause for human review before finalizing
)When enabled, the agent presents its result and waits for human feedback before marking the task complete. Use for critical outputs that need human approval.
python
Task(
...,
human_input=True, # 在任务完成前暂停,等待人工审核
)启用后,Agent会提交结果并等待人工反馈,之后才会标记任务完成。适用于需要人工确认的关键输出。
Markdown Formatting
Markdown格式化
python
Task(
...,
markdown=True, # Add markdown formatting instructions
)Automatically instructs the agent to format output with proper markdown headers, lists, emphasis, and code blocks.
python
Task(
...,
markdown=True, # 添加Markdown格式化指令
)自动指示Agent使用标准的Markdown标题、列表、强调和代码块来格式化输出。
Callbacks
回调函数
python
def log_completion(output):
print(f"Task completed: {output.description[:50]}...")
save_to_database(output.raw)
Task(
...,
callback=log_completion, # Called after task completion
)python
def log_completion(output):
print(f"Task completed: {output.description[:50]}...")
save_to_database(output.raw)
Task(
...,
callback=log_completion, # 任务完成后调用
)4. Task Guardrails — Quality Control
4. 任务防护机制——质量控制
Guardrails validate task output before it passes to the next step. If validation fails, the agent retries.
防护机制会在任务输出传递到下一个步骤前进行验证。若验证失败,Agent会重试任务。
Function-Based Guardrails
基于函数的防护机制
python
def validate_word_count(output) -> tuple[bool, Any]:
"""Ensure output is between 500-2000 words."""
word_count = len(output.raw.split())
if word_count < 500:
return (False, f"Output too short ({word_count} words). Expand to at least 500 words.")
if word_count > 2000:
return (False, f"Output too long ({word_count} words). Condense to under 2000 words.")
return (True, output)
Task(
...,
guardrail=validate_word_count,
guardrail_max_retries=3, # Max retry attempts (default: 3)
)Return format: — first element is pass/fail, second is the result (on success) or error message (on failure).
(bool, Any)python
def validate_word_count(output) -> tuple[bool, Any]:
"""确保输出字数在500-2000之间。"""
word_count = len(output.raw.split())
if word_count < 500:
return (False, f"Output too short ({word_count} words). Expand to at least 500 words.")
if word_count > 2000:
return (False, f"Output too long ({word_count} words). Condense to under 2000 words.")
return (True, output)
Task(
...,
guardrail=validate_word_count,
guardrail_max_retries=3, # 最大重试次数(默认:3)
)返回格式:——第一个元素表示验证是否通过,第二个元素是验证通过后的结果(或验证失败时的错误信息)。
(bool, Any)LLM-Based Guardrails
基于大语言模型(LLM)的防护机制
python
Task(
...,
guardrail="Verify the output contains at least 3 source citations and no speculative claims.",
)String guardrails use the agent's LLM to evaluate the output. Good for subjective quality checks.
python
Task(
...,
guardrail="Verify the output contains at least 3 source citations and no speculative claims.",
)字符串形式的防护机制会使用Agent的LLM来评估输出,适用于主观质量检查。
Chaining Multiple Guardrails
多防护机制链式调用
python
Task(
...,
guardrails=[
validate_word_count, # Function: check length
validate_no_pii, # Function: check for PII
"Ensure the tone is professional and appropriate for a business audience.", # LLM check
],
guardrail_max_retries=3,
)Guardrails execute sequentially. Each receives the output of the previous guardrail. Mix function-based (deterministic) and LLM-based (subjective) checks.
python
Task(
...,
guardrails=[
validate_word_count, # 函数:检查字数
validate_no_pii, # 函数:检查是否包含个人可识别信息
"Ensure the tone is professional and appropriate for a business audience.", # LLM检查
],
guardrail_max_retries=3,
)防护机制会按顺序执行,每个防护机制接收上一个防护机制处理后的输出。可结合基于函数的(确定性)和基于LLM的(主观性)检查。
5. YAML Configuration (Recommended)
5. YAML配置(推荐方式)
tasks.yaml
tasks.yaml
yaml
research_task:
description: >
Conduct thorough research about {topic} for {current_year}.
Identify key trends, breakthrough technologies,
and potential industry impacts.
Focus on the last 6 months of developments.
expected_output: >
A structured research brief with 5 sections.
Each section: trend name, 2-3 paragraph summary,
source citations, and impact assessment.
agent: researcher
analysis_task:
description: >
Analyze the research findings and create actionable recommendations
for {target_audience}.
expected_output: >
A prioritized list of 5 recommendations with:
rationale, estimated effort, and expected impact.
agent: analyst
context:
- research_task
report_task:
description: >
Compile a final report combining research and analysis for {target_audience}.
expected_output: >
A polished markdown report with executive summary,
detailed findings, recommendations, and appendices.
agent: writer
output_file: output/report.mdyaml
research_task:
description: >
Conduct thorough research about {topic} for {current_year}.
Identify key trends, breakthrough technologies,
and potential industry impacts.
Focus on the last 6 months of developments.
expected_output: >
A structured research brief with 5 sections.
Each section: trend name, 2-3 paragraph summary,
source citations, and impact assessment.
agent: researcher
analysis_task:
description: >
Analyze the research findings and create actionable recommendations
for {target_audience}.
expected_output: >
A prioritized list of 5 recommendations with:
rationale, estimated effort, and expected impact.
agent: analyst
context:
- research_task
report_task:
description: >
Compile a final report combining research and analysis for {target_audience}.
expected_output: >
A polished markdown report with executive summary,
detailed findings, recommendations, and appendices.
agent: writer
output_file: output/report.mdWiring in crew.py
在crew.py中关联配置
python
@CrewBase
class ResearchCrew:
agents_config = "config/agents.yaml"
tasks_config = "config/tasks.yaml"
@task
def research_task(self) -> Task:
return Task(config=self.tasks_config["research_task"])
@task
def analysis_task(self) -> Task:
return Task(
config=self.tasks_config["analysis_task"],
context=[self.research_task()],
)
@task
def report_task(self) -> Task:
return Task(
config=self.tasks_config["report_task"],
output_file="output/report.md",
)Critical: The method name () must match the YAML key ().
def research_taskresearch_task:python
@CrewBase
class ResearchCrew:
agents_config = "config/agents.yaml"
tasks_config = "config/tasks.yaml"
@task
def research_task(self) -> Task:
return Task(config=self.tasks_config["research_task"])
@task
def analysis_task(self) -> Task:
return Task(
config=self.tasks_config["analysis_task"],
context=[self.research_task()],
)
@task
def report_task(self) -> Task:
return Task(
config=self.tasks_config["report_task"],
output_file="output/report.md",
)**关键提示:**方法名称()必须与YAML中的键名()完全匹配。
def research_taskresearch_task:6. Task Dependencies and Context Flow
6. 任务依赖与上下文流转
Sequential Process (Default)
顺序流程(默认)
In , tasks run in order. Each task automatically receives all prior task outputs as context.
Process.sequentialresearch_task → analysis_task → report_task
↓ ↓ ↓
output 1 output 1 + 2 output 1 + 2 + 3You don't need in sequential — it's implicit. Use it only to create non-linear dependencies:
context=python
undefined在模式下,任务按顺序执行。每个任务会自动接收所有前置任务的输出作为上下文。
Process.sequentialresearch_task → analysis_task → report_task
↓ ↓ ↓
输出1 输出1+2 输出1+2+3在顺序流程中无需使用——这是隐式的。仅当需要创建非线性依赖时才使用:
context=python
undefinedTask C depends on A but NOT B
任务C依赖任务A,但不依赖任务B
task_c = Task(
...,
context=[task_a], # Only receives task_a output, not task_b
)
undefinedtask_c = Task(
...,
context=[task_a], # 仅接收任务A的输出,不接收任务B的
)
undefinedExplicit Dependencies
显式依赖
python
undefinedpython
undefinedDiamond dependency pattern
菱形依赖模式
task_a = Task(...) # Entry point
task_b = Task(..., context=[task_a]) # Depends on A
task_c = Task(..., context=[task_a]) # Also depends on A
task_d = Task(..., context=[task_b, task_c]) # Depends on both B and C
undefinedtask_a = Task(...) # 入口任务
task_b = Task(..., context=[task_a]) # 依赖任务A
task_c = Task(..., context=[task_a]) # 同样依赖任务A
task_d = Task(..., context=[task_b, task_c]) # 依赖任务B和任务C
undefinedConditional Tasks
条件任务
python
from crewai.task import ConditionalTask
def needs_more_data(output) -> bool:
return len(output.pydantic.items) < 10
extra_research = ConditionalTask(
description="Fetch additional data sources...",
expected_output="...",
agent=researcher,
condition=needs_more_data, # Only runs if previous output has < 10 items
)python
from crewai.task import ConditionalTask
def needs_more_data(output) -> bool:
return len(output.pydantic.items) < 10
extra_research = ConditionalTask(
description="Fetch additional data sources...",
expected_output="...",
agent=researcher,
condition=needs_more_data, # 仅当前置任务输出的条目数<10时才运行
)7. Task Tools
7. 任务工具
Tasks can have their own tools that override the agent's default tools for that specific task:
python
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
Task(
description="Search for and scrape the top 5 articles about {topic}...",
expected_output="...",
agent=researcher,
tools=[SerperDevTool(), ScrapeWebsiteTool()], # Task-specific tools
)When to use task-level tools:
- The task needs tools the agent doesn't normally have
- You want to restrict an agent to specific tools for this task
- Different tasks by the same agent need different tool sets
任务可拥有独立的工具,覆盖Agent的默认工具,仅适用于该任务:
python
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
Task(
description="Search for and scrape the top 5 articles about {topic}...",
expected_output="...",
agent=researcher,
tools=[SerperDevTool(), ScrapeWebsiteTool()], # 任务专属工具
)何时使用任务级工具:
- 任务需要Agent默认未配置的工具
- 希望限制Agent在该任务中仅使用特定工具
- 同一Agent执行的不同任务需要不同的工具集
8. Variable Interpolation
8. 变量插值
Use placeholders in YAML for reusable tasks:
{variable}yaml
research_task:
description: >
Research {topic} trends for {current_year},
targeting {target_audience}.
expected_output: >
A report on {topic} suitable for {target_audience}.Variables are replaced when you call :
crew.kickoff(inputs={...})python
crew.kickoff(inputs={
"topic": "AI Agents",
"current_year": "2025",
"target_audience": "developers",
})Common mistakes:
- Missing variable in → literal
inputsappears in the prompt{variable} - Using Jinja2 syntax → crewAI uses single braces
{{ }}{ } - Unused variables in → silently ignored (no error)
inputs
在YAML中使用占位符实现任务复用:
{variable}yaml
research_task:
description: >
Research {topic} trends for {current_year},
targeting {target_audience}.
expected_output: >
A report on {topic} suitable for {target_audience}.调用时会替换变量:
crew.kickoff(inputs={...})python
crew.kickoff(inputs={
"topic": "AI Agents",
"current_year": "2025",
"target_audience": "developers",
})常见错误:
- 中缺少对应变量→输出中会保留
inputs字面量{variable} - 使用Jinja2语法→CrewAI仅支持单大括号
{{ }}{ } - 中存在未使用的变量→会被静默忽略(无错误提示)
inputs
9. Common Task Design Mistakes
9. 任务设计常见错误
| Mistake | Impact | Fix |
|---|---|---|
| Vague description ("Research the topic") | Agent produces shallow, unfocused output | Add specific steps, constraints, and context |
| Vague expected_output ("A report") | Agent guesses at format and structure | Specify format, sections, length, quality markers |
| Multiple objectives in one task | Agent does all of them poorly | Split into focused single-purpose tasks |
| No context between dependent tasks | Agent lacks information from prior steps | Use |
| Agent sees a class name string, not field names | Keep |
| Missing tools for data tasks | Agent fabricates data instead of fetching it | Add tools to the task or agent |
| No guardrails on critical output | Bad output flows downstream unchecked | Add function or LLM guardrails |
| Overly strict expected_output | Agent loops trying to match impossible criteria | Be specific but achievable; lower |
| Description duplicates backstory | Wasted tokens and confused agent | Description = what to do; backstory = who you are |
| 错误 | 影响 | 修复方案 |
|---|---|---|
| 描述模糊(如“研究该主题”) | Agent产出的内容浅显、缺乏聚焦 | 添加具体步骤、约束条件和上下文 |
| 预期输出模糊(如“一份报告”) | Agent需要猜测格式和结构 | 明确指定格式、章节、篇幅和质量指标 |
| 单个任务包含多个目标 | Agent所有目标都完成得很差 | 拆分为多个聚焦单一目标的任务 |
| 依赖任务间无上下文传递 | Agent缺少前置步骤的关键信息 | 使用 |
| Agent看到的是类名字符串,而非字段名 | |
| 数据类任务缺少工具 | Agent会编造数据而非获取真实数据 | 为任务或Agent添加对应工具 |
| 关键输出未设置防护机制 | 错误输出会直接流入下游任务 | 添加函数或LLM防护机制 |
| 预期输出过于严苛 | Agent会陷入循环尝试匹配不可能的标准 | 明确但设置可实现的要求;降低 |
| 描述重复背景信息 | 浪费Tokens且混淆Agent | 描述=任务内容;背景=Agent的角色设定 |
10. Task Design Checklist
10. 任务设计检查清单
Before running a task, verify:
- Description includes what, how, context, and constraints
- Expected output specifies format, structure, and quality markers
- Single purpose — one clear objective per task
- Agent assigned (or task is in a hierarchical crew)
- Dependencies set via where needed
context - Tools provided for any task requiring external data
- Structured output configured if downstream code parses the result
- Guardrails set for critical outputs
- Variables in YAML match the dict keys
inputs - Expected output is achievable — test with a simple run before adding complexity
在运行任务前,验证以下内容:
- 描述包含任务内容、执行方式、上下文和约束条件
- 预期输出指定了格式、结构和质量指标
- 单一目标——每个任务仅有一个清晰的目标
- 已分配Agent(或任务属于分层Crew)
- 依赖关系已通过正确设置(若需要)
context - 已提供工具给需要外部数据的任务
- 已配置结构化输出(若下游代码需要解析结果)
- 关键输出已设置防护机制
- YAML中的变量与字典的键名匹配
inputs - 预期输出可实现——在添加复杂度前先通过简单运行测试
References
参考资料
For deeper dives into specific topics, see:
- Structured Output — ,
output_pydantic, andoutput_jsonpatterns across LLM, Agent, Task, and Crew levelsresponse_format
For related skills:
- getting-started — project scaffolding, choosing the right abstraction, Flow architecture
- design-agent — agent Role-Goal-Backstory framework, parameter tuning, tool assignment, memory & knowledge configuration
如需深入了解特定主题,请查看:
- 结构化输出——在LLM、Agent、Task和Crew层面的、
output_pydantic和output_json模式response_format
相关技能:
- getting-started——项目脚手架搭建、选择合适的抽象层、Flow架构
- design-agent——Agent的Role-Goal-Backstory框架、参数调优、工具分配、记忆与知识配置