Metering and billing

Gateway telemetry records inference usage and Substrate’s actor lifecycle records measure compute. A durable ledger feeds both to the platform’s self-hosted Lago, which invoices each tenant monthly and charges the card on file through Stripe. These are separate responsibilities, and a missing measurement must remain unknown rather than become a zero charge.

Usage to collection

agentgateway access telemetry          Substrate actor lifecycle records
  → Cloud Logging → Pub/Sub              → Cloud Logging → Pub/Sub
  → platform-api gateway subscriber      → platform-api compute subscriber and folder
  → Firestore usage evidence and the usage_events ledger
  → elected usage emitter (Lease platform-api-billing) → Lago events
  → Lago (namespace lago): plan spooky-usage, one invoice per tenant per month, in arrears
  → Lago's Stripe integration → Stripe charges the card on file

BILLING_USAGE_PROVIDER is lago or disabled; disabled keeps recording the ledger and delivers nothing. Lago runs in the cluster (UI lago.spookylabs.ai, API lago-api.spookylabs.ai).

The subscriber uses gateway request identity and attribution to deduplicate evidence and ledger writes. The emitter keeps Lago’s catalog in line with the platform price book: the billable metrics tokens (a sum of tokens, filtered by model and token_type) and compute_seconds (a sum of seconds), and the plan spooky-usage (monthly, in arrears, no base fee, standard charges priced per token and per second). Every tenant with a Stripe customer gets a Lago customer linked to it and the subscription tenant-<id>.

The emitter sends the ledger in commit order behind a Firestore watermark, in batches of at most 100 events. A gateway row becomes one tokens event per nonzero token type (transaction id gw-<row>-<token_type>); a compute row becomes one compute_seconds event (cu-<row>). Every event carries agent_id. Lago deduplicates on the transaction id and refuses a batch whole: a batch refused only for ids it already holds is sent again without them, and any other refusal leaves the watermark where it was. Usage before BILLING_USAGE_START_AT is never sent. A gateway row for a model the price book does not price is not sent, because Lago would bill it at zero; it stays in the ledger unbilled and the model is logged.

What each money value means

Value Basis and limit
Customer usage and invoiced amount Lago’s plan, priced from the platform price book; current-usage reads carry observation time and coverage
Token cost (cost_micros on the dashboards and consoles) Each model call at the price book’s rate, as the gateway priced it when the call was made: an estimate of the token part of the invoice. Not the invoice, which also bills compute and tool calls, and not what Vertex AI charges the platform
Missing measurement Absent/unknown; never silently converted to measured zero

The price book (iac gcp/workloads/files/price-book.json) is the one rate card: it renders agentgateway’s cost catalog and Lago’s plan. GetPricing serves it read-only; no RPC changes a price, so a price change is a price-book change in iac.

Compute running seconds

Compute is the time a session’s Substrate actor spends in the running state. It is measured from the lifecycle records Substrate’s API server writes on every committed state change. Running time at or after the pinned bill-from, 2026-09-30 05:00 UTC, is billable at the compute rate shown on the pricing page. Suspended time is not charged. The unit is persisted actor state, not CPU consumption: slow crash detection extends the running state until the control plane changes it.

ate-api-server "Actor state changed" / "Actor crashed", written twice (stdout and OTLP)
  → Cloud Logging sink compute-usage → Pub/Sub topic compute-usage
      (a dead-letter topic after ten refused deliveries; a second sink with the same
       filter keeps every record 400 days in the compute-usage-evidence log bucket)
  → platform-api subscriber: one Create-only compute_usage_records document per copy
  → platform-api folder, on one elected replica: running intervals in compute_usage_intervals
  → usage_events rows of kind agent_compute, one per UTC-hour slice
  → Lago billable metric compute_seconds (one event per row, carrying agent_id)

The records. After committing an actor state transition, Substrate’s ate-api-server logs one record twice with the same event time: a JSON line on stdout, which GKE’s workload logging collects, and an OTLP log record exported through the in-cluster collector (ate.actor.state_changed, or ate.actor.crashed for a crash). Nothing in Substrate is forked or patched for this; the record shapes are pinned by fixtures in platform-api. Both copies are routed, either alone is enough to bill, and the folder reports a record whose copies disagree or whose second copy never arrives. Upstream’s guidance for this stream is “Do not sample this stream. A dropped record leaves the last known state wrong, with nothing to show that a record is missing” (Substrate telemetry); the sink filter selects the two record shapes and never samples.

Intervals. The folder merges the two copies, orders an actor’s records by event time and opens one interval per running record. The interval closes at the actor’s next record once that record has settled (10 minutes), with the end decided on what the evidence shows:

Next record End Basis
suspending, crashed, deleting or reverting that record’s time direct
suspended or deleted within 120 s of running (the transition record was lost) that record’s time release_within_gap
anything else, or a released state later than 120 s the start: 0 s billed beyond it lower_bound
no record for 24 h the start: 0 s billed beyond it finalized_open

Every basis other than direct is reported, and an interval still open after 15 minutes is reported. Corrections only add: a later record that would reduce an already billed interval is reported and ignored.

Attribution. An interval is billed only for a tenant namespace, only for a session actor (session-<id>) and only through the session’s immutable binding, which names the agent’s immutable UID the rows are charged to. A binding that is missing, invalid or in another namespace refuses the interval; refused intervals are reported and never billed. Running time before the bill-from, or in a non-tenant namespace such as the golden snapshot build, stays evidence only.

Rows and delivery. A billed interval is cut at every UTC hour boundary into usage_events rows of kind agent_compute, each holding integer milliseconds under the id actor_<uid>_<sliceStart>. A replay of the subscription writes the same rows and changes nothing. The emitter sends each row as one event carrying agent_id and the seconds as an exact decimal (1.831 for 1831 ms), which the metric sums.

The honest limitation. Delivery of the records is best effort: Substrate keeps no outbox, so a record lost by both copies leaves no interval. A lost running record under-bills the whole session, and only the hourly task check (every finished task must fall inside a running interval of its session’s actor) notices it. A lost end record bills nothing beyond the proven start unless a released state arrives within the 120 s gap heuristic, in which case at most that much suspend time is over-counted. The event time is the API server’s clock just after the commit, on whichever replica ran the workflow, so replica clock skew can reorder two close records. What stands against each of these is alerting, not an assumption: alerts fire for sink export errors, subscription backlog, dead letters, an interval open past 15 minutes, copies that disagree, a task no interval covers, a meter silent while tasks ran, an uncertain end, a refused interval, a correction that would reduce billed time, a pipeline fault and a folder that stops heartbeating. The raw records are kept 400 days in the compute-usage-evidence log bucket and in Firestore, so a disputed charge can be checked against what Substrate wrote.

The bill-from is pinned once in Firestore and is one-way: running time before it is never backfilled, and a configured instant that differs from the pin is refused, not applied. This pipeline does not implement measured storage billing; session snapshots and configured storage capacity are not storage-usage measurements.

Monthly invoices

Usage is billed in arrears. Lago invoices each tenant’s subscription once per month, and its Stripe integration charges the invoice to the Stripe customer’s default card, the card the console saved with a SetupIntent. A card on file is required before an agent can be created. There is no prepaid balance, credit pack, auto top-up, free allowance or charge during the month, and nothing stops an agent, at the model gateway or anywhere else, for what it costs.

Lago posts its webhooks to https://api.spookylabs.ai/lago/webhook. platform-api accepts one only with a valid RS256 signature from the organization’s public key (X-Lago-Signature), and records the tenant’s Lago invoice standing from its failed finalized invoices.

Payment standing gates new work

Stripe’s Payment Element captures a payment method using a server-created SetupIntent. The server verifies ownership and webhook-derived billing state; a browser success screen alone does not grant paid work. Missing billing configuration or unverified standing fails closed.

Admission takes the worse of the Stripe standing and the Lago standing. No card on file, action required, a hard decline or an unverified state blocks new paid work. This gate does not cancel an in-flight run or liquidate paper positions.

The console must distinguish a known zero charge from a failed, stale or partial read. GetCurrentUsage and ListInvoices read Lago and report its quantities and amounts. GetCurrentUsage also says when it read them (observed_at) and, when Lago exposes one, its coverage watermark (as_of). The current-usage estimate is not a guarantee that the eventual invoice will be identical.

Payment retries

Lago does not retry a failed charge on its own, and the platform creates no retry or grace timer. While a finalized invoice’s charge has failed, new paid work waits. Making a card the default retries the tenant’s failed invoices at once.