Set up Narev Cloud, install the Narev SDK, meter Vercel AI SDK calls, tag customer usage, and send billable AI events to Polar.
Start with usage-based billing so you can measure usage and bill customers correctly from day one. This guide walks you through connecting a Next.js app to Narev Cloud using the Narev SDK and sending billable AI events to Polar.
1
Get your API keys
To authenticate your app, you need API keys from both Narev Cloud and your billing provider.
This guide uses Polar because it has a concise setup flow. You can use the exact same framework pattern with Stripe, OpenMeter, Lago, or other supported destinations.
2
Install the packages
Install the core Narev SDK billing package, your preferred billing provider destination, and the Next.js UI components.
npm install @ai-billing/core @ai-billing/polar @ai-billing/nextjs ai @ai-sdk/openai
pnpm add @ai-billing/core @ai-billing/polar @ai-billing/nextjs ai @ai-sdk/openai
yarn add @ai-billing/core @ai-billing/polar @ai-billing/nextjs ai @ai-sdk/openai
bun add @ai-billing/core @ai-billing/polar @ai-billing/nextjs ai @ai-sdk/openai
3
Configure your billing destination
Configure the destination that receives your billing events.Create a lib/destinations.ts file to configure Polar. The externalCustomerIdKey is critical because it tells Polar which Customer Tagging field maps the usage to a specific customer.
Use the wrapLanguageModel function from the Vercel AI SDK to attach the Narev billing middleware to your model. This wrapper automatically tracks token usage and sends it to your billing provider.Create a central helper for your billed models that uses your configured destinations.
Next.js
lib/billing.ts
import { wrapLanguageModel } from 'ai';import { openai } from '@ai-sdk/openai';import { createOpenAIV3Middleware } from '@ai-billing/openai';import { getBillingDestinations } from './destinations';const billingMiddleware = createOpenAIV3Middleware({ destinations: getBillingDestinations()});// Wrap your model to automatically meter all generationsexport const billedModel = wrapLanguageModel({ model: openai('gpt-4o'), middleware: billingMiddleware});
Centralizing your model creation in one file ensures that every part of your app uses the metered instance, preventing unbilled usage leaks.
5
Use the model in your API route
Pass the wrapped model into Vercel’s streamText function. Use the providerOptions object to pass Customer Tagging metadata. The billing destination uses this data to know which customer to charge for the tokens consumed.
Next.js
app/api/chat/route.ts
import { streamText } from 'ai';import { billedModel } from '@/lib/billing';export async function POST(req: Request) { const { messages } = await req.json(); // Get the authenticated user ID (e.g., from your auth provider) const userId = 'user_123'; const result = streamText({ model: billedModel, messages, providerOptions: { // These tags are automatically forwarded to your billing destination 'ai-billing-tags': { userId: userId, // You can add additional metadata here for deeper cost attribution feature: 'chat-interface' }, }, }); return result.toDataStreamResponse();}
Always verify that userId correctly maps to the corresponding customer record in your billing provider. Start in a sandbox environment before deploying to production.
6
Add the pre-built billing components
Embed the pre-built React components in your Next.js frontend to show users their real-time usage and provide a native checkout experience for buying credits.