Skip to main content
We’ll go here in two steps: first pull live prices over REST or MCP, then wrap Vercel AI SDK calls with Narev middleware and send dollar usage to Polar.

Part 1: Pull live prices

REST API

Pull current rates for a provider.

MCP

Same catalog, over Model Context Protocol. No API key. Server URL: https://mcp.narev.ai

Install in Cursor

Opens Cursor and prompts to add the Narev MCP server.

Install in VS Code

Opens VS Code and prompts to add the Narev MCP server.
Agents can call list_providers, list_models, get_prices, and calculate_cost, plus docs search tools.

Part 2: Implement usage-based billing with Vercel AI SDK and Polar

We’ll bootstrap a small app here in Next.js that charges users for LLM usage.
  • We’ll use Vercel AI SDK to call the model
  • We’ll use middleware to resolve the cost of the call and send the usage to Polar
  • We’ll use Next.js UI components to show the usage and let users buy credits
This guide uses Polar for a short setup path. The same middleware pattern works with Stripe, OpenMeter, Lago, and other supported destinations.
1

Get your API keys

You need a Narev Cloud key for price resolution, plus credentials for your billing destination.
  1. Create a free Narev Cloud account.
  2. Open Settings → API Keys and create a NAREV_API_KEY.
  3. Create a Polar sandbox and generate a POLAR_ACCESS_TOKEN.
.env.local
2

Install the packages

Install the provider middleware, Polar destination, Next.js UI components, and the Vercel AI SDK packages you call.
3

Configure your billing destination

Create lib/destinations.ts. Set externalCustomerIdKey to the Customer Tagging field Polar should treat as the customer id.
lib/destinations.ts
4

Wrap the model with billing middleware

Attach Narev middleware with wrapLanguageModel. Each generation resolves token cost from the live catalog and forwards usage to your destination.
lib/billing.ts
Keep model creation in one helper so every route uses the metered instance.
5

Call the model from an API route

Pass the wrapped model into streamText. Put Customer Tagging metadata in providerOptions so Polar knows which customer to charge.
app/api/chat/route.ts
Confirm userId maps to the customer record in Polar. Stay on sandbox until the mapping looks right.
6

Add billing UI components

Use @ai-billing/nextjs to show usage and let users buy credits.
app/components/usage-dashboard.tsx
7

Verify billing works

Run a chat request, then check Polar for an llm_usage event tied to your sandbox customer. The UI should show usage for that userId.
Billing dashboard using Narev components showing real-time usage

Usage component showing current consumption

Billing dashboard using Narev components showing credit top-up options

Top-up component for buying credits

Middleware resolves cost from Narev prices and Polar receives billable events for tagged customers.

Next steps

MCP quickstart

Connect Cursor, Claude, or VS Code to https://mcp.narev.ai.

API reference

Full REST paths for providers, models, prices, and cost calculation.

Node.js billing integration

Reusable middleware helpers and coverage checks for any Node server.

Narev SDK overview

Provider middleware, price resolvers, and billing destinations.