
@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
@ai-billing/openai) and your billing destination (e.g., @ai-billing/stripe). See Provider middleware below.
Quickstart
Start with the AI-billing quickstart to capture usage and provider-reported costs with a console destination. Then add a billing destination 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 provides public, keyless REST access. The current@ai-billing/narev client still requires an apiKey option; see access and compatibility 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
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.