Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 1 | # Billing operations: costs, margin and prices |
| 2 | ||
| 3 | How g1t checks what it charges against what Cloudflare charges it, keeps | |
| 4 | prices at cost plus 20%, and tells staff when the margin slips. Internal. | |
| 5 | Code: `services/billing/src/costs.rs` (reading the bill), `margin.rs` | |
| 6 | (reconciliation, drift, alerts), `pricing.rs` (versions, proposals, | |
| Spend caps: a monthly budget for comped workspaces and a daily breaker on what g1t pays | 7 | notice), `keeper.rs` (sandbox and Workers for Platforms measurements), |
| 8 | `budget.rs` (what g1t pays for itself, and its caps; see | |
| 9 | [Spend caps](#spend-caps)). | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 10 | Page: sudo **Costs & margin** (`/costs`). |
| 11 | ||
| 12 | Several Cloudflare products g1t runs on are new. Artifacts bills | |
| 13 | "operations" from 2026-10-14 without defining them (see | |
| 14 | [ARTIFACTS.md](ARTIFACTS.md), M1). So nothing here hard-codes a product | |
| 15 | list or a unit: every line Cloudflare bills is kept, and how a line maps | |
| 16 | to what g1t sells is data you change from sudo, without a deploy. | |
| 17 | ||
| 18 | ## Data sources | |
| 19 | ||
| 20 | | Source | What | Where it lands | | |
| 21 | | --- | --- | --- | | |
| 22 | | Billable usage, `GET /accounts/{account}/billable-usage?from=&to=` | One row per service per day in FOCUS columns: `ServiceFamilyName`, `ServiceName`, `ChargePeriodStart`, `PricingQuantity`, `ContractedCost` / `BilledCost` / `ListCost`. Every product g1t uses appears once it is used: Workers, Workers for Platforms, D1, KV, R2, Queues, Containers, Durable Objects, Artifacts, Browser Rendering, Workers AI, Vectorize, Cloudflare for SaaS, Email. Inside an included amount the cost is 0. | `cost_lines`, source `billable_usage` | | |
| Costs: Cloudflare's count for a pull request's working copy is shared out to its repository's workspace (repos pull_owners) | 23 | | GraphQL `artifactsEventsAdaptiveGroups` | Artifacts' own count by `date`, `eventType` and `repositoryName`. Operations are `create`, `fork`, `push`, `pull`, `delete`; errors (`rateLimited`, `serverError`, …) are kept but not counted. | `cost_lines`, source `artifacts_events`; per workspace (from the store key `<workspace>--<repo>`; a pull request's working copy, `pulls--<id>`, is its repository's workspace's, from repos' `pull_owners`) in `own_counts` as `cloudflare_git` | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 24 | | GraphQL `aiGatewayRequestsAdaptiveGroups`, filtered to `AI_GATEWAY_ID` | What AI Gateway priced g1t's own provider traffic at, by `date`, `provider`, `model` and `wholesale`: `count`, `sum.cost` (dollars), `sum.tokensIn`/`tokensOut`/`cacheReadTokens`/`cacheWriteTokens`. Field names checked against Cloudflare's schema (introspection of `AccountAiGatewayRequestsAdaptiveGroups{Sum,Dimensions,Filter_InputObject}`). An adaptive (sampled) dataset: an estimate, close at g1t's volumes. Only g1t's hosted models go through this gateway: a workspace's own provider is called at its own address, never here. | `cost_lines`, source `ai_gateway`, product `ai_gateway_requests`: per day and model a line `<provider>_<model>` (requests, at the gateway's cost), and at no cost `…__tokens`, `…__cache_read_tokens`, `…__cache_write_tokens`; Cloudflare-billed (unified billing) requests are prefixed `wholesale__`. Mapped to `models` (migration 0036). A re-read day replaces all its gateway lines. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 25 | | The ledger | Every charge: its cost at the price book's cost, what it was charged at price, what paid for it. | read, never written | |
| 26 | | `pending_usage` | Month-end meters (git, storage, scans, embeddings, the cache) as they stand. | snapshotted daily into `pending_days` | | |
| 27 | | `plan_payments` | The plan's $20. | read | | |
| One operation mapping, owned by repos; billing reads it instead of keeping its own | 28 | | repos `git_operations` | Operations customers are charged for, per workspace, counted by repos through its `operation_mapping`. | `own_counts` meter `git_operations` | |
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 29 | | Subscriptions, `GET /accounts/{account}/subscriptions` | What g1t pays each month whatever it uses (Workers Paid, add-ons): each subscription that is paid, trialing or awaiting payment, at its price over its frequency. Not on the billable-usage bill. Read in the daily run with the bill's token; a failure is logged and the last read stays. | `cf_subscriptions` (one row) | |
| One operation mapping, owned by repos; billing reads it instead of keeping its own | 30 | | repos `artifacts_usage` | Every raw meter of the git store (`git.fetch`, `git.receive_pack`, `binding.*`, …) per day and workspace, with repos' `operation_mapping`. | `own_counts` meters `artifacts_<raw meter>`, and `cost_operations` (raw counts × the mapping's `cost_operations`: what g1t expects Cloudflare to bill) | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 31 | |
| 32 | A meter's slug is Cloudflare's name lower-cased with words joined by `_` | |
| 33 | and the "(First … included)" note dropped: `Workers for Platforms CPU ms | |
| 34 | (First 60M ms are included)` under `Workers` is product `workers`, meter | |
| 35 | `workers_for_platforms_cpu_ms`. Several rows of the same day and meter | |
| 36 | (regions, tiers) are added together before they are stored. | |
| 37 | ||
| 38 | ## Credentials | |
| 39 | ||
| 40 | | Secret on g1t-billing | Permissions | Used for | | |
| 41 | | --- | --- | --- | | |
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 42 | | `CLOUDFLARE_BILLING_TOKEN` (optional) | Account: **Billing Read**, Account: **Account Analytics Read**, for the g1t account only | Reading the bill, the Artifacts events and the subscriptions | |
| Billing: AI Gateway's analytics are read with the token that can see them, and a gateway that priced nothing is said | 43 | | `CLOUDFLARE_USAGE_TOKEN` (exists) | Billing Read, Account Analytics Read, AI Gateway Read | The keeper (settling runs from the gateway's logs); also the bill when `CLOUDFLARE_BILLING_TOKEN` is not set. AI Gateway's analytics are read with this token first and, if that is refused, with the bill's: Cloudflare answers a token without AI Gateway Read with no rows rather than an error, so the bill's token would read as a gateway that priced nothing | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 44 | |
| 45 | With neither, the daily run reconciles only what g1t counted itself, and | |
| 46 | the page says the bill cannot be read. Nothing fails. To set the scoped one: | |
| 47 | ||
| 48 | 1. Cloudflare dashboard → My Profile → API Tokens → Create Token → Custom token. | |
| 49 | 2. Permissions: Account · Billing · Read; Account · Account Analytics · Read. | |
| 50 | 3. Account resources: Include · the g1t account. No zone permissions. | |
| 51 | 4. `cd services/billing && npx wrangler secret put CLOUDFLARE_BILLING_TOKEN`. | |
| sudo: the costs run button is named for what it does, the whole nightly analysis | 52 | 5. In sudo, Costs & margin → **Run the analysis now**. |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 53 | |
| 54 | Alerts are emailed through the `EMAIL` binding (Cloudflare Email Sending) | |
| 55 | to `COSTS_ALERT_EMAIL` (`hey@flagon.io`). An empty value sends none. | |
| 56 | ||
| 57 | ## Schedule | |
| 58 | ||
| 59 | The daily cron (`17 4 * * *`, `keeper::DAILY`) runs, in order: | |
| 60 | ||
| 61 | 1. The keeper's measurements (sandbox seconds, app requests and CPU), each | |
| 62 | a proposal now, not a direct change. | |
| 63 | 2. `costs_daily`: | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 64 | 1. Read the bill, the Artifacts events and AI Gateway's analytics. The first run reads the last |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 65 | 31 days (GraphQL keeps 31); later runs the last 4, since Cloudflare |
| 66 | restates recent days, or back to the last day read after a gap. | |
| 67 | Lines are upserted on `(day, source, product, meter)`, so a re-read | |
| 68 | replaces, never adds. | |
| 69 | 2. g1t's own counts for the same days (replaced per day). | |
| 70 | 3. Snapshot `pending_usage` into `pending_days`. | |
| sudo: the costs run button is named for what it does, the whole nightly analysis | 71 | 4. Reconcile the last 31 days (further back after a gap) into |
| 72 | `margin_days` and `workspace_costs` | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 73 | (replaced per day). |
| 74 | 5. Drift over the last 7 days into `cost_drift`. | |
| 75 | 6. Unit costs over the last 30 days, proposed to the price book. | |
| 76 | 7. Apply price versions whose date has come. | |
| 77 | 8. Open, update and close margin alerts; email new ones. | |
| 78 | 9. Email owners on the plan about rises to come. | |
| 79 | ||
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 80 | **Run the analysis now** at the top of sudo's Costs & margin and Bill & pricing pages |
| sudo: the costs run button is named for what it does, the whole nightly analysis | 81 | (`admin_run_costs`) runs all of step 2 at once, alerts included, with no |
| 82 | need to wait for 04:17 UTC. Running it twice is safe: every step replaces | |
| 83 | what it wrote. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 84 | |
| 85 | ## Reconciliation math | |
| 86 | ||
| 87 | Every Cloudflare line goes to one of g1t's products ("buckets") by | |
| 88 | `cost_map`: the row for its product with the longest matching meter | |
| 89 | prefix, `*` last. A line no row claims goes to `unmapped`. | |
| 90 | ||
| 91 | | Bucket | Cloudflare | Paid for by (`revenue_map`) | | |
| 92 | | --- | --- | --- | | |
| 93 | | `sandboxes` | Containers, Durable Objects compute duration | `sandbox`, `self_hosted`, `builds` | | |
| 94 | | `deployments` | Workers for Platforms | `deployments` | | |
| 95 | | `git` | Artifacts operations (and its events, as counts) | `git` | | |
| 96 | | `repo_storage` | Artifacts storage | `storage` | | |
| 97 | | `actions_cache` | R2 | `cache` | | |
| 98 | | `embeddings` | Workers AI, Vectorize | `context` | | |
| 99 | | `security` | (Workers CPU, under `platform`) | `security` | | |
| 100 | | `domains` | Cloudflare for SaaS | `domains` | | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 101 | | `models` | not Cloudflare: AI Gateway's settled cost on the ledger; AI Gateway's own daily total (`ai_gateway_requests`) beside it, to check it | every other task (agent runs) | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 102 | | `platform` | Workers, D1, KV, Queues, Email, Browser Rendering, other Durable Objects | the plan's price | |
| 103 | ||
| 104 | For each day and bucket: | |
| 105 | ||
| Costs: a statement that keeps usage sold, running g1t, subscriptions and what was given away (comped, free use, trial, pool) apart, and says who was paid; free use carries its own cost; the run button says it is running | 106 | - **Cloudflare cost** = Σ the bucket's lines' cost, as billed: after the |
| 107 | included allowances, so a month inside them costs $0 here as on | |
| 108 | Cloudflare's Billable usage page. `models` uses the ledger's cost of the | |
| 109 | tokens instead; that is paid to the model providers and is not on | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 110 | Cloudflare's bill. Its "Cloudflare" column is what AI Gateway priced the |
| 111 | same traffic at, which drift compares with the ledger (below); it is | |
| 112 | never added to the cost. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 113 | - **Own cost** = Σ the ledger's `cost_micros` for the bucket's keys (the |
| 114 | price book's cost when charged), plus month-end deltas. A workspace's own | |
| 115 | model provider is no cost to g1t. | |
| 116 | - **Value** = what customers were charged at price: `-amount_micros` plus | |
| 117 | what the plan's included usage, a trial, the open-source pool or g1t paid. | |
| 118 | g1t's own (comped) workspaces are valued at cost plus the margin. | |
| 119 | - **Cash** = what workspaces paid: `-amount_micros`, and the plan's price. | |
| Merge Stripe Tax, the card fee on card payments, and one free workspace per person | 120 | Never tax or card fees: a payment credits the balance, and |
| 121 | `plan_payments`, without them (see [Tax and the card fee](#tax-and-the-card-fee)). | |
| Costs: margin is measured on what was sold; comped workspaces, free periods, the trial and the pools are given away, a budget shown beside it | 122 | - **Given away** = the part of the cost that went on usage g1t paid for |
| Costs: a statement that keeps usage sold, running g1t, subscriptions and what was given away (comped, free use, trial, pool) apart, and says who was paid; free use carries its own cost; the run button says it is running | 123 | itself on purpose, by why: |
| 124 | - **comped**: all of a comped workspace's cost, every bucket; | |
| 125 | - **free use**: a free period's usage, the overruns g1t covered | |
| 126 | (`ledger.given_micros`), and all of a workspace's cost on a day it had | |
| 127 | nothing priced (free allowances); | |
| 128 | - **trial** and **open-source pool**: what `trial_micros` and | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 129 | `oss_micros` paid; |
| 130 | - **discount**: what a discount on an account's custom terms took below | |
| 131 | cost plus the margin (`ledger.discount_micros`, see | |
| 132 | [Margin floor](#margin-floor)). The usage is valued at its price, so a | |
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 133 | discounted sale never reads as margin lost; |
| 134 | - **promotional credit** and **goodwill credit**: what credit staff gave | |
| 135 | paid for, when it is spent (`given_credit_promotional_micros`, | |
| 136 | `given_credit_goodwill_micros`, migration 0038). That usage's charge is | |
| 137 | taken out of cash, so it is never money in. A refund is not here: see | |
| 138 | [Credits from g1t](#credits-from-g1t). | |
| Costs: a statement that keeps usage sold, running g1t, subscriptions and what was given away (comped, free use, trial, pool) apart, and says who was paid; free use carries its own cost; the run button says it is running | 139 | |
| 140 | Otherwise a workspace's day is split by those shares of its value at | |
| 141 | price, and the same shares of each of its buckets' cost are given, its | |
| 142 | part of running g1t included. The Team plan's included usage is sold: | |
| 143 | the plan's price paid for it. Stored on `margin_days` (`given_micros` | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 144 | and `given_<why>_micros`, `given_discount_micros` from migration 0036) |
| 145 | and `workspace_costs` (`given_micros`). Sudo's Bill & pricing page lists | |
| 146 | comped, free use, trial and pool by name; the discount part is in the | |
| 147 | total until the page names it (`givenDiscountMicros`). | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 148 | - **Month-end meters**: a day's figure is that day's `pending_days` |
| 149 | snapshot less the day before's, within a month. Their month-end ledger | |
| 150 | entries are left out, so nothing is counted twice. | |
| Costs: a statement that keeps usage sold, running g1t, subscriptions and what was given away (comped, free use, trial, pool) apart, and says who was paid; free use carries its own cost; the run button says it is running | 151 | - **Product margin** = (value − cost) / value. |
| 152 | - **Sudo's statement** keeps apart: | |
| 153 | - **Usage sold**: cash for usage against the cost of the usage buckets | |
| 154 | less what was given. Its margin is the headline; at cost plus 20% it | |
| 155 | sits near 16.7%. | |
| 156 | - **Running g1t**: the plan's price against `platform` less its given | |
| 157 | share. | |
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 158 | - **Cloudflare subscriptions**: what Cloudflare lists, a month, over |
| 159 | the range (`cf_subscriptions`); until a read has worked, | |
| 160 | `CLOUDFLARE_FIXED_MONTHLY_MICROS`, an estimate. | |
| Costs: a statement that keeps usage sold, running g1t, subscriptions and what was given away (comped, free use, trial, pool) apart, and says who was paid; free use carries its own cost; the run button says it is running | 161 | - **Not mapped**: billed, charged for by nothing. |
| 162 | - **Given away**: by why. A budget, watched under g1t's own spend, never | |
| 163 | shown as a loss. | |
| 164 | - **All in**: money in against all of it, with the figure without what | |
| 165 | was given beside it. **Who g1t paid** splits the cost into Cloudflare | |
| 166 | and the model providers. | |
| 167 | ||
| 168 | The overall alert is (Σ cash − (Σ cost − Σ given)) / Σ cash. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 169 | - **Quantities**: where a mapping names an `own_meter`, Cloudflare's |
| 170 | billed quantity of those lines (or, without one, Artifacts' operation | |
| 171 | events) against g1t's own count. | |
| 172 | ||
| 173 | **Shared costs to workspaces.** A bucket's cost is shared in proportion | |
| 174 | to, first available: Cloudflare's own per-workspace count | |
| 175 | (`cloudflare_<bucket>`, today the Artifacts events by repository), g1t's | |
| Costs: a statement that keeps usage sold, running g1t, subscriptions and what was given away (comped, free use, trial, pool) apart, and says who was paid; free use carries its own cost; the run button says it is running | 176 | own count, what its usage cost (so free use carries its own cost), what |
| 177 | each was charged for it. `platform` | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 178 | and `unmapped` are shared by each workspace's share of all usage that |
| 179 | day. Shares are whole micros that add up to the bill exactly (largest | |
| 180 | remainder). | |
| 181 | ||
| 182 | ## Drift (last 7 days) | |
| 183 | ||
| 184 | | Kind | When | What to do | | |
| 185 | | --- | --- | --- | | |
| One operation mapping, owned by repos; billing reads it instead of keeping its own | 186 | | Count | g1t's count and Cloudflare's differ by more than the mapping's `drift_percent` (10%) | Find out what Cloudflare counts: compare its events with `own_counts` `artifacts_*` and `cost_operations`. If it counts more (binding reads, `ls-refs`), either change repos' `operation_mapping` so customers are charged for what Cloudflare counts, or leave it and let the per-unit cost rise (below). | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 187 | | Cost | Cloudflare charged more than `drift_percent` away from the price book's cost of the same usage, with at least `min_daily_cost` | A price is stale: check the proposals. | |
| Billing: AI Gateway's analytics are read with the token that can see them, and a gateway that priced nothing is said | 188 | | Cost, on `models` | What AI Gateway priced g1t's own provider traffic at over the 7 days, against the ledger's model cost for the same days (billed to g1t: comped, free and trial use included, a workspace's own provider not), more than the `ai_gateway_requests` mapping's `drift_percent` (10%) apart, with at least `min_daily_cost`. A ledger with none of the gateway's cost is drift too, and so is a gateway that priced nothing against a ledger with at least `min_daily_cost` of model cost (no percentage): that is not agreement, it is a token that cannot see AI Gateway, or calls that went around it | The gateway higher: model calls g1t paid for and charged no one: runs not settled yet (they catch up within the hour), runs with no session, a run started without a billing ticket, or something else on g1t's gateway. The ledger higher: runs that reached a provider without the gateway. The detail adds why the gateway's own figure may be off: prompt-cache read and write tokens (the gateway prices them at its rates for cache tokens, which can lag the provider's; check against the provider's invoice), requests Cloudflare billed itself (unified billing: on Cloudflare's bill, not a provider's), and models with no price. Days are UTC by when a request ran (gateway) and when a charge was entered (ledger), so a run across midnight shifts a little between days; the 7-day sum absorbs it. | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 189 | | Unpriced | Over the 7 days, a model in AI Gateway's analytics with tokens and $0 cost, or runs settled with `runs.gateway_note` (the gateway could not price all of a run) | The gateway has no price for a model g1t runs: add it in the gateway (custom cost) or route away from it. Until then those runs are charged no less than the sandbox reported (Claude Code's own price table), never $0 silently. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 190 | | Leak | Cost of at least `min_daily_cost` and nothing charged for it (never for `platform`), or a meter in `unmapped` | Map the meter (below), or decide it is overhead (`platform`). | |
| 191 | ||
| 192 | ## Prices: versions, proposals, notice | |
| 193 | ||
| 194 | - `price_versions` holds every price ever, never edited. `prices` is the | |
| 195 | version in force. The daily run applies a version once its | |
| 196 | `effective_at` has come, and adds the public `price_changes` record. | |
| 197 | Ledger entries made from the price book carry `price_version` (the | |
| 198 | version ids, comma-separated), so a past statement is always explained | |
| 199 | by the prices of its day. | |
| 200 | - Proposals come from the keeper (sandbox seconds, app requests and CPU) | |
| 201 | and the reconciler (mappings with `scale_to_own`: today git operations). | |
| 202 | For git operations: Cloudflare's rate per its own operation (the median | |
| 203 | over charged days of cost ÷ quantity) × (Cloudflare's operations ÷ g1t's) | |
| 204 | × 1,000. If Cloudflare counts three for each one g1t counts, the per-1,000 | |
| 205 | price triples. At least 1,000 of g1t's operations are needed. | |
| 206 | - Decision (`pricing::decide`): under 2% is noise; more than 4× either way | |
| 207 | is suspect and waits for staff; within `auto_apply_percent` (25%) it is | |
| 208 | applied on its own when `auto_apply` is on; anything else waits. | |
| 209 | - Notice: a fall applies at once. A rise applies `notice_days` (14) after | |
| 210 | the decision, and for a monthly meter (git, storage, cache, domains, | |
| 211 | embeddings, scans) at the start of the month after that, so no month | |
| 212 | is charged at two prices. Owners of workspaces on the plan are emailed | |
| 213 | once per rise (`price_notices`), and the pricing page lists it with | |
| 214 | "takes effect". Rises are never retroactive; margin protection is for | |
| 215 | new usage once notice has run. | |
| 216 | - Staff approve or reject in sudo. A rejection needs a note. | |
| 217 | ||
| 218 | ## Changing a mapping | |
| 219 | ||
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 220 | In sudo, Costs & margin → Bill & pricing → **Mappings**: Cloudflare's product and meter |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 221 | prefix (as **Cloudflare's lines** lists them; `*` for the rest of the |
| 222 | product), g1t's product, and optionally: | |
| 223 | ||
| 224 | - **Price meter**: the price book meter the line measures. | |
| 225 | - **Own meter**: g1t's count of the same units (`own_counts.meter`). | |
| 226 | - **Scale to g1t's count**: price one of g1t's units at as many of | |
| 227 | Cloudflare's as it took (proposals as above). | |
| 228 | - **Drift threshold**. | |
| 229 | ||
| sudo: the costs run button is named for what it does, the whole nightly analysis | 230 | It applies from the next run; **Run the analysis now** applies it at once. |
| One operation mapping, owned by repos; billing reads it instead of keeping its own | 231 | Every change is in the audit log (`cost_mapping`). |
| 232 | ||
| 233 | ## Which raw meters are operations | |
| 234 | ||
| 235 | There is one mapping, and the repos service owns it: `operation_mapping` | |
| 236 | in g1t-repos' database, one row per raw meter with `cost_operations` (how | |
| 237 | many operations Cloudflare bills for it) and `billable_operations` (how | |
| 238 | many the customer is charged for). Change it with repos' | |
| 239 | `set_operation_mapping` RPC (services only), or | |
| 240 | `npx wrangler d1 execute g1t-repos --remote` until sudo has a form. A | |
| 241 | change applies to counts from then on, never to what was counted. | |
| 242 | Billing keeps no mapping of its own: it reads repos' `git_operations` | |
| 243 | (already mapped) for what customers are charged, and `artifacts_usage` | |
| 244 | (raw counts with the mapping) for `cost_operations`. Migration 0023 drops | |
| 245 | the `billable_units` table 0022 made for this, which was never written. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 246 | |
| Models' margin read -14%: usage nothing paid for is valued at price, not $0 | 247 | **Charged at price** (a product's value) is what each day's usage was paid: |
| 248 | charged to a card or credit, or drawn from the plan's included usage, a | |
| 249 | trial, a pool or a gift. Usage nothing paid for, as in a free period, is | |
| 250 | valued at price (cost plus the margin), since it was given away at its price | |
| 251 | rather than sold for nothing; so are g1t's own workspaces. Runs on a | |
| 252 | workspace's own model provider have no cost to g1t. Every daily run | |
| 253 | reconciles the whole 31-day window from what is already kept, so a change in | |
| 254 | how a day is valued reaches every day sudo shows. | |
| 255 | ||
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 256 | ## Alerts runbook |
| 257 | ||
| 258 | | Alert | Raised when | First steps | | |
| 259 | | --- | --- | --- | | |
| 260 | | Margin under the floor | A product's value against cost under `margin_floor_percent` (10%) for `alert_days` (3) days running, each with at least `min_daily_cost` | Open the product on Costs & margin. Cost up? Check proposals (approve a rise; it waits out the notice). Value down? A mapping or `revenue_map` may have moved. | | |
| Costs: margin is measured on what was sold; comped workspaces, free periods, the trial and the pools are given away, a budget shown beside it | 261 | | All of g1t under the floor | The same for money in against the cost of what was sold: every cost less what was given away (comped workspaces, free periods, the trial, the pools), which is a budget watched in budget.rs. While less than $1 a day comes in, it says the dollars, not a percentage | Look at which products moved; check `platform` (it has no revenue of its own and grows with traffic). Before launch, with little paid usage, expect it. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 262 | | Leak | Drift of kind leak | Map the meter, or decide it is overhead. | |
| 263 | | Drift | Count drift | See Drift above. Cloudflare's definitions change in beta: ask them in writing ([ARTIFACTS.md](ARTIFACTS.md), §7). | | |
| Margin alerts measure what is sold, and say dollars when a percentage would mislead | 264 | | Costs more than it pays | A workspace's shared cost over 30 days above what its usage was priced at (`value_micros`, whoever paid: card, trial, gift or included usage) × `anomaly_factor`, at least `anomaly_floor`; not comped workspaces | Shown on Reach out as "Costs more than it pays": its usage is priced below what it costs. Abuse (Abuse & fraud page) or a gap in pricing. Not emailed. A trial or gift paying for usage does not raise it. | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 265 | |
| 266 | Alerts close on their own when the condition clears. Open ones are | |
| 267 | emailed again weekly. The red bar on every sudo page shows margin, | |
| 268 | overall and leak alerts. | |
| 269 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 270 | ## Model costs |
| 271 | ||
| 272 | Every model call g1t pays for is an agent run's (the `claude` CLI in the | |
| Merge the AI Gateway: Anthropic's Messages API on a workspace's tokens | 273 | sandbox, `crates/runner`) or a customer's AI Gateway request (a workspace |
| 274 | token with `models:write` at `models.g1t.sh/anthropic`, `gateway.rs`); the | |
| 275 | only other model is Workers AI's embeddings, which are on Cloudflare's bill | |
| 276 | (`embeddings`). How each reaches the ledger: | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 277 | |
| 278 | | Call | Who pays | Run and session | Ledger cost | Settled to the gateway | | |
| 279 | | --- | --- | --- | --- | --- | | |
| 280 | | Agent run through the model proxy (`services/models`) on g1t's hosted models | g1t | `runs` row; session `ms_…` in `cf-aig-metadata` | On finish, the sandbox's figure (Claude Code's `total_cost_usd`, at its own price table, cache tokens included) | Yes, every 15 minutes | | |
| 281 | | Agent run straight to the gateway (no `MODELS_URL`) | g1t | `runs` row; session `rs_…` in `cf-aig-metadata` (`services/runner` `gatewaySession`) | As above | Yes | | |
| 282 | | Agent run with no gateway (`AI_GATEWAY_ID` empty, self-hosting) | g1t's key | `runs` row, no session | The sandbox's figure | No: nothing to settle against | | |
| Merge branch 'model-routing' | 283 | | Agent run on a workspace's own provider | The workspace | `runs` row, `billed_to = 'workspace'`, session `ms_…` (the proxy counts its tokens by it) | No model cost (none to g1t); the agent rate on `agent_tokens_own`, line `<run>/agent-own` | No; never on g1t's gateway. Closed by the cron's `settle_own_runs`, which charges tokens counted late | |
| Merge the AI Gateway: Anthropic's Messages API on a workspace's tokens | 284 | | AI Gateway request on g1t's key | The workspace, from included usage and AI credit (`gateway_admit` refuses at $0, over the limit, or with no plan) | No run; `gateway_requests` row; session `gw_<token>_<YYYYMMDDHH>` in `cf-aig-metadata` (`task: gateway`) | Per request: its tokens at `gateway_models` (the table, migration 0045: list price per million by kind) plus the `gateway_models` meter's markup (0 in beta); a `usage` line with task `gateway`, reference the request id, quantity 1 | No, not yet: the session tag is there for it. **Cost** drift on `models` compares the gateway's total with these lines too. Requests billed other than by tokens (fast mode, `inference_geo`, fallbacks, server tools, containers) are refused by the proxy | |
| 285 | | AI Gateway request on a workspace's own key | The workspace's provider | `gateway_requests` row, `own_key = 1` | None | No; never on g1t's gateway | | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 286 | | A sandbox that died before reporting | g1t | as its route | Charged from the gateway when settled | Yes | |
| 287 | | Embeddings (indexing) | g1t | none (Workers AI) | Month-end `context` meter | No: Cloudflare's bill, `embeddings` bucket | | |
| 288 | | Embeddings (queries, search and agent context) | g1t | none | None: not charged, by design | No: in Cloudflare's `embeddings` line, shared out | | |
| 289 | ||
| 290 | **Settling.** A run's charge is corrected to what AI Gateway priced its | |
| 291 | session's requests at (`settled_cost` in `keeper.rs`). The gateway's | |
| 292 | figure is trusted in full: it is not held to the $100 cap on a sandbox's | |
| 293 | own report. It is never taken below what the sandbox reported when it | |
| 294 | cannot be the whole cost: a request with tokens and no cost (a model the | |
| 295 | gateway has no price for) or more logs than are read (2,000). Such a run | |
| 296 | keeps `runs.gateway_note`, its correction says why, and it raises the | |
| Merge the AI Gateway: Anthropic's Messages API on a workspace's tokens | 297 | **Unpriced** drift. For agent runs g1t keeps no token rates of its own: |
| 298 | the first figure is Claude Code's, the final one the gateway's. AI Gateway | |
| 299 | requests are the exception: they are charged from `gateway_models` (one | |
| 300 | row per model offered), which has to follow the provider's price list by | |
| 301 | hand until they are settled like runs. | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 302 | |
| 303 | **The daily total.** AI Gateway's analytics for the day (above) against | |
| 304 | the ledger's model cost is the check that nothing slips past: a model call | |
| 305 | with no run, or a run never settled, shows as **Cost** drift on `models`. | |
| 306 | The gateway's per-request `cost` is its estimate from its own price list: | |
| 307 | it can be off for prompt-cache tokens, for requests Cloudflare bills | |
| 308 | itself, and for models it has no price for. The drift's detail says when | |
| 309 | any of those were in the window; the provider's invoice is the last word. | |
| 310 | ||
| 311 | ### Margin floor | |
| 312 | ||
| 313 | A sold charge is cost × (1 + `MARGIN_PERCENT`), rounded up (`margin_on`; | |
| 314 | `charge_micros` for a sandbox's own report). Terms change it only as | |
| 315 | follows (`Terms::discounted`, `Billing::charged`): | |
| 316 | ||
| 317 | - **Standard**: charged in full. | |
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 318 | - **A 100% discount** (what was "comped"; see [Discounts](#discounts)), |
| 319 | `FREE_WHILE_BUILDING`, the plan's included usage, the trial, | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 320 | the open-source pool, and overruns g1t covers: given, and counted by why |
| 321 | (above). | |
| 322 | - **Custom, with a discount**: the discount comes off, and what it took | |
| 323 | below cost plus the margin is written on the entry as | |
| 324 | `ledger.discount_micros` and counted as given (**discount**), so the sale | |
| 325 | is valued at its price and charged plus given is never under cost plus the | |
| 326 | margin. On a settlement correction it moves with the charge (less than | |
| 327 | nothing when the charge comes down). | |
| 328 | - **Goodwill credits** (overages) are separate, given by staff on purpose: | |
| 329 | their margin part first, the cost only up to the cap, each audited. | |
| 330 | ||
| 331 | Every usage path goes through this: `finish_run`, settling, sandbox time, | |
| 332 | features and builds (`charge_feature`), and the month-end meters. | |
| 333 | ||
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 334 | ## Discounts |
| 335 | ||
| 336 | An account's terms are standard, or custom: a **discount** from 1 to 100%, | |
| 337 | a limit of its own, or both, with a reason (the terms' note) and an | |
| 338 | optional end date. What used to be "comped" is a **100% discount** | |
| 339 | (`Terms::full_discount`; migration `0039_discounts_not_comped.sql` moved | |
| 340 | every `comped` row to `custom` at 100%, and code reads a leftover `comped` | |
| 341 | row as 100%). In SQL, `sales::FULL_DISCOUNT_SQL`. | |
| 342 | ||
| 343 | - **Charging.** Every charge records what the discount took off it | |
| 344 | (`ledger.discount_micros`), 100% included: the entry is charged nothing | |
| 345 | and the discount is its whole price. Migration 0039 backfilled the | |
| 346 | discount on a 100%-discounted workspace's earlier entries that were | |
| 347 | charged nothing and paid by nothing, at cost plus 20%. | |
| 348 | - **What a 100% discount still does as "comped" did.** The plan is on | |
| 349 | without its price (`PlanKind::Internal`), trust is `internal` (no limit | |
| 350 | on unpaid usage), nothing is invoiced or closed, and g1t's own spend on | |
| 351 | it is held to the monthly budget (the terms' limit, at cost; see | |
| 352 | [Spend caps](#spend-caps)). | |
| 353 | - **The statement and Usage.** The customer sees every usage line at its | |
| 354 | price (`StatementLine.price_micros`: charged, plus what paid for it, plus | |
| 355 | the discount), the discount per day or project and in the totals | |
| 356 | (`StatementTotals.price_micros`, `discount_micros`, `discount_percent`), | |
| 357 | and the CSV has price and discount columns. The Usage page measures at | |
| 358 | price for a discounted account (`Usage.discount_micros`, | |
| 359 | `discount_percent`). | |
| 360 | - **Margin.** A 100% discount's usage is given away as before, in the | |
| 361 | bucket still named `comped` (`given_comped_micros`); sudo calls it | |
| 362 | **100% discounts**. A partial discount's part below cost plus the margin | |
| 363 | is `given_discount_micros` (**partial discounts**). Both are kept apart | |
| 364 | from margin on what was sold. | |
| 365 | - **sudo.** The workspace's **Terms** form takes a discount (None, 25%, | |
| 366 | 50%, 100%, or Custom, a whole percent; the custom field shows by CSS | |
| 367 | alone), a limit, an end date and the reason. Badges and filters say | |
| 368 | *100% discount* or *N% off*. Each change is audited (`terms`), such as | |
| 369 | `standard → 100% discount, monthly budget $150.00: g1t's own`. | |
| 370 | ||
| 371 | ## Credits from g1t | |
| 372 | ||
| 373 | Staff give a workspace credit from sudo; the code is | |
| 374 | `services/billing/src/grants.rs`, the tables `credit_grants` and | |
| 375 | `ledger.credit_kind` (migration `0038_staff_credits.sql`). | |
| 376 | ||
| 377 | ### Giving credit | |
| 378 | ||
| 379 | sudo → the workspace (or an enterprise, choosing one of its workspaces) → | |
| 380 | **Give credit**: | |
| 381 | ||
| 382 | 1. **Amount**: $10, $20, $25, $50, $100, or **Custom** (up to $10,000). | |
| 383 | Up to $100 it is one step; over $100, type the workspace's slug as well. | |
| 384 | 2. **Kind**: promotional (a welcome, a referral, an event), goodwill (an | |
| 385 | apology), or refund (money back for something that went wrong: say what | |
| 386 | it refunds and, optionally, the day). | |
| 387 | 3. **Expires**: never, 30, 90 or 365 days, or the end of a chosen day | |
| 388 | (UTC). A refund never expires. | |
| 389 | 4. **Note**: required. It is on the statement and in the owners' email. | |
| 390 | ||
| 391 | The form needs no JavaScript: the fields for one choice (the custom amount, | |
| 392 | a refund's details, the expiry date) show by CSS alone, and all show where | |
| 393 | `:has()` is not supported. `admin_credit` checks everything again. | |
| 394 | ||
| 395 | A grant is a `crd_…` ledger line (kind `top_up`, so never a payment) with | |
| 396 | `credit_kind`, and a `credit_grants` row. The balance rises at once. The | |
| 397 | owners are emailed through identity's `notify_owners` (the same path as | |
| 398 | limit notices). It is audited as `credit`. The Overages queue's one-click | |
| 399 | goodwill credit is a grant too, of kind goodwill. | |
| 400 | ||
| 401 | The inbox is not told: its items are threads on a repository, built from | |
| 402 | events, and a credit is a workspace's. That needs a workspace-level inbox | |
| 403 | thread first. | |
| 404 | ||
| 405 | ### How it is spent | |
| 406 | ||
| 407 | Credit is spent before anything prepaid, the soonest-expiring grant first | |
| 408 | (never-expiring last, then the oldest). Given while the workspace owes, it | |
| 409 | pays what is owed first, the most recent usage first. What each grant paid | |
| 410 | for is never stored: `grants::replay` works it out from the ledger in order, | |
| 411 | so the charge paths do not know about credit and the answer is always what | |
| 412 | the ledger says. A charge that comes down (a settled run) gives back to | |
| 413 | the grant that paid last, while it can still be spent. | |
| 414 | ||
| 415 | ### Expiry and revoking | |
| 416 | ||
| 417 | - **Expiry.** The daily run (`expire_credits`, before the reconciliation) | |
| 418 | closes grants past `expires_at` and enters what was left as a negative | |
| 419 | `crd…_expired` line; audited as `credit_expired`. A grant past its expiry | |
| 420 | pays for nothing even before the run. | |
| 421 | - **Revoke.** sudo → the workspace's **Credits** (or **Credits & refunds**) | |
| 422 | → **Revoke unused**, with why (`admin_revoke_credit`): what is left, as a | |
| 423 | `crd…_revoked` line, audited as `credit_revoked`. What was spent stays | |
| 424 | spent. | |
| 425 | ||
| 426 | Neither takes the balance below zero: at most the balance, if a refunded | |
| 427 | payment left less there than the credit. | |
| 428 | ||
| 429 | ### How margin treats them | |
| 430 | ||
| 431 | | Kind | When spent | On the day it was given | | |
| 432 | | --- | --- | --- | | |
| 433 | | Promotional | The usage is valued at its price, its charge comes out of cash and is given (`given_credit_promotional_micros`) | Nothing | | |
| 434 | | Goodwill | The same, as `given_credit_goodwill_micros` | Nothing | | |
| 435 | | Refund | Paid for: cash, as any usage | Its amount (less what was revoked) comes off cash on the day it refunds, shared over that day's paid usage | | |
| 436 | ||
| 437 | A refund gives back money already collected, so counting it as given would | |
| 438 | make it look like a budget g1t chose to spend. Taking it off cash for the | |
| 439 | day it refunds says that day's sale was worth less, and counting what it | |
| 440 | later pays for as cash keeps money in equal to what was collected. The | |
| 441 | refund's day is clamped to the last 30 days, the days the reconciliation | |
| 442 | recomputes; an older one lands on the oldest. Refunds never expire, so | |
| 443 | cash taken back is never stranded. | |
| 444 | ||
| 445 | What credit paid of a month-end meter (storage, git, scans) is its own | |
| 446 | row on the day it was charged, since those meters are reconciled from | |
| 447 | snapshots. Sudo's Costs & margin lists promotional and goodwill credit | |
| 448 | under **Given away**, and below the statement the range's credits given, | |
| 449 | spent, and refunded. **Credits & refunds** (`/credits`, `admin_credits`) | |
| 450 | lists every grant (by kind, month, staff and workspace) and the last 12 | |
| 451 | months by kind: given, spent, expired, revoked. | |
| 452 | ||
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 453 | ### Purchased and scoped credit (prepaid AI) |
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 454 | |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 455 | `credit_grants` also has `scope` (`all`, or `models`: model usage only, |
| 456 | `grants::is_model_usage`, which includes the agent rate) and `source` | |
| 457 | (`staff`, `purchase`, `promo_code`, `upgrade`), and `CreditKind::Purchased`. | |
| 458 | Spending takes credit scoped to models first, then the soonest-expiring. A | |
| 459 | grant scoped to models given while the workspace owes pays only what models | |
| 460 | owed, never other usage (`replay`). Purchased credit is money paid in: its | |
| 461 | ledger line is a payment (Stripe's id, never `crd…`, `credit_kind` | |
| 462 | `purchased`, statement kind *AI credit*), and the usage it pays for stays | |
| 463 | money in, never given. Staff cannot give it (`admin_credit` refuses the | |
| 464 | kind). Code: `services/billing/src/ai.rs`; migration `0040_ai_credit.sql`. | |
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 465 | |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 466 | - **Buying.** `buy_ai_credit` opens Stripe Checkout (payment mode, $10 to |
| 467 | $1,000, a second line *Card processing fee* when the `card_fee` cost | |
| 468 | setting is on, `setup_future_usage=off_session`), recorded in `checkouts` | |
| 469 | with `feature = 'ai_credit'`, `amount_cents` the credit and `fee_cents` | |
| 470 | the fee. The credit is entered by whichever comes first, the person coming | |
| 471 | back (`confirm_ai_credit`, `?ai_credit=cs_…`) or | |
| 472 | `checkout.session.completed`: both claim the row `open → paid`, the | |
| 473 | grant's id is the session's id (`INSERT OR IGNORE`) and the ledger's | |
| 474 | reference is unique, so a payment is credited exactly once. Expires 365 | |
| 475 | days after purchase (the daily `expire_credits`). | |
| 476 | - **Owed.** AI credit props up the balance but is money only for models, so | |
| 477 | what is owed is `max(0, AI credit left − balance)` (`owed_with`; at a | |
| 478 | month's close, `models_left_before` the month's start). | |
| 479 | - **Auto-reload.** `ai_reload` (settings; off by default) and `ai_reloads` | |
| 480 | (one row per attempt). Each cron run (and a run that would be refused) | |
| 481 | calls `reload_now`: below the threshold, it charges the customer's default | |
| 482 | payment method off-session for the target less the balance (whole | |
| 483 | dollars, at least $10, within the month's maximum), with the idempotency | |
| 484 | key `reload/<workspace>/<YYYY-MM>/<n>` (a retry after a crash is the same | |
| 485 | PaymentIntent), and grants purchased credit with the PaymentIntent's id. A | |
| 486 | decline or a payment needing the person turns auto-reload off | |
| 487 | (`failed_at`, `error`), emails the owners and audits `ai_reload_failed`. | |
| 488 | - **At $0.** `start_run` on g1t's models refuses with `payment_required` | |
| 489 | when the workspace is on the paid plan (not a 100% discount, not an | |
| 490 | enterprise), its included usage is used, and AI credit is $0 or less. | |
| 491 | - **Upgrade credit.** The first time a plan subscription is recorded active | |
| 492 | (`features::record`), $5 of promotional credit scoped to models, id | |
| 493 | `crd_upgrade_<workspace>`, expiring in a year: given, never revenue. Never | |
| 494 | for a workspace with a 100% discount. | |
| 495 | ||
| 496 | ### The agent rate and models' markup | |
| 497 | ||
| 498 | Price-book meters (migration 0040, each with versions and a public change): | |
| 499 | `agent_models` (per provider dollar; markup 20% until 2026-10-08, then 0, | |
| 500 | a fall applied at once), `agent_tokens` ($0 until 2026-10-22, then $0.25 a | |
| 501 | million tokens: a rise, after the 14 days' notice, emailed to owners on the | |
| 502 | plan by `tell_owners_of_rises`), `gateway_models` (markup 0 during beta), | |
| 503 | `card_fee_percent` (29,000 micros per dollar) and `card_fee_fixed` | |
| 504 | (300,000). Changing any is a price-book change, never a deploy. `finish_run` | |
| 505 | and `settle` charge models at `agent_models`' markup; `charge_agent_rate` | |
| Merge branch 'model-routing' | 506 | charges the weighted tokens of the run's session since it was last charged |
| 507 | (`runs.agent_tokens`, the weighted tokens charged so far, claimed with a | |
| 508 | compare-and-set), on a line `<run>/agent` (later `<run>/agent/<tokens>`), | |
| 509 | with `quantity` the weighted tokens. What it counts is the more of what | |
| 510 | `token_usage` holds for the session and what the sandbox reported with its | |
| 511 | cost (`finish_run`'s `tokens`, from Claude Code's closing `usage`), each | |
| 512 | weighted by kind. **Card fee switch:** sudo → Costs → Guardrails → *Card fee | |
| Merge Stripe Tax, the card fee on card payments, and one free workspace per person | 513 | on card payments* (`cost_settings.card_fee`, `on`/`off`, on by default). It |
| 514 | covers every card payment now, not only AI credit: see | |
| 515 | [Tax and the card fee](#tax-and-the-card-fee). | |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 516 | |
| Merge branch 'model-routing' | 517 | **On a workspace's own model key** (migration `0041_agent_rate_own_key.sql`): |
| 518 | the run keeps its model session (`runs.session_id`, `ms_…`) so the proxy's | |
| 519 | counts reach it, and the agent rate is charged at `agent_tokens_own` ($0 until | |
| 520 | 2026-10-22, then $0.25 a million, a rise from nothing with its notice), on | |
| 521 | `<run>/agent-own` (later `<run>/agent-own/<tokens>`), `billed_to = 'g1t'` | |
| 522 | (g1t's own charge: it counts toward limits and spend), named *Agent rate, | |
| 523 | your own model key* on Usage and the statement. The model is never charged. | |
| 524 | `settle_runs` skips these runs (nothing on g1t's gateway); the cron's | |
| 525 | `settle_own_runs` closes them 5 minutes after they finish (3 hours after | |
| 526 | they start, for a sandbox that never reported) and charges tokens counted | |
| 527 | late. Runs from before have no session and are never charged the rate. | |
| 528 | ||
| 529 | ### The agent rate's token weights | |
| 530 | ||
| 531 | How much each kind of token counts toward the agent rate, on g1t's models | |
| 532 | and own keys alike, is four price-book meters (migration | |
| 533 | `0042_agent_rate_weights.sql`): `agent_token_weight_input`, `_output`, | |
| 534 | `_cache_read` and `_cache_write`, each a weight in millionths in | |
| Billing: cache reads count a tenth toward the agent rate | 535 | `cost_micros` (1,000,000 counts a token once). They started at 1, which is |
| 536 | what the rate always counted; since 2026-10-08 cache reads count a tenth | |
| 537 | (100,000; migration `0044_cache_reads_count_a_tenth.sql`), as model providers | |
| 538 | price them. Input, output and cache writes count once. A cached agent run reads most of its context from | |
| Merge branch 'model-routing' | 539 | cache (about 90% of its tokens on a typical Sonnet implement run), so the |
| 540 | cache-read weight is the lever: at 1 the rate adds about 44% to such a run's | |
| 541 | model cost; at 0.1, far less. | |
| 542 | ||
| Billing: cache reads count a tenth toward the agent rate | 543 | To change a weight (cache reads went to a tenth this way): |
| Merge branch 'model-routing' | 544 | |
| 545 | 1. Add the version, effective at once (a lower weight is a fall): | |
| 546 | ||
| 547 | ```sql | |
| 548 | INSERT INTO price_versions (id, meter, version, cost_micros, markup_percent, effective_at, reason, created_by, created_at) | |
| 549 | VALUES ('pv_agent_token_weight_cache_read_2', 'agent_token_weight_cache_read', 2, 100000, 0, | |
| 550 | '2026-10-08T00:00:00Z', 'Cache reads count a tenth toward the agent rate', 'staff', '2026-10-08T00:00:00Z'); | |
| 551 | ``` | |
| 552 | ||
| 553 | in a migration, or with `npx wrangler d1 execute g1t-billing --remote` | |
| 554 | until sudo has a form. | |
| 555 | 2. The daily run applies it once `effective_at` has come and writes the | |
| 556 | public `price_changes` record (**Run the analysis now** applies it at | |
| 557 | once). Raising a weight later is a rise: give it an `effective_at` 14 | |
| 558 | days out, and owners on the plan are emailed. | |
| 559 | 3. Check `/pricing`: the agent rate's row lists the weights, and Usage's | |
| 560 | agent-rate lines name them. | |
| 561 | ||
| 562 | `weighted` (`ai.rs`) rounds down to a whole token; a weight is never below 0. | |
| 563 | ||
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 564 | ### Budgets |
| 565 | ||
| 566 | The owners' spend limit is the budget. `limits.alert_levels` (comma | |
| 567 | separated, default every level), `limits.pause_at_limit` (default 1; off, | |
| 568 | 100% is a warning, never a stop; the trust ceiling still stops work) and | |
| 569 | `limits.budget_webhook` (an https address, not g1t's; posted once per alert | |
| 570 | level a month by `warn_limits`). `set_budget` sets them, with `keepLimit` | |
| 571 | to leave the limit itself alone. A free workspace (trust `new`) has no | |
| 572 | spend limit to set: `set_spend_limit` and `set_budget` say so. | |
| 573 | ||
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 574 | ### Earlier credits |
| 575 | ||
| 576 | Migration 0038 makes every earlier `Credit from g1t:` line a goodwill | |
| 577 | grant with no expiry: the old form asked for "a refund or goodwill" with | |
| 578 | no way to tell them apart, and goodwill never reads as money in. | |
| 579 | ||
| Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix | 580 | ## Token usage |
| 581 | ||
| 582 | The model proxy (`services/models`) reads Anthropic's `usage` from every | |
| 583 | `/v1/messages` answer, streamed or whole, on g1t's models and on a | |
| 584 | workspace's own provider alike (OpenAI-shaped providers are translated | |
| 585 | first). Count-tokens requests are not answers and are skipped. After the | |
| 586 | answer, it calls `record_tokens`, which adds input, output, cache reads and | |
| 587 | cache writes to one row per day, workspace, person, session and model in | |
| 588 | `token_usage` (migration `0030_token_usage.sql`). The person is who the run | |
| 589 | was for, from the model session's `requested_by`; never g1t's agent. A | |
| 590 | report that fails is dropped and never affects the answer. | |
| 591 | ||
| 592 | `token_usage` reads a window (42 days by default, 366 at most) for the | |
| 593 | workspace or one person: totals, every day's tokens and the active days, | |
| 594 | with `costMicros` the window's run charges from the ledger, measured as | |
| Merge branch 'model-routing' | 595 | `usage` measures them. Usage's report also lists tokens by model |
| 596 | (`UsageReport.models`). The agent rate is charged on these counts (above); | |
| 597 | a run's model is still priced from AI Gateway's logs, never from them. | |
| Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix | 598 | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 599 | ## Tables (migration `0022_costs_and_margin.sql`) |
| 600 | ||
| One operation mapping, owned by repos; billing reads it instead of keeping its own | 601 | `cost_lines`, `cost_map`, `revenue_map`, `own_counts`, |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 602 | `pending_days`, `margin_days`, `workspace_costs`, `cost_drift`, |
| 603 | `margin_alerts`, `price_versions` (seeded with every current price as | |
| 604 | version 1), `price_proposals`, `price_notices`, `cost_settings` (the | |
| 605 | guardrails, seeded), the `actions_cache` price, and `ledger.price_version`. | |
| 606 | Every create is `IF NOT EXISTS` and every seed `INSERT OR IGNORE`; the one | |
| One operation mapping, owned by repos; billing reads it instead of keeping its own | 607 | `ALTER` is applied once by D1's migration tracking. Migration |
| 608 | `0023_one_operation_mapping.sql` drops `billable_units` (see above). | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 609 | Migration `0036_model_costs_in_full.sql` adds `ledger.discount_micros`, |
| 610 | `margin_days.given_discount_micros`, `runs.gateway_note` and the | |
| Billing: credits with a kind and expiry, discounts instead of comped, and safer charging | 611 | `ai_gateway_requests` → `models` mapping. Migration |
| 612 | `0038_staff_credits.sql` adds `credit_grants`, `ledger.credit_kind` and | |
| 613 | `margin_days.given_credit_{promotional,goodwill}_micros`, and backfills | |
| 614 | earlier credits (see [Credits from g1t](#credits-from-g1t)). | |
| Spend caps: a monthly budget for comped workspaces and a daily breaker on what g1t pays | 615 | |
| 616 | ## Spend caps | |
| 617 | ||
| 618 | Two caps keep what g1t pays for itself bounded while billing takes no | |
| 619 | real money. Both are measured at **cost** (what Cloudflare and the model | |
| 620 | providers charge g1t), never at price. Code: `services/billing/src/budget.rs`. | |
| 621 | Page: sudo **Costs & margin** → **g1t's own spend** (`/costs#spend`). | |
| 622 | ||
| 623 | ### What counts as g1t's own spend | |
| 624 | ||
| 625 | Every charge that settles (an agent run's model cost from `finish_run` or | |
| 626 | AI Gateway's settlement, sandbox time from `record_sandbox`, a build from | |
| 627 | `charge_feature`) is split by what paid for it and g1t's part is added to | |
| 628 | `g1t_spend` (day, bucket, billing account): | |
| 629 | ||
| 630 | | Bucket | What | | |
| 631 | | --- | --- | | |
| 632 | | `comped` | All of a comped account's work (flagon-io) | | |
| 633 | | `trial` | The trial credit's share | | |
| 634 | | `oss` | The open-source pool's share | | |
| 635 | | `given` | A free workspace's overrun past its last bit of trial | | |
| 636 | | `unpaid` | Charged, but with no real money behind it: Stripe's test key, or `FREE_WHILE_BUILDING` | | |
| 637 | ||
| 638 | The plan's included usage and on-demand charges count as revenue only | |
| 639 | with live payments; in test mode they are `unpaid`. A workspace's own | |
| 640 | model provider costs g1t nothing and is not counted. Month-end meters | |
| 641 | (git, storage, scans, embeddings, the cache) are not counted here; the | |
| 642 | daily reconciliation covers them. Migration `0024_spend_caps.sql` | |
| 643 | backfills the current month from the ledger. | |
| 644 | ||
| 645 | ### Caps | |
| 646 | ||
| 647 | | Cap | Variable (g1t-billing) | Default | At the cap | | |
| 648 | | --- | --- | --- | --- | | |
| 649 | | A comped account's monthly budget | `COMPED_MONTHLY_CEILING_MICROS`, or the account's own **Limit** in its terms | $150 a month | New work on the account (agents, checks, workflows, builds) is refused with "<name>'s monthly budget for g1t's own agents is used up … Staff can raise it in sudo". Runs already going finish; the per-run cap still applies to them. It lifts when staff raise the budget or the month turns (UTC). | | |
| 650 | | The daily breaker | `PLATFORM_DAILY_SPEND_CAP_MICROS` | $75 a day (UTC) | New agent runs on g1t's hosted models that g1t would pay for are refused until 00:00 UTC. Not paused: agents on the workspace's own model provider, checks and builds, and workspaces paying with live payments on the plan (not given by staff) or an enterprise contract. In test mode that exemption covers no one. | | |
| 651 | ||
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 652 | `0` turns either off. Cloudflare's subscriptions are read from Cloudflare |
| 653 | each day (`cf_subscriptions`) and shown on the page only; | |
| 654 | `CLOUDFLARE_FIXED_MONTHLY_MICROS` ($30) stands in until a read works. | |
| Spend caps: a monthly budget for comped workspaces and a daily breaker on what g1t pays | 655 | |
| 656 | The checks are cheap: `reserve` reads today's total (one indexed sum) and, | |
| 657 | for a comped account, its month's comped rows. Refusals come back as | |
| 658 | `paused`, which the compute gate honours for every plan, internal and | |
| 659 | enterprise included (`packages/contracts/src/compute.ts`). The runner tells | |
| 660 | billing whether an agent run is on hosted models (`hostedModel` on | |
| 661 | `reserve`); a caller that does not say is treated as hosted. | |
| 662 | ||
| 663 | ### Alerts | |
| 664 | ||
| 665 | All to `COSTS_ALERT_EMAIL` (`hey@flagon.io`), through the `EMAIL` binding: | |
| 666 | ||
| 667 | - **Comped budget**: at 50, 75, 90 and 100%, once each per account and month | |
| 668 | (`budget_alerts`), checked every 15 minutes. A jump past several levels | |
| 669 | sends only the highest. | |
| 670 | - **Breaker**: at once, from the charge that trips it; if that email fails, | |
| 671 | the 15-minute cron sends it (`spend_breaker.told_at`). | |
| 672 | ||
| 673 | While the breaker is open or a comped budget is used up, every sudo page | |
| 674 | shows a red **Spend cap** bar. | |
| 675 | ||
| 676 | ### Raising and lifting | |
| 677 | ||
| 678 | - **Raise a comped budget**: sudo → the workspace → Billing → **Terms**, set | |
| 679 | **Limit $** to the new monthly budget (blank goes back to the default), | |
| 680 | with a note. It applies to the next start; nothing to deploy. The change | |
| 681 | is in the account's audit log. | |
| 682 | - **Lift the breaker for today**: sudo → Costs & margin → **g1t's own | |
| 683 | spend** → **Lift for today**, with why (`admin_lift_breaker`; audit action | |
| 684 | `breaker_lifted`). It resets by itself at 00:00 UTC. | |
| 685 | - **Change a default**: edit the variable in `services/billing/wrangler.jsonc` | |
| 686 | and deploy g1t-billing. | |
| Billing keeps Stripe's view itself: the saved card on the account, missed events replayed every 15 minutes, and the endpoint kept | 687 | |
| sudo: reset a test workspace's billing so it starts again as a new customer; refused on a live Stripe key, for comped workspaces and for an enterprise's | 688 | ## Resetting a test workspace |
| 689 | ||
| 690 | sudo → the workspace → **Reset billing (testing)** (`admin_reset_billing`) | |
| 691 | returns a workspace used for testing to how a new customer starts. It | |
| 692 | deletes the workspace's rows from every billing table: ledger and balance, | |
| 693 | plan and plan payments, limits and limit requests, trial grant, invoices, | |
| 694 | holds, card checks, alerts sent, price notices, month-end snapshots and | |
| sudo: the billing reset no longer names sandbox_months (dropped in 0015), checked against the migrations by a test; a failed reset says why instead of an error page | 695 | closes, storage meters, token usage, spikes, sales records and |
| sudo: reset a test workspace's billing so it starts again as a new customer; refused on a live Stripe key, for comped workspaces and for an enterprise's | 696 | notes, `workspace_costs`, its workspace margin alert and its own billing |
| 697 | account. It keeps `own_counts` (what Cloudflare's bill is compared with) | |
| 698 | and the audit log, which records the reset with the note and the number of | |
| 699 | rows. The workspace, its members and its repositories are identity's and | |
| 700 | repos' and stay. | |
| 701 | ||
| 702 | Billing refuses it while `STRIPE_SECRET_KEY` is a live key, for comped | |
| sudo: a billing reset runs the costs analysis again so every figure is fresh; every submit button shows it is working (CSS only); no margin percentage on less than a cent sold | 703 | workspaces, and for a workspace an enterprise pays for. It then runs the |
| 704 | costs analysis again (as **Run the analysis now** does), so the margin | |
| 705 | figures drop the workspace's past usage at once; if that run does not | |
| 706 | finish, the page says so and the button does it. | |
| sudo: reset a test workspace's billing so it starts again as a new customer; refused on a live Stripe key, for comped workspaces and for an enterprise's | 707 | |
| Billing keeps Stripe's view itself: the saved card on the account, missed events replayed every 15 minutes, and the endpoint kept | 708 | ## Stripe |
| 709 | ||
| 710 | Billing keeps what it needs from Stripe so reads never wait on it, and | |
| 711 | hears of changes three ways (`webhooks.rs`, `stripe_sync.rs`). | |
| 712 | ||
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 713 | **API version.** Every request sends `Stripe-Version: 2025-02-24.acacia` |
| 714 | (`stripe::STRIPE_VERSION`), the version billing's field reads are written | |
| 715 | for; without it Stripe answers at the account's default. Webhook events | |
| 716 | come at the destination's own version: billing reads an invoice's | |
| 717 | subscription from `subscription` or `parent.subscription_details.subscription`. | |
| 718 | Raising the version is a code change: read Stripe's upgrade notes for every | |
| 719 | field billing reads. | |
| 720 | ||
| 721 | **Failures.** No Stripe failure reaches a page as a 500: each payment page | |
| 722 | (`page_opened`), the portal, confirmations and plan changes turn it into | |
| 723 | `stripe::friendly` (Stripe's own message, never the request or a key), and | |
| 724 | log the full error with the workspace. Every payment page is recorded | |
| 725 | through one insert (`CHECKOUT_INSERT`), checked against the migrations by | |
| 726 | `every_checkout_insert_fills_the_table`, and has an idempotency key | |
| 727 | (`page/<purpose>/<workspace>/…/<10-minute bucket>`). | |
| 728 | ||
| Stripe's webhook secret is a Worker secret, STRIPE_WEBHOOK_SECRET, from a destination made in Stripe's dashboard | 729 | **The webhook.** A destination made in Stripe's dashboard (Developers → |
| 730 | Webhooks → Add destination) with the endpoint URL | |
| 731 | `https://api.g1t.sh/stripe/webhook`, in the mode of billing's key (at | |
| 732 | launch, make one in live mode and put its secret). Its signing secret | |
| 733 | (`whsec_…`: the destination, Signing secret, Reveal) is the billing | |
| 734 | Worker's secret: | |
| 735 | ||
| 736 | ```sh | |
| 737 | cd services/billing && npx wrangler secret put STRIPE_WEBHOOK_SECRET | |
| 738 | ``` | |
| 739 | ||
| 740 | Without it every event is refused with 400. After rolling the secret in | |
| 741 | Stripe, put the new one; during the roll Stripe signs with both, so there | |
| 742 | is no gap. The event list need not be exact: billing adds any event it | |
| 743 | handles that the destination does not send (daily, or **Fix destination** | |
| 744 | in sudo → Stripe), and enables it again if Stripe disabled it. It never | |
| 745 | changes the secret. sudo → Stripe shows whether the secret is set, the | |
| 746 | destination and its status, missing events, and the latest events. | |
| 747 | Events are claimed once each in `stripe_events`; a handler that fails | |
| 748 | forgets its claim, and Stripe retries. | |
| Billing keeps Stripe's view itself: the saved card on the account, missed events replayed every 15 minutes, and the endpoint kept | 749 | |
| 750 | **What is kept, and how it stays current** | |
| 751 | ||
| 752 | | Kept | Where | Refreshed by | | |
| 753 | | --- | --- | --- | | |
| 754 | | Saved card (brand, last 4, expiry) | `accounts.card_*`, `card_synced_at` | `customer.updated`, `payment_method.*`, `setup_intent.succeeded`; forgotten when g1t sets a default card, so the next read asks once; the daily pass for cards older than 7 days | | |
| 755 | | Plans | `subscriptions` | `customer.subscription.*`, `invoice.*`; a read asks Stripe at most hourly once a period is over; the daily pass for rows older than a day | | |
| 756 | | Payments, refunds, disputes | ledger, `checkouts`, invoices | their events | | |
| 757 | ||
| 758 | **Every cron run (every 15 minutes)** replays missed events: Stripe's event | |
| 759 | list from an hour before `stripe_sync.through`, oldest first, through the | |
| 760 | same once-only claim. `through` moves to 5 minutes before now when all were | |
| 761 | handled, back to the first failure otherwise, and stays when more than | |
| 762 | 1,000 events were listed. Claims stuck at `handling` for 10 minutes are | |
| 763 | dropped so the replay retries them. The first run reads 3 days back. | |
| 764 | ||
| Stripe's webhook secret is a Worker secret, STRIPE_WEBHOOK_SECRET, from a destination made in Stripe's dashboard | 765 | **Daily** (`keeper::DAILY`): the destination at billing's address is |
| 766 | enabled again if Stripe disabled it and given any missing event, audited as | |
| 767 | `stripe`/`webhook`; then up to 25 stale cards and 25 stale plans are read | |
| 768 | again. | |
| Billing keeps Stripe's view itself: the saved card on the account, missed events replayed every 15 minutes, and the endpoint kept | 769 | |
| 770 | **What still calls Stripe on a request**: starting a payment page, a plan | |
| 771 | or a card check; opening the billing portal; settling a page the person | |
| 772 | came back from; renaming a workspace (the customer's name). Nothing a page | |
| 773 | view reads. | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 774 | |
| Merge Stripe Tax, the card fee on card payments, and one free workspace per person | 775 | ## Tax and the card fee |
| 776 | ||
| 777 | Owner decision 2026-10-08. Code: `services/billing/src/tax.rs` (what is | |
| 778 | kept, the address hold), `stripe.rs` (every request's tax fields), | |
| 779 | `invoices.rs`, `webhooks.rs`, `ai.rs`; migration | |
| 780 | `0043_tax_and_card_fees.sql`. | |
| 781 | ||
| 782 | ### What Stripe is asked | |
| 783 | ||
| 784 | Every price excludes tax (`tax_behavior=exclusive`) and carries tax code | |
| 785 | `txcd_10103001` (software as a service, business use; the card fee too, | |
| 786 | since a fee for paying for a sale follows the sale). Fields are valid for | |
| 787 | `2025-02-24.acacia`. | |
| 788 | ||
| 789 | | Request | Tax | Card fee | | |
| 790 | | --- | --- | --- | | |
| 791 | | Checkout, plan or Security (`subscription_fields`) | `automatic_tax[enabled]`, `billing_address_collection=required`, `tax_id_collection[enabled]`, with a customer `customer_update[address]=auto` and `[name]=auto`; the subscription keeps automatic tax for every renewal | A second recurring line, *Card processing fee* | | |
| 792 | | Subscription on a saved card (`saved_subscription_fields`) | `automatic_tax[enabled]`; a customer Stripe Tax cannot place fails, and the person is sent to Checkout, which asks for the address | `items[1]`, the `card_fee` product | | |
| 793 | | Checkout, prepay (`prepay_fields`) | As above | By card only; none by bank transfer | | |
| 794 | | Checkout, AI credit (`credit_fields`) | As above | Its own line | | |
| 795 | | Checkout, card check (`card_check_fields`) | Setup mode: nothing charged, nothing taxed. `billing_address_collection=required`, and the card's address is copied onto a customer with none (`fill_address`) | — | | |
| 796 | | Auto-reload (`charge_saved`) | `POST /tax/calculations` first (credit and fee as lines), the PaymentIntent for the total, then `POST /tax/transactions/create_from_calculation` with the PaymentIntent as reference; a refund reverses its share (`create_reversal`, `mode=partial`) | In the calculation and the amount | | |
| 797 | | Workspace invoice, month close and threshold (`invoice_workspace`) | `automatic_tax[enabled]` on the draft; each item `tax_behavior`, `tax_code` | An item *Card processing fee*, when the default payment method is a card | | |
| 798 | | Enterprise invoice (`invoice_enterprise`) | The same | Never | | |
| 799 | | Products (`Stripe::product`) | Made with `tax_code`; one found without it is given it | The `card_fee` product, `metadata[g1t]=card_fee` | | |
| 800 | ||
| 801 | ### What is kept | |
| 802 | ||
| 803 | A payment credits the balance with what it paid for, never its tax or fee: | |
| 804 | prepay credits the page's `amount_subtotal` less the fee line | |
| 805 | (`credit_prepayment`), a workspace invoice `amount_paid − tax − fee` | |
| 806 | (`credit_invoice`), AI credit its credit amount, and `plan_payments` the | |
| 807 | plan's invoice less its tax and *Card processing fee* lines | |
| 808 | (`stripe::invoice_split`). Each payment's tax and fee are rows in | |
| 809 | `tax_and_fees` (`<reference>/tax`, `<reference>/card_fee`; the enterprise's | |
| 810 | account id in `workspace` for its invoices), with the PaymentIntent, so a | |
| 811 | refund (`charge.refunded`) gives back the balance, tax and fee in | |
| 812 | proportion (`tax::refund_split`, negative rows under `refund/<charge>/…`). | |
| 813 | `workspace_invoices.fee_micros` and `tax_micros` sit beside each usage | |
| 814 | invoice. | |
| 815 | ||
| 816 | - **Statement.** *Tax* and *Card processing fees* are their own lines per | |
| 817 | day (`StatementLine.passed_micros`), never in `charged_micros`; totals | |
| 818 | `tax_micros`, `card_fee_micros`. The CSV has a row a day for each. | |
| 819 | - **Margin.** Cash never holds them, so margin is untouched. sudo → Costs | |
| 820 | shows **Tax collected** and **Card fees passed on** for the range | |
| 821 | (`OverallMargin.tax_collected_micros`, `card_fees_micros`). Tax is owed to | |
| 822 | the authorities: file it from Stripe Tax's reports, never from g1t's. | |
| 823 | ||
| 824 | ### No address | |
| 825 | ||
| 826 | Stripe Tax needs a country (in the US a ZIP code, in Canada a postal code | |
| 827 | or province: `stripe::address_places_customer`). Before a workspace | |
| 828 | invoice is drafted, g1t checks the customer; without an address, or when | |
| 829 | Stripe leaves the draft at `requires_location_inputs` or refuses with | |
| 830 | `customer_tax_location_invalid`, nothing is charged: | |
| 831 | `accounts.tax_address_needed_at` is set, the owners are emailed once | |
| 832 | (`notify_owners`), and Billing shows **Add a billing address**. Saving | |
| 833 | Invoice details with an address Stripe Tax can use clears it, as does a | |
| 834 | charge that goes through. Work is not stopped for it; the limits still | |
| 835 | apply. Auto-reload without an address fails like a declined card (turned | |
| 836 | off, owners told). An enterprise's invoice is not sent without an address: | |
| 837 | sudo → the enterprise → Invoices → **Billing address** (`admin_enterprise_address`, | |
| 838 | with its tax ID; audited `billing_address`). | |
| 839 | ||
| 840 | ### The card fee | |
| 841 | ||
| 842 | `card_fee_cents` grosses Stripe's fee up so the amount paid for is left | |
| 843 | after it: `(amount + 30¢) / (1 − 2.9%)`, rounded up; $0.91 on $20, $1.06 | |
| 844 | on $25. It is worked out on the amount before tax, so Stripe's fee on the | |
| 845 | tax itself (a few cents) is g1t's. It is shown before paying: the plan card | |
| 846 | and pricing page (`Plan.card_fee_cents`), AI credit (*Card processing fee | |
| 847 | $1.06, plus tax where it applies*), Prepay. Never on a bank transfer or an | |
| 848 | enterprise's (`send_invoice`) invoice. Meters stay at cost + 20% and models | |
| 849 | at the provider's price plus the agent rate (price versions in migration | |
| 850 | 0040); the fee is passed through, not margin. | |
| 851 | ||
| 852 | ### Tax-exempt customers and tax IDs | |
| 853 | ||
| 854 | g1t never sets `tax_exempt`. For a customer who sends an exemption | |
| 855 | certificate, set it in Stripe's dashboard (Customers → the customer → Tax | |
| 856 | status: Exempt, or Reverse charge); Billing then says so. Tax IDs come | |
| 857 | from Checkout (`tax_id_collection`) or Invoice details (`set_billing_details`, | |
| 858 | validated against Stripe's types in `details::TAX_ID_TYPES`); Stripe checks | |
| 859 | EU VAT numbers and Stripe Tax applies a reverse charge where it should. | |
| 860 | Billing shows Stripe's `verification.status`. | |
| 861 | ||
| 862 | ### In Stripe's dashboard (not done by g1t) | |
| 863 | ||
| 864 | 1. **Settings → Tax → Get started**: turn on Stripe Tax in live and test | |
| 865 | mode. | |
| 866 | 2. **Origin address**: Flagon, Inc.'s head office address. | |
| 867 | 3. **Default tax code**: Software as a service, business use | |
| 868 | (`txcd_10103001`); **default tax behavior**: exclusive. | |
| 869 | 4. **Registrations**: add each jurisdiction where Flagon is registered to | |
| 870 | collect (its home state at least; then states as thresholds are | |
| 871 | crossed, which Stripe Tax's monitoring flags; the EU's OSS and the UK | |
| 872 | if selling there). Stripe collects only where a registration exists. | |
| 873 | 5. **Customer portal**: tax ID and address updates are already allowed | |
| 874 | (`portal_configuration`). | |
| 875 | 6. Refund an invoice through a **credit note**, so its tax is reversed in | |
| 876 | Stripe Tax. | |
| 877 | ||
| 878 | ## Free workspaces | |
| 879 | ||
| 880 | Owner decision 2026-10-08: one free workspace per person, and a free | |
| 881 | workspace adds no one. Billing answers one question, `free_workspaces` | |
| 882 | (`credits.rs`): which of the given workspaces are on no paid plan | |
| 883 | (`plan_kind_for` is `Free`). The plan, an enterprise's terms and a 100% | |
| 884 | discount (flagon-io) count as paid; with payments off nothing is free. | |
| 885 | Identity asks it (`services/identity/src/paid.rs`) and refuses with | |
| 886 | `payment_required`: | |
| 887 | ||
| 888 | | Where | Refused when | | |
| 889 | | --- | --- | | |
| 890 | | `create_workspace` | The person owns any free workspace. Several from before are kept (grandfathered); none can be added until each is paid for or deleted. | | |
| 891 | | `add_member`, `invite_member` | The workspace is free. | | |
| 892 | | `add_collaborator` | Someone outside a free workspace (a username who is not a member, or an address). Members' roles are fine. | | |
| 893 | | `accept_invite`, `respond_repo_invitation` | The invite's workspace (or repository's) is free now: it waits. A sign-up with such an invite makes the account without joining. | | |
| 894 | ||
| 895 | `@g1t` is never counted as someone added. If billing cannot be asked, the | |
| 896 | change is refused for now ("try again"), never let through. The site says | |
| 897 | so first (New workspace, People, a repository's Access, from the same RPC); | |
| 898 | the API and MCP pass identity's refusal on as `402`. | |
| 899 | ||
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 900 | ## The Security and quality activation |
| 901 | ||
| 902 | A second monthly subscription a workspace can hold beside the plan | |
| 903 | (`Feature::Security`, `feature = 'security'` in `subscriptions`). It turns | |
| 904 | on the security suite's paid features for the workspace's private | |
| 905 | repositories: custom secret patterns, validity checks, delegated bypass, | |
| 906 | code scanning, dependency review and the security overview. Public | |
| 907 | repositories have them free; secret scanning, push protection, | |
| 908 | vulnerability alerts and security updates are free everywhere. | |
| 909 | ||
| 910 | - **Price.** The price book's `security_activation` meter (unit | |
| 911 | `workspace-month`, `source` `list`, markup 0): 10,000,000 micros, $10, | |
| 912 | from migration `0037_security_activation.sql` with its first | |
| 913 | `price_versions` row and a public `price_changes` record. `plan()` and the | |
| 914 | `prices` RPC read it (`features::security_plan_at`); if the price book | |
| 915 | cannot be read, $10. Nothing in the web app hard-codes it. A change is a | |
| 916 | new price version, like any other: noticed on the pricing page and | |
| 917 | applied from its `effective_at` to new subscriptions. Subscriptions | |
| 918 | already running keep the amount Stripe has until they are changed in | |
| 919 | Stripe. | |
| 920 | - **Stripe.** Its own subscription and its own product, tagged | |
| 921 | `metadata[g1t]=security` (the plan's is `plan`). Started from the Billing | |
| 922 | page with `subscribe` (`feature: security`) on the checked card, or | |
| 923 | through Checkout; ended with `cancel_subscription` (`feature: security`) | |
| 924 | at the period's end. Its invoices count in `plan_payments` like the | |
| 925 | plan's, as paid revenue. | |
| 926 | - **Who has it.** `has_feature(workspace, security)`: on with an active | |
| 927 | subscription, with comped terms or as an enterprise's workspace, or when | |
| 928 | Stripe is not configured. The plan's allowance (`allowances.plan`) does | |
| 929 | not include it. Refusals are `PaymentRequired` with the price from the | |
| 930 | price book and the Billing page's address. | |
| 931 | - **Where it is checked.** The security service, on each paid call for a | |
| 932 | private repository (`suite::entitled`) and before using custom patterns | |
| 933 | in a push (`patterns_for`). When billing cannot be reached it is taken | |
| 934 | as off: a paid feature waits rather than running unpaid. | |
| 935 | - **Fixes.** "Fix with g1t" runs g1t's agent, charged as agent usage, never | |
| 936 | to the activation. | |
| 937 | - **Sales figures.** MRR in sudo counts `feature = 'plan'` only; the | |
| 938 | activation's subscriptions are not in it yet. |
This file's history is long; its oldest lines are credited to the oldest commit read.