> ## Documentation Index
> Fetch the complete documentation index at: https://www.narev.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AI-billing quickstart

> Capture Vercel AI SDK usage and provider-reported costs with AI-billing middleware.

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:

```bash theme={null}
npm install ai@^6 @ai-sdk/provider@^3 @ai-sdk/gateway@^3 @ai-billing/gateway@^0.2 @ai-billing/core@^0.1.6
npm install --save-dev tsx
```

Set `AI_GATEWAY_API_KEY` in your server environment. The gateway can charge for inference.

## Wrap your model

Create `billing.ts`:

```typescript theme={null}
import { generateText, wrapLanguageModel } from 'ai';
import { createGateway } from '@ai-sdk/gateway';
import { createGatewayV3Middleware } from '@ai-billing/gateway';
import { consoleDestination } from '@ai-billing/core';

const apiKey = process.env.AI_GATEWAY_API_KEY;
if (!apiKey) throw new Error('Set AI_GATEWAY_API_KEY');

const gateway = createGateway({ apiKey });
const model = wrapLanguageModel({
  model: gateway('openai/gpt-4o'),
  middleware: createGatewayV3Middleware({
    destinations: [consoleDestination()],
  }),
});

const result = await generateText({
  model,
  prompt: 'Explain token usage in one sentence.',
  providerOptions: {
    'ai-billing-tags': { userId: 'local-test', feature: 'quickstart' },
  },
});

console.log(result.text);
```

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](https://www.narev.ai/docs/pricing-api/quickstart.md) rates, or another compatible resolver. Static maps do not refresh themselves.

Follow the [DeepSeek usage guide](https://www.narev.ai/docs/guides/deepseek-usage-based-billing.md) 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](https://www.narev.ai/docs/ai-billing/integrations/billing-platforms/polar.md), [Stripe](https://www.narev.ai/docs/ai-billing/integrations/billing-platforms/stripe.md), [Lago](https://www.narev.ai/docs/ai-billing/integrations/billing-platforms/lago.md), or [OpenMeter](https://www.narev.ai/docs/ai-billing/integrations/billing-platforms/openmeter.md).

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](https://www.narev.ai/docs/ai-billing/integrations/frameworks/nodejs.md) for a shared server wrapper and the [chatbot guide](https://www.narev.ai/docs/guides/chatbot-with-billing-polar.md) for a Next.js and Polar example.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.