Observability for OpenAI ChatKit Apps
OpenAI's ChatKit SDK (@openai/chatkit-react) lets you embed a fully managed AI chat widget in any React application. The Anosys integration hooks into ChatKit's lifecycle callbacks to automatically capture every conversation turn, measure performance metrics, and correlate sessions — so you can understand how users interact with your AI assistant in production.
Why Monitor Your ChatKit App
A ChatKit-powered assistant is a production AI interface. Every conversation is a user experience that generates cost, latency, and potential failure points:
| What Anosys Captures | Why It Matters |
|---|---|
| Prompts & responses | See exactly what users ask and what the model returns |
| Time-to-first-token | Measure perceived responsiveness of the chat widget |
| Response duration | End-to-end latency from user submit to full response |
| Thread & session IDs | Correlate multi-turn conversations across page views |
Architecture Overview
The integration uses a custom React hook (useChatObservability) that plugs into ChatKit's built-in callbacks:
%%{init: {"flowchart": {"curve": "linear"}}}%%
graph LR
A[User Message] -->B["ChatKit Widget (@openai/chatkit-react)"]
B -->C[OpenAI API]
C -->B
B -->D["useChatObservability Hook"]
D -->E["Anosys Platform (POST)"]
style B stroke:#1e88e5,color:#fff
style D stroke:#c62828,color:#fff
style E stroke:#388e3c,color:#fff
How it works:
- ChatKit fires
onLog,onResponseStart, andonResponseEndcallbacks during each conversation turn. - The observability hook timestamps each phase to compute time-to-first-token and response duration.
- On response end, the hook collects the prompt, response and correlation IDs for the turn.
- The hook sends one event per turn to your Anosys endpoint.
Getting Started
Prerequisites
- A React application using
@openai/chatkit-react(v1.4+) - An Anosys endpoint of type OpenTelemetry / AI Agents (create one under Collecting → Endpoints)
- A small backend route that creates ChatKit sessions (your OpenAI key must stay on the server)
Keep secrets out of the browser
ChatKit runs in the browser, so anything in your frontend bundle is public. Never put your OpenAI key or an Anosys API key in frontend code or VITE_ variables. The setup below uses your endpoint's unauthorized POST URL, which identifies the endpoint but can't read data, and creates ChatKit sessions on your server.
Step 1 — Install ChatKit
Step 2 — Add the Observability Hook
Copy your endpoint's POST URL from Collecting → Integration Options → POST with Unauthorized selected. Then create a hook that provides the lifecycle callbacks:
Step 3 — Wire It Into Your ChatKit Widget
Pass the callbacks from the hook into useChatKit:
Step 4 — Set Environment Variables
Add the following to your .env file:
Frontend (.env, public):
Backend (server only, never in the frontend):
Your /api/chatkit/session route calls POST https://api.openai.com/v1/chatkit/sessions with OPENAI_API_KEY and the workflow ID, and returns the client_secret.
What Gets Sent to Anosys
Each conversation turn produces a structured payload with the following fields:
| Field | Type | Description |
|---|---|---|
cvs1 |
String | User's prompt text |
cvs2 |
String | Assistant's response text |
cvs3 |
String | App session ID (unique per page load) |
cvs4 |
String | OpenAI thread ID |
cvn1 |
Number | Response duration (ms) |
cvn2 |
Number | Time-to-first-token (ms) |
Name these fields in Collecting → Field Mapping (for example cvn2 → time_to_first_token_ms).
Full Reference Implementation
The complete working implementation is available as an open-source demo:
Anosys-AI/AnoSys-Demo-Customer — a React + Vite application built with @openai/chatkit-react and full Anosys observability.
Key files:
src/components/ChatWidget.jsx— ChatKit widget with observability callbackssrc/hooks/useChatObservability.js— the observability hook (metrics and thread extraction)src/utils/chatHelpers.js— helper functions for message extraction and Anosys payload mapping
Next Steps
- OpenAI Agents Observability — instrument the OpenAI Python SDK for backend agents
- Claude Code Observability — SDK hook setup for Claude Code sessions
- Product analytics — track the rest of your app with the same session IDs
- Send any data — connect backend events to ChatKit behavior
- FAQ — frequently asked questions