跳转到主要内容

CrewAI 内置追踪

CrewAI 提供了内置的追踪功能,让您可以实时监控和调试您的 Crews 和 Flows。本指南将演示如何使用 CrewAI 的集成可观测性平台为 CrewsFlows 启用追踪。
什么是 CrewAI 追踪? CrewAI 的内置追踪为您的 AI 智能体提供了全面的可观测性,包括智能体决策、任务执行时间线、工具使用情况和 LLM 调用——所有这些都可以通过 CrewAI AMP 平台访问。
CrewAI Tracing Interface

先决条件

在使用 CrewAI 追踪之前,您需要:
  1. CrewAI AMP 账户:在 app.crewai.com 注册一个免费账户
  2. CLI 身份验证:使用 CrewAI CLI 对您的本地环境进行身份验证
crewai login

设置说明

第一步:创建您的 CrewAI AMP 账户

访问 app.crewai.com 并创建您的免费账户。这将使您能够访问 CrewAI AMP 平台,在这里您可以查看追踪、指标和管理您的 crews。

第二步:安装 CrewAI CLI 并进行身份验证

如果您还没有安装,请使用 CLI 工具安装 CrewAI
uv add crewai[tools]
然后使用您的 CrewAI AMP 账户对您的 CLI 进行身份验证
crewai login
该命令将:
  1. 在您的浏览器中打开身份验证页面
  2. 提示您输入设备代码
  3. 使用您的 CrewAI AMP 账户对您的本地环境进行身份验证
  4. 为您的本地开发启用追踪功能

第三步:在您的 Crew 中启用追踪

您可以通过将 tracing 参数设置为 True 来为您的 Crew 启用追踪
from crewai import Agent, Crew, Process, Task
from crewai_tools import SerperDevTool

# Define your agents
researcher = Agent(
    role="Senior Research Analyst",
    goal="Uncover cutting-edge developments in AI and data science",
    backstory="""You work at a leading tech think tank.
    Your expertise lies in identifying emerging trends.
    You have a knack for dissecting complex data and presenting actionable insights.""",
    verbose=True,
    tools=[SerperDevTool()],
)

writer = Agent(
    role="Tech Content Strategist",
    goal="Craft compelling content on tech advancements",
    backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
    You transform complex concepts into compelling narratives.""",
    verbose=True,
)

# Create tasks for your agents
research_task = Task(
    description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024.
    Identify key trends, breakthrough technologies, and potential industry impacts.""",
    expected_output="Full analysis report in bullet points",
    agent=researcher,
)

writing_task = Task(
    description="""Using the insights provided, develop an engaging blog
    post that highlights the most significant AI advancements.
    Your post should be informative yet accessible, catering to a tech-savvy audience.""",
    expected_output="Full blog post of at least 4 paragraphs",
    agent=writer,
)

# Enable tracing in your crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    tracing=True,  # Enable built-in tracing
    verbose=True
)

# Execute your crew
result = crew.kickoff()

第四步:在您的 Flow 中启用追踪

同样,您也可以为 CrewAI Flows 启用追踪
from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel

class ExampleState(BaseModel):
    counter: int = 0
    message: str = ""

class ExampleFlow(Flow[ExampleState]):
    def __init__(self):
        super().__init__(tracing=True)  # Enable tracing for the flow

    @start()
    def first_method(self):
        print("Starting the flow")
        self.state.counter = 1
        self.state.message = "Flow started"
        return "continue"

    @listen("continue")
    def second_method(self):
        print("Continuing the flow")
        self.state.counter += 1
        self.state.message = "Flow continued"
        return "finish"

    @listen("finish")
    def final_method(self):
        print("Finishing the flow")
        self.state.counter += 1
        self.state.message = "Flow completed"

# Create and run the flow with tracing enabled
flow = ExampleFlow(tracing=True)
result = flow.kickoff()

第五步:在 CrewAI AMP 仪表板中查看追踪

运行 crew 或 flow 后,您可以在 CrewAI AMP 仪表板中查看由您的 CrewAI 应用程序生成的追踪。您应该能看到智能体交互、工具使用和 LLM 调用的详细步骤。只需点击下面的链接即可查看追踪,或前往仪表板中的“追踪”选项卡此处 CrewAI 追踪界面

替代方案:环境变量配置

您还可以通过设置环境变量来全局启用追踪
export CREWAI_TRACING_ENABLED=true
或者将其添加到您的 .env 文件中
CREWAI_TRACING_ENABLED=true
设置此环境变量后,所有 Crews 和 Flows 都会自动启用追踪,即使没有明确设置 tracing=True

查看您的追踪

访问 CrewAI AMP 仪表板

  1. 访问 app.crewai.com 并登录您的账户
  2. 导航到您的项目仪表板
  3. 点击 Traces 选项卡以查看执行详情

您将在追踪中看到什么

CrewAI 追踪提供了对以下方面的全面可见性:
  • 智能体决策:查看智能体如何通过任务进行推理并做出决策
  • 任务执行时间线:任务序列和依赖关系的可视化表示
  • 工具使用情况:监控调用了哪些工具及其结果
  • LLM 调用:跟踪所有语言模型交互,包括提示和响应
  • 性能指标:执行时间、令牌使用量和成本
  • 错误跟踪:详细的错误信息和堆栈跟踪

追踪功能

  • 执行时间线:点击浏览执行的不同阶段
  • 详细日志:访问用于调试的全面日志
  • 性能分析:分析执行模式并优化性能
  • 导出功能:下载追踪以进行进一步分析

身份验证问题

如果您遇到身份验证问题:
  1. 确保您已登录:crewai login
  2. 检查您的互联网连接
  3. app.crewai.com 验证您的账户

追踪未显示

如果追踪未在仪表板中显示:
  1. 确认在您的 Crew/Flow 中设置了 tracing=True
  2. 如果使用环境变量,请检查是否设置了 CREWAI_TRACING_ENABLED=true
  3. 确保您已通过 crewai login 进行身份验证
  4. 验证您的 crew/flow 确实在执行