## Core Concepts
### Agent Definition
```python
from crewai import Agent, Task, Crew, Process
from langchain.tools import DuckDuckGoSearchRun
researcher = Agent(
role='Senior Research Analyst',
goal='Uncover cutting-edge developments in AI and data science',
backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting
actionable insights.""",
verbose=True,
allow_delegation=False,
tools=[DuckDuckGoSearchRun()],
llm=llm
)
writer = Agent(
role='Tech Content Strategist',
goal='Craft compelling content on tech advancements',
backstory="""You are a renowned Content Strategist, known for
your insightful and engaging articles. You transform complex
concepts into compelling narratives.""",
verbose=True,
allow_delegation=True,
llm=llm
)
editor = Agent(
role='Editor-in-Chief',
goal='Ensure content quality and accuracy',
backstory="""You are a meticulous editor with years of experience
in tech journalism. You ensure all content meets high standards
of quality and factual accuracy.""",
verbose=True,
allow_delegation=False,
llm=llm
)
```
### Task Definition
```python
task1 = Task(
description="""Conduct a comprehensive analysis of the latest
advancements in AI as of 2024. Identify key trends, breakthrough
technologies, and potential industry impacts.
Your final answer MUST be a full analysis report""",
agent=researcher,
expected_output="A comprehensive 3-paragraph report on AI advancements"
)
task2 = Task(
description="""Using the research findings, write an engaging blog
post that highlights the most significant AI advancements.
Your post should be informative yet accessible.""",
agent=writer,
context=[task1],
expected_output="A 4-paragraph blog post in markdown format"
)
task3 = Task(
description="""Review the blog post for clarity, accuracy, and
engagement. Provide constructive feedback and suggest improvements.""",
agent=editor,
context=[task2],
expected_output="A list of specific improvements and final approval"
)
```
### Crew Assembly
```python
content_team = Crew(
agents=[researcher, writer, editor],
tasks=[task1, task2, task3],
verbose=2,
process=Process.sequential, # Options: sequential, hierarchical
memory=True, # Enable memory for context
cache=True # Cache results
)
result = content_team.kickoff()
print(result)
```
## Advanced Patterns
### Hierarchical Process
```python
from crewai import Crew, Process
content_team = Crew(
agents=[researcher, writer, editor],
tasks=[task1, task2, task3],
process=Process.hierarchical,
manager_llm=llm, # LLM for the manager agent
verbose=2
)
```
### Custom Tools
```python
from langchain.tools import tool
@tool
def analyze_sentiment(text: str) -> str:
"""Analyze sentiment of given text"""
return "positive" if "good" in text else "negative"
@tool
def fetch_stock_data(ticker: str) -> dict:
"""Fetch stock data for ticker symbol"""
return {"price": 150.0, "change": 2.5}
analyst = Agent(
role='Financial Analyst',
tools=[analyze_sentiment, fetch_stock_data],
)
```
### Memory and Context
```python
from crewai.memory import EntityMemory
research_crew = Crew(
agents=[researcher, analyst],
tasks=[task1, task2],
memory=True,
entity_memory=EntityMemory(),
verbose=2
)
```
## Best Practices
- Define clear, specific roles for each agent
- Use allow_delegation wisely (only for managers)
- Provide detailed backstories for better context
- Chain tasks using context parameter
- Enable memory for long-running workflows