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 Provider middleware 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: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
createNarevPriceResolver from @ai-billing/narev for live Narev rates, createObjectPriceResolver from @ai-billing/core for custom mappings, or skip this entirely 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). Destinations ensure that identity mapping, metadata constraints, and cost formatting (like cost_nanos) comply perfectly with each platform’s rules.Packages
Every published@ai-billing package has generated TypeDoc reference docs under /sdk/ai-billing/reference/<package>/typedoc/index.
Core and integrations
Provider middleware
Text model support is the current priority. Each package ships V3 and V4 middleware.Billing destinations
Roadmap
Note: We are prioritizing support for text models.Active development To prioritize a provider that is not listed above, open a GitHub issue.
Reference
Core primitives
Middleware, destinations, price resolvers, and shared types in
@ai-billing/core.Narev pricing client
createNarevClient, createNarevPriceResolver, and trace cost helpers in @ai-billing/narev.Next.js components
Chat UI, credit usage widgets, and server factories in
@ai-billing/nextjs.Full API reference
Browse the sidebar for every provider, destination, and integration package.