Coverage for app / services / plans.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1""" 

2Plan prices — what a workspace pays CHAIN-R (cost-control plan A3). 

3 

4Every figure of spend on the platform is a cost: what a model call cost the 

5platform, measured. A plan is the other side: what the workspace pays for 

6it. One price per kind of workspace, set in the deployment's settings — as 

7the platform allowances are, and for the same reason: a dedicated instance 

8per design partner carries its contract's terms in its own settings: 

9 

10``PLAN_<KIND>_FEE_USD`` 

11 The platform fee, a calendar month. 

12``PLAN_<KIND>_USAGE_MARKUP_PERCENT`` 

13 The markup on model usage, as a percentage of its cost. 

14``PLAN_<KIND>_INCLUDED_USAGE_USD`` 

15 Usage the fee includes a month, charged nothing. 

16 

17``<KIND>`` is ``PERSONAL`` (each person's Personal workspace, with their 

18personal projects) or ``ORGANISATION`` (each organisation, with every project 

19in it). **Unset is no plan**: all three empty, ``off`` or ``none``, and no 

20charges are shown anywhere. 

21 

22A month's charges (:meth:`Plan.charges`) are the fee, plus the usage past the 

23included amount at cost plus the markup — model usage at cost plus a margin, 

24the pricing in ``docs/business/go-to-market.md``. Usage is the ledger's 

25*measured* spend. A call no catalogue could price has no cost to charge on, 

26so it is left out and counted beside the charges, never guessed. Every figure 

27is in US dollars, as budgets are; an invoice in another currency converts 

28when it is written. A statement shows the charges as an estimate to invoice 

29by hand, frozen when the month closes (``statements``'s ``charges``); the 

30economics export carries each workspace's plan in force (``plans`` table), 

31where Athena measures gross margin against the bill (``gross-margin.sql``). 

32""" 

33 

34from __future__ import annotations 

35 

36from dataclasses import dataclass 

37from decimal import ROUND_HALF_UP, Decimal 

38from typing import Any 

39 

40from app.common.settings import get_settings 

41from app.services import workspace as workspace_service 

42from app.services.budgets.allowance import parse_amount 

43 

44#: The currency every figure of a plan is in. 

45CURRENCY = "USD" 

46_CENT = Decimal("0.01") 

47 

48 

49def _cents(amount: Decimal) -> Decimal: 

50 return amount.quantize(_CENT, rounding=ROUND_HALF_UP) 

51 

52 

53@dataclass(frozen=True) 

54class Charges: 

55 """One month's charges under a plan.""" 

56 

57 fee_usd: Decimal 

58 #: The month's measured model usage, at cost. 

59 usage_usd: Decimal 

60 included_usd: Decimal 

61 #: Usage past what the fee includes. 

62 usage_above_usd: Decimal 

63 markup_percent: Decimal 

64 #: ``usage_above_usd`` at cost plus the markup. 

65 usage_charge_usd: Decimal 

66 total_usd: Decimal 

67 #: Calls no catalogue could price: left out of the charges. 

68 unpriced_calls: int = 0 

69 

70 def as_json(self) -> dict[str, Any]: 

71 return { 

72 "currency": CURRENCY, 

73 "fee_usd": float(self.fee_usd), 

74 "usage_usd": float(self.usage_usd), 

75 "included_usd": float(self.included_usd), 

76 "usage_above_usd": float(self.usage_above_usd), 

77 "markup_percent": float(self.markup_percent), 

78 "usage_charge_usd": float(self.usage_charge_usd), 

79 "total_usd": float(self.total_usd), 

80 "unpriced_calls": self.unpriced_calls, 

81 } 

82 

83 

84@dataclass(frozen=True) 

85class Plan: 

86 """What one kind of workspace pays a month.""" 

87 

88 #: ``personal`` or ``organisation``. 

89 kind: str 

90 fee_usd: Decimal = Decimal(0) 

91 markup_percent: Decimal = Decimal(0) 

92 included_usd: Decimal = Decimal(0) 

93 

94 def charges(self, usage_usd: Decimal | None, unpriced_calls: int = 0) -> Charges: 

95 """The month's charges for ``usage_usd`` of measured usage.""" 

96 usage = max(usage_usd or Decimal(0), Decimal(0)) 

97 above = max(usage - self.included_usd, Decimal(0)) 

98 usage_charge = _cents(above * (1 + self.markup_percent / 100)) 

99 fee = _cents(self.fee_usd) 

100 return Charges( 

101 fee_usd=fee, 

102 usage_usd=_cents(usage), 

103 included_usd=_cents(self.included_usd), 

104 usage_above_usd=_cents(above), 

105 markup_percent=self.markup_percent, 

106 usage_charge_usd=usage_charge, 

107 total_usd=fee + usage_charge, 

108 unpriced_calls=int(unpriced_calls or 0), 

109 ) 

110 

111 def as_json(self) -> dict[str, Any]: 

112 return { 

113 "kind": self.kind, 

114 "currency": CURRENCY, 

115 "fee_usd": float(self.fee_usd), 

116 "markup_percent": float(self.markup_percent), 

117 "included_usd": float(self.included_usd), 

118 } 

119 

120 

121def for_kind(kind: str) -> Plan | None: 

122 """The plan a kind of workspace (``personal`` / ``organisation``) is on 

123 in this deployment, or None when none is set.""" 

124 settings = get_settings() 

125 prefix = "PLAN_ORGANISATION" if kind == "organisation" else "PLAN_PERSONAL" 

126 fee = parse_amount(getattr(settings, f"{prefix}_FEE_USD", "")) 

127 markup = parse_amount(getattr(settings, f"{prefix}_USAGE_MARKUP_PERCENT", "")) 

128 included = parse_amount(getattr(settings, f"{prefix}_INCLUDED_USAGE_USD", "")) 

129 if fee is None and markup is None and included is None: 

130 return None 

131 return Plan( 

132 kind="organisation" if kind == "organisation" else "personal", 

133 fee_usd=fee or Decimal(0), 

134 markup_percent=markup or Decimal(0), 

135 included_usd=included or Decimal(0), 

136 ) 

137 

138 

139def for_workspace(ws: workspace_service.Workspace | None) -> Plan | None: 

140 """The plan ``ws`` pays under: a Personal workspace's or an 

141 organisation's. A project pays through its parent, so has none of its 

142 own — its parent's statement carries the charges.""" 

143 if ws is None or ws.kind is workspace_service.WorkspaceKind.project: 

144 return None 

145 return for_kind(ws.kind.value)