Quickstart
Build your first AI agent with CrewAI in under 5 minutes.
Build your first CrewAI Agent
Let’s create a simple crew that will help us research
and report
on the latest AI developments
for a given topic or subject.
Before we proceed, make sure you have crewai
and crewai-tools
installed.
If you haven’t installed them yet, you can do so by following the installation guide.
Follow the steps below to get crewing! 🚣♂️
Create your crew
Create a new crew project by running the following command in your terminal.
This will create a new directory called latest-ai-development
with the basic structure for your crew.
Modify your `agents.yaml` file
You can also modify the agents as needed to fit your use case or copy and paste as is to your project.
Any variable interpolated in your agents.yaml
and tasks.yaml
files like {topic}
will be replaced by the value of the variable in the main.py
file.
Modify your `tasks.yaml` file
Modify your `crew.py` file
[Optional] Add before and after crew functions
Feel free to pass custom inputs to your crew
For example, you can pass the topic
input to your crew to customize the research and reporting.
Set your environment variables
Before running your crew, make sure you have the following keys set as environment variables in your .env
file:
- An OpenAI API key (or other LLM API key):
OPENAI_API_KEY=sk-...
- A Serper.dev API key:
SERPER_API_KEY=YOUR_KEY_HERE
Lock and install the dependencies
Lock the dependencies and install them by using the CLI command but first, navigate to your project directory:
Run your crew
To run your crew, execute the following command in the root of your project:
View your final report
You should see the output in the console and the report.md
file should be created in the root of your project with the final report.
Here’s an example of what the report should look like:
Note on Consistency in Naming
The names you use in your YAML files (agents.yaml
and tasks.yaml
) should match the method names in your Python code.
For example, you can reference the agent for specific tasks from tasks.yaml
file.
This naming consistency allows CrewAI to automatically link your configurations with your code; otherwise, your task won’t recognize the reference properly.
Example References
Note how we use the same name for the agent in the agents.yaml
(email_summarizer
) file as the method name in the crew.py
(email_summarizer
) file.
Note how we use the same name for the agent in the tasks.yaml
(email_summarizer_task
) file as the method name in the crew.py
(email_summarizer_task
) file.
Use the annotations to properly reference the agent and task in the crew.py
file.
Annotations include:
Here are examples of how to use each annotation in your CrewAI project, and when you should use them:
@agent
Used to define an agent in your crew. Use this when:
- You need to create a specialized AI agent with a specific role
- You want the agent to be automatically collected and managed by the crew
- You need to reuse the same agent configuration across multiple tasks
@task
Used to define a task that can be executed by agents. Use this when:
- You need to define a specific piece of work for an agent
- You want tasks to be automatically sequenced and managed
- You need to establish dependencies between different tasks
@crew
Used to define your crew configuration. Use this when:
- You want to automatically collect all @agent and @task definitions
- You need to specify how tasks should be processed (sequential or hierarchical)
- You want to set up crew-wide configurations
@tool
Used to create custom tools for your agents. Use this when:
- You need to give agents specific capabilities (like web search, data analysis)
- You want to encapsulate external API calls or complex operations
- You need to share functionality across multiple agents
@before_kickoff
Used to execute logic before the crew starts. Use this when:
- You need to validate or preprocess input data
- You want to set up resources or configurations before execution
- You need to perform any initialization logic
@after_kickoff
Used to process results after the crew completes. Use this when:
- You need to format or transform the final output
- You want to perform cleanup operations
- You need to save or log the results in a specific way
@callback
Used to handle events during crew execution. Use this when:
- You need to monitor task progress
- You want to log intermediate results
- You need to implement custom progress tracking or metrics
@cache_handler
Used to implement custom caching for task results. Use this when:
- You want to avoid redundant expensive operations
- You need to implement custom cache storage or expiration logic
- You want to persist results between runs
These decorators are part of the CrewAI framework and help organize your crew’s structure by automatically collecting agents, tasks, and handling various lifecycle events.
They should be used within a class decorated with @CrewBase
.
Replay Tasks from Latest Crew Kickoff
CrewAI now includes a replay feature that allows you to list the tasks from the last run and replay from a specific one. To use this feature, run.
Replace <task_id>
with the ID of the task you want to replay.
Reset Crew Memory
If you need to reset the memory of your crew before running it again, you can do so by calling the reset memory feature:
This will clear the crew’s memory, allowing for a fresh start.
Deploying Your Project
The easiest way to deploy your crew is through CrewAI Enterprise, where you can deploy your crew in a few clicks.
Was this page helpful?