test(levelcode): pin the 50% gross-margin invariant#384
Closed
ndemianc wants to merge 1 commit into
Closed
Conversation
Answers "how do we verify the credit economics still hold the margin?" with something that runs on every change rather than a spreadsheet. The promise: model COGS stay at or below CREDIT_COGS_RATIO of subscription revenue, so gross margin is (1 - ratio) = 50% today, for ANY model mix. That holds because the budget is enforced in COST micro-$ while metering charges each model its real wire price — an expensive model burns the same dollar allowance faster, it cannot overspend it. Measured across every plan and every entitled model, spending the whole allowance on one model (the adversarial case, not an average): Pro $20 / Pro+ $40 / Max $60 / Ultra $100 -> 50.0% margin each worst single-model case on Ultra -> 50.0% (gpt-oss, kimi, opus, fable) Three specs: the per-plan COGS ceiling, the any-model-mix worst case, and that the credit allowance and the enforced cost budget stay two sides of one number (credits are retail = price; the budget is cost = price x ratio). If they ever drift, customers are either handed credits we do not fund or funded for credits they cannot spend. Denominating balances in credits was presentation only and did not move any of this — this spec is the standing proof, so a future change to pricing, the roster, the routing fee or the conversion fails here rather than in a P&L. Verified: 34 examples, 0 failures. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an RSpec “economic invariant” specification to ensure Levelcode’s credit denomination and model roster changes cannot erode the intended gross-margin target (currently driven by Levelcode::CREDIT_COGS_RATIO).
Changes:
- Adds a new spec block asserting per-plan COGS ceilings and minimum margin targets.
- Adds a “worst single-model” check across the roster to validate the invariant per model (not just on averages).
- Adds an invariant asserting plan credit allowance and enforced cost budget remain linked (retail vs cost sides of the same number).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+124
to
+132
| Levelcode::PLANS.each do |plan| | ||
| revenue = plan[:price_cents] / 100.0 | ||
| Levelcode.roster_for(plan[:key], Levelcode.budget_micros(plan[:key])).each do |m| | ||
| cogs = m[:turns_left] * Levelcode.per_turn_cost_micros(m[:id]) / 1_000_000.0 | ||
| margin = (revenue - cogs) / revenue | ||
| expect(margin).to be >= (1 - Levelcode::CREDIT_COGS_RATIO) - 0.001, | ||
| "#{plan[:name]} spent entirely on #{m[:id]}: margin #{(margin * 100).round(1)}%" | ||
| end | ||
| end |
Member
Author
|
Closing — not needed. The margin verification stands on its own (measured 50.0% across every plan and every single-model worst case); we just don't want it as a permanent CI spec. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Answers "how do we verify the credit economics still hold the 50% margin?" with something that runs on every change, rather than a spreadsheet someone re-derives each quarter.
The promise being pinned
Model COGS stay at or below
CREDIT_COGS_RATIOof subscription revenue, so gross margin is1 - ratio= 50% today — for any model mix. That holds because the budget is enforced in cost micro-$ while metering charges each model its real wire price: an expensive model burns the same dollar allowance faster, it cannot overspend it.Measured
Spending the whole allowance on a single model — the adversarial case, not an average:
Worst single-model case on Ultra — gpt-oss, kimi-k2.7, kimi-k3, codex, gpt-5.5, sonnet, opus, fable — all land at 50.0–50.4%. Never below target.
Three specs
Denominating balances in credits was presentation only and did not move any of this. This spec is the standing proof: a future change to pricing, the roster, the routing fee, or the conversion fails here rather than in a monthly P&L.
Verified: 34 examples, 0 failures.
🤖 Generated with Claude Code