Implementation:CrewAIInc CrewAI Task Constructor For Hierarchical
Overview
Concrete Task class used in hierarchical crews where agent assignment is optional, enabling manager-driven delegation provided by the CrewAI framework.
Source
Import
from crewai import Task
Signature
The Task class is the same class used in all CrewAI workflows. In hierarchical mode, the key difference is that the agent parameter is intentionally left as None, allowing the manager to assign agents dynamically at runtime.
| Parameter | Type | Default | Description |
|---|---|---|---|
| description | str | required | A detailed description of what the task requires. The manager uses this to decide which specialist to delegate to. |
| expected_output | str | required | A clear description of what the task's output should look like. Helps the manager evaluate specialist responses. |
| agent | Agent or None | None | Intentionally left as None in hierarchical mode. The manager assigns the appropriate specialist at runtime. |
| context | list[Task] or None | None | Other tasks whose outputs provide context for this task. Establishes data flow between tasks even when the manager controls execution order. |
| tools | list[BaseTool] | [] | Task-specific tools that override the assigned agent's tools for this task only. |
| output_file | str or None | None | Optional file path to write the task output to. |
| output_json | type[BaseModel] or None | None | Optional Pydantic model class to structure the output as JSON. |
| output_pydantic | type[BaseModel] or None | None | Optional Pydantic model class to structure the output as a Pydantic object. |
| callback | Callable or None | None | Optional function called with the task output after completion. |
| human_input | bool | False | Whether to request human review/input before finalizing the task output. |
| async_execution | bool | False | Whether this task can execute asynchronously. |
Key Behaviors
- No agent pre-assignment -- In hierarchical mode, the
agentfield isNone. The manager agent receives the task and uses its delegation tools to route it to the appropriate specialist. This is the fundamental difference from sequential mode, where each task must have an assigned agent. - Manager-driven routing -- The manager reads the task's
descriptionandexpected_outputto determine which specialist's role best matches the task requirements. Clear, detailed descriptions lead to better routing decisions. - Context chaining -- The
contextparameter allows tasks to reference the outputs of earlier tasks, creating data dependencies. In hierarchical mode, the manager respects these dependencies and ensures context tasks are completed before dependent tasks are delegated. - Task-level tools -- Even without a pre-assigned agent, tasks can carry their own tools. When the manager delegates the task to a specialist, these task-level tools become available to that specialist for the duration of the task.
Example
from crewai import Task
# Task 1: Research task - no agent assigned
research_task = Task(
description=(
"Conduct thorough research on the current state of quantum computing "
"in 2024. Focus on recent breakthroughs, major players in the industry, "
"and practical applications that have emerged. Include specific examples "
"and cite your sources."
),
expected_output=(
"A comprehensive research report covering: (1) recent breakthroughs in "
"quantum computing, (2) key companies and research institutions, "
"(3) practical applications, and (4) future outlook. Include source citations."
),
# agent is intentionally NOT set - manager will delegate
)
# Task 2: Writing task - depends on research, no agent assigned
writing_task = Task(
description=(
"Write an engaging and informative blog post about quantum computing "
"based on the research findings. The post should be accessible to a "
"general technical audience and highlight the most impactful developments."
),
expected_output=(
"A well-structured 1500-word blog post with an engaging title, introduction, "
"main sections covering key developments, and a forward-looking conclusion."
),
context=[research_task], # Depends on the research task's output
# agent is intentionally NOT set - manager will delegate
)
# Task 3: Review task - depends on writing, no agent assigned
review_task = Task(
description=(
"Review the blog post for factual accuracy, clarity, grammar, and "
"overall quality. Verify that claims are supported by the research. "
"Provide the final polished version."
),
expected_output=(
"The final reviewed and polished blog post, with any corrections applied "
"and a brief summary of changes made during review."
),
context=[writing_task], # Depends on the writing task's output
# agent is intentionally NOT set - manager will delegate
)
In this example, three tasks are defined without any agent assignment. When used in a hierarchical crew, the manager agent will:
- Receive the research task, identify the research specialist by role, and delegate using DelegateWorkTool.
- Receive the writing task (with research output as context), identify the writing specialist, and delegate.
- Receive the review task (with writing output as context), identify the review specialist, and delegate.
The manager may also deviate from this expected flow -- for instance, asking the researcher clarifying questions before delegating the full research task, or re-delegating the writing task if the first output is unsatisfactory.
Notes
- While it is possible to set the
agentfield on tasks in hierarchical mode, doing so partially defeats the purpose of hierarchical execution. If you need deterministic agent assignment for some tasks, consider a mixed approach or use sequential mode for those tasks. - The
contextfield is especially important in hierarchical mode because it explicitly signals data dependencies to the manager. Without context links, the manager may not realize that one task depends on another's output. - The quality of the
descriptionfield directly impacts the manager's ability to route tasks effectively. Vague descriptions like "Do the work" will lead to poor delegation decisions.