Skip to content

test(levelcode): pin the 50% gross-margin invariant#384

Closed
ndemianc wants to merge 1 commit into
developfrom
test/credit-margin-invariant
Closed

test(levelcode): pin the 50% gross-margin invariant#384
ndemianc wants to merge 1 commit into
developfrom
test/credit-margin-invariant

Conversation

@ndemianc

Copy link
Copy Markdown
Member

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_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

Spending the whole allowance on a single model — the adversarial case, not an average:

plan price credits enforced budget (cost) margin
Pro $20 2,000 $10.00 50.0%
Pro+ $40 4,000 $20.00 50.0%
Max $60 6,000 $30.00 50.0%
Ultra $100 10,000 $50.00 50.0%

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

  1. the per-plan COGS ceiling
  2. the any-model-mix worst case, checked per model rather than on an average
  3. that the credit allowance and the enforced cost budget stay two sides of one number — credits are retail (= price), the budget is cost (= price × ratio). If those drift, customers are either handed credits we don't fund, or funded for credits they can't spend.

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

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>
Copilot AI review requested due to automatic review settings July 24, 2026 01:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@ndemianc

Copy link
Copy Markdown
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.

@ndemianc ndemianc closed this Jul 24, 2026
@ndemianc
ndemianc deleted the test/credit-margin-invariant branch July 24, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants