Observing agents built with CrewAI

CrewAI is a Python framework for creating and managing AI agents that collaborate to complete tasks. You can use CrewAI to build autonomous AI agents that work together, execute workflows, and optimize decision-making. CrewAI integrates with large language models (LLMs) and external tools, making it useful for automation, research, customer service, and more.

Instana provides observability for agents built with CrewAI, enabling real-time monitoring of AI agent workflows, task execution, and LLM performance.

Prerequisites

Make sure that the following prerequisites are met:

  • Python 3.8 or later

  • An LLM API key (Anthropic, OpenAI, or any LLM provider supported by CrewAI)

  • Instana configured for your application, see Getting started

Instrumenting CrewAI applications

To instrument your CrewAI application with Instana, complete the following steps:

  1. Install the required packages for CrewAI and OpenLLMetry.

    pip install crewai crewai-tools traceloop-sdk
  2. Set your LLM provider credentials and disable CrewAI's internal telemetry.

    export ANTHROPIC_API_KEY="<your-api-key>"
    export CREWAI_DISABLE_TELEMETRY=true
    Note:

    Replace ANTHROPIC_API_KEY with your LLM provider's environment variable if using a different provider (e.g., OPENAI_API_KEY for OpenAI).

    Disabling CrewAI's internal telemetry ensures all traces are captured through OpenLLMetry for consistent observability.

  3. Create your CrewAI application.

    In the following example, two agents are created (a researcher and a writer) to collaborate for a task:

    from crewai import Agent, Task, Crew, Process
    from crewai_tools import SerperDevTool
    from traceloop.sdk import Traceloop
    from traceloop.sdk.decorators import workflow
    
    # Initialize OpenLLMetry
    Traceloop.init(app_name="crewai_demo")
    
    # Initialize search tool
    search_tool = SerperDevTool()
    
    # Define agents
    researcher = Agent(
        role="Senior Research Analyst",
        goal="Uncover cutting-edge developments in AI and data science",
        backstory="You are a Senior Research Analyst at a leading tech think tank.",
        verbose=True,
        allow_delegation=False,
        tools=[search_tool]
    )
    
    writer = Agent(
        role="Tech Content Strategist",
        goal="Craft compelling content on tech advancements",
        backstory="You are a renowned Tech Content Strategist, known for insightful and engaging articles on technology and innovation.",
        verbose=True,
        allow_delegation=False,
        tools=[search_tool]
    )
    
    # Define tasks
    task1 = Task(
        description="Perform an in-depth analysis of the following topic: {topic}",
        expected_output="Comprehensive analysis report in bullet points",
        agent=researcher
    )
    
    task2 = Task(
        description="Using the insights from the researcher's report, develop an engaging blog post that highlights the most significant advancements",
        expected_output="A compelling 3-paragraph blog post formatted as markdown",
        agent=writer
    )
    
    # Create the crew
    crew = Crew(
        agents=[researcher, writer],
        tasks=[task1, task2],
        verbose=True,
        process=Process.sequential
    )
    
    # Wrap crew execution in a workflow for better tracing
    @workflow(name="crewai_workflow")
    def run_crew(topic):
        return crew.kickoff(inputs={"topic": topic})
    
    # Execute the crew
    if __name__ == "__main__":
        topics = ["Artificial Intelligence", "Machine Learning", "Neural Networks"]
    
        for topic in topics:
            result = run_crew(topic)
            print(f"\nCompleted analysis for: {topic}\n")
  4. Run your CrewAI application.

    python crewai_app.py

    CrewAI generates output showing the agents' collaboration:

    Figure 1. CrewAI output
    CrewAI output
  5. View data in Instana

    After running your application, data will appear in the Instana Gen AI observability dashboard:

    Figure 2. CrewAI traces in Instana
    CrewAI traces in Instana

Troubleshooting

Troubleshooting information on agents built with CrewAI.

Traces appear in crewAI-telemetry instead of the expected service name

Issue: Traces do not appear under the expected service name (e.g., crewai_demo) but are visible under crewAI-telemetry.

Cause: CrewAI's internal telemetry is still enabled.

Solution: Disable CrewAI's internal telemetry by setting the following environment variable:

export CREWAI_DISABLE_TELEMETRY=true

After setting this variable, all trace data will be captured and reported consistently through OpenLLMetry under your configured service name.