# CrewAI

[CrewAI](https://www.crewai.com/) is a framework for orchestrating role-playing AI agents. Connect it to Tensorix using the OpenAI-compatible configuration.

## Prerequisites

* Python 3.10+
* CrewAI installed
* Tensorix API key from [app.tensorix.ai](https://app.tensorix.ai)

## Installation

```bash
pip install crewai
# or with OpenAI support
pip install "crewai[openai]"
```

## Configuration

### Environment Variables

Set environment variables in your `.env` file:

```env
OPENAI_API_KEY=your-tensorix-api-key
OPENAI_BASE_URL=https://api.tensorix.ai/v1
MODEL=openai/claude-sonnet-4-20250514
```

### Direct Code Configuration

```python
from crewai import LLM

llm = LLM(
    model="openai/claude-sonnet-4-20250514",
    api_key="your-tensorix-api-key",
    base_url="https://api.tensorix.ai/v1",
    temperature=0.7,
    max_tokens=4000
)
```

## Basic Usage

### Creating Agents

```python
from crewai import Agent, LLM

# Configure Tensorix LLM
llm = LLM(
    model="openai/claude-sonnet-4-20250514",
    api_key="your-tensorix-api-key",
    base_url="https://api.tensorix.ai/v1"
)

# Create an agent
researcher = Agent(
    role="Research Analyst",
    goal="Find and summarize the latest AI trends",
    backstory="You are an expert research analyst specializing in AI technology",
    llm=llm,
    verbose=True
)
```

### Creating Tasks

```python
from crewai import Task

research_task = Task(
    description="Research the latest developments in large language models and summarize key findings",
    expected_output="A comprehensive summary of LLM developments in 2024",
    agent=researcher
)
```

### Running a Crew

```python
from crewai import Crew, Agent, Task, LLM

# Configure LLM
llm = LLM(
    model="openai/claude-sonnet-4-20250514",
    api_key="your-tensorix-api-key",
    base_url="https://api.tensorix.ai/v1"
)

# Create agents
researcher = Agent(
    role="Research Analyst",
    goal="Research and analyze information",
    backstory="Expert analyst with deep research skills",
    llm=llm
)

writer = Agent(
    role="Content Writer",
    goal="Write clear and engaging content",
    backstory="Professional writer with technical expertise",
    llm=llm
)

# Create tasks
research_task = Task(
    description="Research the benefits of AI in healthcare",
    expected_output="Key findings about AI healthcare applications",
    agent=researcher
)

writing_task = Task(
    description="Write a blog post based on the research findings",
    expected_output="A 500-word blog post about AI in healthcare",
    agent=writer
)

# Create and run crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    verbose=True
)

result = crew.kickoff()
print(result)
```

## YAML Configuration

For larger projects, use YAML configuration:

### agents.yaml

```yaml
researcher:
  role: Research Analyst
  goal: Conduct comprehensive research and analysis
  backstory: A dedicated research professional with years of experience
  verbose: true
  llm: openai/claude-sonnet-4-20250514

writer:
  role: Content Writer
  goal: Create engaging and informative content
  backstory: Expert writer with technical communication skills
  verbose: true
  llm: openai/gpt-4o
```

### tasks.yaml

```yaml
research_task:
  description: Research the specified topic thoroughly
  expected_output: Detailed research findings with citations
  agent: researcher

writing_task:
  description: Write content based on research findings
  expected_output: Well-structured article or report
  agent: writer
```

## Advanced Configuration

### Custom Parameters

```python
from crewai import LLM

llm = LLM(
    model="openai/claude-sonnet-4-20250514",
    api_key="your-tensorix-api-key",
    base_url="https://api.tensorix.ai/v1",
    temperature=0.7,
    max_tokens=4000,
    top_p=0.9,
    frequency_penalty=0.1,
    presence_penalty=0.1,
    timeout=120,
    max_retries=3,
    stream=True
)
```

### Multiple Models per Crew

```python
from crewai import Agent, LLM

# High-capability model for complex tasks
claude_llm = LLM(
    model="openai/claude-sonnet-4-20250514",
    api_key="your-tensorix-api-key",
    base_url="https://api.tensorix.ai/v1"
)

# Fast model for simple tasks
gpt_mini_llm = LLM(
    model="openai/gpt-4o-mini",
    api_key="your-tensorix-api-key",
    base_url="https://api.tensorix.ai/v1"
)

# Complex reasoning agent
analyst = Agent(
    role="Senior Analyst",
    goal="Perform deep analysis",
    backstory="Expert analyst",
    llm=claude_llm
)

# Quick response agent
assistant = Agent(
    role="Assistant",
    goal="Handle routine tasks",
    backstory="Efficient assistant",
    llm=gpt_mini_llm
)
```

## Available Models

See [Tensorix Models](/api-reference/models.md) for all available models.

| Model                               | Best For                  |
| ----------------------------------- | ------------------------- |
| `openai/claude-sonnet-4-20250514`   | Complex reasoning, agents |
| `openai/claude-3-5-sonnet-20241022` | General tasks             |
| `openai/gpt-4o`                     | Multi-modal, tool use     |
| `openai/gpt-4o-mini`                | Fast, cost-effective      |

**Note:** Prefix model names with `openai/` when using with CrewAI.

## Troubleshooting

### Connection errors

Ensure environment variables are set:

```bash
export OPENAI_API_KEY=your-tensorix-api-key
export OPENAI_BASE_URL=https://api.tensorix.ai/v1
```

### Model not found

Use the `openai/` prefix for model names:

```python
llm = LLM(model="openai/claude-sonnet-4-20250514")  # Correct
llm = LLM(model="claude-sonnet-4-20250514")         # May not work
```

### Timeout issues

Increase timeout for complex tasks:

```python
llm = LLM(
    model="openai/claude-sonnet-4-20250514",
    timeout=300  # 5 minutes
)
```

## Resources

* [CrewAI Documentation](https://docs.crewai.com/)
* [CrewAI GitHub](https://github.com/crewAIInc/crewAI)
* [Tensorix API Reference](/api-reference/overview.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tensorix.ai/developer-sdks/crewai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
