# Building Collaborative AI Agents With CrewAI

- Canonical: https://33rdsquare.com/building-collaborative-ai-agents-with-crewai/
- Published: 2024-09-03
- Author: Jordan Brown
- Categories: [Artificial Intelligence & Machine Learning & ChatGPT](https://33rdsquare.com/category/tech/ai/)

---

## The Evolution of AI Collaboration

Artificial intelligence is undergoing a profound transformation. For decades, AI progress focused on building narrow systems to handle specific tasks in isolation, like playing chess or recognizing images. But we‘re now entering a new era where AI models can engage in open-ended dialogue, reason about novel situations, and even collaborate to achieve complex goals.

This shift is driven by large language models (LLMs) — expansive neural networks trained on vast corpora of human knowledge. By ingesting billions of pages of text, LLMs learn concepts and skills that can be flexibly applied to all sorts of Language tasks. Pioneers like GPT-3, PaLM, and ChatGPT have shown remarkable abilities to engage in freeform conversation, answer follow-up questions, and combine multiple steps of reasoning.

As LLMs grow in scale and expressiveness, we‘re seeing the rise of a powerful new type of AI system: the AI agent. An agent is essentially an autonomous AI that can perceive its environment, formulate plans, and take actions to achieve objectives. By leveraging LLMs to convert natural Language instructions into executable behaviors, AI agents can already help with writing, analysis, math, coding, and more.

But agents are even more impactful when they collaborate. Much like humans, AI agents can accomplish ambitious missions by forming teams with specialized roles, dividing up tasks, and coordinating their efforts. Imagine AI engineering crews that architect software based on a few prompts, or AI scientist squads that trade hypotheses to unearth discoveries. The applications are endless.

However, building collaborative AI agents is technically challenging. How do we define the roles and abilities of each agent? How do they decide who should do what? How can we combine their outputs into coherent solutions? We need new tools and paradigms.

Enter CrewAI — an open-source Python framework that simplifies the orchestration of AI agent teams. Let‘s explore how it works and why it represents such an exciting frontier.

## The CrewAI Framework

At its core, CrewAI provides three key abstractions for building AI agent crews:

1. **Agents** – The individual AI agents that make up the crew. For each agent, you specify its role, goals, skills, and the tools it can access.
2. **Tasks** – The top-level objectives given to the crew, like "Write an article on quantum computing advances". Tasks can be recursively broken down into smaller subtasks.
3. **Crew** – The overall team of agents that will collaborate to accomplish the tasks. The crew is instantiated with references to the member agents.

Here‘s a simplified architecture diagram:

![CrewAI Architecture](https://i.imgur.com/TKf50is.png)

Under the hood, CrewAI wraps around LangChain, a popular open-source library that connects LLMs with external data and tools. This allows CrewAI agents to easily access capabilities like web searches, database queries, Python execution, and more. It also means CrewAI can interoperate with the growing ecosystem of LangChain-enabled apps and tools.

When a crew is given a task, it kicks off a dynamic multi-step process:

1. Break down the top-level task into subtasks and delegate them to individual agents based on their roles and skills. This is done recursively until the subtasks are granular enough for the agents to directly handle.
2. Agents independently work on completing their assigned subtasks by leveraging their skills and available tools. They package up their outputs.
3. Once all subtasks for a given parent task are finished, the agent that originally delegated those subtasks is responsible for reviewing the outputs and aggregating them into a coherent intermediate result.
4. As subtasks are completed, the crew reassesses the overall task status and determines next steps. This may involve generating new subtasks, re-delegating work, or escalating issues to a human operator.
5. The process continues until the original top-level task is fully complete, at which point the crew returns the final result.

Throughout the flow, CrewAI maintains a detailed event log of each action taken by the agents. This provides valuable transparency into their reasoning process and makes it easier to debug issues or audit the crew‘s behavior after the fact.

Let‘s make this concrete with an example.

## Collaborating on Content Creation

Suppose we want to streamline the workflow of researching, outlining, drafting and polishing in-depth articles. With CrewAI, we can delegate this open-ended task to an autonomous crew of AI writers.

Here‘s a code sketch of what that might look like:

```
from crewai import Crew, Agent, Task

# Define the researcher agent
researcher = Agent(
    name="Researcher",
    role="Conduct research on the article topic",
    skills=["Google Search", "Wikipedia", "Paper Extraction"],
)

# Define the writer agent
writer = Agent(
    name="Writer",
    role="Write the article based on research",
    skills=["Outlining", "Drafting", "Formatting"],
)

# Define the editor agent
editor = Agent(
    name="Editor",
    role="Polish the final article",
    skills=["Proofreading", "Fact-checking", "Search optimizing"],
    tools=[GrammarChecker(), Thesaurus()]
)

# Instantiate the content crew
content_crew = Crew(agents=[researcher, writer, editor])

# Kick off an article
task = Task(
    objective="Write an article on recent breakthroughs in quantum computing",
    context="Focus on significant new algorithms, hardware progress, commercial initiatives",
    success_criteria="2000+ words, clear explanations, authoritative sources"
)
result = content_crew.run(task)

print(result)
```

When `content_crew.run(task)` is called, CrewAI springs into action:

1. The researcher agent is first activated to gather information on quantum computing breakthroughs by conducting web searches, extracting key details from technical papers and Wikipedia, and synthesizing the findings into a concise research brief.
2. Next, the writer agent receives the research brief and begins outlining a rough narrative flow for the article. It identifies key points to cover in each section.
3. The writer then fleshes out the outline into full prose, aiming to explain complex ideas in accessible terms and bring the significance to life with vivid examples. It may loop in the researcher for additional facts and figures to incorporate.
4. Once the writer has a complete draft, the editor takes over with a fine-toothed comb. It runs the content through a grammar checker and rephrases awkward sentences, fills in missing context, and ensures a consistent voice and structure.
5. The editor does a final pass to optimize the article for likely search engine queries, adding relevant headers and links. It then packages up the polished piece.
6. With the article now complete, the content crew returns the finished document, along with a log of each revisions and intermediary artifact.

The end result is a meticulously researched, engagingly narrated, and professionally edited article — all generated from a single high-level prompt. By combining the specialized skills of researcher, writer, and editor agents, the content crew achieves an ambitious outcome that would be much harder for any narrow AI model.

And this is just a tiny glimpse of what‘s possible when AI agents collaborate! Let‘s zoom out and survey some of the revolutionary use cases on the horizon.

## The Future of AI Collaboration

As AI agents grow more sophisticated, the sorts of goals they can team up to achieve will get more and more ambitious. Some exciting possibilities:

**Scientific discovery crews**: Imagine AI scientist agents that autonomously pose novel hypotheses, design experiments to test them, run lab equipment, statistically analyze the results, and trade insights to uncover nature‘s secrets. AI crews could accelerate breakthroughs in medicine, materials science, and fundamental physics.

**Software engineering crews**: What if AI programmer agents could take a high-level product spec and collaboratively architect a full-stack implementation? AI designer agents could mock up the UX, AI tester agents could generate edge cases, and AI code reviewer agents could stress test for bugs and security holes. The future of software development may be prompting AI crews.

**Business strategy crews**: Some of the highest-leverage applications of AI collaboration will be in the C-suite. Imagine AI executive teams that crunch real-time financial data, simulate market scenarios, and wargame competitor moves to devise optimal growth plans. Or AI investment crews that pore over 10-Ks, extract key risks and opportunities, and rebalance positions to maximize returns.

**Personalized education crews**: One of the grand challenges in education is providing high-quality, individualized learning at scale. AI tutor crews could be a major part of the solution. Picture AI agents that engage students in Socratic dialogue, guide them through exercises tailored to their goals, and even "learn alongside" by asking questions and connecting concepts. Adaptive AI crews may become the most patient, perceptive instructors.

We‘re truly just scratching the surface of what AI collaboration make possible. As the underlying language models, reasoning skills, and world interfaces improve, AI agents may be able to take on open-ended workstreams like optimizing supply chains, designing products from scratch, or even managing entire companies.

But realizing this future requires ongoing advances in fundamental AI capabilities as well as careful interdisciplinary research to ensure safety and alignment. We‘ll need robust mechanisms for specifying agent goals, constraining their actions within guard rails, and interpreting their reasoning so we can stay in control. Initiatives like OpenAI‘s Constitutional AI and Anthropic‘s Cooperative AI represent promising steps in this direction.

## Get Involved!

If you‘re excited by the potential of AI collaboration, there are lots of ways to get involved and help shape this crucial technology:

- **Experiment with CrewAI and other agent frameworks**: The best way to build intuition for collaborative AI development is to try it yourself. Spin up a simple agent crew and explore the possibilities. Don‘t be afraid to think big!
- **Contribute to open-source projects**: CrewAI, LangChain, and related tools are community-driven open source efforts. Join the Discord, report issues on Github, or even submit a pull request to help make these frameworks more capable and robust.
- **Dive into the research literature**: Start with seminal papers like the Apples-to-Apples Comparison on Cooperative Ability, the InstructGPT paper on training instructable agents, and the Cooperative AI Agenda. The Agent Directory also curates links to the latest work.
- **Shape the discourse**: As transformative a technology as collaborative AI will be, it‘s crucial that a wide range of voices and perspectives help guide its trajectory. Share your views on forums like the AI Alignment Forum, the Effective Altruism Forum, and LessWrong.

We‘re at an inflection point in the history of intelligence. AI agents are already expanding the boundaries of what machines can do — and their impact will only accelerate as they learn to team up. Frameworks like CrewAI offer a glimpse of how we can tap into the power of AI collaboration in a scalable, steerable way.

The age of AI agents has arrived. Together, let‘s ensure this technology helps create a future we can all look forward to.

---

Source: [Building Collaborative AI Agents With CrewAI](https://33rdsquare.com/building-collaborative-ai-agents-with-crewai/)
