Crewai Agent
CrewAI is an open-source framework for multi-agent collaboration, specifically designed to orchestrate and coordinate multiple AI Agents for collaboration.
CrewAI can break down a complex task into multiple roles, each responsible for a part, and complete it through process collaboration.
**Comparative Understanding:**
* **Single Agent**: One large model, working from start to finish
* **CrewAI**: Product Manager + Engineer + Analyst + Editor, each with their own responsibilities
CrewAI is a tool for coordinating, managing, and structuring AI Agents. It is built on LangChain and Pydantic to facilitate role-playing, autonomous, and collaborative Agent teams.
* **Crew (Team)**: A project group composed of multiple `Agent`s.
* **Agent (Member)**: An individual in the team, each with a clear `role`, `goal`, and `backstory`.
* **Task (Task)**: Specific work that needs to be completed by the team. A `Crew` contains multiple ordered or parallel `Task`s, assigned to appropriate `Agent`s.
* **Process (Process)**: Defines the team's workflow, such as sequential or simultaneous execution of tasks.
Simply put, crewAI provides a structured way to define who (Agent) does what (Task) under what process (Process), ultimately achieving the team goal.
The following flowchart clearly shows how the core components in the crewAI framework work together:
!(#)
The process starts with defining agents with specific roles and goals, then creating specific tasks for them.
Next, these agents and their task groups are assembled into a crew, and a process is selected for the team to collaborate, such as sequential or parallel execution. Finally, the team executes all tasks according to the established process, producing the final result.
* * *
## Environment Setup and Installation
Before starting construction, we need to prepare the development environment.
### Prerequisites
**Python Version Requirements:**
* **Required**: Python β₯ 3.10 and < 3.14
* You can enter `python --version` in the terminal to check. If it doesn't meet the version range, subsequent problems will be numerous. It's not recommended to force it.
CrewAI uses **UV** for dependency and package management, with one purpose:
**To make multi-Agent projects more stable and not be dragged down by environment issues**
For UV getting started tutorial, refer to: (#).
API Keys:
crewAI itself does not provide AI models; it needs to connect to large language models like OpenAI's GPT and Anthropic's Claude.
### Installing crewAI
Open your terminal or command line tool and use the pip command to install the crewAI package.
# Normal installation
pip install crewai
# Other dependencies
pip install langchain
pip install openai
# If installation is slow, you can use domestic mirrors
pip install crewai -i https://mirrors.aliyun.com/pypi/simple/
pip install langchain -i https://mirrors.aliyun.com/pypi/simple/
pip install openai -i https://mirrors.aliyun.com/pypi/simple/
If you want to use some advanced tools built into crewAI (such as web search), you can install additional dependencies:
# Normal installation
pip install 'crewai'
# If installation is slow, you can use domestic mirrors
pip install 'crewai' -i https://mirrors.aliyun.com/pypi/simple/
Domestically, we can use DeepSeek large models for testing. If you don't have one yet, you need to go to [https://platform.deepseek.com/api_keys](https://platform.deepseek.com/api_keys) to create an API key.
For DeepSeek API documentation, refer to: [https://api-docs.deepseek.com/zh-cn/](https://api-docs.deepseek.com/zh-cn/).
After installation, create a new Python file, such as `my_first_crew.py`, and import the necessary libraries.
CrewAI's LLM for third-party models (including DeepSeek) must go through LiteLLM at the bottom. We need to install it before use:
pip install -U litellm
## Example
from crewai import Agent, Task, Crew, Process
from crewai.llm import LLM
# =====================================================
# LLM Configuration (DeepSeek | LiteLLM Standard Writing)
# =====================================================
llm = LLM(
model="deepseek/deepseek-v4-flash",# Key: Must include the provider deepseek, then write the model name deepseek-v4-flash or deepseek-v4-pro
api_key="sk-xxxxx",# Set API key
api_base="https://api.deepseek.com/v1",
temperature=0.7,
)
# =====================================================
# Agents
# =====================================================
researcher = Agent(
role="Technical Researcher",
goal="Find the latest, accurate, and verifiable technical resources, and provide code demonstrations.",
backstory="Excels at systematically analyzing technical problems, with a focus on facts and reproducibility.",
llm=llm,
verbose=True,
allow_delegation=False,
)
writer = Agent(
role="Tech Blog Writer",
goal="Compile research findings into a technical article suitable for beginners.",
backstory="Excels at breaking down complex concepts into clear steps and providing complete examples.",
llm=llm,
verbose=True,
allow_delegation=True,
)
# =====================================================
# Tasks
# =====================================================
research_task = Task(
description=(
"In-depth research: Automated data cleaning using Python.n"
"Key focus areas include: pandas, numpy, missing values, duplicate values, and inconsistent formatting issues.n"
"Must provide complete, runnable code examples."
),
agent=researcher,
expected_output=(
"A research report including problem categorization, solution code, and a complete data cleaning workflow."
),
)
write_task = Task(
description=(
"Based on the research report, write an introductory-level tech blog post.n"
"Title: "Introduction to Python Data Cleaning: Say Goodbye to Dirty Data with pandas""
),
agent=writer,
context=,
expected_output="A Markdown tech blog post of at least 800 words.",
)
# =====================================================
# Crew
# =====================================================
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential,
verbose=True,
)
# =====================================================
# Run
# =====================================================
if __name__ =="__main__":
result = crew.kickoff()
output = result.raw
with open("python_data_cleaning_blog.md","w", encoding="utf-8")as f:
f.write(output)
Next, it will start executing tasks and output relevant information:
!(#)
After completion, the output content will be written to the python_data_cleaning_blog.md file.
The following explains the relevant attributes in the code.
### LLM (Model Layer) Key Attributes
| Attribute | Example Value | Function | Error Consequence |
| --- | --- | --- | --- |
| `model` | `deepseek/deepseek-v4-flash` | **LiteLLM standard writing**: `provider/model name` | Missing `deepseek/` will directly report `LLM Provider NOT provided` |
| `api_key` | `sk-xxxxx` | Model authentication | Empty or wrong will directly cause 401 / LLM Failed |
| `api_base` | `https://api.deepseek.com/v1` | DeepSeek API address | According to the specific model address |
| `temperature` | `0.7` | Controls output randomness | Too high causes content divergence, too low makes text stiff |
### Agent (Intelligent Agent) Key Attributes
| Attribute | Function | Essence |
| --- | --- | --- |
| `role` | Agent's "identity tag" | Written into system prompt |
| `goal` | Current Agent's core goal | Determines answer direction |
| `backstory` | Behavior constraints and style | Stabilizes output quality |
| `llm` | Which model to use | Must be explicitly bound |
| `verbose` | Prints execution process | Only affects logs |
| `allow_delegation` | Whether to allow task delegation | **Very easy to stumble** |
### researcher (Researcher)
| Attribute | Value | Design Intent |
| --- | --- | --- |
| `allow_delegation` | `False` | Prevents infinite task splitting |
| `goal` | Find materials + provide code | Output is "raw material" |
### writer (Writing)
| Attribute | Value | Risk |
| --- | --- | --- |
| `allow_delegation` | `True` | May trigger delegate for further research |
| `context` | `` | Has input, actually doesn't need to delegate |
**Conclusion**
In the sequential pipeline, the writing Agent **should have delegation turned off**, otherwise secondary delegation failures are likely.
### Task (Task) Key Attributes
**Task Common Fields:**
| Attribute | Function | Key Points |
| --- | --- | --- |
| `description` | Actual task text sent to LLM | More specific is more stable |
| `agent` | Who executes | Must be unique |
| `expected_output` | Result constraint | Not mandatory, but strongly recommended |
| `context` | Upstream task dependencies | **Core of sequential execution** |
**research_task:**
| Attribute | Description |
| --- | --- |
| `agent=researcher` | Binds researcher |
| No `context` | First phase task |
| Output | Structured research content |
**write_task:**
| Attribute | Description |
| --- | --- |
| `context=` | Force read research results |
| `agent=writer` | Only do organization and expression |
| Output | Final Markdown text |
### Crew (Executor) Key Attributes
| Attribute | Example Value | Function | Error Consequence |
| --- | --- | --- | --- |
| `agents` | `[researcher, writer]` | All available Agents | Missing one will directly error |
| `tasks` | `[research_task, write_task]` | Execution queue | Order according to list |
| `process` | `Process.sequential` | Execution strategy | Parallel will disrupt dependencies |
| `verbose` | `True` | Log switch | Must be bool |
### kickoff() and Output Structure
| Expression | Type | Purpose |
| --- | --- | --- |
| `crew.kickoff()` | `CrewOutput` | Execution result container |
| `result.raw` | `str` | **Final text output** |
| `result.tasks_output` | list/dict | Output of each Task |
| `result.token_usage` | dict | Statistics |
* * *
## crewai Command
We can use the crewai command to generate a complete project structure:
crewai create crew
For example, to create a project called tutorial-agent-test, run the following command:
crewai create crew tutorial-agent-test
Next, the following content will appear. You can first select the large model provider:
Creating folder tutorial_agent_test...
Cache expired or not found. Fetching provider data from the web...
Downloading [####################################] 1102019/56339
Select a provider to set up:
1. openai 2. anthropic 3. gemini 4. nvidia_nim 5. groq 6. huggingface 7. ollama 8. watson 9. bedrock 10. azure 11. cerebras 12. sambanova 13. other q. Quit
In this chapter, we select DeepSeek. First enter 13 to select other, then you can scroll with the mouse to see DeepSeek at 23. Enter 23:
!(#)
If you have API keys for other models, select according to the number.
After completion, you can see the generated directory as follows:
!(#)
Project Structure Description:
my_project/
βββ .env # Environment variables (API Key)
βββ pyproject.toml # Project dependency declaration
βββ README.md
βββ src/
βββ my_project/
βββ main.py # Program entry
βββ crew.py # Core logic of Crew and Agent
βββ tools/ # Custom tools
βββ config/
βββ agents.yaml # Agent definitions
βββ tasks.yaml # Task definitions
### Running Your Crew
Enter the project root directory:
cd my_project
Lock and install dependencies:
crewai install
Run:
crewai run
Or directly:
python src/my_project/main.py
### Configuring API Keys
For security, it is not recommended to write API Keys directly in code. We can set them as environment variables.
**Setting in code (for testing only)**:
import os
os.environ = "your-openai-api-key-here"
**Recommended: Using `.env` file**
Create a file named `.env` in the project root directory.
Write in the file: `OPENAI_API_KEY=your-openai-api-key-here`
In Python code, use the `python-dotenv` package to load it.
Installation command:
pip install python-dotenv
Next, load in code:
from dotenv import load_dotenv
load_dotenv() # This will automatically load variables from the .env file
# Now os.env
YouTip