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

# CrewAI

> Add persistent memory to CrewAI agents with Cognee.

Give your [CrewAI](https://www.crewai.com/) agents a shared, persistent knowledge base powered by cognee. Agents store information with `add_tool` and retrieve it via natural language with `search_tool` — memory that survives across crews and runs.

## Why Use This Integration

* **Shared crew memory**: Multiple agents read and write the same knowledge graph
* **Semantic recall**: Retrieve information using natural language queries
* **Session isolation**: Multi-tenant support with per-user/session data separation
* **Drop-in tools**: Add `add_tool` and `search_tool` to any CrewAI agent

## Installation

```bash theme={null}
pip install cognee-integration-crewai
```

<Info>
  Requires Python 3.10+. Pins `cognee>=0.4.0`, `crewai>=1.5.0`, and `crewai-tools>=1.5.0`.
</Info>

## Quick Start

Set your LLM key, then attach the tools to a CrewAI agent:

```bash theme={null}
export LLM_API_KEY="your-openai-api-key-here"   # or OPENAI_API_KEY
```

```python theme={null}
import asyncio
from dotenv import load_dotenv
import cognee
from crewai import Agent
from cognee_integration_crewai import add_tool, search_tool

load_dotenv()

async def main():
    # Optional: start from a clean knowledge base
    await cognee.prune.prune_data()
    await cognee.prune.prune_system(metadata=True)

    agent = Agent(
        role="Research Analyst",
        goal="Find and analyze information using the knowledge base",
        backstory="You are an expert analyst with access to a comprehensive knowledge base.",
        tools=[add_tool, search_tool],
        verbose=True,
    )

    # Store information
    response = agent.kickoff(
        "Remember that our company signed a contract with HealthBridge Systems "
        "in the healthcare industry, starting Feb 2023, ending Jan 2026, worth £2.4M"
    )
    print(response.raw)

    # Query the stored information
    response = agent.kickoff("What contracts do we have in the healthcare industry?")
    print(response.raw)

if __name__ == "__main__":
    asyncio.run(main())
```

## Tools

| Tool          | Signature                                                   | Description                                                  |
| ------------- | ----------------------------------------------------------- | ------------------------------------------------------------ |
| `add_tool`    | `add_tool(data: str, node_set: Optional[List[str]] = None)` | Store information in the knowledge base for later retrieval  |
| `search_tool` | `search_tool(query_text: str)`                              | Search and retrieve stored information with natural language |

<Info>
  `add_tool` queues writes and runs `cognee.cognify()` after each batch, so data is indexed before searches. Async operations have a default 120-second timeout.
</Info>

## Session Management

Use `get_sessionized_cognee_tools(session_id)` to isolate data per user or session:

```python theme={null}
from cognee_integration_crewai import get_sessionized_cognee_tools

user1_add, user1_search = get_sessionized_cognee_tools("user-123")
user2_add, user2_search = get_sessionized_cognee_tools("user-456")

agent1 = Agent(
    role="Assistant",
    goal="Help user 1",
    backstory="You are a helpful assistant.",
    tools=[user1_add, user1_search],
)
```

<Info>
  If you omit `session_id`, a UUID-based session ID is generated automatically. Session isolation is implemented by injecting the session ID into the `node_set` of `add_tool`.
</Info>

## How It Works

1. **Add Tool**: Stores data in cognee's knowledge graph with embeddings, then cognifies it
2. **Search Tool**: Retrieves relevant information via `cognee.search()`
3. **Auto-Processing**: cognee extracts entities, relationships, and context automatically
4. **Background loop**: cognee's async API runs on a dedicated background event loop with safe concurrent writes

***

<CardGroup cols={2}>
  <Card title="GitHub Repository" icon="github" href="https://github.com/topoteretes/cognee-integrations/tree/main/integrations/crewai">
    View source code and examples
  </Card>

  <Card title="Examples" icon="book" href="https://github.com/topoteretes/cognee-integrations/tree/main/integrations/crewai/examples">
    Runnable example scripts
  </Card>
</CardGroup>
