Documentation Index
Fetch the complete documentation index at: https://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 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 real-time cost resolution via Narev.
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
@ai-billing/openai) and your billing destination (e.g., @ai-billing/stripe). See Supported providers below.
Quick start
1. Initialize the destination
Set up the destination where your billing events will be sent. The destination knows exactly how to format the data (e.g., converting to nano-dollars) and map your tags to the correct customer identity for that platform.2. Set up the middleware & price resolution
Initialize the provider middleware. If your provider doesn’t supply costs natively in the API response (like OpenAI or Anthropic), attach aPriceResolver to calculate the cost of the tokens.
3. Wrap your model
Apply the middleware to your language model using the Vercel AI SDK’swrapLanguageModel.
4. Pass tags during generation
Generate text exactly as you normally would. UseproviderOptions to pass ai-billing-tags so the destination knows which customer to bill.
Architecture
The library is built on three composable primitives: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.Price resolvers
Functions that turn token metrics into precise financial costs. Use the
NarevPriceResolver for real-time rates, the ObjectPriceResolver for custom mappings, or skip this entirely if your provider (like OpenRouter) calculates costs natively.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). Destinations ensure that identity mapping, metadata constraints, and cost formatting (like cost_nanos) comply perfectly with each platform’s rules.Supported providers
Text model support is the current priority.| Provider | Package | Features |
|---|---|---|
| OpenAI | @ai-billing/openai | Caching, Search |
| Anthropic | @ai-billing/anthropic | Caching |
@ai-billing/google | Caching, Reasoning | |
| OpenRouter | @ai-billing/openrouter | Native Pricing |
| DeepSeek | @ai-billing/deepseek | Caching |
| Minimax | @ai-billing/minimax | Reasoning |
| xAI Grok | @ai-billing/xai | Caching |
| Groq | @ai-billing/groq | |
| OpenAI Compatible | @ai-billing/openai-compatible | Caching, Reasoning |
| Vercel AI Gateway | @ai-billing/gateway | |
| Chutes | @ai-billing/chutes | Caching |
Status and Roadmap
Note: We are prioritizing support for TEXT models.Active Development The following providers are planned for future implementation. To prioritize a specific provider, please open a GitHub issue.
Supported destinations
| Destination | Package | Format Requirements |
|---|---|---|
| Stripe | @ai-billing/stripe | Meter Events, Nano-dollars |
| Polar.sh | @ai-billing/polar | Polar Events, Nano-dollars |
| OpenMeter | @ai-billing/openmeter | CloudEvents |
| Lago | @ai-billing/lago | Lago Events, Nano-dollars |
| Console | @ai-billing/core | Debug logging |