What is a task in CrewAI
task in CrewAI is a discrete unit of work or operation that the platform manages and executes, often involving AI model calls or data processing steps. Tasks enable modular, reusable, and orchestrated AI workflows within CrewAI's environment.Task in CrewAI is a discrete unit of work that encapsulates a specific AI operation or process within a workflow.How it works
A task in CrewAI functions like a single step in a production line, where each step performs a defined action such as calling an AI model, processing data, or integrating external APIs. Tasks are designed to be modular and composable, allowing developers to build complex AI workflows by chaining multiple tasks together. This modularity ensures that each task can be independently developed, tested, and reused across different projects.
Concrete example
Here is a simplified example of defining and running a task in CrewAI that calls an AI model to generate text based on a prompt:
import os
from crewai import CrewAIClient
client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])
# Define a task that calls an AI text generation model
text_generation_task = client.tasks.create(
name="GenerateText",
model="gpt-4o",
input_schema={"prompt": "string"},
output_schema={"text": "string"}
)
# Run the task with input
result = text_generation_task.run({"prompt": "Explain what a task is in CrewAI."})
print(result["text"]) Explain what a task is in CrewAI. A task in CrewAI is a modular unit of work that performs a specific AI operation, such as generating text, which can be reused and orchestrated within workflows.
When to use it
Use tasks in CrewAI when you need to modularize AI operations for reuse, orchestration, and scalability. Tasks are ideal for breaking down complex AI workflows into manageable steps, enabling easier debugging and maintenance. Avoid using tasks for trivial or one-off operations that do not benefit from modularity or reuse.
Key terms
| Term | Definition |
|---|---|
| Task | A modular unit of work in CrewAI that performs a specific AI operation. |
| Workflow | An orchestrated sequence of tasks executed to achieve a complex AI process. |
| Model | An AI model referenced by a task to perform inference or generation. |
| Input Schema | The expected input data format for a task. |
| Output Schema | The expected output data format produced by a task. |
Key Takeaways
- A task in CrewAI encapsulates a single AI operation for modularity and reuse.
- Tasks enable building complex AI workflows by chaining discrete units of work.
- Use tasks to improve maintainability and scalability of AI integrations.