langwatch/agent-billing-demo is a deliberately small agent-platform SaaS: customers sign up, create agents, and chat with them. The platform meters every LLM call and rebills its customers, with zero metering code of its own. It exists so you can see the whole metering and rebilling pattern running, then copy it.
What it demonstrates
- Provisioning: customer signup mints one virtual key (the tenant boundary) and attaches a hard cap, a soft cap, and an attributed-user template, four REST calls.
- The request path: the chat calls the gateway on the OpenAI wire with the
userfield set. That one field is all the attribution the billing pipeline needs. - Billing: meters fed exclusively by LangWatch’s signed webhooks, with signature verification, dedup by envelope id, and settled-row supersession (replace, never sum). If the numbers on screen are right, the pipeline works end to end.
- Reconciliation: checksum comparison against
spend-summaries, cursor diff againstspend-eventson divergence. - Breach UX: the
402meta’sbudget_scopedrives two different messages, “your allowance ran out” versus “your workspace’s budget ran out”. - Period close: a reset button drives
POST /budgets/:id/reseton themanualwindows; recorded spend never changes.
The app
A React + Tailwind single page app, built by Vite and served by the Express server that also hosts the API and the webhook receiver, all on port4100. It has a landing page, a sign-up page, a customer dashboard, a developer panel, and a SaaS owner console.
Sign-up provisions a real tenant
OnePOST mints the virtual key and attaches three real budgets to it:
The form then redirects into the dashboard with a success toast naming the caps it just created, so the customer sees the outcome instead of a spinner. A workspace name that is already taken comes back as a structured
409 carrying code: "customer_exists" and an offer to open the existing workspace, never a database error.
The dashboard is the product
Each signed-in customer creates agents (name, system prompt, model) and chats with them. Chat is streamed through the gateway over Server-Sent Events, on that customer’s virtual key, with the seat’s email in the OpenAIuser field. A usage meter at the bottom of the page shows live spend against the real caps. Switching workspace swaps agents, transcripts, meter and event feed together, so nothing is shared between tenants but the code.
The app receives its own billing events
The app hosts its receiver in-process atPOST /webhooks/langwatch. It verifies the X-LangWatch-Signature HMAC over the raw request bytes, dedups by envelope id, and answers 2xx only once the batch is stored. Ingested events drive the meters and stream to the browser over Server-Sent Events, so a delivered event moves the meter without a page refresh.
The owner console
/admin is the SaaS owner’s side of the same data: every customer with their virtual key, caps, spend and request counts, plus the actions a platform operator needs.
- Close billing period, per customer, resets the
manual-window caps only. Themonth-window seat allowance is left to roll over on its own, because closing the company’s books does not hand every seat a fresh personal allowance, and recorded spend is never mutated. - Adjust caps, per customer, calls the budgets update API. Raising a breached cap admits traffic again with the books intact.
- Webhook receiver health: whether this app’s endpoint is registered, and how many events it has ingested.
- A live feed of billing events, signed deliveries as they arrive.
The developer panel
/developer keeps the raw signed envelopes one click away, expandable as JSON, next to the request-path and webhook-contract snippets that are the whole integration.
Breaches read as sentences
A402 from the gateway surfaces as a friendly UI state, branched on the budget_scope meta:
virtual_key: “Your workspace has reached its AI budget for this period.”attributed_user: “You have used up your personal AI allowance for this period.”
The integration surfaces, twice
Every surface you would copy exists in both languages, self-contained:
The app’s own receiver lives at
app/src/webhooks.ts and implements the identical contract as the standalone one. Both standalone receivers run side by side against the same LangWatch instance, each registered as its own endpoint with its own secret; every request lands in both ledgers, signed and deduped, which doubles as a demonstration that delivery is consumer-agnostic.
Running it
You need a LangWatch instance (local dev, self-hosted, or cloud) with an enterprise license for the webhook surface, and an organization API key carrying the gateway and webhook permissions.See also
- Metering and rebilling your customers: the cookbook this demo implements.
- Billing & spend events: the payload and money contract.
- Webhooks: endpoints, signatures, retries, health.