Tasks

A Task is a concrete assignment for an Agent: what to do and what “done” looks like. The Crew’s Process decides whether tasks run in list order or are assigned by a manager agent.


Required and common fields

FieldMeaning
descriptionWhat to do ({placeholder} filled by crew.kickoff(inputs=...))
expected_outputWhat completion looks like — more specific than the description
agentWho runs it (set this in sequential crews; hierarchical crews may assign later)
contextOther Task objects whose outputs become context
output_fileWrite the result to a path; create_directory defaults to True
markdownAsk for Markdown
human_inputHuman review of the final answer
async_executionRun asynchronously (default False)
toolsTools allowed for this task (limit / override the agent’s set)
output_pydantic / output_jsonStructure output with a Pydantic model
guardrail / guardrailsValidate output; retries via guardrail_max_retries (default 3)

max_retries is deprecated; use guardrail_max_retries.

from crewai import Task

research = Task(
    description="Conduct thorough research about {topic}. Prefer primary sources.",
    expected_output="Bullet list of the most relevant facts about {topic}.",
    agent=researcher,
)

report = Task(
    description="Expand the research into a short briefing for engineers.",
    expected_output="Markdown with sections: summary, findings, risks. No wrapping code fence.",
    agent=writer,
    context=[research],
    markdown=True,
    output_file="output/report.md",
)

In sequential crews, later tasks usually see earlier outputs. Use context to make that dependency explicit.


Tasks in JSONC

The tasks array in crew.jsonc is execution order when process is "sequential":

{
  "name": "Research Crew",
  "agents": ["researcher", "reporting_analyst"],
  "tasks": [
    {
      "name": "research_task",
      "description": "Conduct thorough research about {topic}.",
      "expected_output": "A list of the most relevant information about {topic}.",
      "agent": "researcher"
    },
    {
      "name": "reporting_task",
      "description": "Review the research and expand it into a detailed report.",
      "expected_output": "A polished markdown report without fenced code blocks.",
      "agent": "reporting_analyst",
      "context": ["research_task"],
      "markdown": true,
      "output_file": "report.md"
    }
  ],
  "inputs": { "topic": "AI Agents" }
}

context may only name already defined tasks — no forward references. Conditional tasks use "type": "ConditionalTask" plus condition (see official Tasks docs).


TaskOutput

After a run, read task.output. Common attributes: raw (default), pydantic, json_dict (only if you configured those output models), agent, messages. CrewOutput.tasks_output lists every task.

A sharp expected_output beats a longer description — the model treats it as the acceptance test. For fixed JSON, use output_pydantic; do not only say “please output JSON” in the prompt.


How execution relates to Process

from crewai import Crew, Process

crew = Crew(
    agents=[researcher, writer],
    tasks=[research, report],
    process=Process.sequential,  # or Process.hierarchical
)
  • Sequential: list order; prior output becomes later context.
  • Hierarchical: a manager assigns work; the Crew needs manager_llm or manager_agent. See Crews and Processes.

Human-in-the-loop: task-level human_input=True, or Flow-level @human_feedback from the official docs (CrewAI ≥ 1.8.0). Do not invent decorator names that are not in the docs.


Next steps

评论