Skip to content

AI Usage & Preferences

Track AI assistant usage and manage per-user preferences including provider selection and enable/disable toggle.

User Preferences

Each user can set their preferred AI provider and enable/disable the assistant for their account.

Preferences Attributes

FieldTypeDescription
idintegerPrimary key
user_idintegerForeign key to users (unique)
preferred_providerstringauto, deepseek, or openai
is_enabledbooleanEnable/disable AI for this user

Preferences Endpoints

MethodURLDescriptionPermission
GET/api/ai/preferencesGet current user's preferencescore.ai.chat
PUT/api/ai/preferencesUpdate current user's preferencescore.ai.chat
bash
# Get preferences
curl -X GET https://moon-erp.test/api/ai/preferences \
  -H "Authorization: Bearer {token}"

# Update preferences
curl -X PUT https://moon-erp.test/api/ai/preferences \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{"preferred_provider": "openai", "is_enabled": true}'

Usage Tracking

Every AI request is logged with token counts, costs, and cache status.

Usage Log Attributes

FieldTypeDescription
idintegerPrimary key
user_idintegerUser who made the request
providerstringProvider used (deepseek or openai)
modelstringModel used
input_tokensintegerInput/prompt token count
output_tokensintegerOutput/completion token count
total_tokensintegerTotal token count
cost_usddecimal(10,6)Cost in USD
cost_egpdecimal(10,4)Cost in EGP
cachedbooleanWhether response was served from cache
created_atdatetimeRequest timestamp

Usage Endpoints

MethodURLDescriptionPermission
GET/api/ai/usageCompany-wide usage reportcore.ai.usage.view
GET/api/ai/my-usageCurrent user's usagecore.ai.chat

Query Parameters (Company Usage)

ParameterTypeDescription
user_idinteger?Filter by user
providerstring?Filter by provider (deepseek, openai)
date_fromdate?Start date filter
date_todate?End date filter
monthstring?Month filter (format: YYYY-MM)
bash
# Company usage (admin)
curl -X GET "https://moon-erp.test/api/ai/usage?month=2026-03" \
  -H "Authorization: Bearer {token}"

# My usage
curl -X GET https://moon-erp.test/api/ai/my-usage \
  -H "Authorization: Bearer {token}"

Company Usage Response

json
{
  "data": {
    "totals": {
      "total_requests": 150,
      "total_tokens": 45000,
      "total_cost_usd": 0.0189,
      "total_cost_egp": 0.9545,
      "cached_requests": 42
    },
    "by_provider": [
      {"provider": "deepseek", "requests": 120, "tokens": 36000, "cost_usd": 0.0151, "cost_egp": 0.7626}
    ],
    "by_user": [
      {"user_id": 1, "user_name": "Ahmed", "requests": 80, "tokens": 24000, "cost_usd": 0.01, "cost_egp": 0.505}
    ]
  }
}

Permissions

PermissionRolesDescription
core.ai.settings.manageowner, managerView/update AI configuration
core.ai.chatall rolesUse the AI chat and manage own preferences
core.ai.usage.viewowner, managerView company-wide usage reports

Moon ERP API Documentation