runtime
AI Platform Economics · milestone 04 / 04

milestone 4/4 AI usage cost & contribution margin

Compare usage revenue with costs to calculate contribution margin.

AI Platform Economics · Calculate success rates and usage costs, prevent join fan-out, and reconcile split payments with refunds.

approach

Usage cost equals input tokens × input rate plus output tokens × output rate, divided by one million. These rates are fictional. Under this exercise’s rule, successful requests are chargeable; errors are excluded. Compare January billed revenue to January successful usage cost, not all-time billing.

worked-example.sql
SELECT
  r.model,
  ROUND(SUM(r.input_tokens*m.input_per_million+ r.output_tokens*m.output_per_million)/1000000.0,6) AS usage_cost
FROM ai_requests r
JOIN model_rates m ON m.model=r.model
WHERE r.status='success'
GROUP BY r.model;

data for this milestone

ai_requests7 columns · 12 rows
idinteger · keyaccount_idintegermodeltextrequested_attextinput_tokensintegeroutput_tokensintegerstatustext
11swift2025-01-01 10:00:001000200success
21reason2025-01-02 11:00:002000600success
32swift2025-01-02 12:00:005000error
43reason2025-01-02 13:00:0080002000success
53swift2025-01-03 14:00:0040001000success

showing 5 of 12 rows

model_rates3 columns · 2 rows
modeltext · keyinput_per_millionrealoutput_per_millionreal
swift14
reason312

all 2 rows

ai_accounts4 columns · 6 rows
idinteger · keyplantextregiontextsigned_up_attext
1proUS2025-01-01
2freeUS2025-01-02
3teamEU2025-01-02
4proIN2025-01-03
5freeEU2025-01-04

showing 5 of 6 rows

invoices4 columns · 6 rows
idinteger · keyaccount_idintegerperiodtextbilled_amountreal
112025-0120
232025-0180
342025-0120
462025-0180
512025-0220

showing 5 of 6 rows

challenge 1

Return model and usage_cost in fictional USD, rounded to 6 decimals, for successful requests across all dates.

Return modelusage_costany row order

challenge-1.sql
 
3 steps left · check both queries and pass the concept checkOpen the report
AI usage cost & contribution margin — AI Platform Economics · Runtime