Observability for CrewAI with Opik

CrewAI is a cutting-edge framework for orchestrating autonomous AI agents.

CrewAI enables you to create AI teams where each agent has specific roles, tools, and goals, working together to accomplish complex tasks.

Think of it as assembling your dream team - each member (agent) brings unique skills and expertise, collaborating seamlessly to achieve your objectives.

Opik integrates with CrewAI to log traces for all CrewAI activity, including both classic Crew/Agent/Task pipelines and the new CrewAI Flows API.

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Getting Started

Installation

First, ensure you have both opik and crewai installed:

$pip install opik crewai crewai-tools

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Configuring CrewAI

In order to configure CrewAI, you will need to have your LLM provider API key. For this example, we’ll use OpenAI. You can find or create your OpenAI API Key in this page.

You can set it as an environment variable:

$export OPENAI_API_KEY="YOUR_API_KEY"

Or set it programmatically:

1import os
2import getpass
3
4if "OPENAI_API_KEY" not in os.environ:
5 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

Logging CrewAI calls

To log a CrewAI pipeline run, you can use the track_crewai. This callback will log each CrewAI call to Opik.

Creating a CrewAI Project

The first step is to create our project. We will use an example from CrewAI’s documentation:

1from crewai import Agent, Crew, Task, Process
2
3class YourCrewName:
4 def agent_one(self) -> Agent:
5 return Agent(
6 role="Data Analyst",
7 goal="Analyze data trends in the market",
8 backstory="An experienced data analyst with a background in economics",
9 verbose=True,
10 )
11
12 def agent_two(self) -> Agent:
13 return Agent(
14 role="Market Researcher",
15 goal="Gather information on market dynamics",
16 backstory="A diligent researcher with a keen eye for detail",
17 verbose=True,
18 )
19
20 def task_one(self) -> Task:
21 return Task(
22 name="Collect Data Task",
23 description="Collect recent market data and identify trends.",
24 expected_output="A report summarizing key trends in the market.",
25 agent=self.agent_one(),
26 )
27
28 def task_two(self) -> Task:
29 return Task(
30 name="Market Research Task",
31 description="Research factors affecting market dynamics.",
32 expected_output="An analysis of factors influencing the market.",
33 agent=self.agent_two(),
34 )
35
36 def crew(self) -> Crew:
37 return Crew(
38 agents=[self.agent_one(), self.agent_two()],
39 tasks=[self.task_one(), self.task_two()],
40 process=Process.sequential,
41 verbose=True,
42 )

Running with Opik Tracking

Now we can import Opik’s tracker and run our crew:

1from opik.integrations.crewai import track_crewai
2
3track_crewai(project_name="crewai-integration-demo")
4
5my_crew = YourCrewName().crew()
6result = my_crew.kickoff()
7
8print(result)

Each run will now be logged to the Opik platform.

Logging CrewAI Flows

Opik also supports the CrewAI Flows API. When you enable tracking with track_crewai, Opik automatically:

  • Tracks Flow.kickoff() and Flow.kickoff_async() as the root span/trace with inputs and outputs
  • Tracks flow step methods decorated with @start and @listen as nested spans
  • Captures any LLM calls (via LiteLLM) within those steps with token usage
  • Flow methods are compatible with other Opik integrations (e.g., OpenAI, Anthropic, LangChain) and the @opik.track decorator. Any spans created inside flow steps are correctly attached to the flow’s span tree.

Example:

1from crewai.flow.flow import Flow, start, listen
2from opik.integrations.crewai import track_crewai
3
4track_crewai(project_name="crewai-integration-demo")
5
6class ExampleFlow(Flow):
7 model = "gpt-4o-mini"
8
9 @start()
10 def generate_city(self):
11 from litellm import completion
12 response = completion(
13 model=self.model,
14 messages=[{"role": "user", "content": "Return the name of a random city."}],
15 )
16 return response["choices"][0]["message"]["content"]
17
18 @listen(generate_city)
19 def generate_fun_fact(self, random_city):
20 from litellm import completion
21 response = completion(
22 model=self.model,
23 messages=[{"role": "user", "content": f"Tell me a fun fact about {random_city}"}],
24 )
25 return response["choices"][0]["message"]["content"]
26
27flow = ExampleFlow()
28result = flow.kickoff()

Cost Tracking

The track_crewai integration automatically tracks token usage and cost for all supported LLM models used during CrewAI agent execution.

Cost information is automatically captured and displayed in the Opik UI, including:

  • Token usage details
  • Cost per request based on model pricing
  • Total trace cost

View the complete list of supported models and providers on the Supported Models page.