From 4e3acdf7009f1c4974c96b26d770d99a372ccb93 Mon Sep 17 00:00:00 2001 From: Farah Abdou Date: Sat, 23 May 2026 20:10:26 +0300 Subject: [PATCH 1/3] Add materials for CrewAI tutorial --- .../01_single_agent.py | 25 ++++++++ .../02_research_and_writer_crew.py | 53 +++++++++++++++++ .../03_explicit_context.py | 56 ++++++++++++++++++ .../04_agent_with_tools.py | 58 +++++++++++++++++++ .../README.md | 58 +++++++++++++++++++ .../requirements.txt | 1 + 6 files changed, 251 insertions(+) create mode 100644 coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py create mode 100644 coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py create mode 100644 coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py create mode 100644 coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py create mode 100644 coordinating-teams-of-ai-agents-with-crewai-in-python/README.md create mode 100644 coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py new file mode 100644 index 0000000000..9b39e71a72 --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py @@ -0,0 +1,25 @@ +from crewai import Agent, Task, Crew, LLM + +llm = LLM(model="gemini/gemini-2.5-flash") + +travel_agent = Agent( + role="Travel Advisor", + goal="Provide helpful travel recommendations", + backstory=( + "You're an experienced travel advisor who has visited" + " over fifty countries and specializes in budget-friendly" + " adventure travel." + ), + llm=llm, + verbose=True, +) + +task = Task( + description="Suggest three budget-friendly destinations in Southeast Asia", + expected_output="A short list of three destinations with brief descriptions", + agent=travel_agent, +) + +crew = Crew(agents=[travel_agent], tasks=[task]) +result = crew.kickoff() +print(result.raw) \ No newline at end of file diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py new file mode 100644 index 0000000000..77365c654e --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py @@ -0,0 +1,53 @@ +from crewai import Agent, Task, Crew, Process, LLM + +llm = LLM(model="gemini/gemini-2.5-flash") + +researcher = Agent( + role="Senior Research Analyst", + goal="Find comprehensive information about a given topic", + backstory=( + "You're a seasoned research analyst with a knack for" + " uncovering the most relevant and accurate information." + " You're known for your thorough and well-organized research." + ), + llm=llm, +) + +writer = Agent( + role="Content Writer", + goal="Write clear, engaging content based on research findings", + backstory=( + "You're an experienced writer who excels at transforming" + " complex research into accessible, well-structured articles" + " that readers enjoy." + ), + llm=llm, +) + +research_task = Task( + description="Research the latest trends in renewable energy technology", + expected_output=( + "A detailed list of the top five renewable energy trends" + " with explanations" + ), + agent=researcher, +) + +writing_task = Task( + description="Write a short article based on the research findings", + expected_output=( + "A well-structured article of about three hundred words" + " on renewable energy trends" + ), + agent=writer, +) + +crew = Crew( + agents=[researcher, writer], + tasks=[research_task, writing_task], + process=Process.sequential, + verbose=True, +) + +result = crew.kickoff() +print(result.raw) \ No newline at end of file diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py new file mode 100644 index 0000000000..af6dcb47a0 --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py @@ -0,0 +1,56 @@ +from crewai import Agent, Task, Crew, LLM + +llm = LLM(model="gemini/gemini-2.5-flash") + +researcher = Agent( + role="Senior Research Analyst", + goal="Find comprehensive information about a given topic", + backstory=( + "You're a seasoned research analyst with a knack for" + " uncovering the most relevant and accurate information." + " You're known for your thorough and well-organized research." + ), + llm=llm, +) + +writer = Agent( + role="Content Writer", + goal="Write clear, engaging content based on research findings", + backstory=( + "You're an experienced writer who excels at transforming" + " complex research into accessible, well-structured articles" + " that readers enjoy." + ), + llm=llm, +) + +research_task = Task( + description=( + "Research the current state of electric vehicle adoption" + " worldwide" + ), + expected_output=( + "A bullet-point list of key statistics and trends" + " in EV adoption" + ), + agent=researcher, +) + +writing_task = Task( + description=( + "Using the provided research, write a concise summary article" + " about electric vehicle adoption" + ), + expected_output="A two hundred word article summarizing EV adoption trends", + agent=writer, + context=[research_task], +) + +crew = Crew( + agents=[researcher, writer], + tasks=[research_task, writing_task], + verbose=True, +) + +result = crew.kickoff() +print(result.raw) \ No newline at end of file diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py new file mode 100644 index 0000000000..f5030e4099 --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py @@ -0,0 +1,58 @@ +from crewai import Agent, Task, Crew, LLM +from crewai_tools import ScrapeWebsiteTool + +llm = LLM(model="gemini/gemini-2.5-flash") + +scrape_tool = ScrapeWebsiteTool() + +researcher = Agent( + role="Python Release Analyst", + goal="Find the latest Python release information", + backstory=( + "You're a developer advocate who tracks Python releases" + " and summarizes what's new for the community." + ), + tools=[scrape_tool], + llm=llm, +) + +writer = Agent( + role="Tech Blogger", + goal="Write concise release summaries for a developer audience", + backstory=( + "You write clear, engaging blog posts that help developers" + " stay up to date with the Python ecosystem." + ), + llm=llm, +) + +research_task = Task( + description=( + "Scrape https://www.python.org/downloads/ and report" + " the latest stable Python version number and its release date" + ), + expected_output="The latest Python version number and release date", + agent=researcher, +) + +writing_task = Task( + description=( + "Write a one-paragraph announcement based on the Python" + " release information provided by the research task" + ), + expected_output=( + "A concise one hundred word announcement covering the" + " latest Python version number and release date" + ), + agent=writer, + context=[research_task], +) + +crew = Crew( + agents=[researcher, writer], + tasks=[research_task, writing_task], + verbose=True, +) + +result = crew.kickoff() +print(result.raw) \ No newline at end of file diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/README.md b/coordinating-teams-of-ai-agents-with-crewai-in-python/README.md new file mode 100644 index 0000000000..5e72c9805a --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/README.md @@ -0,0 +1,58 @@ +# Coordinating Teams of AI Agents with CrewAI in Python + +Sample code for the Real Python tutorial +[Coordinating Teams of AI Agents with CrewAI in Python](https://realpython.com/coordinating-teams-of-ai-agents-with-crewai-in-python/). + +## Requirements + +- Python 3.10 to 3.13 (CrewAI does not support 3.14+) +- A free [Google AI Studio](https://aistudio.google.com/) API key for Gemini + +## Setup + +Create and activate a virtual environment, then install the dependencies: + +```console +$ python -m venv venv +$ source venv/bin/activate # On Windows: .\venv\Scripts\activate +$ python -m pip install -r requirements.txt +``` + +Set your Gemini API key as an environment variable: + +```console +$ export GEMINI_API_KEY="your-gemini-api-key-here" +``` + +On Windows PowerShell: + +```pscon +PS> $ENV:GEMINI_API_KEY = "your-gemini-api-key-here" +``` + +## Running the Examples + +Each script corresponds to one section of the tutorial: + +| File | Tutorial Section | +|------|------------------| +| `01_single_agent.py` | Start Using CrewAI to Create Agent Teams | +| `02_research_and_writer_crew.py` | Build Your First Multi-Agent Team | +| `03_explicit_context.py` | Control Task Dependencies Explicitly | +| `04_agent_with_tools.py` | Expand Agent Capabilities With Tools | + +Run any script with: + +```console +$ python 01_single_agent.py +``` + +## Notes + +- The `verbose=True` flag prints detailed agent reasoning logs — useful for + learning, but noisy for production. +- If CrewAI complains about a missing `OPENAI_API_KEY` (e.g., when memory + or certain tools are enabled), set it to any non-empty string to suppress + the error. +- On first run, CrewAI may show a "Would you like to view your execution + traces?" prompt that auto-dismisses after twenty seconds. \ No newline at end of file diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt b/coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt new file mode 100644 index 0000000000..d75623b71e --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt @@ -0,0 +1 @@ +crewai[tools,google-genai] \ No newline at end of file From 4ca8374bc08f62f8c352452191bdafb9641fd4f8 Mon Sep 17 00:00:00 2001 From: Farah Abdou Date: Sat, 23 May 2026 20:53:14 +0300 Subject: [PATCH 2/3] Apply Black formatting --- .../01_single_agent.py | 2 +- .../02_research_and_writer_crew.py | 5 ++--- .../03_explicit_context.py | 8 +++----- .../04_agent_with_tools.py | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py index 9b39e71a72..98acee857a 100644 --- a/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py @@ -22,4 +22,4 @@ crew = Crew(agents=[travel_agent], tasks=[task]) result = crew.kickoff() -print(result.raw) \ No newline at end of file +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py index 77365c654e..8ad7e27640 100644 --- a/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py @@ -27,8 +27,7 @@ research_task = Task( description="Research the latest trends in renewable energy technology", expected_output=( - "A detailed list of the top five renewable energy trends" - " with explanations" + "A detailed list of the top five renewable energy trends" " with explanations" ), agent=researcher, ) @@ -50,4 +49,4 @@ ) result = crew.kickoff() -print(result.raw) \ No newline at end of file +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py index af6dcb47a0..d7ac973879 100644 --- a/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py @@ -26,12 +26,10 @@ research_task = Task( description=( - "Research the current state of electric vehicle adoption" - " worldwide" + "Research the current state of electric vehicle adoption" " worldwide" ), expected_output=( - "A bullet-point list of key statistics and trends" - " in EV adoption" + "A bullet-point list of key statistics and trends" " in EV adoption" ), agent=researcher, ) @@ -53,4 +51,4 @@ ) result = crew.kickoff() -print(result.raw) \ No newline at end of file +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py index f5030e4099..cc33d20131 100644 --- a/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py @@ -55,4 +55,4 @@ ) result = crew.kickoff() -print(result.raw) \ No newline at end of file +print(result.raw) From c4856cb8567e76d12fc30b0efe7fc3da8670e9f5 Mon Sep 17 00:00:00 2001 From: Farah Abdou Date: Sat, 23 May 2026 20:59:07 +0300 Subject: [PATCH 3/3] Apply Ruff formatting --- .../02_research_and_writer_crew.py | 3 ++- .../03_explicit_context.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py index 8ad7e27640..04e1aaffef 100644 --- a/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py @@ -27,7 +27,8 @@ research_task = Task( description="Research the latest trends in renewable energy technology", expected_output=( - "A detailed list of the top five renewable energy trends" " with explanations" + "A detailed list of the top five renewable energy trends" + " with explanations" ), agent=researcher, ) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py index d7ac973879..28fe576400 100644 --- a/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py @@ -26,10 +26,10 @@ research_task = Task( description=( - "Research the current state of electric vehicle adoption" " worldwide" + "Research the current state of electric vehicle adoption worldwide" ), expected_output=( - "A bullet-point list of key statistics and trends" " in EV adoption" + "A bullet-point list of key statistics and trends in EV adoption" ), agent=researcher, )