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
units property) 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_id and wallet_id values in requests.
- Copy the production webhook URL from n8n and configure your AI app or service to POST usage payloads to the
lago-ai-usage endpoint (including a unique transaction_id and the desired low_credit_threshold).