> ## Documentation Index
> Fetch the complete documentation index at: https://agentcontrol-abhi-agent-control-auth-contract-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# CrewAI Integration

> Combine Agent Control security with CrewAI Guardrails for production customer support.

Combines Agent Control (security and compliance) with CrewAI Guardrails (quality retries) for production customer support.

## What It Does

* Agent Control (security): PRE, POST, and FINAL blocks unauthorized access and PII.
* CrewAI Guardrails (quality): validates length, structure, and tone with up to three retries.

## Prerequisites

Before running this example, ensure you have:

* Python 3.12+
* uv (fast Python package manager)
* Docker (for PostgreSQL required by Agent Control server)

## Installation

### 1. Install Monorepo Dependencies

From the monorepo root, install all workspace packages:

```bash theme={null}
cd /path/to/agent-control
make sync
```

This installs the Agent Control SDK and all workspace packages in editable mode.

### 2. Install CrewAI Example Dependencies

Navigate to the CrewAI example and install its specific dependencies:

```bash theme={null}
cd examples/crewai
uv pip install -e . --upgrade
```

### 3. Set OpenAI API Key

Create a `.env` file or export the environment variable:

```bash theme={null}
export OPENAI_API_KEY="your-key-here"
```

### 4. Start the Agent Control Server

In a separate terminal, start the server from the monorepo root:

```bash theme={null}
cd /path/to/agent-control
make server-run
```

Verify the server is running:

```bash theme={null}
curl http://localhost:8000/health
```

### 5. Setup Content Controls (One-Time)

From the `examples/crewai` directory, run the setup script:

```bash theme={null}
uv run python setup_content_controls.py
```

## Running the Example

Make sure you're in the `examples/crewai` directory and run:

```bash theme={null}
uv run python content_agent_protection.py
```

## Expected Behavior

| Scenario                      | Layer               | Result          |
| ----------------------------- | ------------------- | --------------- |
| Unauthorized access           | Agent Control PRE   | Blocked         |
| PII in tool output            | Agent Control POST  | Blocked         |
| Short or low-quality response | Guardrails          | Retry then pass |
| Agent bypass attempt          | Agent Control FINAL | Blocked         |

## Output Legend

* PRE checks input before the LLM
* POST checks tool output for PII
* FINAL checks the crew’s final response
* Agent Control blocks immediately (no retries), violations are logged
* Guardrails retry with feedback (quality only)

## Agent Control and CrewAI Integration

Agent Control works with CrewAI’s orchestration:

1. CrewAI agent layer: plans tasks, selects tools, manages conversation flow
2. Agent Control layer: enforces controls and business rules at tool boundaries

```text theme={null}
User Request
    ↓
CrewAI Agent (planning & orchestration)
    ↓
Decides to call tool
    ↓
@control() decorator (PRE-execution)  ← LAYER 1: Validates input
    ↓
Tool executes (LLM generation)
    ↓
@control() decorator (POST-execution) ← LAYER 2: Validates tool output
    ↓
If blocked, agent may generate own response
    ↓
Final Output Validation              ← LAYER 3: Validates crew output (bypass protection)
    ↓
Return to user (or block if control violated)
```

## Source Code

View the complete example with all scripts and setup instructions:

[`CrewAI Integration Example`](https://github.com/agentcontrol/agent-control/tree/main/examples/crewai)
