Intermediate

Tasks

Learn to define tasks with clear descriptions, expected outputs, context dependencies, output formats, callbacks, and async execution.

Task Basics

Python
from crewai import Task

task = Task(
    # What should the agent do?
    description="""Research the current state of AI in healthcare.
    Focus on:
    1. FDA-approved AI diagnostics tools
    2. AI in drug discovery pipelines
    3. Patient outcome improvements
    Include specific statistics and case studies.""",

    # What should the output look like?
    expected_output="""A structured research report with:
    - Executive summary (3 sentences)
    - 3 sections with findings and statistics
    - List of sources referenced""",

    # Which agent handles this?
    agent=researcher,
)

Task Dependencies with Context

Tasks can receive the output of previous tasks as context. This is how agents build on each other's work:

Python
# Task 1: Research (no dependencies)
research_task = Task(
    description="Research AI trends in education for 2026.",
    expected_output="A detailed research report with 5 key trends.",
    agent=researcher
)

# Task 2: Writing (depends on research)
writing_task = Task(
    description="Write a blog post based on the research findings.",
    expected_output="A 800-word blog post in markdown format.",
    agent=writer,
    context=[research_task]  # Receives research output as context
)

# Task 3: Editing (depends on writing)
editing_task = Task(
    description="""Review and edit the blog post.
    Fix grammar, improve clarity, and ensure accuracy
    against the original research.""",
    expected_output="A polished, publication-ready blog post.",
    agent=editor,
    context=[writing_task, research_task]  # Access both previous outputs
)

Structured Output with Pydantic

Python
from pydantic import BaseModel
from typing import List

class TrendReport(BaseModel):
    title: str
    trends: List[str]
    summary: str
    confidence_score: float

task = Task(
    description="Identify the top 5 AI trends for 2026.",
    expected_output="A structured report with trends and confidence.",
    agent=researcher,
    output_pydantic=TrendReport  # Forces structured output
)

# After crew.kickoff(), access structured data:
# task.output.pydantic.trends
# task.output.pydantic.confidence_score

Output to File

Python
task = Task(
    description="Write a comprehensive market analysis.",
    expected_output="A detailed market analysis report.",
    agent=analyst,
    output_file="reports/market_analysis.md"  # Saves output to file
)

Callbacks

Python
def task_completed(output):
    """Called when a task finishes."""
    print(f"Task completed! Output length: {len(str(output))}")
    # Send notification, log to database, etc.

task = Task(
    description="Analyze customer feedback data.",
    expected_output="Summary of customer sentiment trends.",
    agent=analyst,
    callback=task_completed  # Called after task execution
)

Task Writing Tips

Be specific in descriptions:
  • State exactly what information to include
  • Specify the format (bullet points, paragraphs, JSON)
  • Set length expectations (500 words, 5 bullet points)
  • Define quality criteria (include sources, use data)
Expected output matters: The expected_output field is not just documentation — it guides the LLM toward the right format and level of detail. Vague expected outputs produce vague results. Be as specific as possible.

What's Next?

In the next lesson, we will learn how to orchestrate agents and tasks into crews with sequential and hierarchical processes, memory, and advanced configuration.