> ## 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 AG2

> Connect an AG2 (AutoGen) 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

* An **AG2 ConversableAgent** with callable tools
* 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

***

## Prerequisites

* A CometChat account and an app: **[Create App](https://app.cometchat.com/apps)**
* Python 3.10+ and `pip`
* Environment variables:
  * `OPENAI_API_KEY` (required for AG2 LLM calls)
  * `WEATHER_API_KEY` (optional — enables real weather data in the sample tool)
* Git (to clone the example project)

***

## Step 1 - Create your CometChat app

**1. Create or open an app**

Sign in at **[app.cometchat.com](https://app.cometchat.com/apps)**. Create a new app or open an existing one.

**2. Copy credentials**

Note your **App ID**, **Region**, and **Auth Key** (needed if you export the Chat Widget later).

***

## Step 2 - Connect your AG2 Agent

Navigate to **AI Agent → Get Started** and then **AI Agents → Add Agent**.

**1. Choose provider**

Select **AG2 (AutoGen)**.

**2. Basic details**

Provide:

<ul>
  <li>**Name** and optional **Icon**</li>
  <li>(Optional) **Greeting** and **Introductory Message**</li>
  <li>(Optional) **Suggested messages** such as “What’s the weather in Austin?”</li>
</ul>

**3. AG2 configuration**

Paste the following from your deployment:

<ul>
  <li>**Agent ID** — for the sample project, use `weather`.</li>
  <li>**Deployment URL** — the HTTPS endpoint that proxies to `/agent` on your server.</li>
</ul>

**4. Save & enable**

Click **Save**, then ensure the agent’s toggle is **ON** in the **AI Agents** list.

> **Tip:** Keep your Deployment URL stable (e.g., `https://your-domain.tld/agent`). Update server logic without changing the URL to avoid reconfiguration.

***

## Step 3 - Define Frontend Actions (Optional)

**1. Add an action**

Go to **AI Agent → Actions** and click **Add** to create a frontend action your agent can call (e.g., “Open Product,” “Start Demo,” “Book Slot”).

**2. Define fields**

Include:

<ul>
  <li>**Display Name** — Shown to users (e.g., “Open Product Page”).</li>
  <li>**Execution Text** — How the agent describes running it.</li>
  <li>**Name** — A unique, code-friendly key (e.g., `open_product`).</li>
  <li>**Description** — What the tool does and when to use it.</li>
  <li>**Parameters** — JSON Schema describing inputs (the agent will fill these).</li>
</ul>

**3. Validate inputs (schema)**

Example parameters JSON:

```json theme={null}
{
  "type": "object",
  "required": ["location"],
  "properties": {
    "location": {
      "type": "string",
      "description": "City, zip code, or coordinates"
    }
  }
}
```

**4. Handle in your UI**

Listen for tool calls at runtime and execute them client-side (route changes, dashboards, highlights).

***

## Step 4 - Customize in UI Kit Builder

**1. Open variant**

From **AI Agents** click the variant (or Get Started) to enter UI Kit Builder.

**2. Customize & Deploy**

Select **Customize and Deploy**.

**3. Adjust settings**

Theme, layout, features; ensure the AG2 agent is attached.

**4. Preview**

Use live preview to validate responses & tool triggers.

![](https://mintcdn.com/cometchat-22654f5b/2U5tVIzH12dbbFtr/images/ai-agent-chat-builder-preview.png?fit=max\&auto=format\&n=2U5tVIzH12dbbFtr\&q=85\&s=7e5a476c0f779f984406b979ed12f972)

***

## Step 5 - Export & Integrate

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

> [card] [Widget Builder](https://www.cometchat.com/docs/ai-agents/chat-widget.md)
>
> Embed / script

> [card] [React UI Kit](https://www.cometchat.com/docs/ui-kit/react/ai-assistant-chat.md)
>
> Pre Built UI Components

> The AG2 agent from Step 2 is included automatically in exported variants—no extra code needed for basic conversations.

**1. Decide delivery mode**

Pick **Chat Widget** (fastest) or export **React UI Kit** for code-level customization.

**2. Widget path**

Open **Widget Builder** → Get Embedded Code → copy script + credentials.

**3. React UI Kit path**

Export the variant as code (UI Kit) if you need deep theming or custom logic.

**4. Verify agent inclusion**

Preview: the AG2 agent should appear without extra config.

***

## Step 6 - Run Your AG2 Agent (Reference)

> [accordion] Clone & install dependencies
>
> <ol>
>   <li>`git clone https://github.com/cometchat/ai-agent-ag2-examples.git`</li>
>   <li>`cd ai-agent-ag2-examples/ag2-cometchat-agent`</li>
>   <li>`python -m venv .venv && source .venv/bin/activate` (or `.venv\Scripts\activate` on Windows)</li>
>   <li>`pip install -r requirements.txt`</li>
> </ol>

> [accordion] Configure environment
>
> Create a `.env` file:
>
> ```bash theme={null}
> OPENAI_API_KEY=sk-your-key
> # Optional: enables live data from OpenWeatherMap
> WEATHER_API_KEY=your-openweather-key
> ```
>
> Without `WEATHER_API_KEY`, the tool still returns stubbed error messages that the agent can surface gracefully.

> [accordion] Inspect the agent
>
> ```python theme={null}
> # agent.py (excerpt)
> llm_config = LLMConfig(
>     config_list=[{
>         "api_type": "openai",
>         "model": "gpt-4o-mini",
>         "api_key": self.openai_api_key,
>     }],
>     temperature=0.7,
> )
>
> self.agent = ConversableAgent(
>     name="WeatherAssistant",
>     system_message="You are a helpful assistant that can provide weather information...",
>     llm_config=llm_config,
>     human_input_mode="NEVER",
> )
>
> @self.agent.register_for_llm
> def get_weather(location: Annotated[str, "City name, zip code, or coordinates"]) -> Dict[str, Any]:
>     return self.get_weather(location)
> ```
>
> The agent streams Server-Sent Events (SSE) with tool call telemetry and message chunks so CometChat can render partial replies in real time.

> [accordion] Run & expose the API
>
> <ol>
>   <li>`uvicorn server:app --reload --host 0.0.0.0 --port 8000`</li>
>   <li>Verify health: `curl http://localhost:8000/health`</li>
>   <li>Trigger a message (SSE response):</li>
> </ol>
>
> ```bash theme={null}
> curl -N -X POST http://localhost:8000/agent \
>   -H "Content-Type: application/json" \
>   -d '{
>     "thread_id": "demo-thread",
>     "run_id": "demo-run",
>     "messages": [
>       { "role": "user", "content": "What is the weather in Austin?" }
>     ],
>     "tools": []
>   }'
> ```
>
> Use a tunneling tool (ngrok, Cloudflare Tunnel, etc.) to create the public **Deployment URL** CometChat needs.

> [accordion] Production checklist
>
> <ul>
>   <li>Configure logging, rate limiting, and auth (API key/JWT) on the `/agent` route.</li>
>   <li>Store secrets in server-side env vars only; never expose them in client code.</li>
>   <li>Namespace tool calls and sanitize user input before hitting external APIs.</li>
>   <li>Scale the FastAPI app behind your preferred hosting (Render, Fly.io, Vercel functions, etc.).</li>
> </ul>

***

## Test your setup

**1. Enable the agent**

In **AI Agents**, ensure your AG2 agent shows **Enabled**.

**2. Preview in UI Kit Builder**

Open **UI Kit Builder** and start a preview session.

**3. Validate conversation**

Send a message; confirm the agent responds.

**4. Test actions**

Trigger a **Frontend Action** and verify your UI handles the tool call.

***

## Troubleshooting

> [accordion] Agent not responding
>
> <ul>
>   <li>Verify your **Deployment URL** is publicly reachable (no VPN/firewall).</li>
>   <li>Check server logs for 4xx/5xx errors or missing API keys.</li>
> </ul>

> [accordion] Tool call not executed
>
> <ul>
>   <li>Confirm the Action’s **Name** matches the tool name emitted by AG2.</li>
>   <li>Ensure your agent registers tools via `register_for_llm` and proxies execution.</li>
> </ul>

> [accordion] Auth issues in exports
>
> <ul>
>   <li>Use `authKey` only for development. For production, implement a **secure token** flow for user login.</li>
> </ul>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.