Codapult's AI layer includes two production controls that most demos skip: persistent chat history and usage limits. They are designed for SaaS products where AI usage has real infrastructure cost.
Conversation memory
Conversation history is stored in two tables:
| Table | Purpose |
|---|---|
ai_chat_conversation | Conversation metadata: owner, title, timestamps |
ai_chat_message | Individual messages with role and content |
The helper module is src/lib/ai/conversations.ts. It exposes list, create, update-title, delete, read messages, and save message helpers.
API routes
| Route | Method | Purpose |
|---|---|---|
/api/ai/chat/conversations | GET | List conversations |
/api/ai/chat/conversations | POST | Create a conversation |
/api/ai/chat/conversations | DELETE | Delete a conversation |
/api/ai/chat/conversations/[id]/messages | GET | Read messages |
Quotas
AI requests are checked against the organization aiChat quota before model work starts. The default limits and their time windows are defined in src/lib/org-quotas.ts; the same access check is used by the chat, gateway, and batch API entry points. This request quota is separate from token-cost analytics and the optional daily or monthly spend budgets in the AI gateway.
When a request is allowed, token usage, provider cost, latency, and request status are recorded by the AI metering layer. This keeps cost analytics and budget checks on the same gateway path instead of hiding them inside a one-off chat counter.
Rate limiting
POST /api/ai/chat also applies a per-user request rate limit before the provider call. This protects you from accidental loops, scripted abuse, and runaway UI retries.
Product customization
Common changes:
- Increase or reduce the
aiChatrequest quota per plan. - Add per-organization override controls in the admin panel.
- Configure daily or monthly AI spend budgets when cost ceilings are required.
- Disable conversation persistence for privacy-sensitive products.