Skip to main content
Article
crewaimulti-agentai-orchestrationpythonlangchainautonomous-agentsllmopen-source

Build Your First AI Agent Crew with CrewAI

Use CrewAI to orchestrate role-playing AI agents. This guide walks you through defining agents with specific roles, assigning them collaborative tasks, and launching a crew to research a topic and generate a report.

intermediate30 min4 steps
The play
  1. Install CrewAI and Set Environment
    Install the necessary Python libraries. CrewAI requires an LLM to function, so you must set an API key (e.g., for OpenAI, Groq, or Anthropic) as an environment variable named `OPENAI_API_KEY`. This guide uses OpenAI.
  2. Define Your Agents
    Create agents by defining their `role`, `goal`, and `backstory`. This gives them context and personality for their tasks. You can also assign specific tools and an LLM to each agent.
  3. Create Tasks for the Agents
    Define the work for your agents using the `Task` class. Provide a clear `description` of what needs to be done and assign the `agent` responsible. You can chain tasks by passing the output of one as the `context` for another.
  4. Assemble and Launch Your Crew
    Combine your agents and tasks into a `Crew`. The `process` determines how tasks are executed (e.g., sequential). Call the `kickoff()` method to start the collaboration and get the final result.
Starter code
import os
from crewai import Agent, Task, Crew, Process

# IMPORTANT: Set your OpenAI API key as an environment variable
# You can get a key from https://platform.openai.com/api-keys
# For example, in your terminal: export OPENAI_API_KEY='your-key-here'

# Check if the API key is set
if not os.getenv("OPENAI_API_KEY"):
    raise ValueError("OPENAI_API_KEY environment variable not set!")

# Define Agents
researcher = Agent(
  role='Senior Tech Researcher',
  goal='Find the most exciting and impactful AI advancements of the last year',
  backstory=(
    "You are a seasoned technology analyst with a knack for identifying "
    "truly transformative trends. You have a deep understanding of the AI landscape "
    "and can distinguish hype from substance."
  ),
  verbose=True,
  allow_delegation=False
)

writer = Agent(
  role='Engaging Tech Content Writer',
  goal='Craft a compelling and easy-to-understand blog post about recent AI advancements',
  backstory=(
    "You are a skilled writer who can take complex technical topics and make them "
    "accessible to a broad audience. Your writing is known for being clear, concise, and engaging."
  ),
  verbose=True,
  allow_delegation=True
)

# Define Tasks
research_task = Task(
  description=(
    "Identify and summarize the top 3 most significant AI advancements in the past year. "
    "Focus on breakthroughs in models, applications, or accessibility."
  ),
  expected_output='A concise, bullet-point summary of the top 3 advancements.',
  agent=researcher
)

write_task = Task(
  description=(
    "Using the research findings, write a 500-word blog post titled 'AI's Leap Forward: The Top 3 Breakthroughs of the Year'. "
    "The tone should be optimistic but grounded in facts. Make it informative for a non-technical audience."
  ),
  expected_output='A well-structured blog post of approximately 500 words.',
  agent=writer,
  context=[research_task] # This task depends on the output of the research_task
)

# Assemble the Crew
tech_crew = Crew(
  agents=[researcher, writer],
  tasks=[research_task, write_task],
  process=Process.sequential # Tasks will be executed one after another
)

# Launch the Crew and get the result
print("################################################")
print("## Launching Crew to Research and Write Post... ##")
print("################################################")

result = tech_crew.kickoff()

print("\n\n################################################")
print("## Crew execution finished. Final Result: ##")
print("################################################\n")
print(result)
Build Your First AI Agent Crew with CrewAI — Action Pack