This guide demonstrates how to use TrueFoundry OtelCollector along with the Traceloop SDK to instrument CrewAI agent code.
In this example, the CrewAI agent is a research agent which researches the latest trends to conduct detailed market research with keen attention to detail. For example, it can generate “A comprehensive report on AI and machine learning.”
Create Tracing Project, API Key and copy tracing code
Follow the instructions in Getting Started to create a tracing project, generate API key and copy the
tracing code.
Install Dependencies
First, you need to install the following
pip install crewai==0.102.0 traceloop-sdk==0.38.12
Add Tracing code to CrewAI application
For CrewAI agents, we need to add the Traceloop.init()
call to the application. The Traceloop SDK will automatically trace all agent activities.
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, LLM
import os
# importing traceloop sdk
from traceloop.sdk import Traceloop
load_dotenv()
# Add the traceloop init code to your application
TFY_API_KEY = os.environ.get("TFY_API_KEY")
Traceloop.init(
api_endpoint="<enter_your_api_endpoint>",
headers = {
"Authorization": f"Bearer {TFY_API_KEY}",
"TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
},
)
researcher = Agent(
role='Research Analyst',
goal='Conduct detailed market research on a topic obtained using a tool.',
backstory='Expert in market analysis with keen attention to detail',
llm=LLM(
model="gpt-4o-mini"
),
verbose=True
)
research_task = Task(
description='Conduct detailed research on a topic.',
agent=researcher,
expected_output='A comprehensive report on the selected topic',
expected_output_type=str
)
crew = Crew(
agents=[researcher],
tasks=[research_task],
verbose=True,
debug=True,
)
result = crew.kickoff()
print("Final Result:", result)
Run your application and view logged trace