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

# Create an AI Agent with CrewAI

> Connect a CrewAI agent to CometChat, customize it with UI Kit Builder, and ship it as React UI Kit code or a Chat Widget.

## What you’ll build

* A **CrewAI** agent exposed via a public endpoint (e.g., FastAPI `/kickoff`) that streams NDJSON.
* The same agent **connected to CometChat** (Agent ID + Deployment URL).
* A **customized chat experience** using **UI Kit Builder**.
* An export to **React UI Kit code** *or* **Chat Widget** for integration.
* A streaming contract that emits `text_start` → `text_delta` → `text_end` → `done` (plus `error`), with `message_id`/`thread_id`/`run_id`.

***

## Prerequisites

* A CometChat account and an app: **[Create App](https://app.cometchat.com/apps)**
* A CrewAI agent endpoint (Python/FastAPI sample shown below)
* Python 3.10+ (3.11 recommended) with your preferred package manager (`uv` or `pip`)
* OpenAI (or Anthropic) API key for the agent
* Optional service-specific env vars (e.g., Product Hunt token) can live in repo root `.env` or service-level `.env`.

***

## Step 1 - Create your CometChat app

<Steps>
  <Step title="Create or open an app">
    Sign in at <b><a href="https://app.cometchat.com/apps">app.cometchat.com</a></b>. Create a new app or open an existing one.
  </Step>

  <Step title="Copy credentials">
    Note your <b>App ID</b>, <b>Region</b>, and <b>Auth Key</b> (needed if you export the Chat Widget later).
  </Step>
</Steps>

***

## Step 2 - Connect your CrewAI Agent

Navigate to **AI Agent → BYO Agents** and then **Get Started / Integrate**.

<Steps>
  <Step title="Choose provider">
    Select **CrewAI**.
  </Step>

  <Step title="Basic details">
    Provide:

    <ul>
      <li><b>Name</b> and optional <b>Icon</b></li>
      <li>(Optional) <b>Greeting</b> and <b>Introductory Message</b></li>
      <li>(Optional) <b>Suggested messages</b></li>
    </ul>
  </Step>

  <Step title="CrewAI configuration">
    Paste:

    <ul>
      <li><b>Agent ID</b> — a unique handle that matches how you route traffic (e.g., <code>support</code>).</li>
      <li><b>Deployment URL</b> — the public HTTPS endpoint exposed by your CrewAI service (e.g., <code>/kickoff</code>).</li>
      <li>(Optional) <b>Headers</b> — flat JSON auth headers your FastAPI deployment expects.</li>
    </ul>
  </Step>

  <Step title="Save & enable">
    Click **Save**, then ensure the agent’s toggle is **ON** in **AI Agents** list.
  </Step>
</Steps>

> **Tip:** If you update your CrewAI agent later (prompts, tools, routing), you won’t need to re-connect it in CometChat—just keep the **Agent ID** and **Deployment URL** the same.

***

## Step 3 - Define Frontend Actions (Optional)

<Steps>
  <Step title="Add an action">
    Go to <b>AI Agent → Actions</b> and click <b>Add</b> to create a frontend action your agent can call (e.g., “Open Product,” “Start Demo,” “Book Slot”).
  </Step>

  <Step title="Define fields">
    Include:

    <ul>
      <li><b>Display Name</b> — Shown to users (e.g., “Open Product Page”).</li>
      <li><b>Execution Text</b> — How the agent describes running it (e.g., “Opening product details for the user.”).</li>
      <li><b>Name</b> — A unique, code‑friendly key (e.g., <code>open\_product</code>).</li>
      <li><b>Description</b> — What the tool does and when to use it.</li>
      <li><b>Parameters</b> — JSON Schema describing inputs (the agent will fill these).</li>
    </ul>
  </Step>

  <Step title="Validate inputs (schema)">
    Example parameters JSON:

    ```json theme={null}
    {
      "type": "object",
      "required": ["productId"],
      "properties": {
        "productId": {
          "type": "string",
          "description": "The internal product ID to open"
        },
        "utm": {
          "type": "string",
          "description": "Optional tracking code"
        }
      }
    }
    ```
  </Step>

  <Step title="Handle in your UI">
    At runtime, listen for tool calls and execute them client‑side (e.g., route changes, modals, highlights).
  </Step>
</Steps>

***

## Step 4 - Customize in UI Kit Builder

<Steps>
  <Step title="Open variant">From <b>AI Agents</b> click the variant (or Get Started) to enter UI Kit Builder.</Step>
  <Step title="Customize & Deploy">Select <b>Customize and Deploy</b>.</Step>
  <Step title="Adjust settings">Theme, layout, features; ensure the CrewAI agent is attached.</Step>
  <Step title="Preview">Use live preview to validate responses & any tool triggers.</Step>
</Steps>

<CardGroup>
  <Card title="Widget Builder" icon={<img src="/docs/images/icons/ai-agents.svg" alt="Widget" />} description="Embed / script" href="/ai-agents/chat-widget" horizontal />

  <Card title="React UI Kit" icon={<img src="/docs/images/icons/react.svg" alt="React" />} href="https://www.cometchat.com/docs/ui-kit/react/ai-assistant-chat" horizontal>Pre Built UI Components</Card>
</CardGroup>

***

## Step 5 - Export & Integrate

Choose how you’ll ship the experience (Widget or React UI Kit export).

<Steps>
  <Step title="Decide delivery mode">Pick <b>Chat Widget</b> (fastest) or export <b>React UI Kit</b> for code-level customization.</Step>
  <Step title="Widget path">Open <b>Widget Builder</b> → Get Embedded Code → copy script + credentials.</Step>
  <Step title="React UI Kit path">Export the variant as code (UI Kit) if you need deep theming or custom logic.</Step>
  <Step title="Verify agent inclusion">Preview: the CrewAI agent should appear without extra config.</Step>
</Steps>

***

## Step 6 - Deploy & Secure (Reference)

If you need a starter CrewAI endpoint, you can reuse the `/kickoff` pattern from your CrewAI project. Stream newline-delimited JSON events in this order: `text_start` → `text_delta` chunks → `text_end` → `done` (emit `error` on failures). Include `message_id` plus optional `thread_id` and `run_id` for threading.

```python theme={null}
@app.post("/kickoff")
async def kickoff(request: KickoffRequest):
  inputs = {
    "user_message": request.messages[-1].content,
    "conversation_history": "\n".join([f"{m.role}: {m.content}" for m in request.messages[:-1]]),
  }
  return StreamingResponse(stream_crew(inputs), media_type="application/x-ndjson")
```

Deploy behind HTTPS, add auth headers, and keep API keys server-side. Once live, update your CometChat agent’s **Deployment URL** to point at the public endpoint and keep the toggle **ON**.
