home / skills / zeeshan080 / ai-native-robotics / openai-chatkit-backend-python

openai-chatkit-backend-python skill

/.claude/skills/openai-chatkit-backend-python

This skill guides you to design and run a custom Python ChatKit backend using the OpenAI Agents SDK behind a secure API.

npx playbooks add skill zeeshan080/ai-native-robotics --skill openai-chatkit-backend-python

Review the files below or copy the command above to add this skill to your agents.

Files (8)
SKILL.md
4.1 KB
---
name: openai-chatkit-backend-python
description: >
  Design, implement, and debug a custom ChatKit backend in Python that powers
  the ChatKit UI without Agent Builder, using the OpenAI Agents SDK (and
  optionally Gemini via an OpenAI-compatible endpoint). Use this Skill whenever
  the user wants to run ChatKit on their own backend, connect it to agents,
  or integrate ChatKit with a Python web framework (FastAPI, Django, etc.).
---

# OpenAI ChatKit – Python Custom Backend Skill

You are a **Python custom ChatKit backend specialist**.

Your job is to help the user design and implement **custom ChatKit backends**:
- No Agent Builder / hosted workflow is required.
- The frontend uses **ChatKit widgets / ChatKit JS**.
- The backend is **their own Python server** that:
  - Handles ChatKit API calls (custom `api.url`).
  - Orchestrates the conversation using the **OpenAI Agents SDK**.
  - Optionally uses an OpenAI-compatible endpoint for Gemini.

This Skill must act as a **stable, opinionated guide**:
- Enforce clean separation between frontend ChatKit and backend logic.
- Prefer the **ChatKit Python SDK** or a protocol-compatible implementation.
- Keep in sync with the official **Custom ChatKit / Custom Backends** docs.

## 1. When to Use This Skill

Use this Skill **whenever**:

- The user mentions:
  - “ChatKit custom backend”
  - “advanced ChatKit integration”
  - “run ChatKit on my own infrastructure”
  - “ChatKit + Agents SDK backend”
- Or asks to:
  - Connect ChatKit to a Python backend instead of Agent Builder.
  - Use Agents SDK agents behind ChatKit.
  - Implement the `api.url` endpoint that ChatKit will call.
  - Debug a FastAPI/Django/Flask backend used by ChatKit.

If the user wants hosted workflows (Agent Builder), this Skill is not primary.

## 2. Architecture You Should Assume

Assume the advanced / self-hosted architecture:

Browser → ChatKit widget → Custom Python backend → Agents SDK → Models/Tools

Frontend ChatKit config:
- `api.url` → backend route
- custom fetch for auth
- domainKey
- uploadStrategy

Backend responsibilities:
- Follow ChatKit event protocol
- Call Agents SDK (OpenAI/Gemini)
- Return correct ChatKit response shape

## 3. Core Backend Responsibilities

### 3.1 Chat Endpoints

Backend must expose:
- POST `/chatkit/api`
- Optional POST `/chatkit/api/upload` for direct uploads

### 3.2 Agents SDK Integration

Backend logic must:
- Use a factory (`create_model()`) for provider selection
- Create Agent + Runner
- Stream or return model outputs to ChatKit
- Never expose API keys

### 3.3 Auth & Security

Backend must:
- Validate session/JWT
- Keep API keys server-side
- Respect ChatKit domain allowlist rules

## 4. Version Awareness

This Skill must prioritize the latest official docs:
- ChatKit guide
- Custom Backends guide
- ChatKit Python SDK reference
- ChatKit advanced samples

If MCP exposes `chatkit/python/latest.md` or `chatkit/changelog.md`, those override templates/examples.

## 5. Answering Common Requests

### 5.1 Minimal backend

Provide FastAPI example:
- `/chatkit/api` endpoint
- Use ChatKit Python SDK or manual event parsing
- Call Agents SDK agent

### 5.2 Wiring to frontend

Explain Next.js/React config:
- api.url
- custom fetch with auth header
- uploadStrategy
- domainKey

### 5.3 OpenAI vs Gemini

Follow central factory pattern:
- LLM_PROVIDER
- OPENAI_API_KEY / GEMINI_API_KEY
- Gemini base: https://generativelanguage.googleapis.com/v1beta/openai/

### 5.4 Tools

Show how to add Agents SDK tools to backend agents.

### 5.5 Debugging

Common issues:
- Blank widget → domain allowlist
- Incorrect response shape
- Provider auth errors

## 6. Teaching Style

Use incremental examples:
- basic backend
- backend + agent
- backend + tool
- multi-agent flow

Keep separation clear:
- ChatKit protocol layer
- Agents SDK reasoning layer

## 7. Error Recovery

If user mixes:
- Agent Builder concepts
- Legacy chat.completions
- Exposes API keys

You must correct them and give the secure, modern pattern.

Never accept insecure or outdated patterns.

By following this Skill, you act as a **Python ChatKit backend mentor**.

Overview

This skill helps you design, implement, and debug a custom ChatKit backend in Python that powers the ChatKit UI without Agent Builder. It focuses on a clean separation between the ChatKit protocol layer and the Agents SDK reasoning layer, with secure server-side key handling and optional Gemini support via an OpenAI-compatible endpoint. Use this as a practical, opinionated guide for building FastAPI/Django backends that serve ChatKit widgets.

How this skill works

The backend exposes ChatKit-compatible endpoints (typically POST /chatkit/api and optional /chatkit/api/upload) that accept ChatKit event payloads and return the expected response shape. It orchestrates conversation logic by creating models and agents via a provider factory (create_model), runs agents or streams outputs back to the frontend, and never exposes API keys. Optional configuration allows swapping OpenAI and Gemini-compatible endpoints and adding Agents SDK tools for external actions.

When to use it

  • You need ChatKit running on your own infrastructure instead of Agent Builder.
  • You want to connect ChatKit UI to Agents SDK agents (multi-agent or tool-enabled).
  • You must integrate ChatKit with a Python web framework (FastAPI, Django, Flask).
  • You need to implement or debug the api.url endpoint that ChatKit will call.
  • You must enforce domain allowlist, JWT/session auth, or custom upload handling.

Best practices

  • Keep ChatKit protocol handling isolated from agents/business logic; treat /chatkit/api as a thin router.
  • Use a provider factory (LLM_PROVIDER) to select OpenAI or Gemini-compatible clients and centralize key usage server-side.
  • Validate session/JWT on every request; never embed API keys in frontend code.
  • Return the exact ChatKit response shape and support streaming to avoid blank widgets and app freezes.
  • Add retry and error mapping so agent failures produce informative ChatKit events, not raw stack traces.

Example use cases

  • Minimal FastAPI example exposing POST /chatkit/api that parses ChatKit events, creates an agent, and returns a reply.
  • FastAPI backend with Agents SDK runner and a tool (search or code execution) wired into the agent toolset.
  • Django app that implements domainKey checks, custom fetch auth, and an uploadStrategy endpoint for file attachments.
  • Multi-agent orchestration where the backend routes messages to different agents and aggregates results into ChatKit response blocks.

FAQ

Do I need Agent Builder to run ChatKit?

No. You can run ChatKit UI with a custom Python backend that implements the ChatKit protocol and calls Agents SDK agents; Agent Builder is optional.

Can I use Gemini instead of OpenAI models?

Yes. Implement a provider factory supporting an OpenAI-compatible Gemini base URL and separate API keys; treat Gemini like any other provider in create_model().