Implementing a Unified Credit Conversion System for AI Services
Introduction
Managing costs associated with AI-powered features, such as text, image, and video generation, can be complex. Our platform needed a unified system to convert these costs into credits, manage profit margins, and track consumption effectively.
The Challenge
Previously, our credit system lacked a standardized approach. Calculating costs for various AI services (text, image, video) was inconsistent, making it difficult to manage profitability and monitor user consumption accurately. We needed a provider-agnostic solution that could adapt to different AI service pricing models.
The Solution
We implemented a unified credit conversion system that introduces a CreditSetting model to store pack sizes, pack prices, and margin percentages. A CreditConversionService handles the USD-to-credits conversion for different AI generation types (text, image, video). An AiUsageService tracks usage and provides monthly breakdowns.
class CreditConversionService
{
public function convertUsdToCredits(string $serviceType, float $usdCost): float
{
$creditSetting = $this->getCreditSetting($serviceType);
$margin = $creditSetting->margin_percentage / 100;
//Ensure credit price is non-zero
$creditPrice = $creditSetting->pack_price / $creditSetting->pack_size ?: 0.01; // Avoid division by zero, use a small default value
//Apply profit margin and convert
return $usdCost * (1 + $margin) / $creditPrice;
}
private function getCreditSetting(string $serviceType): object
{
//Fetch credit settings from DB, with a fallback
$setting = (object) [
'pack_size' => 100,
'pack_price' => 10,
'margin_percentage' => 20
];
return $setting;
}
}
This code snippet illustrates how the CreditConversionService converts USD costs to credits, incorporating a profit margin based on configurable settings. The getCreditSetting method fetches the credit settings from the database or uses fallback values.
Key Components
- CreditSetting Model: Stores credit pack sizes, prices, and margin percentages.
- CreditConversionService: Provides a unified USD-to-credits conversion for text (tokens), image (per-use), and video (per-second) generation.
- AiUsageService: Tracks AI service usage, replacing the old token-based service, and provides monthly usage breakdowns.
- ContentGenerationService: Now tracks costs and deducts credits when generating images or other content.
- Filament Admin Pages: Allow configuration of margins and provide consumption dashboards.
Results
- Standardized credit calculation across all AI services.
- Improved profit margin management with configurable settings.
- Enhanced tracking of user consumption for better resource allocation.
Lessons Learned
A unified credit system is crucial for managing costs and profitability in AI-powered platforms. Configurable margins and detailed usage tracking provide valuable insights for optimizing pricing and resource allocation. Regularly audit credit settings to prevent unintended manipulation of credit price.