Chat Billing Calculation
You are 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,plan
You are given a list of chat session records for a single month. A user may appear multiple times in the list, representing multiple chat sessions during the month.
You will implement the function calculateMonthlyBilling that takes the list of chat sessions, and returns a list of strings representing each user's total spend for that month, in the format user_id: $xx.xx.
Requirement 1: Pay as you go pricing (test cases 0–4)
Pay-as-you-go pricing (plan: "payg"):
- Input tokens are billed at $0.03 per 100 tokens
- Output tokens are billed at $0.04 per 100 tokens
Notes:
- Token counts will always be non-negative integers
- Billing is done in blocks of 100 tokens. Any partial block is not charged. For example, 0–99 tokens → 0 blocks billed, 100–199 tokens → 1 block billed, 200–299 tokens → 2 blocks billed, etc.
- A user that has no full blocks (i.e. uses less than 100 tokens per session) should still be in the output array with $0.00 as their spend
- The output array should be ordered alphabetically by user_id
Requirement 2: Fixed pricing (test cases 5–9)
Extend the function to calculate spend for the fixed monthly plan (plan: "fixed"):
- Flat fee: $15.00 per month
- Includes: 40,000 input tokens and 20,000 output tokens
- Any usage above the included tokens ("overage") is charged at the pay-as-you-go rates
Requirement 3: Plan switching (test cases 10–13)
Users are able to switch plans during a billing cycle. If a user switches plans, prorate the fixed plan fee and allowances by number of sessions. For example, if the user had 2 sessions on "pay-as-you-go", and 2 sessions on "fixed" (4 sessions total), their $15 fee and allowances for the fixed sessions would be reduced by 50%. Note: token usage blocks should be calculated per plan, and costed separately.