> ## 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

> Middleware for the Vercel AI SDK that sends usage events directly to billing platforms like Stripe, Polar, OpenMeter, and Lago.

![Close-up of painted fingers checking a wrist, lit with vivid orange and teal highlights.](https://mintcdn.com/narev/R2v6PexAUeeLUXE0/images/art/neon-details/billing-vivid.webp?fit=max\&auto=format\&n=R2v6PexAUeeLUXE0\&q=85\&s=8493a949817b6ec89f34092b4b108b39)

Middleware for the [Vercel AI SDK](https://sdk.vercel.ai/docs) that captures LLM usage, calculates exact costs, and sends normalized billing events directly to your billing platforms.

Whether you're using OpenAI, Anthropic, OpenRouter, or local models, `@ai-billing` ensures that every token—including prompt caching and reasoning tokens—is precisely measured and billed to the correct user.

> **Provider-aware metrics**
>
> Understands provider-specific metrics like reasoning tokens, prompt caching, and web search costs so you don't miss a cent.

> **Flexible cost calculation**
>
> Use native provider pricing (like OpenRouter), hardcoded price maps, or a compatible resolver backed by the Pricing API.

> **Multiple destinations**
>
> Format and forward usage seamlessly to Stripe, Polar, OpenMeter, Lago, or your own endpoint—all at once.

> **Drop-in middleware**
>
> Works with `wrapLanguageModel`—no changes to your existing `streamText` / `generateText` calls.

## Install

```bash npm theme={null}
npm install @ai-billing/core
```

```bash pnpm theme={null}
pnpm add @ai-billing/core
```

```bash yarn theme={null}
yarn add @ai-billing/core
```

Then, install the package for your specific AI provider (e.g., `@ai-billing/openai`) and your billing destination (e.g., `@ai-billing/stripe`). See [Provider middleware](#provider-middleware) below.

## Quickstart

Start with the [AI-billing quickstart](https://www.narev.ai/docs/ai-billing/quickstart.md) to capture usage and provider-reported costs with a console destination. Then add a [billing destination](https://www.narev.ai/docs/ai-billing/integrations/index.md) and Customer Tagging from authenticated server state.

For direct providers that need rates, use a verified static price map or a compatible price resolver. The [Pricing API](https://www.narev.ai/docs/pricing-api/index.md) provides public, keyless REST access. The current `@ai-billing/narev` client still requires an `apiKey` option; see [access and compatibility](https://www.narev.ai/docs/pricing-api/reference/authentication.md) before choosing that client.

## Architecture

The library is built on three composable primitives:

**1. Provider middlewares**

Specialized middleware for each AI SDK provider (e.g., `@ai-billing/openai`, `@ai-billing/anthropic`). These understand provider-specific metadata shapes, extracting standard tokens alongside advanced metrics like `inputCacheReadTokens`, `internalReasoningTokens`, and `webSearch`.

**2. Price resolvers**

Functions that turn token metrics into precise financial costs. Use `createObjectPriceResolver` from `@ai-billing/core` for custom mappings, or use a compatible live resolver. Skip a separate resolver if your provider (like OpenRouter or Vercel AI Gateway) reports costs natively.

**3. Destinations**

Functions that receive a normalized `BillingEvent` and handle the API call to an external billing service (e.g., `@ai-billing/stripe`, `@ai-billing/polar`). Configure identity mapping and meter units for your destination. Polar uses base currency units by default; other destinations can use nanos.

## Packages

Every published `@ai-billing` package has generated TypeDoc reference docs under `/ai-billing/reference/<package>/typedoc/index`.

### Core and integrations

| Package | Role |
| :- | :- |
| [`@ai-billing/core`](https://www.narev.ai/docs/ai-billing/reference/core/typedoc/index) | V3/V4 billing middleware, destinations, price resolvers, and shared types |
| [`@ai-billing/narev`](https://www.narev.ai/docs/ai-billing/reference/narev/typedoc/index) | Narev pricing API client and `createNarevPriceResolver` |
| [`@ai-billing/nextjs`](https://www.narev.ai/docs/ai-billing/reference/nextjs/typedoc/index) | Next.js chat UI, Polar/Stripe credit components, and `create*WithBilling` server factories |

### Provider middleware

Text model support is the current priority. Each package ships V3 and V4 middleware.

| Provider | Package | Features |
| :- | :- | :- |
| [Alibaba](https://ai-sdk.dev/providers/ai-sdk-providers/alibaba) | [`@ai-billing/alibaba`](https://www.narev.ai/docs/ai-billing/reference/alibaba/typedoc/index) | Caching, Reasoning |
| [Amazon Bedrock](https://ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock) | [`@ai-billing/amazon-bedrock`](https://www.narev.ai/docs/ai-billing/reference/amazon-bedrock/typedoc/index) | Caching |
| [Anthropic](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic) | [`@ai-billing/anthropic`](https://www.narev.ai/docs/ai-billing/reference/anthropic/typedoc/index) | Caching |
| [Azure](https://ai-sdk.dev/providers/ai-sdk-providers/azure) | [`@ai-billing/azure`](https://www.narev.ai/docs/ai-billing/reference/azure/typedoc/index) | Caching, Reasoning |
| [Baseten](https://www.baseten.co/) | [`@ai-billing/baseten`](https://www.narev.ai/docs/ai-billing/reference/baseten/typedoc/index) | |
| [Cerebras](https://ai-sdk.dev/providers/ai-sdk-providers/cerebras) | [`@ai-billing/cerebras`](https://www.narev.ai/docs/ai-billing/reference/cerebras/typedoc/index) | Caching, Reasoning |
| [Chutes](https://chutes.ai) | [`@ai-billing/chutes`](https://www.narev.ai/docs/ai-billing/reference/chutes/typedoc/index) | Caching |
| [Cohere](https://ai-sdk.dev/providers/ai-sdk-providers/cohere) | [`@ai-billing/cohere`](https://www.narev.ai/docs/ai-billing/reference/cohere/typedoc/index) | |
| [DeepInfra](https://ai-sdk.dev/providers/ai-sdk-providers/deepinfra) | [`@ai-billing/deepinfra`](https://www.narev.ai/docs/ai-billing/reference/deepinfra/typedoc/index) | Caching |
| [DeepSeek](https://ai-sdk.dev/providers/ai-sdk-providers/deepseek) | [`@ai-billing/deepseek`](https://www.narev.ai/docs/ai-billing/reference/deepseek/typedoc/index) | Caching, Reasoning |
| [Fireworks](https://ai-sdk.dev/providers/ai-sdk-providers/fireworks) | [`@ai-billing/fireworks`](https://www.narev.ai/docs/ai-billing/reference/fireworks/typedoc/index) | Caching, Reasoning |
| [Vercel AI Gateway](https://ai-sdk.dev/providers/ai-sdk-providers/ai-gateway) | [`@ai-billing/gateway`](https://www.narev.ai/docs/ai-billing/reference/gateway/typedoc/index) | Native pricing |
| [GMI Cloud](https://gmicloud.ai/) | [`@ai-billing/gmicloud`](https://www.narev.ai/docs/ai-billing/reference/gmicloud/typedoc/index) | Caching, Reasoning |
| [Google](https://ai-sdk.dev/providers/ai-sdk-providers/google) | [`@ai-billing/google`](https://www.narev.ai/docs/ai-billing/reference/google/typedoc/index) | Caching, Reasoning |
| [Google Vertex AI](https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex) | [`@ai-billing/google-vertex`](https://www.narev.ai/docs/ai-billing/reference/google-vertex/typedoc/index) | Caching, Reasoning |
| [Groq](https://ai-sdk.dev/providers/ai-sdk-providers/groq) | [`@ai-billing/groq`](https://www.narev.ai/docs/ai-billing/reference/groq/typedoc/index) | Caching |
| [Hugging Face](https://ai-sdk.dev/providers/ai-sdk-providers/huggingface) | [`@ai-billing/huggingface`](https://www.narev.ai/docs/ai-billing/reference/huggingface/typedoc/index) | Caching, Reasoning |
| [Minimax](https://ai-sdk.dev/providers/community-providers/minimax) | [`@ai-billing/minimax`](https://www.narev.ai/docs/ai-billing/reference/minimax/typedoc/index) | Reasoning |
| [Mistral](https://ai-sdk.dev/providers/ai-sdk-providers/mistral) | [`@ai-billing/mistral`](https://www.narev.ai/docs/ai-billing/reference/mistral/typedoc/index) | Caching |
| [Moonshot AI](https://ai-sdk.dev/providers/ai-sdk-providers/moonshotai) | [`@ai-billing/moonshotai`](https://www.narev.ai/docs/ai-billing/reference/moonshotai/typedoc/index) | Caching, Reasoning |
| [OpenAI](https://ai-sdk.dev/providers/ai-sdk-providers/openai) | [`@ai-billing/openai`](https://www.narev.ai/docs/ai-billing/reference/openai/typedoc/index) | Caching, Search |
| [OpenAI Compatible](https://ai-sdk.dev/providers/openai-compatible-providers) | [`@ai-billing/openai-compatible`](https://www.narev.ai/docs/ai-billing/reference/openai-compatible/typedoc/index) | Caching, Reasoning |
| [OpenRouter](https://ai-sdk.dev/providers/community-providers/openrouter) | [`@ai-billing/openrouter`](https://www.narev.ai/docs/ai-billing/reference/openrouter/typedoc/index) | Native pricing |
| [Perplexity](https://ai-sdk.dev/providers/ai-sdk-providers/perplexity) | [`@ai-billing/perplexity`](https://www.narev.ai/docs/ai-billing/reference/perplexity/typedoc/index) | Reasoning, Search fee |
| [Together AI](https://ai-sdk.dev/providers/ai-sdk-providers/togetherai) | [`@ai-billing/togetherai`](https://www.narev.ai/docs/ai-billing/reference/togetherai/typedoc/index) | Reasoning |
| [xAI](https://ai-sdk.dev/providers/ai-sdk-providers/xai) | [`@ai-billing/xai`](https://www.narev.ai/docs/ai-billing/reference/xai/typedoc/index) | Caching, Reasoning |
| [Z.ai](https://z.ai/) | [`@ai-billing/zai`](https://www.narev.ai/docs/ai-billing/reference/zai/typedoc/index) | Caching, Reasoning |

### Billing destinations

| Destination | Package | Format |
| :- | :- | :- |
| Stripe | [`@ai-billing/stripe`](https://www.narev.ai/docs/ai-billing/reference/stripe/typedoc/index) | Meter events, nano-dollars |
| Polar.sh | [`@ai-billing/polar`](https://www.narev.ai/docs/ai-billing/reference/polar/typedoc/index) | Polar events, base currency units |
| OpenMeter | [`@ai-billing/openmeter`](https://www.narev.ai/docs/ai-billing/reference/openmeter/typedoc/index) | CloudEvents |
| Lago | [`@ai-billing/lago`](https://www.narev.ai/docs/ai-billing/reference/lago/typedoc/index) | Lago events, nano-dollars |
| Console | [`@ai-billing/core`](https://www.narev.ai/docs/ai-billing/reference/core/typedoc/index) | `consoleDestination` debug logging |

### Roadmap

> **Note:** We are prioritizing support for **text models**.

**Active development**

* [Requesty](https://ai-sdk.dev/providers/community-providers/requesty)
* [Cloudflare AI Gateway](https://ai-sdk.dev/providers/community-providers/cloudflare-ai-gateway)

To prioritize a provider that is not listed above, [open a GitHub issue](https://github.com/narevai/ai-billing/issues).

## Reference

> **[Core primitives](https://www.narev.ai/docs/ai-billing/reference/core/typedoc/index.md)**
>
> Middleware, destinations, price resolvers, and shared types in `@ai-billing/core`.

> **[Narev pricing client](https://www.narev.ai/docs/ai-billing/reference/narev/typedoc/index.md)**
>
> `createNarevClient`, `createNarevPriceResolver`, and trace cost helpers in `@ai-billing/narev`.

> **[Next.js components](https://www.narev.ai/docs/ai-billing/reference/nextjs/typedoc/index.md)**
>
> Chat UI, credit usage widgets, and server factories in `@ai-billing/nextjs`.

> **[Full API reference](https://www.narev.ai/docs/ai-billing/reference/openai/typedoc/index.md)**
>
> Browse the sidebar for every provider, destination, and integration package.


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