Overview
TheAiUsageService manages AI diagnostic usage tracking, quota validation, and usage recording for companies. It enforces plan-based limits on queries and token consumption.
Namespace: App\Services\AiUsageService
Dependencies:
AiPlanPolicyService- Provides plan limit policiesAiTokenEstimator- Estimates token usage from character counts
Methods
validateBeforeUsage
Validates that a company can use AI diagnostics before making the request. Checks plan support, query limits, and token limits.Parameters
The company model instance requesting AI diagnostics
The subscription plan identifier (e.g., “starter”, “professional”, “enterprise”)
Estimated number of tokens for the prompt
Year-month string in “Y-m” format. Defaults to current month if null
Throws
AiUsageException with specific status codes:
blocked_plan- Plan does not support AI featuresblocked_quota- Monthly query limit exceededblocked_tokens- Monthly token limit would be exceeded
Example Usage
validateAfterUsage
Validates token usage after receiving the AI response. Ensures actual token consumption doesn’t exceed limits.Parameters
The company model instance
The subscription plan identifier
Actual total tokens consumed (prompt + response)
Year-month string in “Y-m” format. Defaults to current month if null
Throws
AiUsageException with status blocked_tokens if limit exceeded
monthlyUsage
Retrieves the current month’s AI usage statistics for a company.Parameters
The company model instance
Year-month string in “Y-m” format. Defaults to current month if null
Returns
Returns an associative array:Example Usage
registerSuccess
Records a successful AI diagnostic usage in the database.Parameters
The company model instance
The order that received the AI diagnostic
The subscription plan identifier at time of usage
Character count of the prompt sent to AI
Character count of the AI response received
Year-month string in “Y-m” format. Defaults to current month if null
Returns
Returns the createdCompanyAiUsage model instance with status 'success'
Example Usage
registerBlocked
Records a blocked or failed AI diagnostic attempt.Parameters
The company model instance
The order that attempted AI diagnostic (can be null for pre-order validation)
The subscription plan identifier
The blocking status (e.g., “blocked_plan”, “blocked_quota”, “blocked_tokens”)
Human-readable error message describing why the request was blocked
Character count of the prompt (defaults to 0)
Character count of the response (defaults to 0)
Year-month string in “Y-m” format. Defaults to current month if null
Returns
Returns the createdCompanyAiUsage model instance with the specified error status
Example Usage
Token Estimation
The service usesAiTokenEstimator to convert character counts to estimated tokens:
Usage Tracking
All usage records are stored in thecompany_ai_usage table with:
year_month- Month identifier for quota trackingstatus-'success','blocked_plan','blocked_quota', or'blocked_tokens'plan_snapshot- The plan at time of usage- Token estimates for prompt, response, and total
- Error messages for blocked attempts
status = 'success' count toward monthly limits.