# CrewAI

CrewAI provides a framework for coordinating autonomous AI agents that operate as a team. Each agent assumes a distinct role with dedicated tools and objectives, collaborating with other agents to tackle multi-step tasks. The framework handles orchestration, delegation, and communication between agents automatically.

This guide covers instrumenting CrewAI applications with InteractiveAI for visibility into agent collaboration, task execution, and model interactions.

### Prerequisites

* InteractiveAI account with API credentials
* LLM provider credentials (OpenAI, Ollama, or other supported provider)

***

### Installation

```bash
pip install interactiveai crewai openinference-instrumentation-crewai
```

***

### Enabling Trace Capture

CrewAI has a dedicated OpenInference instrumentor that captures all framework operations automatically:

```python
from openinference.instrumentation.crewai import CrewAIInstrumentor

CrewAIInstrumentor().instrument(skip_dep_check=True)
```

Once activated, agent interactions, task processing, and model calls generate spans that route to InteractiveAI.

***

### Configuration

Set your API credentials as environment variables:

```python
import os

# InteractiveAI credentials
os.environ["INTERACTIVEAI_PUBLIC_KEY"] = "pk-..."
os.environ["INTERACTIVEAI_SECRET_KEY"] = "sk-..."

# Model provider credentials
os.environ["OPENAI_API_KEY"] = "sk-proj-..."
```

Initialize the client and confirm connectivity:

```python
from interactiveai import Interactive

interactiveai = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
)

if interactiveai.auth_check():
    print("Connection established")
else:
    print("Authentication failed - verify credentials")
```

***

### Running a CrewAI Application

Here's a working example with a single agent performing a defined task:

```python
from crewai import Agent, Task, Crew
from interactiveai import Interactive
import os

interactiveai = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
)

# Define an agent with a specific role
analyst = Agent(
    role="Data Analyst",
    goal="Extract insights from data and present findings clearly",
    backstory="A seasoned analyst with expertise in identifying patterns and trends in complex datasets.",
)

# Assign a task to the agent
analysis_task = Task(
    description="Analyze the key factors that influence customer retention in SaaS businesses and summarize the top three drivers.",
    expected_output="A concise summary of the three most impactful customer retention factors",
    agent=analyst
)

# Assemble the crew
crew = Crew(
    agents=[analyst],
    tasks=[analysis_task],
)

# Execute within a traced span with polished trace output
with interactiveai.start_as_current_span(
    name="crewai-analysis-task",
    input={
        "task_description": analysis_task.description,
        "expected_output": analysis_task.expected_output,
        "agent_role": analyst.role,
    }
) as span:
    result = crew.kickoff()
    
    # Update span with structured output (polished trace)
    span.update(
        output={
            "result": result
        }
    )
    
    print(result)

interactiveai.flush()
```

***

### Trace Visibility

The InteractiveAI dashboard captures the full execution flow:

* Individual agent activations and role assignments
* Task delegation and completion status
* Model requests with prompts, completions, and token usage
* Inter-agent handoffs and collaboration patterns
* Timing data for each operation in the pipeline
