AI Service
Product Factory provides a unified AI service through the worker-ai service. It acts as a gateway between your application and multiple AI providers, handling model routing, usage tracking, and credit deduction.
Architecture
Frontend (React Island) → Gateway (/ai/*) → worker-ai → Cloudflare AI Gateway / Direct Provider API → OpenAI / Anthropic / Google / etc.All AI requests go through the gateway. The frontend never connects directly to AI providers.
Providers and Models
AI providers and models are managed through the admin console. Each provider has:
| Field | Description |
|---|---|
name | Provider identifier (e.g., “openai”, “anthropic”) |
api_key | Encrypted API key |
base_url | Provider API endpoint |
enabled | Whether the provider is active |
Models are registered per provider:
| Field | Description |
|---|---|
model_id | Model identifier (e.g., “gpt-4”, “claude-3-opus”) |
provider_id | Parent provider |
credit_cost | Credits per 1K tokens |
enabled | Whether the model is available to users |
Chat Completions
The primary AI endpoint is /ai/chat, which accepts messages and returns completions:
curl -X POST http://localhost:8787/ai/chat \ -H "Content-Type: application/json" \ -H "Cookie: better-auth.session_token=..." \ -d '{ "model": "gpt-4", "messages": [ {"role": "user", "content": "Hello"} ] }'The service handles:
- Session verification (via gateway)
- Credit preflight check
- Model routing to the correct provider
- Response streaming (when supported)
- Usage tracking and credit deduction
Usage Tracking
Every AI request is logged with:
| Field | Description |
|---|---|
user_id | Requesting user |
model_id | Model used |
prompt_tokens | Input token count |
completion_tokens | Output token count |
credit_cost | Credits deducted |
duration_ms | Request duration |
API Endpoints
See the AI API Reference for the complete endpoint documentation.