Skip to content

CrewAI

CrewAI Inc.

一个用于编排角色扮演 AI agent 的框架,使多个 agent 能够协作完成复杂任务。

Official Site
01

概述

CrewAI 是由 Joao Moura 开发的开源框架,专注于多 Agent 协作。与单 Agent 系统不同,CrewAI 让多个 AI Agent 扮演不同角色,如研究员、作者、审查者等,协作完成复杂任务。每个 Agent 有独特的角色、目标、背景故事和工具集。CrewAI 支持顺序和并行两种执行模式,Agent 之间可以委派任务和共享信息。

02

安装

CrewAI 通过 pip 安装,需要 Python 3.10+。安装后需要配置 LLM API key。CrewAI 支持多种 LLM 后端,包括 OpenAI、Anthropic 和本地模型。

bash
# Install CrewAI
pip install crewai
pip install 'crewai[tools]'

# Configure API
export OPENAI_API_KEY=your-key-here

# Or use uv (recommended)
uv add crewai
uv add 'crewai[tools]'
03

创建 Crew

Crew 是 CrewAI 的核心概念,由 Agent、任务和流程组成。创建 Crew 需要定义 Agent 列表、任务列表和执行流程。Crew 管理 Agent 之间的协作和任务分配。

bash
from crewai import Crew, Process

crew = Crew(
    agents=[researcher, writer, editor],
    tasks=[research_task, writing_task, editing_task],
    process=Process.sequential,
    verbose=True
)

result = crew.kickoff()
04

Agent

Agent 是 CrewAI 中的基本单元。每个 Agent 有角色、目标、背景故事和工具。角色定义 Agent 的专业领域,目标引导 Agent 的行为,背景故事塑造 Agent 的个性和风格。Agent 可以使用工具执行操作。

bash
from crewai import Agent

researcher = Agent(
    role='Senior Research Analyst',
    goal='In-depth analysis of the latest AI technology trends',
    backstory='You are an experienced technology analyst skilled at researching and summarizing complex technical topics.',
    tools=[search_tool, web_scraper],
    verbose=True
)
05

任务

Task 是分配给 Agent 的具体工作单元。每个任务有描述、预期输出和负责的 Agent。任务可以设置依赖关系,支持顺序和并行执行。任务输出可以传递给后续任务。

bash
from crewai import Task

research_task = Task(
    description='Research the most important AI agent frameworks of 2024',
    expected_output='A detailed report, including framework comparisons and usage recommendations',
    agent=researcher,
    output_file='research_report.md'
)
06

工具

CrewAI 提供丰富的工具,包括搜索、网页抓取、文件操作、数据库查询等。工具扩展了 Agent 的能力,使其能与外部世界交互。支持自定义工具开发。

bash
from crewai_tools import SerperDevTool, WebsiteSearchTool

search_tool = SerperDevTool()
web_tool = WebsiteSearchTool()

# Custom tool
from crewai.tools import BaseTool

class MyTool(BaseTool):
    name: str = "My Tool"
    description: str = "Custom tool description"
    
    def _run(self, argument: str) -> str:
        return f"Result: {argument}"
07

流程

CrewAI 支持两种执行流程:Sequential(顺序)和 Hierarchical(分层)。顺序流程按任务列表顺序执行任务,分层流程由一个管理者 Agent 动态分配任务。流程的选择影响 Agent 的协作方式和执行效率。

bash
# Sequential process
crew = Crew(agents=agents, tasks=tasks, process=Process.sequential)

# Hierarchical process (requires a manager agent)
crew = Crew(
    agents=agents, 
    tasks=tasks, 
    process=Process.hierarchical,
    manager_llm=ChatOpenAI(model="gpt-4")
)
08

记忆

CrewAI 支持短期记忆、长期记忆和实体记忆。短期记忆维护会话上下文,长期记忆跨会话存储信息,实体记忆追踪特定实体(人物、组织等)。记忆系统使用向量数据库进行语义搜索。

bash
crew = Crew(
    agents=agents,
    tasks=tasks,
    memory=True,
    embedder={
        "provider": "openai",
        "config": {"model": "text-embedding-3-small"}
    }
)
09

协作

CrewAI 的核心是多 Agent 协作。Agent 之间可以委派任务、共享信息、相互审查。通过设计合理的角色和流程,可以构建高度协作的 Agent 团队。协作模式包括分工、审查改进、迭代优化等。

10

部署

CrewAI 支持多种部署方式,包括本地运行、Docker 容器和云平台部署。CrewAI+ 提供托管服务,支持 API 调用和监控。可作为 AI 后端服务集成到现有应用中。

bash
# Deploy as a FastAPI service
from fastapi import FastAPI
from crewai import Crew

app = FastAPI()
crew = Crew(agents=agents, tasks=tasks)

@app.post("/run")
async def run_crew(input: str):
    result = crew.kickoff(inputs={"topic": input})
    return {"result": result}

Deploy the crew behind a FastAPI endpoint to call it from existing apps.

11

Configuration

CrewAI is configured in Python code and via environment variables. Set LLM API keys (OPENAI_API_KEY, ANTHROPIC_API_KEY) as env vars. Each Crew takes agents, tasks, a process (sequential/hierarchical), and optional memory with an embedder config. Tools are attached per agent. A .env file is the standard place for secrets.

bash
# .env
OPENAI_API_KEY=your-key
ANTHROPIC_API_KEY=your-key

# Python config
from crewai import Crew, Process, Agent, Task

researcher = Agent(
    role='Researcher',
    goal='Analyze AI trends',
    backstory='Experienced analyst.',
    tools=[search_tool],
    llm='gpt-4o'  # or a ChatOpenAI instance
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    memory=True,
    embedder={"provider": "openai",
              "config": {"model": "text-embedding-3-small"}}
)

Use hierarchical process with a manager_llm when tasks need dynamic delegation; sequential is simpler for linear flows.

12

FAQ

Common questions cover model choice, cost, custom tools, process selection, and memory. CrewAI supports any LLM via LiteLLM (OpenAI, Anthropic, Ollama, etc.). Sequential process runs tasks in order; hierarchical uses a manager agent to delegate.

bash
Q: Which LLMs are supported?
A: Any provider supported by LiteLLM: OpenAI, Anthropic, Google, Ollama,
   and more. Set the key as an env var and pass the model name to Agent(llm=).

Q: How do I cut costs?
A: Use a smaller model for simple agents, disable memory when not needed,
   and limit verbose logging.

Q: Sequential or hierarchical process?
A: Sequential runs tasks in list order (simpler). Hierarchical uses a
   manager agent to delegate (better for dynamic, complex workflows).

Q: How do I add custom tools?
A: Subclass BaseTool and implement _run(); attach the tool to an Agent.

Q: Does memory persist across runs?
A: Long-term memory can persist across sessions using a vector store;
   short-term memory is per-crew-run.

Design roles with clear, non-overlapping goals—conflicting agent goals cause delegation thrash.

Ready to try CrewAI?

Visit the official site for the latest version and full documentation.

Visit CrewAI