Start with a console destination so you can inspect usage before configuring customer billing. This example uses Vercel AI Gateway, which reports cost in its provider metadata. You do not need a Narev account or key.
Install
Use Node.js 22 or later and install AI SDK 6 with V3 middleware. Select @ai-sdk/provider@^3 explicitly so npm resolves compatible middleware types:
Set AI_GATEWAY_API_KEY in your server environment. The gateway can charge for inference.
Wrap your model
Create billing.ts:
Run it with npx tsx billing.ts. Inspect the console billing event for usage, cost, and tags. Compare it with the provider’s usage record. Generating an answer alone does not prove that metering worked.
Choose a pricing source
This example uses the cost returned by the gateway. A direct provider such as OpenAI or DeepSeek can require a price resolver. Use a static map populated from verified Pricing API rates, or another compatible resolver. Static maps do not refresh themselves.
Follow the DeepSeek usage guide for a complete static-map example, including cache and reasoning units. Do not assume every middleware resolves prices automatically.
Send events to a billing destination
Replace or supplement the console destination with Polar, Stripe, Lago, or OpenMeter.
Use Customer Tagging from authenticated server state. Match the event’s customer identifier and numeric unit to your destination’s meter before enabling customer charges. For Polar, the default credits_consumed value is in base currency units, not nanos.
See the Node.js integration for a shared server wrapper and the chatbot guide for a Next.js and Polar example. Last modified on October 29, 2026