π€ Meter AI token usage and prepaid Lago credits with webhook receipts
β‘ 4 views Β· π€ CRM & Sales Operations
Description
Quick overview
This workflow receives AI token usage via a webhook, estimates the prepaid cost and projected credit balance in Lago, records the usage as a Lago event, and returns a receipt that warns when credits are at or below a configurable threshold.
How it works
- Receives a POST webhook request containing subscription identifiers, token counts, and metering details.
- Validates required fields, normalizes data (including an optional timestamp), and returns a 400 JSON error response if the request is invalid.
- Uses Lago to estimate pay-in-advance fees for the provided metric and sums the returned fee amounts.
- Fetches the customerβs Lago prepaid wallet and converts the estimated fee into credits to calculate the projected post-usage credit balance.
- Sends the usage event to Lago with the provided transaction ID for idempotency and builds a JSON receipt including estimated cost and projected balance.
- Returns an HTTP 202 JSON receipt that either flags low credit or confirms healthy credit status based on the configured threshold.
Setup
- Add Lago API credentials for the Lago community node.
- In Lago, create a billable metric (for example, using Sum aggregation on a
unitsproperty) and add it to an active plan as a pay-in-advance charge so fee estimation works. - Ensure each customer has an active Lago subscription and prepaid wallet, and send the correct
external_subscription_idandwallet_idvalues in requests. - Copy the production webhook URL from n8n and configure your AI app or service to POST usage payloads to the
lago-ai-usageendpoint (including a uniquetransaction_idand the desiredlow_credit_threshold).
π Nodes Used
Webhook
π₯ Import
Download workflow.json and import into n8n:
Workflow menu β Import from File