"""Model price drop stress test: gross margin and gross profit after a fall in LLM prices, when you keep the saving and when your own prices fall in step. Inputs: monthly_revenue (0 to 1e12 USD), monthly_ai_cost (0 to 1e12 USD, part of cost of revenue, at most revenue x (1 - margin)), gross_margin_percent_today (0 to 100), price_drop_scenarios (1 to 10 drops, each 0 to 100 percent, default [10, 30, 50]). Returns todayGrossProfit and todayNonAiCost (USD a month), and one list entry per drop: newAiCost, newGrossMarginKeepSavings, newGrossMarginPassThrough (percent), newRevenueIfPassThrough, newGrossProfitKeepSavings, newGrossProfitPassThrough (USD a month). All rounded to 2 decimals. mysi, Model price drop stress test, mysi.dev/model-price-drop-stress-test, version 1, changed 2026-12-04. MIT. """ import math def _model_price_drop_stress_test_num(name, value, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high: raise ValueError(f"{name} must be a number from {low} to {high}") return float(value) def _model_price_drop_stress_test_r2(x): return math.floor(x * 100 + 0.5) / 100 # cents, halves rounded up def model_price_drop_stress_test(monthly_revenue=100000, monthly_ai_cost=15000, gross_margin_percent_today=60, price_drop_scenarios=(10, 30, 50)): num, r2 = _model_price_drop_stress_test_num, _model_price_drop_stress_test_r2 rev = num("monthly_revenue", monthly_revenue, 0, 1e12) ai = num("monthly_ai_cost", monthly_ai_cost, 0, 1e12) margin = num("gross_margin_percent_today", gross_margin_percent_today, 0, 100) drops = price_drop_scenarios if not isinstance(drops, (list, tuple)) or not 1 <= len(drops) <= 10: raise ValueError("price_drop_scenarios must be a list of 1 to 10 numbers from 0 to 100") drops = [num("price_drop_scenarios", d, 0, 100) for d in drops] profit = rev * (margin / 100) cogs = rev - profit if ai > cogs + 0.005: raise ValueError(f"monthly_ai_cost must be at most the cost of revenue, {cogs:.2f}") non_ai = max(0.0, cogs - ai) share = ai / cogs if cogs > 0 else 0.0 out = {"todayGrossProfit": r2(profit), "todayNonAiCost": r2(non_ai)} keys = ["newAiCost", "newGrossMarginKeepSavings", "newGrossMarginPassThrough", "newRevenueIfPassThrough", "newGrossProfitKeepSavings", "newGrossProfitPassThrough"] for k in keys: out[k] = [] for d in drops: new_ai = ai * (1 - d / 100) new_cogs = non_ai + new_ai keep_margin = (rev - new_cogs) / rev * 100 if rev > 0 else 0.0 pass_rev = max(0.0, rev - rev * share * (d / 100)) # prices fall by the cost saving's share pass_margin = (pass_rev - new_cogs) / pass_rev * 100 if pass_rev > 0 else 0.0 row = [new_ai, keep_margin, pass_margin, pass_rev, rev - new_cogs, pass_rev - new_cogs] for k, v in zip(keys, row): out[k].append(r2(v)) return out MODEL_PRICE_DROP_STRESS_TEST_CASES = [ ({"monthly_revenue": 100000, "monthly_ai_cost": 15000, "gross_margin_percent_today": 60, "price_drop_scenarios": [10, 30, 50]}, {"todayGrossProfit": 60000, "newGrossMarginKeepSavings": [61.5, 64.5, 67.5], "newGrossProfitPassThrough": [57750, 53250, 48750]}), ({"monthly_revenue": 10000, "monthly_ai_cost": 2000, "gross_margin_percent_today": 50, "price_drop_scenarios": [50]}, {"todayNonAiCost": 3000, "newGrossProfitKeepSavings": [6000], "newRevenueIfPassThrough": [8000], "newGrossProfitPassThrough": [4000]}), ({"monthly_revenue": 200000, "monthly_ai_cost": 30000, "gross_margin_percent_today": 80, "price_drop_scenarios": [20, 100]}, {"newGrossMarginKeepSavings": [83, 95], "newRevenueIfPassThrough": [170000, 50000], "newGrossMarginPassThrough": [80, 80]}), ] def model_price_drop_stress_test_check(tolerance=1e-3): for given, want in MODEL_PRICE_DROP_STRESS_TEST_CASES: got = model_price_drop_stress_test(**given) for name, value in want.items(): pairs = zip(got[name], value) if isinstance(value, list) else [(got[name], value)] for g, v in pairs: assert abs(g - v) <= tolerance * max(1, abs(v)), (name, got[name], value) return "ok" if __name__ == "__main__": import json, sys model_price_drop_stress_test_check() given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a) js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null") given = {k: json.loads(v) if js(v) else v for k, v in given.items()} if given: print(json.dumps(model_price_drop_stress_test(**given)))