Skip to content

narevai/ai-billing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

205 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ai-billing

  • Middleware for the Vercel AI SDK
  • Sends billing events directly to billing platforms (Stripe, Polar, and Lago)
  • Ships with components to make usage-based billing easy

codecov Node Current NPM License Discord Chat

AI Billing Header 1

UI Components

Storybook: Explore the @ai-billing/nextjs components

<CreditTopUpPolar>

Topup Component from @ai-billing/nextjs

<CreditUsagePolar>

Usage Component from @ai-billing/nextjs

Full-stack examples

Name Demo Link Repo Deploy
Chatbot (OpenRouter + Polar) View Demo GitHub Deploy with Vercel
Chatbot (OpenAI + Polar) View Demo GitHub Deploy with Vercel
Chatbot (Stripe) View Demo GitHub Deploy with Vercel

Billing Architecture

AI Billing Header 2

Supported Providers

Provider Package Size
OpenRouter @ai-billing/openrouter NPM Unpacked Size
OpenAI @ai-billing/openai NPM Unpacked Size
Vercel AI Gateway @ai-billing/gateway NPM Unpacked Size
OpenAI Compatible @ai-billing/openai-compatible NPM Unpacked Size
Groq @ai-billing/groq NPM Unpacked Size
Google Generative AI @ai-billing/google NPM Unpacked Size
Anthropic @ai-billing/anthropic NPM Unpacked Size
xAI Grok @ai-billing/xai NPM Unpacked Size
MiniMax @ai-billing/minimax NPM Unpacked Size
DeepSeek @ai-billing/deepseek NPM Unpacked Size
Chutes @ai-billing/chutes NPM Unpacked Size

Supported Destinations

Destination Package Size
Polar.sh @ai-billing/polar NPM Unpacked Size
Stripe @ai-billing/stripe NPM Unpacked Size
OpenMeter (Kong) @ai-billing/openmeter NPM Unpacked Size
Lago @ai-billing/lago NPM Unpacked Size

UI & SDKs

Package Description Size
@ai-billing/nextjs Next.js UI components for displaying billing usage and managing top-ups. NPM Unpacked Size
@ai-billing/ui Internal headless UI components shared across @ai-billing/* packages. NPM Unpacked Size
@ai-billing/narev TypeScript SDK for the Narev billing API. NPM Unpacked Size

Installation

npm install @ai-billing/core @ai-billing/openrouter # Example for OpenRouter

Basic Usage

Wrap your model provider with the billing middleware and define your destinations.

import { streamText, wrapLanguageModel } from 'ai';
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { createOpenRouterV3Middleware } from '@ai-billing/openrouter';

const billingMiddleware = createOpenRouterV3Middleware({});

const model = wrapLanguageModel({
  model: createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY })('google/gemini-2.0-flash-001'),
  middleware: billingMiddleware,
});

Send usage to Polar.sh

Wrap your model provider with the billing middleware and define your destinations.

import { streamText, wrapLanguageModel } from 'ai';
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { createOpenRouterV3Middleware } from '@ai-billing/openrouter';
import { createPolarDestination } from '@ai-billing/polar';

const billingMiddleware = createOpenRouterV3Middleware({
  destinations: [
    createPolarDestination({
      accessToken: process.env.POLAR_ACCESS_TOKEN,
      eventName: 'llm_usage',
    })
  ],
});

const model = wrapLanguageModel({
  model: createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY })('google/gemini-2.0-flash-001'),
  middleware: billingMiddleware,
});

const { textStream } = await streamText({
  model,
  messages: [{ role: 'user', content: 'Quantify the value of metadata.' }],
  providerOptions: { 
    'ai-billing-tags': { userId: 'usr_123', org: 'Acme' } 
  },
});

Status and Roadmap

Note: We are prioritizing support for TEXT models.

Active Development

Full list of providers can be found here: https://ai-sdk.dev/providers/ The following providers are planned for future implementation. To prioritize a specific provider, please open a GitHub issue.

Architecture

The package consists of two primary components:

1. Provider Middleware

  • specialized for @ai-sdk/* that understand the specific providerMetadata shapes of different LLM usage
  • provider-specific cost calculation logic that that turn usage into cost
  • PriceResolver allowing to pass custom prices at time of request

2. Destinations

  • functions that receive a normalized BillingEvent and handle the API calls to external services
  • allow charging in credits using standardized meters

About

Easy billing for vercel/ai-sdk

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors

Languages