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.
| Prices keep themselves current with what g1t pays | 1 | //! Keeps every price current with what g1t actually pays. |
| 2 | //! | |
| 3 | //! g1t passes its own costs through, so a price is only right while the | |
| 4 | //! cost under it is. Two jobs keep them right, on the billing service's | |
| 5 | //! cron: | |
| 6 | //! | |
| 7 | //! - **Settling runs** (every 15 minutes). A model run is charged when it | |
| 8 | //! finishes at what the sandbox reported. Each of g1t's hosted runs goes | |
| 9 | //! through its AI Gateway, which prices every request at the provider's | |
| 10 | //! current rates and logs it with the run's session. Settling sums those | |
| 11 | //! logs and corrects the charge to the gateway's figure, with a | |
| 12 | //! correction on the statement. A run whose sandbox died before | |
| 13 | //! reporting is charged here instead of never. | |
| 14 | //! - **Checking costs** (daily). What Cloudflare billed g1t's account, from | |
| 15 | //! its usage API, is measured against how much was used: Containers | |
| 16 | //! against the seconds containers ran, Workers for Platforms per request | |
| 17 | //! and per CPU millisecond. When a measured cost moves, the price book | |
| 18 | //! moves with it, since each price is its cost plus a set markup, and | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 19 | //! the change is recorded where anyone can see it. A measurement goes |
| 20 | //! through `pricing` as a proposal: a small move is applied on its own (a | |
| 21 | //! rise only after customers have had notice), a large or suspect one | |
| 22 | //! waits for staff in sudo, so one odd day of data cannot reprice | |
| 23 | //! everything. | |
| Prices keep themselves current with what g1t pays | 24 | |
| 25 | use g1t_contracts::billing::{EntryKind, MICROS_PER_DOLLAR, Price, PriceBook, PriceChange}; | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 26 | |
| Prices keep themselves current with what g1t pays | 27 | use g1t_contracts::time::rfc3339; |
| 28 | use g1t_kit::now_ms; | |
| 29 | use serde::Deserialize; | |
| 30 | use serde_json::{Value, json}; | |
| 31 | use worker::{Env, Fetch, Headers, Method, Request, RequestInit, Result}; | |
| 32 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 33 | use crate::{Billing, RunRow}; |
| Prices keep themselves current with what g1t pays | 34 | |
| 35 | /// The cron that also checks costs against Cloudflare's bill. | |
| 36 | pub(crate) const DAILY: &str = "17 4 * * *"; | |
| Merge platform pause and the hourly usage watcher: staff can pause compute, schedules, indexing or renders for everyone, the watcher emails on a breach and is never blind quietly, and the models proxy holds each run to its cap (billing 0051, integrations 0006) | 37 | /// The quarter-hourly tick: settling, and once an hour the platform watch. |
| 38 | pub(crate) const QUARTER_HOURLY: &str = "*/15 * * * *"; | |
| Prices keep themselves current with what g1t pays | 39 | |
| 40 | /// A run is settled once its logs have had time to land. | |
| 41 | const SETTLE_AFTER_MS: u64 = 5 * 60 * 1000; | |
| 42 | /// A run with no gateway logs after this is left as reported. | |
| 43 | const GIVE_UP_AFTER_MS: u64 = 3 * 60 * 60 * 1000; | |
| 44 | /// A run never finished after this died without reporting. | |
| 45 | const ABANDONED_AFTER_MS: u64 = 3 * 60 * 60 * 1000; | |
| 46 | ||
| 47 | /// Where the keeper reads what g1t pays. | |
| 48 | pub(crate) struct Keeper { | |
| 49 | /// `CLOUDFLARE_USAGE_TOKEN`: Billing, Account Analytics and AI Gateway, | |
| 50 | /// read only. | |
| 51 | token: Option<String>, | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 52 | /// What reads the bill for `costs`: `CLOUDFLARE_BILLING_TOKEN` |
| 53 | /// (Account: Billing Read and Account Analytics Read), or the usage | |
| 54 | /// token, which has both. | |
| 55 | billing_token: Option<String>, | |
| Prices keep themselves current with what g1t pays | 56 | account: String, |
| 57 | gateway: String, | |
| 58 | } | |
| 59 | ||
| 60 | impl Keeper { | |
| 61 | pub(crate) fn from_env(env: &Env) -> Self { | |
| 62 | let var = |name: &str| env.var(name).map(|v| v.to_string()).unwrap_or_default(); | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 63 | let secret = |name: &str| env.secret(name).ok().map(|v| v.to_string()).filter(|v| !v.is_empty()); |
| 64 | let token = secret("CLOUDFLARE_USAGE_TOKEN"); | |
| Prices keep themselves current with what g1t pays | 65 | Keeper { |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 66 | billing_token: secret("CLOUDFLARE_BILLING_TOKEN").or_else(|| token.clone()), |
| 67 | token, | |
| Prices keep themselves current with what g1t pays | 68 | account: var("CLOUDFLARE_ACCOUNT_ID"), |
| 69 | gateway: var("AI_GATEWAY_ID"), | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | async fn send(&self, method: Method, url: &str, body: Option<Value>) -> Result<Value> { | |
| 74 | let Some(token) = &self.token else { | |
| 75 | return Err(worker::Error::RustError("no CLOUDFLARE_USAGE_TOKEN".into())); | |
| 76 | }; | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 77 | send_with(token, method, url, body).await |
| 78 | } | |
| 79 | ||
| 80 | /// Whether Cloudflare's bill can be read. | |
| 81 | pub(crate) fn can_read_bill(&self) -> bool { | |
| 82 | self.billing_token.is_some() && !self.account.is_empty() | |
| 83 | } | |
| 84 | ||
| 85 | fn billing_token(&self) -> Result<&str> { | |
| 86 | self.billing_token | |
| 87 | .as_deref() | |
| 88 | .ok_or_else(|| worker::Error::RustError("no CLOUDFLARE_BILLING_TOKEN or CLOUDFLARE_USAGE_TOKEN".into())) | |
| 89 | } | |
| 90 | ||
| Merge Cloudflare's usage over its billing cycle: every page read, included amounts once a cycle, a projection, test-mode charges never money in (billing 0052) | 91 | /// Billable usage from `from` to `to` (dates), every page of it, as one |
| 92 | /// answer (`result` holds all the rows), and how many pages it took. | |
| 93 | /// An answer that says it failed is returned as it is. | |
| 94 | pub(crate) async fn billable_usage_pages(&self, from: &str, to: &str) -> Result<(Value, u32)> { | |
| 95 | usage_pages(self.billing_token()?, &self.api(&format!("/billable-usage?from={from}&to={to}"))).await | |
| Prices keep themselves current with what g1t pays | 96 | } |
| 97 | ||
| Costs: Cloudflare's subscriptions read from Cloudflare each day, the estimate only until then; sudo's costs split into Costs & margin and Bill & pricing | 98 | /// The account's subscriptions (Workers Paid, add-ons), as Cloudflare |
| 99 | /// answers them. Needs Account: Billing Read. | |
| 100 | pub(crate) async fn subscriptions_body(&self) -> Result<Value> { | |
| 101 | send_with(self.billing_token()?, Method::Get, &self.api("/subscriptions"), None).await | |
| 102 | } | |
| 103 | ||
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 104 | /// A GraphQL Analytics query, as Cloudflare answers it, errors and all. |
| 105 | pub(crate) async fn graphql(&self, body: Value) -> Result<Value> { | |
| 106 | send_with(self.billing_token()?, Method::Post, "https://api.cloudflare.com/client/v4/graphql", Some(body)).await | |
| 107 | } | |
| 108 | ||
| 109 | pub(crate) fn account(&self) -> &str { | |
| 110 | &self.account | |
| 111 | } | |
| 112 | ||
| Prices keep themselves current with what g1t pays | 113 | fn api(&self, path: &str) -> String { |
| 114 | format!("https://api.cloudflare.com/client/v4/accounts/{}{path}", self.account) | |
| 115 | } | |
| 116 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 117 | /// AI Gateway's id, empty when there is none. |
| 118 | pub(crate) fn gateway(&self) -> &str { | |
| 119 | &self.gateway | |
| 120 | } | |
| 121 | ||
| Billing: AI Gateway's analytics are read with the token that can see them, and a gateway that priced nothing is said | 122 | /// A GraphQL query over AI Gateway's analytics: with the keeper's token |
| Merge costs and margin review: gateway query, own spend, discount meters, superseded rises | 123 | /// (AI Gateway Read) first, and on failure with the bill's. A token |
| 124 | /// without Account Analytics Read gets an error ("caller does not hold | |
| 125 | /// any of the required permissions for this dataset"); when the answer | |
| 126 | /// has no rows, `gateway_visible` tells a token that cannot see the | |
| 127 | /// gateway from a gateway nothing went through. | |
| Billing: AI Gateway's analytics are read with the token that can see them, and a gateway that priced nothing is said | 128 | pub(crate) async fn gateway_graphql(&self, body: Value) -> Result<Value> { |
| 129 | let Some(token) = &self.token else { | |
| 130 | return self.graphql(body).await; | |
| 131 | }; | |
| 132 | match send_with(token, Method::Post, "https://api.cloudflare.com/client/v4/graphql", Some(body.clone())).await { | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 133 | Ok(answer) => Ok(answer), |
| Billing: AI Gateway's analytics are read with the token that can see them, and a gateway that priced nothing is said | 134 | Err(error) => match &self.billing_token { |
| 135 | Some(billing) if billing != token => self.graphql(body).await, | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 136 | _ => Err(error), |
| 137 | }, | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| Merge costs and margin review: gateway query, own spend, discount meters, superseded rises | 141 | /// Whether the token AI Gateway's analytics are read with can see the |
| 142 | /// gateway: the REST API answers 403 for a token without AI Gateway | |
| 143 | /// Read and 404 for a gateway id that is not there. None when the | |
| 144 | /// answer says neither (Cloudflare down, no token). | |
| 145 | pub(crate) async fn gateway_visible(&self) -> Option<bool> { | |
| 146 | let token = self.token.as_deref().or(self.billing_token.as_deref())?; | |
| 147 | match send_with(token, Method::Get, &self.api(&format!("/ai-gateway/gateways/{}", self.gateway)), None).await { | |
| 148 | Ok(_) => Some(true), | |
| 149 | Err(error) => refused(&error.to_string()).then_some(false), | |
| 150 | } | |
| 151 | } | |
| 152 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 153 | /// What AI Gateway priced a session's requests at, and how many there |
| 154 | /// were, with the requests it had no price for. | |
| 155 | async fn session_cost(&self, session: &str) -> Result<SessionCost> { | |
| 156 | let mut total = SessionCost { complete: true, ..SessionCost::default() }; | |
| 157 | for page in 1..=MAX_LOG_PAGES { | |
| The keeper reads Cloudflare as it really answers | 158 | // The filter goes as URL-encoded JSON; the bracket form is |
| 159 | // ignored, and would sum every log there is. Session ids are | |
| 160 | // [a-z0-9_], which need no escaping inside it. | |
| 161 | let filter = format!( | |
| 162 | "%5B%7B%22key%22%3A%22metadata.value%22%2C%22operator%22%3A%22eq%22%2C%22value%22%3A%5B%22{session}%22%5D%7D%5D" | |
| 163 | ); | |
| Prices keep themselves current with what g1t pays | 164 | let url = self.api(&format!( |
| The keeper reads Cloudflare as it really answers | 165 | "/ai-gateway/gateways/{}/logs?per_page=50&page={page}&filters={filter}", |
| Prices keep themselves current with what g1t pays | 166 | self.gateway |
| 167 | )); | |
| 168 | let body = self.send(Method::Get, &url, None).await?; | |
| 169 | let logs = body["result"].as_array().cloned().unwrap_or_default(); | |
| 170 | for log in &logs { | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 171 | total.add(log); |
| Prices keep themselves current with what g1t pays | 172 | } |
| 173 | if logs.len() < 50 { | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 174 | return Ok(total); |
| Prices keep themselves current with what g1t pays | 175 | } |
| 176 | } | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 177 | // More logs than were read: what was read is less than the run. |
| 178 | total.complete = false; | |
| 179 | Ok(total) | |
| Prices keep themselves current with what g1t pays | 180 | } |
| 181 | ||
| The keeper reads Cloudflare as it really answers | 182 | /// The account's billable usage, one row per service per day, as |
| 183 | /// Cloudflare reports it. | |
| Prices keep themselves current with what g1t pays | 184 | async fn billable_usage(&self, from: &str, to: &str) -> Result<Vec<UsageRow>> { |
| Merge Cloudflare's usage over its billing cycle: every page read, included amounts once a cycle, a projection, test-mode charges never money in (billing 0052) | 185 | let Some(token) = &self.token else { |
| 186 | return Err(worker::Error::RustError("no CLOUDFLARE_USAGE_TOKEN".into())); | |
| 187 | }; | |
| 188 | let (body, _) = usage_pages(token, &self.api(&format!("/billable-usage?from={from}&to={to}"))).await?; | |
| Prices keep themselves current with what g1t pays | 189 | let rows = body["result"].as_array().cloned().unwrap_or_default(); |
| 190 | Ok(rows.iter().filter_map(UsageRow::from_value).collect()) | |
| 191 | } | |
| 192 | ||
| The keeper reads Cloudflare as it really answers | 193 | /// What g1t's containers used from `since` to `until` (dates), as |
| 194 | /// Cloudflare bills it: memory in byte-seconds, and CPU seconds. | |
| 195 | async fn container_usage(&self, since: &str, until: &str) -> Result<ContainerUsage> { | |
| 196 | let query = "query ($account: String!, $since: Date!, $until: Date!) { | |
| Prices keep themselves current with what g1t pays | 197 | viewer { accounts(filter: { accountTag: $account }) { |
| The keeper reads Cloudflare as it really answers | 198 | containersUsageAdaptiveGroups(limit: 1000, filter: { date_geq: $since, date_leq: $until }) { |
| 199 | sum { cpuTimeSec allocatedMemory } | |
| Prices keep themselves current with what g1t pays | 200 | } |
| 201 | } } | |
| 202 | }"; | |
| 203 | let body = self | |
| 204 | .send( | |
| 205 | Method::Post, | |
| 206 | "https://api.cloudflare.com/client/v4/graphql", | |
| 207 | Some(json!({ "query": query, "variables": { "account": self.account, "since": since, "until": until } })), | |
| 208 | ) | |
| 209 | .await?; | |
| The keeper reads Cloudflare as it really answers | 210 | let groups = body["data"]["viewer"]["accounts"][0]["containersUsageAdaptiveGroups"] |
| Prices keep themselves current with what g1t pays | 211 | .as_array() |
| 212 | .cloned() | |
| 213 | .unwrap_or_default(); | |
| The keeper reads Cloudflare as it really answers | 214 | Ok(groups.iter().fold(ContainerUsage::default(), |total, g| ContainerUsage { |
| 215 | cpu_seconds: total.cpu_seconds + g["sum"]["cpuTimeSec"].as_f64().unwrap_or(0.0), | |
| 216 | memory_byte_seconds: total.memory_byte_seconds + g["sum"]["allocatedMemory"].as_f64().unwrap_or(0.0), | |
| 217 | })) | |
| Prices keep themselves current with what g1t pays | 218 | } |
| 219 | } | |
| 220 | ||
| Merge costs and margin review: gateway query, own spend, discount meters, superseded rises | 221 | /// Whether an error from `send_with` is Cloudflare saying no to the token |
| 222 | /// (401, 403) or that there is no such thing for it (404), rather than | |
| 223 | /// failing. | |
| 224 | pub(crate) fn refused(error: &str) -> bool { | |
| 225 | ["Cloudflare answered 401", "Cloudflare answered 403", "Cloudflare answered 404"].iter().any(|s| error.contains(s)) | |
| 226 | } | |
| 227 | ||
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 228 | /// A request to Cloudflare's API with a bearer token; anything but 200 is |
| 229 | /// an error with what Cloudflare said. | |
| Merge Cloudflare's usage over its billing cycle: every page read, included amounts once a cycle, a projection, test-mode charges never money in (billing 0052) | 230 | /// The most pages of billable usage read in one go: far more than a few |
| 231 | /// months of g1t's meters. | |
| 232 | const MAX_USAGE_PAGES: u32 = 50; | |
| 233 | ||
| 234 | /// Every page of a billable-usage answer, its rows together. Before | |
| 235 | /// 2026-10-09 only the first page was read. | |
| 236 | async fn usage_pages(token: &str, url: &str) -> Result<(Value, u32)> { | |
| 237 | let mut rows: Vec<Value> = Vec::new(); | |
| 238 | let mut pages = 0; | |
| 239 | let mut next = url.to_owned(); | |
| 240 | loop { | |
| 241 | let body = send_with(token, Method::Get, &next, None).await?; | |
| 242 | if body["success"] == Value::Bool(false) { | |
| 243 | return Ok((body, pages + 1)); | |
| 244 | } | |
| 245 | pages += 1; | |
| 246 | rows.extend(body["result"].as_array().cloned().unwrap_or_default()); | |
| 247 | match crate::costs::next_page(&body, pages).filter(|_| pages < MAX_USAGE_PAGES) { | |
| 248 | Some(query) => next = format!("{url}&{query}"), | |
| 249 | None => break, | |
| 250 | } | |
| 251 | } | |
| 252 | Ok((json!({ "success": true, "result": rows }), pages)) | |
| 253 | } | |
| 254 | ||
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 255 | async fn send_with(token: &str, method: Method, url: &str, body: Option<Value>) -> Result<Value> { |
| 256 | let headers = Headers::new(); | |
| 257 | headers.set("authorization", &format!("Bearer {token}"))?; | |
| 258 | headers.set("content-type", "application/json")?; | |
| 259 | let mut init = RequestInit::new(); | |
| 260 | init.with_method(method).with_headers(headers); | |
| 261 | if let Some(body) = body { | |
| 262 | init.with_body(Some(body.to_string().into())); | |
| 263 | } | |
| 264 | let mut response = Fetch::Request(Request::new_with_init(url, &init)?).send().await?; | |
| 265 | let status = response.status_code(); | |
| 266 | let value: Value = response.json().await.unwrap_or(Value::Null); | |
| 267 | if status != 200 { | |
| 268 | return Err(worker::Error::RustError(format!("Cloudflare answered {status}: {value}"))); | |
| 269 | } | |
| 270 | Ok(value) | |
| 271 | } | |
| 272 | ||
| The keeper reads Cloudflare as it really answers | 273 | #[derive(Debug, Default, Clone, Copy)] |
| 274 | pub(crate) struct ContainerUsage { | |
| 275 | cpu_seconds: f64, | |
| 276 | memory_byte_seconds: f64, | |
| 277 | } | |
| 278 | ||
| 279 | /// g1t's sandboxes: Containers' standard-1, half a vCPU, 4 GiB, 8 GB disk. | |
| 280 | const SANDBOX_GIB: f64 = 4.0; | |
| 281 | const SANDBOX_DISK_GB: f64 = 8.0; | |
| 282 | const GIB: f64 = 1024.0 * 1024.0 * 1024.0; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 283 | /// The Durable Object behind each container is billed for as long as the |
| 284 | /// container runs, at 128 MB. | |
| 285 | const SANDBOX_DO_GB: f64 = 0.125; | |
| The keeper reads Cloudflare as it really answers | 286 | |
| 287 | /// Cloudflare's published Containers rates, in dollars, used for any rate | |
| 288 | /// the bill does not show yet (while usage is inside the included amount). | |
| 289 | const LIST_MEMORY_GIB_SECOND: f64 = 0.000_002_5; | |
| 290 | const LIST_DISK_GB_SECOND: f64 = 0.000_000_07; | |
| 291 | const LIST_VCPU_SECOND: f64 = 0.000_02; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 292 | /// Durable Objects duration: $12.50 per million GB-seconds. |
| 293 | const LIST_DO_GB_SECOND: f64 = 0.000_012_5; | |
| The keeper reads Cloudflare as it really answers | 294 | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 295 | /// What one second of a sandbox costs whatever it does, in millionths of a |
| 296 | /// dollar: its memory and disk, and the Durable Object behind it, for the | |
| 297 | /// whole second. CPU is billed only while busy, on top. | |
| 298 | pub(crate) fn sandbox_base_micros(memory: f64, disk: f64, durable_object: f64) -> f64 { | |
| 299 | (SANDBOX_GIB * memory + SANDBOX_DISK_GB * disk + SANDBOX_DO_GB * durable_object) * MICROS_PER_DOLLAR as f64 | |
| 300 | } | |
| 301 | ||
| 302 | /// What one second of a sandbox costs on average, in millionths of a | |
| 303 | /// dollar: its base, and the CPU sandboxes actually use per second of | |
| 304 | /// running. Runs that report their own CPU are priced on it instead (see | |
| 305 | /// `run_cost`). | |
| 306 | pub(crate) fn sandbox_second_micros(usage: ContainerUsage, memory: f64, disk: f64, vcpu: f64, durable_object: f64) -> Option<f64> { | |
| The keeper reads Cloudflare as it really answers | 307 | let instance_seconds = usage.memory_byte_seconds / (SANDBOX_GIB * GIB); |
| 308 | if instance_seconds < 3600.0 { | |
| 309 | return None; | |
| 310 | } | |
| 311 | let cpu_share = usage.cpu_seconds / instance_seconds; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 312 | Some(sandbox_base_micros(memory, disk, durable_object) + cpu_share * vcpu * MICROS_PER_DOLLAR as f64) |
| 313 | } | |
| 314 | ||
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 315 | /// How much more a second of a larger machine's memory and disk (and the |
| 316 | /// Durable Object behind it) costs than the standard sandbox's, at | |
| 317 | /// Cloudflare's list rates: 1 for the standard machine. | |
| 318 | pub(crate) fn base_scale(memory_gib: f64, disk_gb: f64) -> f64 { | |
| 319 | let base = |memory: f64, disk: f64| memory * LIST_MEMORY_GIB_SECOND + disk * LIST_DISK_GB_SECOND + SANDBOX_DO_GB * LIST_DO_GB_SECOND; | |
| 320 | base(memory_gib, disk_gb) / base(SANDBOX_GIB, SANDBOX_DISK_GB) | |
| 321 | } | |
| 322 | ||
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 323 | /// What a run that reported its own CPU cost g1t: its base for every |
| 324 | /// second, and its vCPU-seconds at the vCPU rate. | |
| 325 | pub(crate) fn run_cost(seconds: i64, cpu_seconds: f64, base_per_second: f64, per_vcpu_second: f64) -> f64 { | |
| 326 | seconds.max(0) as f64 * base_per_second + cpu_seconds.max(0.0) * per_vcpu_second | |
| The keeper reads Cloudflare as it really answers | 327 | } |
| 328 | ||
| 329 | /// A unit's marginal rate from the bill: the median, over the days that | |
| 330 | /// were charged, of cost over quantity. None while nothing was charged. | |
| 331 | pub(crate) fn billed_rate(rows: &[&UsageRow]) -> Option<f64> { | |
| 332 | let mut rates: Vec<f64> = rows | |
| 333 | .iter() | |
| 334 | .filter(|r| r.cost > 0.0 && r.quantity > 0.0) | |
| 335 | .map(|r| r.cost / r.quantity) | |
| 336 | .collect(); | |
| 337 | if rates.is_empty() { | |
| 338 | return None; | |
| 339 | } | |
| 340 | rates.sort_by(f64::total_cmp); | |
| 341 | Some(rates[rates.len() / 2]) | |
| 342 | } | |
| 343 | ||
| Prices keep themselves current with what g1t pays | 344 | /// One line of Cloudflare's billable usage. |
| 345 | #[derive(Debug, Clone)] | |
| 346 | pub(crate) struct UsageRow { | |
| 347 | period_start: String, | |
| 348 | period_end: String, | |
| 349 | service: String, | |
| 350 | unit: String, | |
| 351 | quantity: f64, | |
| 352 | cost: f64, | |
| 353 | } | |
| 354 | ||
| 355 | impl UsageRow { | |
| 356 | /// Read leniently: the API is new, and its field names are FOCUS's. | |
| 357 | fn from_value(row: &Value) -> Option<Self> { | |
| 358 | let text = |keys: &[&str]| keys.iter().find_map(|k| row[*k].as_str()).unwrap_or_default().to_owned(); | |
| 359 | let number = |keys: &[&str]| { | |
| 360 | keys.iter() | |
| 361 | .find_map(|k| row[*k].as_f64().or_else(|| row[*k].as_str().and_then(|s| s.parse().ok()))) | |
| 362 | .unwrap_or(0.0) | |
| 363 | }; | |
| 364 | let service = text(&["ServiceName", "service_name", "service"]); | |
| 365 | if service.is_empty() { | |
| 366 | return None; | |
| 367 | } | |
| 368 | let family = text(&["ServiceFamilyName", "service_family_name"]); | |
| 369 | Some(UsageRow { | |
| 370 | period_start: text(&["ChargePeriodStart", "charge_period_start"]), | |
| 371 | period_end: text(&["ChargePeriodEnd", "charge_period_end"]), | |
| 372 | service: if family.is_empty() { service } else { format!("{family} / {service}") }, | |
| The keeper reads Cloudflare as it really answers | 373 | unit: text(&["PricingUnit", "ConsumedUnit", "consumed_unit"]), |
| Prices keep themselves current with what g1t pays | 374 | quantity: number(&["PricingQuantity", "ConsumedQuantity", "pricing_quantity"]), |
| The keeper reads Cloudflare as it really answers | 375 | // What g1t pays; list price if nothing was contracted. |
| 376 | cost: Some(number(&["ContractedCost", "BilledCost", "contracted_cost"])) | |
| 377 | .filter(|cost| *cost > 0.0) | |
| 378 | .unwrap_or_else(|| number(&["ListCost", "list_cost"])), | |
| Prices keep themselves current with what g1t pays | 379 | }) |
| 380 | } | |
| 381 | } | |
| 382 | ||
| 383 | #[derive(Deserialize)] | |
| 384 | struct PriceRow { | |
| 385 | meter: String, | |
| 386 | title: String, | |
| 387 | unit: String, | |
| 388 | cost_micros: f64, | |
| 389 | markup_percent: u32, | |
| 390 | source: String, | |
| 391 | checked_at: Option<String>, | |
| 392 | updated_at: String, | |
| 393 | } | |
| 394 | ||
| 395 | #[derive(Deserialize)] | |
| 396 | struct ChangeRow { | |
| 397 | meter: String, | |
| 398 | old_cost_micros: f64, | |
| 399 | new_cost_micros: f64, | |
| 400 | markup_percent: u32, | |
| Prices are what g1t pays plus 20%, from the first second | 401 | old_markup_percent: Option<u32>, |
| Prices keep themselves current with what g1t pays | 402 | reason: String, |
| 403 | created_at: String, | |
| 404 | } | |
| 405 | ||
| 406 | #[derive(Deserialize)] | |
| 407 | struct Unsettled { | |
| 408 | id: String, | |
| 409 | workspace: String, | |
| 410 | repo: String, | |
| 411 | number: u32, | |
| 412 | task: String, | |
| 413 | model: String, | |
| 414 | token_hash: String, | |
| 415 | billed_to: Option<String>, | |
| 416 | session_id: String, | |
| 417 | created_at: String, | |
| 418 | finished_at: Option<String>, | |
| 419 | } | |
| 420 | ||
| 421 | #[derive(Deserialize)] | |
| 422 | struct Charged { | |
| 423 | cost_micros: Option<i64>, | |
| 424 | description: String, | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 425 | amount_micros: i64, |
| 426 | } | |
| 427 | ||
| 428 | /// What a settled run's correction comes to, from the charges at the | |
| 429 | /// reported and the gateway's cost and what the workspace was charged at | |
| 430 | /// first. A charge up is drawn down like any charge; a charge down is | |
| 431 | /// given back only up to what the workspace paid, since what a credit or | |
| 432 | /// a pool paid was never the workspace's money. | |
| 433 | pub(crate) fn correction(reported_charge: i64, gateway_charge: i64, first_charged: i64) -> i64 { | |
| 434 | let delta = gateway_charge - reported_charge; | |
| 435 | if delta >= 0 { delta } else { delta.max(-first_charged.max(0)) } | |
| Prices keep themselves current with what g1t pays | 436 | } |
| 437 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 438 | /// A session's logs are read 50 at a time, up to this many pages. |
| 439 | const MAX_LOG_PAGES: u32 = 40; | |
| 440 | ||
| 441 | /// What AI Gateway's logs say a session cost. | |
| 442 | #[derive(Clone, Debug, Default, PartialEq)] | |
| 443 | pub(crate) struct SessionCost { | |
| 444 | /// What the gateway priced the requests at, in dollars. | |
| 445 | pub cost_usd: f64, | |
| 446 | pub requests: u32, | |
| 447 | /// Requests that used tokens but that the gateway put no price on: a | |
| 448 | /// model it has no price for. Their cost is not in `cost_usd`. | |
| 449 | pub unpriced: u32, | |
| 450 | /// The models of those, for the statement and the drift. | |
| 451 | pub unpriced_models: Vec<String>, | |
| 452 | /// False when there were more logs than were read. | |
| 453 | pub complete: bool, | |
| 454 | } | |
| 455 | ||
| 456 | impl SessionCost { | |
| 457 | /// Adds one log, read leniently: `cost` in dollars, `tokens_in` and | |
| 458 | /// `tokens_out`, `cached` for an answer from the gateway's own cache | |
| 459 | /// (which costs nothing). | |
| 460 | pub(crate) fn add(&mut self, log: &Value) { | |
| 461 | self.requests += 1; | |
| 462 | let number = |key: &str| log[key].as_f64().or_else(|| log[key].as_str().and_then(|s| s.parse().ok())); | |
| 463 | let cost = number("cost").filter(|c| c.is_finite() && *c > 0.0); | |
| 464 | let tokens = number("tokens_in").unwrap_or(0.0) + number("tokens_out").unwrap_or(0.0); | |
| 465 | let cached = log["cached"].as_bool().unwrap_or(false); | |
| 466 | match cost { | |
| 467 | Some(cost) => self.cost_usd += cost, | |
| 468 | None if tokens > 0.0 && !cached => { | |
| 469 | self.unpriced += 1; | |
| 470 | let model = log["model"].as_str().unwrap_or("an unnamed model").to_owned(); | |
| 471 | if !self.unpriced_models.contains(&model) { | |
| 472 | self.unpriced_models.push(model); | |
| 473 | } | |
| 474 | } | |
| 475 | None => {} | |
| 476 | } | |
| 477 | } | |
| 478 | ||
| 479 | /// Whether the gateway's figure is the whole of what the run cost. | |
| 480 | pub(crate) fn whole(&self) -> bool { | |
| 481 | self.complete && self.unpriced == 0 | |
| 482 | } | |
| 483 | } | |
| 484 | ||
| 485 | /// The cost a run is settled at, in millionths: the gateway's figure when | |
| 486 | /// it priced every request; otherwise (a model it has no price for, or | |
| 487 | /// more logs than were read) never less than the sandbox reported, since | |
| 488 | /// the gateway's sum is then short of what the provider bills. With a | |
| 489 | /// reason for the statement and the drift when it is not the gateway's | |
| 490 | /// figure alone. | |
| 491 | pub(crate) fn settled_cost(reported_micros: i64, gateway: &SessionCost) -> (i64, Option<String>) { | |
| 492 | // Not held to MAX_RUN_COST_USD: the gateway's figure is trusted. | |
| 493 | let priced = if gateway.cost_usd.is_finite() { (gateway.cost_usd.max(0.0) * MICROS_PER_DOLLAR as f64).ceil() as i64 } else { 0 }; | |
| 494 | if gateway.whole() { | |
| 495 | return (priced, None); | |
| 496 | } | |
| 497 | let mut why = Vec::new(); | |
| 498 | if gateway.unpriced > 0 { | |
| 499 | why.push(format!( | |
| 500 | "AI Gateway has no price for {} of its {} requests ({})", | |
| 501 | gateway.unpriced, | |
| 502 | gateway.requests, | |
| 503 | gateway.unpriced_models.join(", ") | |
| 504 | )); | |
| 505 | } | |
| 506 | if !gateway.complete { | |
| 507 | why.push(format!("more than {} of its requests were logged", gateway.requests)); | |
| 508 | } | |
| 509 | (priced.max(reported_micros.max(0)), Some(why.join("; "))) | |
| 510 | } | |
| 511 | ||
| Prices keep themselves current with what g1t pays | 512 | fn ms(timestamp: &str) -> u64 { |
| 513 | // RFC 3339 in UTC, as g1t writes them. | |
| 514 | worker::js_sys::Date::parse(timestamp) as u64 | |
| 515 | } | |
| 516 | ||
| 517 | impl Billing { | |
| 518 | pub(crate) async fn prices(&self) -> Result<PriceBook> { | |
| Billing answers every page in a few round trips, not forty: its reads run together, Stripe is asked at most hourly, and the ledger has a time index | 519 | // Three reads at once; the plans are priced from the rows already |
| 520 | // read, not one query per meter (status.g1t.sh times this call). | |
| 521 | let (prices, changes, coming) = futures_util::future::join3( | |
| 522 | async { self.db.prepare("SELECT * FROM prices ORDER BY rowid").all().await?.results::<PriceRow>() }, | |
| 523 | async { | |
| 524 | self.db | |
| 525 | .prepare("SELECT * FROM price_changes ORDER BY created_at DESC LIMIT 20") | |
| 526 | .all() | |
| 527 | .await? | |
| 528 | .results::<ChangeRow>() | |
| 529 | }, | |
| 530 | // Changes still to come first, so a rise is seen before it is charged. | |
| 531 | self.coming_changes(), | |
| 532 | ) | |
| 533 | .await; | |
| 534 | let (prices, changes, coming) = (prices?, changes?, coming?); | |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 535 | let model_markup = prices.iter().find(|row| row.meter == "agent_models").map_or(self.margin_percent, |row| row.markup_percent); |
| Billing answers every page in a few round trips, not forty: its reads run together, Stripe is asked at most hourly, and the ledger has a time index | 536 | let book: std::collections::BTreeMap<&str, f64> = prices |
| 537 | .iter() | |
| 538 | .map(|row| (row.meter.as_str(), Price::price_for(row.cost_micros, row.markup_percent))) | |
| 539 | .collect(); | |
| Merge Stripe Tax, the card fee on card payments, and one free workspace per person | 540 | // With the card fee on top of each monthly price, as it is charged. |
| 541 | let card_fee = self.card_fee().await?; | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 542 | let plans: Vec<_> = g1t_contracts::billing::Feature::ALL |
| 543 | .iter() | |
| Merge Stripe Tax, the card fee on card payments, and one free workspace per person | 544 | .map(|feature| { |
| 545 | let mut plan = match feature { | |
| 546 | g1t_contracts::billing::Feature::Security => crate::features::security_plan_at(&book), | |
| 547 | _ => self.plan_at(&book), | |
| 548 | }; | |
| 549 | plan.card_fee_cents = crate::tax::fee_for(i64::from(plan.monthly_cents), &card_fee) as u32; | |
| 550 | plan | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 551 | }) |
| 552 | .collect(); | |
| Prices keep themselves current with what g1t pays | 553 | Ok(PriceBook { |
| 554 | prices: prices | |
| 555 | .into_iter() | |
| 556 | .map(|row| Price { | |
| 557 | price_micros: Price::price_for(row.cost_micros, row.markup_percent), | |
| 558 | meter: row.meter, | |
| 559 | title: row.title, | |
| 560 | unit: row.unit, | |
| 561 | cost_micros: row.cost_micros, | |
| 562 | markup_percent: row.markup_percent, | |
| 563 | source: row.source, | |
| 564 | checked_at: row.checked_at, | |
| 565 | updated_at: row.updated_at, | |
| 566 | }) | |
| 567 | .collect(), | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 568 | changes: coming |
| Prices keep themselves current with what g1t pays | 569 | .into_iter() |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 570 | .chain(changes.into_iter().map(|row| PriceChange { |
| Prices keep themselves current with what g1t pays | 571 | meter: row.meter, |
| 572 | old_cost_micros: row.old_cost_micros, | |
| 573 | new_cost_micros: row.new_cost_micros, | |
| 574 | markup_percent: row.markup_percent, | |
| Prices are what g1t pays plus 20%, from the first second | 575 | old_markup_percent: row.old_markup_percent, |
| Prices keep themselves current with what g1t pays | 576 | reason: row.reason, |
| 577 | created_at: row.created_at, | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 578 | effective_at: None, |
| 579 | })) | |
| Prices keep themselves current with what g1t pays | 580 | .collect(), |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 581 | // The markup on models' provider price: the price book's |
| 582 | // `agent_models` (none from 2026-10-08). | |
| 583 | model_margin_percent: model_markup, | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 584 | plans, |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 585 | free: Some(g1t_contracts::billing::FreeTier { |
| 586 | trial_workspace_micros: if self.trials_on { self.plans.trial_workspace_micros } else { 0 }, | |
| 587 | trial_monthly_pool_micros: if self.trials_on { self.plans.trial_monthly_pool_micros } else { 0 }, | |
| 588 | oss_pool_micros: self.plans.oss_pool_micros, | |
| 589 | oss_repo_micros: self.plans.oss_repo_micros, | |
| 590 | free_private_storage_bytes: self.plans.free_storage_bytes, | |
| Audit logs are kept by plan: a week on free, 90 days on the plan, and what staff set for an account in sudo | 591 | audit_retention_days: self.plans.free_audit_days, |
| 592 | plan_audit_retention_days: self.plans.audit_days, | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 593 | min_charge_micros: self.plans.min_charge_micros, |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 594 | git_operations_included: self.plans.git_included, |
| 595 | paid_start_ceiling_micros: self.plans.paid_start_micros, | |
| 596 | overage_forgive_cost_micros: self.plans.forgive_cost_micros, | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 597 | }), |
| Prices keep themselves current with what g1t pays | 598 | }) |
| 599 | } | |
| 600 | ||
| The keeper reads Cloudflare as it really answers | 601 | /// Whether the costs have never been checked against Cloudflare's bill. |
| 602 | pub(crate) async fn never_checked(&self) -> Result<bool> { | |
| 603 | Ok(self | |
| 604 | .db | |
| 605 | .prepare("SELECT meter FROM prices WHERE checked_at IS NOT NULL LIMIT 1") | |
| 606 | .first::<Value>(None) | |
| 607 | .await? | |
| 608 | .is_none()) | |
| 609 | } | |
| 610 | ||
| Prices keep themselves current with what g1t pays | 611 | /// A meter's cost and price per unit, from the book. |
| 612 | pub(crate) async fn price(&self, meter: &str) -> Result<Option<(f64, f64)>> { | |
| 613 | #[derive(Deserialize)] | |
| 614 | struct Row { | |
| 615 | cost_micros: f64, | |
| 616 | markup_percent: u32, | |
| 617 | } | |
| 618 | Ok(self | |
| 619 | .db | |
| 620 | .prepare("SELECT cost_micros, markup_percent FROM prices WHERE meter = ?") | |
| 621 | .bind(&[meter.into()])? | |
| 622 | .first::<Row>(None) | |
| 623 | .await? | |
| 624 | .map(|row| (row.cost_micros, Price::price_for(row.cost_micros, row.markup_percent)))) | |
| 625 | } | |
| 626 | ||
| 627 | /// Corrects finished runs to what AI Gateway priced them at, and | |
| 628 | /// charges runs whose sandbox died before reporting. | |
| 629 | pub(crate) async fn settle_runs(&self, keeper: &Keeper) -> Result<()> { | |
| 630 | if keeper.token.is_none() || keeper.gateway.is_empty() { | |
| 631 | return Ok(()); | |
| 632 | } | |
| 633 | let now = now_ms(); | |
| 634 | let runs = self | |
| 635 | .db | |
| 636 | .prepare( | |
| 637 | "SELECT id, workspace, repo, number, task, model, token_hash, billed_to, session_id, created_at, finished_at | |
| 638 | FROM runs | |
| Merge branch 'model-routing' | 639 | WHERE session_id IS NOT NULL AND settled_at IS NULL AND COALESCE(billed_to, 'g1t') = 'g1t' |
| Prices keep themselves current with what g1t pays | 640 | AND ((finished_at IS NOT NULL AND finished_at < ?1) OR created_at < ?2) |
| 641 | ORDER BY created_at LIMIT 10", | |
| 642 | ) | |
| 643 | .bind(&[rfc3339(now - SETTLE_AFTER_MS).into(), rfc3339(now - ABANDONED_AFTER_MS).into()])? | |
| 644 | .all() | |
| 645 | .await? | |
| 646 | .results::<Unsettled>()?; | |
| 647 | for run in runs { | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 648 | let gateway = match keeper.session_cost(&run.session_id).await { |
| Prices keep themselves current with what g1t pays | 649 | Ok(found) => found, |
| 650 | Err(error) => { | |
| 651 | worker::console_error!("could not read gateway logs for {}: {error}", run.id); | |
| 652 | continue; | |
| 653 | } | |
| 654 | }; | |
| 655 | let since = ms(run.finished_at.as_deref().unwrap_or(&run.created_at)); | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 656 | if gateway.requests == 0 && now.saturating_sub(since) < GIVE_UP_AFTER_MS { |
| Prices keep themselves current with what g1t pays | 657 | continue; |
| 658 | } | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 659 | self.settle(&run, &gateway).await?; |
| Prices keep themselves current with what g1t pays | 660 | } |
| 661 | Ok(()) | |
| 662 | } | |
| 663 | ||
| Merge branch 'model-routing' | 664 | /// Closes runs on a workspace's own model provider: none is on g1t's |
| 665 | /// gateway, so nothing is corrected, but tokens the proxy counted after | |
| 666 | /// the run reported, or for a sandbox that died before reporting, are | |
| 667 | /// charged their agent rate now. Needs no gateway token. | |
| 668 | pub(crate) async fn settle_own_runs(&self) -> Result<()> { | |
| 669 | #[derive(Deserialize)] | |
| 670 | struct Own { | |
| 671 | id: String, | |
| 672 | workspace: String, | |
| 673 | repo: String, | |
| 674 | number: u32, | |
| 675 | task: String, | |
| 676 | model: String, | |
| 677 | token_hash: String, | |
| 678 | billed_to: Option<String>, | |
| 679 | } | |
| 680 | let now = now_ms(); | |
| 681 | let runs = self | |
| 682 | .db | |
| 683 | .prepare( | |
| 684 | "SELECT id, workspace, repo, number, task, model, token_hash, billed_to | |
| 685 | FROM runs | |
| 686 | WHERE billed_to = 'workspace' AND session_id IS NOT NULL AND settled_at IS NULL | |
| 687 | AND ((finished_at IS NOT NULL AND finished_at < ?1) OR created_at < ?2) | |
| 688 | ORDER BY created_at LIMIT 25", | |
| 689 | ) | |
| 690 | .bind(&[rfc3339(now - SETTLE_AFTER_MS).into(), rfc3339(now - ABANDONED_AFTER_MS).into()])? | |
| 691 | .all() | |
| 692 | .await? | |
| 693 | .results::<Own>()?; | |
| 694 | for run in runs { | |
| 695 | let settled_at = rfc3339(now_ms()); | |
| 696 | let claimed = self | |
| 697 | .db | |
| 698 | .prepare( | |
| 699 | "UPDATE runs SET settled_at = ?1, finished_at = COALESCE(finished_at, ?1) | |
| 700 | WHERE id = ?2 AND settled_at IS NULL RETURNING id", | |
| 701 | ) | |
| 702 | .bind(&[settled_at.as_str().into(), run.id.as_str().into()])? | |
| 703 | .first::<Value>(None) | |
| 704 | .await?; | |
| 705 | if claimed.is_none() { | |
| 706 | continue; | |
| 707 | } | |
| 708 | let row = RunRow { | |
| 709 | workspace: run.workspace, | |
| 710 | repo: run.repo, | |
| 711 | number: run.number, | |
| 712 | task: run.task, | |
| 713 | model: run.model, | |
| 714 | token_hash: run.token_hash, | |
| 715 | billed_to: run.billed_to, | |
| 716 | }; | |
| 717 | self.charge_agent_rate(&run.id, &row, None).await?; | |
| 718 | } | |
| 719 | Ok(()) | |
| 720 | } | |
| 721 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 722 | async fn settle(&self, run: &Unsettled, gateway: &SessionCost) -> Result<()> { |
| 723 | let requests = gateway.requests; | |
| Prices keep themselves current with what g1t pays | 724 | let row = RunRow { |
| 725 | workspace: run.workspace.clone(), | |
| 726 | repo: run.repo.clone(), | |
| 727 | number: run.number, | |
| 728 | task: run.task.clone(), | |
| 729 | model: run.model.clone(), | |
| 730 | token_hash: run.token_hash.clone(), | |
| 731 | billed_to: run.billed_to.clone(), | |
| 732 | }; | |
| 733 | let charged = self | |
| 734 | .db | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 735 | .prepare("SELECT cost_micros, description, amount_micros FROM ledger WHERE reference = ?") |
| Prices keep themselves current with what g1t pays | 736 | .bind(&[run.id.as_str().into()])? |
| 737 | .first::<Charged>(None) | |
| 738 | .await?; | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 739 | let reported = charged.as_ref().and_then(|c| c.cost_micros).unwrap_or(0); |
| 740 | // The gateway's figure; never under what the sandbox reported when | |
| 741 | // the gateway could not price all of it (see `settled_cost`). | |
| 742 | let (gateway_micros, short) = settled_cost(reported, gateway); | |
| Billing accounts, terms and enterprises; g1t is no longer free | 743 | let terms = self.terms_of(&run.workspace).await?; |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 744 | // Models at the price book's markup on the provider's price |
| 745 | // (`agent_models`), as `finish_run` charges them. | |
| 746 | let markup = self.model_markup().await?; | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 747 | // A cost's charge on the account's terms, and what a discount gave |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 748 | // below cost plus the markup (counted as given, see `charged`). |
| Prices keep themselves current with what g1t pays | 749 | let charge_for = |micros: i64| { |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 750 | if self.free { (0, 0) } else { terms.discounted(crate::margin_on(micros, markup)) } |
| Prices keep themselves current with what g1t pays | 751 | }; |
| 752 | let settled_at = rfc3339(now_ms()); | |
| 753 | // Claim it, so two crons never settle it twice. | |
| 754 | let claimed = self | |
| 755 | .db | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 756 | .prepare( |
| 757 | "UPDATE runs SET settled_at = ?, gateway_cost_micros = ?, gateway_note = ?, finished_at = COALESCE(finished_at, ?) | |
| 758 | WHERE id = ? AND settled_at IS NULL RETURNING id", | |
| 759 | ) | |
| Prices keep themselves current with what g1t pays | 760 | .bind(&[ |
| 761 | settled_at.as_str().into(), | |
| 762 | (gateway_micros as f64).into(), | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 763 | short.as_deref().map_or(worker::wasm_bindgen::JsValue::NULL, Into::into), |
| Prices keep themselves current with what g1t pays | 764 | settled_at.as_str().into(), |
| 765 | run.id.as_str().into(), | |
| 766 | ])? | |
| 767 | .first::<Value>(None) | |
| 768 | .await?; | |
| 769 | if claimed.is_none() || requests == 0 { | |
| 770 | return Ok(()); | |
| 771 | } | |
| Usage, Billing settings and prepaid AI credit; fixes from the UX audit | 772 | // Tokens counted after the run reported are charged their agent |
| 773 | // rate now (ai.rs). | |
| Merge branch 'model-routing' | 774 | self.charge_agent_rate(&run.id, &row, None).await?; |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 775 | if let Some(why) = &short { |
| 776 | worker::console_warn!("run {} settled at no less than reported: {why}", run.id); | |
| 777 | } | |
| 778 | let short_note = short.as_ref().map_or(String::new(), |why| format!(" ({why}; charged at no less than the sandbox reported)")); | |
| Prices keep themselves current with what g1t pays | 779 | let free_note = if self.free { " (free while g1t is being built out)" } else { "" }; |
| 780 | match charged { | |
| 781 | // Never reported: charged now, from the gateway's figure. | |
| 782 | None => { | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 783 | let (charge, discount) = charge_for(gateway_micros); |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 784 | let eligible = crate::credits::eligible_for(Some(g1t_contracts::billing::ComputeKind::Agent), None); |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 785 | let drawn = self.draw(&run.workspace, charge, &settled_at[..7], &eligible).await?; |
| Prices keep themselves current with what g1t pays | 786 | let description = format!( |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 787 | "Work on {}#{}, settled from AI Gateway after the sandbox stopped without reporting{short_note}{free_note}{}", |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 788 | run.repo, |
| 789 | run.number, | |
| 790 | drawn.note() | |
| Prices keep themselves current with what g1t pays | 791 | ); |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 792 | self.enter(&run.workspace, EntryKind::Usage, -(charge - drawn.total()), &description, &run.id, Some(&row), Some(gateway_micros), None, None) |
| Prices keep themselves current with what g1t pays | 793 | .await?; |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 794 | self.record_drawn(&run.id, &drawn).await?; |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 795 | self.record_discount(&run.id, discount).await?; |
| Spend caps: a monthly budget for comped workspaces and a daily breaker on what g1t pays | 796 | self.count_spend(&run.workspace, gateway_micros, charge - drawn.total(), &drawn).await; |
| Prices keep themselves current with what g1t pays | 797 | } |
| 798 | Some(charged) => { | |
| 799 | let delta = gateway_micros - reported; | |
| 800 | if delta == 0 { | |
| 801 | return Ok(()); | |
| 802 | } | |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 803 | let ((was, was_given), (now, now_given)) = (charge_for(reported), charge_for(gateway_micros)); |
| 804 | let change = correction(was, now, -charged.amount_micros); | |
| 805 | // What the discount gives moves with the charge. | |
| 806 | let discount = now_given - was_given; | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 807 | // A charge up is paid for like any other charge. |
| 808 | let drawn = if change > 0 { | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 809 | let eligible = crate::credits::eligible_for(Some(g1t_contracts::billing::ComputeKind::Agent), None); |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 810 | self.draw(&run.workspace, change, &settled_at[..7], &eligible).await? |
| 811 | } else { | |
| 812 | crate::credits::Drawn::default() | |
| 813 | }; | |
| Prices keep themselves current with what g1t pays | 814 | let description = format!( |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 815 | "Correction to “{}”: AI Gateway priced its {requests} model requests at {}, not {}{short_note}{}", |
| Prices keep themselves current with what g1t pays | 816 | charged.description, |
| 817 | crate::features::dollars(gateway_micros), | |
| 818 | crate::features::dollars(reported), | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 819 | drawn.note(), |
| Prices keep themselves current with what g1t pays | 820 | ); |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 821 | let reference = format!("{}/settled", run.id); |
| Prices keep themselves current with what g1t pays | 822 | self.enter( |
| 823 | &run.workspace, | |
| 824 | EntryKind::Usage, | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 825 | -(change - drawn.total()), |
| Prices keep themselves current with what g1t pays | 826 | &description, |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 827 | &reference, |
| Prices keep themselves current with what g1t pays | 828 | Some(&row), |
| 829 | Some(delta), | |
| 830 | None, | |
| 831 | None, | |
| 832 | ) | |
| 833 | .await?; | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 834 | self.record_drawn(&reference, &drawn).await?; |
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 835 | self.record_discount(&reference, discount).await?; |
| Spend caps: a monthly budget for comped workspaces and a daily breaker on what g1t pays | 836 | self.count_spend(&run.workspace, delta, change - drawn.total(), &drawn).await; |
| Prices keep themselves current with what g1t pays | 837 | } |
| 838 | } | |
| 839 | Ok(()) | |
| 840 | } | |
| 841 | ||
| 842 | /// Checks each cost against what Cloudflare billed this month, and | |
| 843 | /// moves the ones that changed. | |
| 844 | pub(crate) async fn reconcile(&self, keeper: &Keeper) -> Result<()> { | |
| 845 | if keeper.token.is_none() { | |
| 846 | return Ok(()); | |
| 847 | } | |
| 848 | let now = rfc3339(now_ms()); | |
| 849 | let today = &now[..10]; | |
| The keeper reads Cloudflare as it really answers | 850 | let since = rfc3339(now_ms() - 30 * 24 * 60 * 60 * 1000); |
| 851 | let rows = keeper.billable_usage(&since[..10], today).await?; | |
| Prices keep themselves current with what g1t pays | 852 | for row in &rows { |
| 853 | self.db | |
| 854 | .prepare( | |
| 855 | "INSERT INTO cloudflare_usage (period_start, period_end, service, unit, quantity, cost_usd, fetched_at) | |
| 856 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) | |
| 857 | ON CONFLICT (period_start, service, unit) DO UPDATE SET | |
| 858 | period_end = ?2, quantity = ?5, cost_usd = ?6, fetched_at = ?7", | |
| 859 | ) | |
| 860 | .bind(&[ | |
| 861 | row.period_start.as_str().into(), | |
| 862 | row.period_end.as_str().into(), | |
| 863 | row.service.as_str().into(), | |
| 864 | row.unit.as_str().into(), | |
| 865 | row.quantity.into(), | |
| 866 | row.cost.into(), | |
| 867 | now.as_str().into(), | |
| 868 | ])? | |
| 869 | .run() | |
| 870 | .await?; | |
| 871 | } | |
| 872 | ||
| The keeper reads Cloudflare as it really answers | 873 | let named = |words: &[&str]| -> Vec<&UsageRow> { |
| Prices keep themselves current with what g1t pays | 874 | rows.iter() |
| The keeper reads Cloudflare as it really answers | 875 | .filter(|r| { |
| 876 | let service = r.service.to_lowercase(); | |
| 877 | words.iter().all(|word| service.contains(word)) | |
| 878 | }) | |
| 879 | .collect() | |
| Prices keep themselves current with what g1t pays | 880 | }; |
| 881 | ||
| The keeper reads Cloudflare as it really answers | 882 | // Containers: each resource at what the bill shows it costs, or |
| 883 | // the published rate while the included amount still covers it, | |
| 884 | // over how much CPU g1t's sandboxes really use per second. | |
| 885 | let memory = billed_rate(&named(&["container memory"])); | |
| 886 | let disk = billed_rate(&named(&["container disk"])); | |
| 887 | let vcpu = billed_rate(&named(&["container vcpu"])); | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 888 | let durable_object = billed_rate(&named(&["durable objects", "duration"])); |
| The keeper reads Cloudflare as it really answers | 889 | let usage = keeper.container_usage(&since[..10], today).await?; |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 890 | let rates = ( |
| The keeper reads Cloudflare as it really answers | 891 | memory.unwrap_or(LIST_MEMORY_GIB_SECOND), |
| 892 | disk.unwrap_or(LIST_DISK_GB_SECOND), | |
| 893 | vcpu.unwrap_or(LIST_VCPU_SECOND), | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 894 | durable_object.unwrap_or(LIST_DO_GB_SECOND), |
| 895 | ); | |
| 896 | // The parts, for runs that report their own CPU. | |
| 897 | let parts_reason = "Cloudflare's Containers and Durable Objects rates, as billed or published"; | |
| 898 | self.measure("sandbox_base_second", sandbox_base_micros(rates.0, rates.1, rates.3), parts_reason).await?; | |
| 899 | self.measure("sandbox_cpu_second", rates.2 * MICROS_PER_DOLLAR as f64, parts_reason).await?; | |
| 900 | if let Some(per_second) = sandbox_second_micros(usage, rates.0, rates.1, rates.2, rates.3) { | |
| The keeper reads Cloudflare as it really answers | 901 | let instance_seconds = usage.memory_byte_seconds / (SANDBOX_GIB * GIB); |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 902 | let billed = [("memory", memory), ("disk", disk), ("vCPU", vcpu), ("Durable Object duration", durable_object)] |
| The keeper reads Cloudflare as it really answers | 903 | .iter() |
| 904 | .filter(|(_, rate)| rate.is_some()) | |
| 905 | .map(|(name, _)| *name) | |
| 906 | .collect::<Vec<_>>(); | |
| 907 | let reason = format!( | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 908 | "Sandboxes used {:.2} vCPU per second over {:.0} hours of Cloudflare Containers in the last 30 days, with the Durable Object behind each; {}", |
| The keeper reads Cloudflare as it really answers | 909 | usage.cpu_seconds / instance_seconds, |
| 910 | instance_seconds / 3600.0, | |
| 911 | if billed.is_empty() { | |
| 912 | "rates are Cloudflare's published ones".to_owned() | |
| 913 | } else { | |
| 914 | format!("{} at what Cloudflare billed", billed.join(", ")) | |
| 915 | }, | |
| 916 | ); | |
| 917 | for meter in ["sandbox_second", "build_second"] { | |
| 918 | self.measure(meter, per_second, &reason).await?; | |
| Prices keep themselves current with what g1t pays | 919 | } |
| 920 | } | |
| The keeper reads Cloudflare as it really answers | 921 | // Apps run as Workers: per million requests and CPU milliseconds, |
| 922 | // once the bill shows them charged. | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 923 | // Security scans' CPU follows the same Workers CPU rate. |
| 924 | let app_meters: [(&str, &[&str], &str); 3] = [ | |
| The keeper reads Cloudflare as it really answers | 925 | ("app_requests", &["workers", "requests"], "requests"), |
| 926 | ("app_cpu", &["workers cpu"], "CPU ms"), | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 927 | ("scan_cpu", &["workers cpu"], "CPU ms"), |
| The keeper reads Cloudflare as it really answers | 928 | ]; |
| 929 | for (meter, words, unit) in app_meters { | |
| 930 | if let Some(rate) = billed_rate(&named(words)) { | |
| 931 | let reason = format!("Cloudflare billed Workers {unit} at ${:.2} per million", rate * 1e6); | |
| 932 | self.measure(meter, rate * 1e6 * MICROS_PER_DOLLAR as f64, &reason).await?; | |
| Prices keep themselves current with what g1t pays | 933 | } |
| 934 | } | |
| 935 | self.db | |
| 936 | .prepare("UPDATE prices SET checked_at = ?") | |
| 937 | .bind(&[now.as_str().into()])? | |
| 938 | .run() | |
| 939 | .await?; | |
| 940 | Ok(()) | |
| 941 | } | |
| 942 | ||
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 943 | /// Proposes moving a meter's cost to a measurement (see `pricing`): |
| 944 | /// applied on its own when small, after notice when a rise; left for | |
| 945 | /// staff when large or suspect. | |
| Prices keep themselves current with what g1t pays | 946 | async fn measure(&self, meter: &str, measured: f64, reason: &str) -> Result<()> { |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 947 | if let Some(outcome) = self.propose(meter, measured, reason, "keeper").await? { |
| 948 | worker::console_log!("{meter}: {outcome}"); | |
| Prices keep themselves current with what g1t pays | 949 | } |
| 950 | Ok(()) | |
| 951 | } | |
| 952 | } | |
| 953 | ||
| 954 | #[cfg(test)] | |
| 955 | mod tests { | |
| 956 | use super::*; | |
| 957 | ||
| 958 | #[test] | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 959 | fn a_correction_never_gives_back_what_the_workspace_did_not_pay() { |
| 960 | // Up by 2 cents: charged in full (then drawn down like any charge). | |
| 961 | assert_eq!(correction(100_000, 120_000, 100_000), 20_000); | |
| 962 | // Down by 2 cents, all of it paid by the workspace: given back. | |
| 963 | assert_eq!(correction(120_000, 100_000, 120_000), -20_000); | |
| 964 | // Down, but the open-source pool paid all but a cent: a cent back. | |
| 965 | assert_eq!(correction(120_000, 100_000, 10_000), -10_000); | |
| 966 | // Paid entirely by a credit or pool: nothing back. | |
| 967 | assert_eq!(correction(120_000, 100_000, 0), 0); | |
| Prices keep themselves current with what g1t pays | 968 | } |
| 969 | ||
| Merge branch 'worktree-agent-a633ac0f7f66d419d' | 970 | fn logs(entries: &[Value]) -> SessionCost { |
| 971 | let mut total = SessionCost { complete: true, ..SessionCost::default() }; | |
| 972 | for log in entries { | |
| 973 | total.add(log); | |
| 974 | } | |
| 975 | total | |
| 976 | } | |
| 977 | ||
| 978 | #[test] | |
| 979 | fn a_run_is_settled_at_the_gateways_figure_when_it_priced_every_request() { | |
| 980 | let gateway = logs(&[ | |
| 981 | json!({ "cost": 0.012, "tokens_in": 4000, "tokens_out": 300, "model": "claude-sonnet-5-5" }), | |
| 982 | json!({ "cost": "0.003", "tokens_in": 900, "tokens_out": 40, "model": "claude-haiku-4-5" }), | |
| 983 | // Served from the gateway's own cache: no cost, and none owed. | |
| 984 | json!({ "cost": 0, "tokens_in": 900, "tokens_out": 40, "cached": true }), | |
| 985 | // An error with no tokens costs nothing either. | |
| 986 | json!({ "cost": null, "tokens_in": 0, "tokens_out": 0 }), | |
| 987 | ]); | |
| 988 | assert!(gateway.whole()); | |
| 989 | assert_eq!(gateway.requests, 4); | |
| 990 | // Down from what the sandbox said, or up: the gateway's figure. | |
| 991 | assert_eq!(settled_cost(20_000, &gateway), (15_000, None)); | |
| 992 | assert_eq!(settled_cost(9_000, &gateway), (15_000, None)); | |
| 993 | } | |
| 994 | ||
| 995 | #[test] | |
| 996 | fn a_model_the_gateway_cannot_price_is_never_settled_down_to_nothing() { | |
| 997 | let gateway = logs(&[ | |
| 998 | json!({ "cost": 0.002, "tokens_in": 100, "tokens_out": 10, "model": "claude-haiku-4-5" }), | |
| 999 | json!({ "cost": 0, "tokens_in": 50_000, "tokens_out": 2_000, "model": "claude-new-1" }), | |
| 1000 | json!({ "tokens_in": 50_000, "tokens_out": 2_000, "model": "claude-new-1" }), | |
| 1001 | ]); | |
| 1002 | assert!(!gateway.whole()); | |
| 1003 | assert_eq!((gateway.unpriced, gateway.unpriced_models.clone()), (2, vec!["claude-new-1".to_owned()])); | |
| 1004 | // The sandbox said $0.90: kept, not cut to the gateway's $0.002. | |
| 1005 | let (cost, why) = settled_cost(900_000, &gateway); | |
| 1006 | assert_eq!(cost, 900_000); | |
| 1007 | assert!(why.unwrap().contains("no price for 2 of its 3 requests (claude-new-1)")); | |
| 1008 | // A sandbox that reported less than the gateway priced: the gateway's. | |
| 1009 | assert_eq!(settled_cost(1_000, &gateway).0, 2_000); | |
| 1010 | } | |
| 1011 | ||
| 1012 | #[test] | |
| 1013 | fn more_logs_than_were_read_never_settle_a_run_down() { | |
| 1014 | let mut gateway = logs(&[json!({ "cost": 1.0, "tokens_in": 1, "tokens_out": 1 })]); | |
| 1015 | gateway.complete = false; | |
| 1016 | let (cost, why) = settled_cost(3_000_000, &gateway); | |
| 1017 | assert_eq!(cost, 3_000_000); | |
| 1018 | assert!(why.unwrap().contains("more than 1 of its requests")); | |
| 1019 | } | |
| 1020 | ||
| 1021 | #[test] | |
| 1022 | fn a_gateway_figure_over_the_report_cap_is_charged_in_full() { | |
| 1023 | // A sandbox's report is believed up to $100; the gateway's is not capped. | |
| 1024 | let gateway = logs(&[json!({ "cost": 140.0, "tokens_in": 1, "tokens_out": 1 })]); | |
| 1025 | assert_eq!(settled_cost(100_000_000, &gateway).0, 140_000_000); | |
| 1026 | assert_eq!(crate::charge_micros(140.0, 20), 120_000_000); | |
| 1027 | assert_eq!(crate::margin_on(140_000_000, 20), 168_000_000); | |
| 1028 | // Exactly cost plus the margin, rounded up, in whole micros (dollars | |
| 1029 | // as floats can come out a micro high), never under it. | |
| 1030 | for cost in [0_i64, 1, 7, 999, 123_457, 99_999_999] { | |
| 1031 | let exact = (cost * 120 + 99) / 100; | |
| 1032 | assert_eq!(crate::margin_on(cost, 20), exact, "{cost}"); | |
| 1033 | assert!(crate::margin_on(cost, 20) * 100 >= cost * 120, "{cost}"); | |
| 1034 | assert!(crate::charge_micros(cost as f64 / 1e6, 20) >= exact, "{cost}"); | |
| 1035 | } | |
| 1036 | } | |
| 1037 | ||
| Prices keep themselves current with what g1t pays | 1038 | #[test] |
| The keeper reads Cloudflare as it really answers | 1039 | fn a_sandbox_second_is_its_memory_and_disk_and_the_cpu_it_uses() { |
| 1040 | // An hour of sandboxes that kept a fifth of a vCPU busy. | |
| 1041 | let usage = ContainerUsage { cpu_seconds: 720.0, memory_byte_seconds: 3600.0 * 4.0 * GIB }; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 1042 | let micros = |
| 1043 | sandbox_second_micros(usage, LIST_MEMORY_GIB_SECOND, LIST_DISK_GB_SECOND, LIST_VCPU_SECOND, LIST_DO_GB_SECOND).unwrap(); | |
| 1044 | // 4 x 2.5 + 8 x 0.07 + 0.125 x 12.5 + 0.2 x 20 = 16.1225 | |
| 1045 | assert!((micros - 16.1225).abs() < 1e-9, "{micros}"); | |
| 1046 | // The Durable Object adds about 11% to the second it left out. | |
| 1047 | assert!((sandbox_base_micros(LIST_MEMORY_GIB_SECOND, LIST_DISK_GB_SECOND, LIST_DO_GB_SECOND) - 12.1225).abs() < 1e-9); | |
| The keeper reads Cloudflare as it really answers | 1048 | // Too little use to say anything. |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 1049 | assert!(sandbox_second_micros(ContainerUsage { cpu_seconds: 1.0, memory_byte_seconds: GIB }, 1.0, 1.0, 1.0, 1.0).is_none()); |
| 1050 | } | |
| 1051 | ||
| 1052 | #[test] | |
| 1053 | fn a_run_that_reports_its_cpu_is_priced_on_it() { | |
| 1054 | let base = sandbox_base_micros(LIST_MEMORY_GIB_SECOND, LIST_DISK_GB_SECOND, LIST_DO_GB_SECOND); | |
| 1055 | let vcpu = LIST_VCPU_SECOND * MICROS_PER_DOLLAR as f64; | |
| 1056 | // A 10-minute cargo build that kept its half vCPU busy throughout. | |
| 1057 | let heavy = run_cost(600, 300.0, base, vcpu); | |
| 1058 | assert!((heavy - (600.0 * 12.1225 + 300.0 * 20.0)).abs() < 1e-6); | |
| 1059 | // The same ten minutes, mostly idle, costs less. | |
| 1060 | let light = run_cost(600, 30.0, base, vcpu); | |
| 1061 | assert!(light < heavy); | |
| 1062 | // The average would have under-priced the heavy one. | |
| 1063 | let average = 600.0 * (base + 0.195 * vcpu); | |
| 1064 | assert!(average < heavy && average > light); | |
| 1065 | assert_eq!(run_cost(0, -1.0, base, vcpu), 0.0); | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 1066 | // A larger machine's base: its memory and disk, not its CPU. |
| 1067 | assert!((base_scale(SANDBOX_GIB, SANDBOX_DISK_GB) - 1.0).abs() < 1e-12); | |
| 1068 | assert!((base_scale(12.0, 20.0) - 2.72).abs() < 0.01, "{}", base_scale(12.0, 20.0)); | |
| 1069 | assert!((base_scale(8.0, 16.0) - 1.87).abs() < 0.01, "{}", base_scale(8.0, 16.0)); | |
| The keeper reads Cloudflare as it really answers | 1070 | } |
| 1071 | ||
| 1072 | #[test] | |
| 1073 | fn a_billed_rate_is_the_median_of_the_charged_days() { | |
| 1074 | let row = |quantity: f64, cost: f64| UsageRow { | |
| 1075 | period_start: String::new(), | |
| 1076 | period_end: String::new(), | |
| 1077 | service: "Containers / Container Memory".into(), | |
| 1078 | unit: "Count".into(), | |
| 1079 | quantity, | |
| 1080 | cost, | |
| 1081 | }; | |
| 1082 | let rows = [row(100.0, 0.0), row(100.0, 0.0002), row(100.0, 0.00025), row(100.0, 0.00025)]; | |
| 1083 | assert_eq!(billed_rate(&rows.iter().collect::<Vec<_>>()), Some(0.000_002_5)); | |
| 1084 | assert_eq!(billed_rate(&[&row(5.0, 0.0)]), None); | |
| 1085 | } | |
| 1086 | ||
| 1087 | #[test] | |
| Prices keep themselves current with what g1t pays | 1088 | fn usage_rows_are_read_by_their_focus_names() { |
| 1089 | let row = UsageRow::from_value(&json!({ | |
| 1090 | "ServiceFamilyName": "Containers", | |
| 1091 | "ServiceName": "Memory", | |
| The keeper reads Cloudflare as it really answers | 1092 | "PricingUnit": "GiB-seconds", |
| Prices keep themselves current with what g1t pays | 1093 | "PricingQuantity": "1200.5", |
| 1094 | "ContractedCost": 0.003, | |
| 1095 | "ChargePeriodStart": "2026-10-01", | |
| 1096 | })) | |
| 1097 | .unwrap(); | |
| 1098 | assert_eq!(row.service, "Containers / Memory"); | |
| 1099 | assert_eq!(row.quantity, 1200.5); | |
| 1100 | assert_eq!(row.cost, 0.003); | |
| 1101 | assert!(UsageRow::from_value(&json!({ "nothing": 1 })).is_none()); | |
| 1102 | } | |
| 1103 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.