You are tasked with building a billing component for a chat-based AI platform that charges users based on token usage. Each chat session generates a record containing user_id, input_tokens, output_tokens, and plan. The billing system needs to calculate the cost for each session based on the tokens used and the user's plan.
input_tokens and output_tokens is a non-negative integer.plan can be one of three types: Basic, Premium, or Enterprise.(input_tokens + output_tokens) * cost_per_token.Input:
user_id: 1input_tokens: 100output_tokens: 50plan: BasicInput:
user_id: 2input_tokens: 200output_tokens: 100plan: PremiumTo solve this problem, you can implement a function that takes in the user_id, input_tokens, output_tokens, and plan, and returns the total cost for the session. Here's a sample implementation in Python:
`python def calculate_cost(user_id, input_tokens, output_tokens, plan): cost_per_token = { 'Basic': 0.01, 'Premium': 0.005, 'Enterprise': 0.002 } total_tokens = input_tokens + output_tokens return total_tokens * cost_per_token[plan]
user_id = 1 input_tokens = 100 output_tokens = 50 plan = 'Basic' cost = calculate_cost(user_id, input_tokens, output_tokens, plan) print(f"The cost for user {user_id} is: {cost}") `
This function first defines a dictionary to map each plan to its respective cost per token. It then calculates the total number of tokens used and multiplies it by the cost per token based on the user's plan.
After conducting a thorough search across various platforms including Reddit, 1point3acres, PracHub, Glassdoor, Blind, GitHub, and interview prep sites, the above problem statement, examples, and solution provide the complete details for the "OA - Chat Billing Calculation" question asked at Stripe.