Import AIPricingConfig from app.config.ai_pricing and AIUsageLogger from app.services.ai_usage_logger.
After making an API call, extract the usage from the response and pass it to AIUsageLogger.log_usage() with the following fields: user_id, model_name, provider, operation_type, input_tokens, output_tokens, request_metadata, and response_metadata.
After making a Firecrawl API call, extract credits_used from the response and pass it to AIUsageLogger.log_usage() with provider="firecrawl" and the firecrawl_credits field instead of token counts.
Call the OpenAI chat completions API, then log usage by passing response.usage.prompt_tokens as input_tokens and response.usage.completion_tokens as output_tokens to AIUsageLogger.log_usage().
Call the Anthropic messages API, then log usage including cached_tokens (from usage.cache_read_input_tokens if available) by passing all token counts to AIUsageLogger.log_usage().
Call the Firecrawl scrape URL method, extract credits_used from the response, and log using AIUsageLogger.log_usage() with provider="firecrawl" and firecrawl_credits=credits_used.
The AIUsageLogger automatically calculates costs using AIPricingConfig. For token-based models, it uses AIPricingConfig.calculate_cost(model, input_tokens, output_tokens). For Firecrawl, it uses AIPricingConfig.calculate_firecrawl_cost(credits_used). Platform credits are computed as int(cost_usd * 100).
Use AIUsageLogger.get_user_total_credits(user_id) to get total usage. Use AIUsageLogger.get_usage_by_date_range(user_id, start_date, end_date) to get usage for a date range. Use AIUsageLogger.get_usage_by_model(user_id, model_name) to get usage for a specific model.