Recently, I found myself pondering how best to demystify agentic systems and demonstrate their potential. The first challenge? Choosing the right programming language. As someone who has worked extensively with Java, it was tempting to use it here. However, Python’s rich ecosystem of AI frameworks made it the better choice for this exercise. For this demo, I decided to explore CrewAI, a high-level framework for building agentic systems. Let’s dive in!
Why CrewAI
CrewAI stood out to me because of its abstraction and ease of use. It offers pre-built components for agents, tasks, tools, and workflows, which makes it a fantastic option for prototyping and educational purposes. That said, the framework hides much of its internal logic, which might make debugging a bit challenging initially.
Despite this, CrewAI is powerful and intuitive—perfect for demonstrating core agentic AI concepts without needing extensive boilerplate code.
Installation
When working with Python, I always recommend starting with a fresh environment. While CrewAI supports the latest versions of Python, I encountered some issues.
Should you face similar issues, rest assured that you are not alone. 🙂 I decided to use a slightly older Python version:
python3.12 -m venv .venv
Activate it the environment
. .venv/bin/activate
and then install the required libraries
pip install crewai crewai-tools
Core concepts
When we look at the example code later, it will be quite obvious how the concepts work together. But let’s summarize the individual pieces first.
Crew
The central orchestrator that manages agents and tasks.
Agents
AI-powered entities that execute tasks and use tools.
Tasks
Units of work or problems to be solved by agents.
Tools
Functional utilities agents can use to complete tasks (e.g., calculators, web scrapers).
Example crew
For this demo, we’ll create a simple math assistant that verifies basic arithmetic problems. This example uses CrewAI’s abstractions to define an agent, tasks, and tools.
Code
Please note that CrewAI comes with a handy command line tool, but I would actually not recommend that for a start. I had a ton of issues getting it up and running with my LLM provider and it creates yet another Python environment that doesn’t work out of the box. At least for me, the experience was not ideal and I had to tweak quite a few things. Maybe that is because I’m not using OpenAI APIs, but you won’t believe how often I read the error message about a missing OpenAI API key – defaults can help, but they are not always the best approach.
from crewai import Agent, Crew, Task, Process, LLM
from crewai.tools import BaseTool
import os
os.environ['AZURE_API_KEY'] = 'xxxxxxxx'
os.environ['AZURE_API_BASE'] = 'https://xxxx.openai.azure.com/'
os.environ['AZURE_API_VERSION'] = '2024-02-15-preview'
llm = LLM(
model="azure/gpt-4o-mini"
)
class MathTool(BaseTool):
name: str = "Math tool"
description: str = "add two numbers"
def _run(self, number1: int, number2: int) -> int:
return number1 + number2
class MathClass:
"""Description of your crew"""
def student(self) -> Agent:
return Agent(
role="Math student",
goal="Solve math problems and become better at math.",
backstory="Always eager to learn and a mindset to continuously improve.",
verbose=True,
llm=llm
)
def correct(self) -> Task:
return Task(
description="Verify the math problem solution: 1+2=5",
expected_output="Feedback for provided the solution.",
tools=[MathTool()],
agent=self.student()
)
def crew(self) -> Crew:
return Crew(
agents=[self.student()],
tasks=[self.correct()],
process=Process.sequential,
verbose=True,
)
MathClass().crew().kickoff()
Results
When we run the crew, we will see what the agent is doing and thinking.
python hello.py
As you can see, the agent is using the simple add tool to get the result of our simple math problem. That’s really cool!
Conclusion
This demo showcased how to build a simple agentic system using CrewAI, connecting an LLM to a custom tool for solving math problems. The potential for extending this system is immense – imagine agents managing complex workflows, analyzing datasets, or automating tasks in your daily life.
The key takeaway is the seamless integration of LLMs with external tools, enabling agents to interact with the world in actionable ways.
What will you build next with agentic AI? The possibilities are endless. 🚀