Cost Tracking and Budget Limits for AI Operations
Introduction
This post explores the implementation of cost tracking and budget limits for AI operations within the devlog-ist/landing project. We'll examine how per-request costs are tracked in EUR, how pricing is stored per model, and how monthly cost limits are enforced per plan.
Cost Tracking in EUR
The system now tracks the cost of each AI operation in EUR. This involves:
- Recording the cost per request.
- Storing pricing information for each AI model.
This granular tracking enables accurate cost analysis and budget enforcement.
Monthly Budget Limits
Monthly cost limits are enforced based on the user's plan. Different plans have different limits:
- Free plan: EUR 0.10
- Paid plan: EUR 1.00
- Corporate plan: EUR 5.00
These limits help manage overall AI operation expenses and prevent overspending.
Gemini Model Integration
The gemini-2.5-flash-image model has been integrated into the system for LinkedIn banner generation. The system tracks the token usage of this model to accurately calculate costs.
class UsageTracker:
def __init__(self, model_name, price_per_token_eur):
self.model_name = model_name
self.price_per_token = price_per_token_eur
def calculate_cost(self, token_count):
return token_count * self.price_per_token
def log_usage(self, token_count, user_id):
cost = self.calculate_cost(token_count)
print(f"Logged {token_count} tokens for {self.model_name} (User ID: {user_id}) at a cost of {cost} EUR")
# Example usage
tracker = UsageTracker(model_name="gemini-2.5-flash-image", price_per_token_eur=0.000002) # Example pricing
tracker.log_usage(token_count=1500, user_id=123)
This Python example demonstrates how token usage can be tracked and the corresponding cost calculated for a specific AI model. A UsageTracker class is initialized with the model name and price per token. The calculate_cost method computes the cost based on token count, and the log_usage method records the usage details.
Conclusion
Implementing cost tracking and budget limits is crucial for managing AI operation expenses. By tracking costs in EUR, enforcing monthly limits, and monitoring token usage for specific models, the system provides better control over AI spending. Ensure you have clear pricing models and usage tracking in place before scaling AI-powered features.