The Mission: Defining Tasks
The Mission: Defining Tasks
Section titled “The Mission: Defining Tasks”An agent without a task is just potential energy waiting to be released. A Task in CrewAI is a specific assignment that an Agent must complete. Creating clear, actionable tasks is the single most important factor in getting good results.
Anatomy of a Task
Section titled “Anatomy of a Task”A Task is comprised of two primary components describing the “What” and the “How”.
1. Description
Section titled “1. Description”This is the prompt sent to the agent. It should be descriptive and clear. It can contain variable placeholders (like {topic}) that are filled in at runtime.
2. Expected Output
Section titled “2. Expected Output”This is a game-changer. By explicitly defining what the output should look like, you reduce the variance in the LLM’s response. Do you want a JSON object? A bulleted list? A Python script?
from crewai import Task
reporting_task = Task( description="Analyze the dataset and find anomalies.", expected_output="A bulleted list of top 3 anomalies with a risk score (1-10) for each.", agent=analyst_agent)Context and Dependencies
Section titled “Context and Dependencies”In a real workflow, Task B usually depends on the result of Task A. CrewAI handles this automatically in sequential processes, but you can also be explicit.
The context parameter allows you to feed the output of previous tasks into the current task. This is how agents “talk” to each other—by passing task outputs.
write_report = Task( description="Write a summary based on the analysis.", expected_output="A text summary.", agent=writer_agent, context=[analysis_task] # explicitly waits for analysis_task)Asynchronous Execution
Section titled “Asynchronous Execution”Sometimes, tasks don’t depend on each other. If you have an agent that needs to scrape 5 different websites, doing it one by one is slow. You can set async_execution=True.
Tasks marked as async run in parallel. The Crew will automatically wait for all async tasks to finish if a subsequent task requires their output as context.
Output Pydantic/JSON
Section titled “Output Pydantic/JSON”For strict structured data, you can use the output_pydantic or output_json property. This forces the agent to return data validating against a schema, which is vital for integrating AI into traditional software pipelines.