crew.py
file.
Introduction
Annotations in the CrewAI framework are used to decorate classes and methods, providing metadata and functionality to various components of your crew. These annotations help in organizing and structuring your code, making it more readable and maintainable.Available Annotations
The CrewAI framework provides the following annotations:@CrewBase
: Used to decorate the main crew class.@agent
: Decorates methods that define and return Agent objects.@task
: Decorates methods that define and return Task objects.@crew
: Decorates the method that creates and returns the Crew object.@llm
: Decorates methods that initialize and return Language Model objects.@tool
: Decorates methods that initialize and return Tool objects.@callback
: Used for defining callback methods.@output_json
: Used for methods that output JSON data.@output_pydantic
: Used for methods that output Pydantic models.@cache_handler
: Used for defining cache handling methods.
Usage Examples
Let’s go through examples of how to use these annotations:1. Crew Base Class
@CrewBase
annotation is used to decorate the main crew class. This class typically contains configurations and methods for creating agents, tasks, and the crew itself.
@CrewBase
does more than register the class:- Configuration bootstrapping: looks for
agents_config
andtasks_config
(defaulting toconfig/agents.yaml
andconfig/tasks.yaml
) beside the class file, loads them at instantiation, and safely falls back to empty dicts if files are missing. - Decorator orchestration: keeps memoized references to every method marked with
@agent
,@task
,@before_kickoff
, or@after_kickoff
so they are instantiated once per crew and executed in declaration order. - Hook wiring: automatically attaches the preserved kickoff hooks to the
Crew
object returned by the@crew
method, making them run before and after.kickoff()
. - MCP integration: when the class defines
mcp_server_params
,get_mcp_tools()
lazily starts an MCP server adapter, hydrates the declared tools, and an internal after-kickoff hook stops the adapter. See MCP overview for adapter configuration details.
2. Tool Definition
@tool
annotation is used to decorate methods that return tool objects. These tools can be used by agents to perform specific tasks.
3. LLM Definition
@llm
annotation is used to decorate methods that initialize and return Language Model objects. These LLMs are used by agents for natural language processing tasks.
4. Agent Definition
@agent
annotation is used to decorate methods that define and return Agent objects.
5. Task Definition
@task
annotation is used to decorate methods that define and return Task objects. These methods specify the task configuration and the agent responsible for the task.
6. Crew Creation
@crew
annotation is used to decorate the method that creates and returns the Crew
object. This method assembles all the components (agents and tasks) into a functional crew.
YAML Configuration
The agent configurations are typically stored in a YAML file. Here’s an example of how theagents.yaml
file might look for the researcher agent:
LinkedinProfileCrew
class. The configuration specifies the agent’s role, goal, backstory, and other properties such as the LLM and tools it uses.
Note how the llm
and tools
in the YAML file correspond to the methods decorated with @llm
and @tool
in the Python class.
Best Practices
- Consistent Naming: Use clear and consistent naming conventions for your methods. For example, agent methods could be named after their roles (e.g., researcher, reporting_analyst).
- Environment Variables: Use environment variables for sensitive information like API keys.
- Flexibility: Design your crew to be flexible by allowing easy addition or removal of agents and tasks.
- YAML-Code Correspondence: Ensure that the names and structures in your YAML files correspond correctly to the decorated methods in your Python code.