> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Tracing to CrewAI Agent

> Configure tracing for CrewAI agent workflows using the Traceloop SDK and TrueFoundry OtelCollector.

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."

<Steps>
  <Step title="Create Tracing Project, API Key and copy tracing code">
    Follow the instructions in [Getting Started](/docs/tracing/tracing-getting-started) to create a tracing project, generate API key and copy the
    tracing code.
  </Step>

  <Step title="Install Dependencies">
    First, you need to install the following

    ```shell lines theme={"dark"}
    pip install crewai==0.102.0 traceloop-sdk
    ```
  </Step>

  <Step title="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.

    ```python CrewAI Code {5-6,10-19} lines theme={"dark"}
    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>",
        app_name="your tracing application name",
        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)
    ```
  </Step>

  <Step title="Run your application and view logged trace">
    <Frame caption="">
      <img src="https://mintcdn.com/truefoundry/2MMcllD7kMlpnaWX/images/tracing_crewai.png?fit=max&auto=format&n=2MMcllD7kMlpnaWX&q=85&s=00268478a592daab44e58aff109538ed" width="3024" height="1720" data-path="images/tracing_crewai.png" />
    </Frame>
  </Step>
</Steps>
