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 |
|---|---|
chat_conversation | Conversation metadata: owner, title, timestamps |
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/chat/conversations | GET | List conversations |
/api/chat/conversations | POST | Create a conversation |
/api/chat/conversations/[id]/messages | GET | Read messages |
/api/chat/conversations/[id]/messages | POST | Save messages |
Quotas
AI chat requests are checked against the aiChat usage resource before the model call. The default monthly allowance comes from DEFAULT_MONTHLY_CREDITS and plan configuration.
When a request is allowed, usage is recorded through the same usage-credit system used elsewhere in the template. This keeps AI controls aligned with SaaS billing and organization limits instead of living as a separate one-off counter.
Rate limiting
POST /api/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 monthly AI credits per plan.
- Add per-organization override controls in the admin panel.
- Track token cost by provider if you add a custom cost analytics layer.
- Disable conversation persistence for privacy-sensitive products.