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 · key | account_idinteger | modeltext | requested_attext | input_tokensinteger | output_tokensinteger | statustext |
|---|---|---|---|---|---|---|
| 1 | 1 | swift | 2025-01-01 10:00:00 | 1000 | 200 | success |
| 2 | 1 | reason | 2025-01-02 11:00:00 | 2000 | 600 | success |
| 3 | 2 | swift | 2025-01-02 12:00:00 | 500 | 0 | error |
| 4 | 3 | reason | 2025-01-02 13:00:00 | 8000 | 2000 | success |
| 5 | 3 | swift | 2025-01-03 14:00:00 | 4000 | 1000 | success |
showing 5 of 12 rows
model_rates3 columns · 2 rows
| modeltext · key | input_per_millionreal | output_per_millionreal |
|---|---|---|
| swift | 1 | 4 |
| reason | 3 | 12 |
all 2 rows
ai_accounts4 columns · 6 rows
| idinteger · key | plantext | regiontext | signed_up_attext |
|---|---|---|---|
| 1 | pro | US | 2025-01-01 |
| 2 | free | US | 2025-01-02 |
| 3 | team | EU | 2025-01-02 |
| 4 | pro | IN | 2025-01-03 |
| 5 | free | EU | 2025-01-04 |
showing 5 of 6 rows
invoices4 columns · 6 rows
| idinteger · key | account_idinteger | periodtext | billed_amountreal |
|---|---|---|---|
| 1 | 1 | 2025-01 | 20 |
| 2 | 3 | 2025-01 | 80 |
| 3 | 4 | 2025-01 | 20 |
| 4 | 6 | 2025-01 | 80 |
| 5 | 1 | 2025-02 | 20 |
showing 5 of 6 rows