* Add AI SDK Provider * add ai-sdk-billing-wrapper * remove sensitive console.log * add token-billing sdk * Update and rename ai-sdk and token-billing packages * Update readmes, remove tool calling * rename stripe to stripeLLM for ai sdk provider * update readmes, remove emojis, sentence case * Remove incorrect readme * Update llm/ai-sdk/README.md Co-authored-by: Steve Kaliski <59668283+stevekaliski-stripe@users.noreply.github.com> * update name to token-meter * add README to llm folder * Update llm/README.md Co-authored-by: matv-stripe <55463370+matv-stripe@users.noreply.github.com> * Update llm/README.md Co-authored-by: matv-stripe <55463370+matv-stripe@users.noreply.github.com> * Update llm/ai-sdk/meter/README.md Co-authored-by: mpbagwell-stripe <mpbagwell@stripe.com> * Apply suggestions from code review Co-authored-by: mpbagwell-stripe <mpbagwell@stripe.com> * update readmes for style --------- Co-authored-by: Steve Kaliski <59668283+stevekaliski-stripe@users.noreply.github.com> Co-authored-by: matv-stripe <55463370+matv-stripe@users.noreply.github.com> Co-authored-by: mpbagwell-stripe <mpbagwell@stripe.com>
AI SDK Billing Wrapper Examples
This directory contains examples demonstrating how to use the AI SDK Billing Wrapper to automatically track and report token usage to Stripe for billing purposes when using the Vercel AI SDK.
Overview
The AI SDK Billing Wrapper intercepts calls to language models and automatically sends usage data to Stripe's meter events API, enabling you to bill customers based on their AI token consumption without manually tracking usage.
Setup
- Install dependencies from the ai-sdk-billing-wrapper directory:
cd llm/ai-sdk-billing-wrapper
npm install
- Set up environment variables in the examples directory:
cd examples
cp .env.example .env
Then edit .env and add your credentials:
# Required for all examples
STRIPE_API_KEY=sk_test_...
STRIPE_CUSTOMER_ID=cus_...
# Provider-specific (only required for the provider you want to test)
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_GENERATIVE_AI_API_KEY=...
Running examples
Run examples from the examples directory using ts-node:
OpenAI examples
npx ts-node openai.ts
Demonstrates:
- Basic text generation
- Streaming responses
- System messages
- Multi-turn conversations
- Different GPT models (GPT-4o-mini, GPT-4)
- Max tokens configuration
Anthropic Claude examples
npx ts-node anthropic.ts
Demonstrates:
- Basic text generation with Claude Sonnet
- Streaming responses
- System messages
- Multi-turn conversations
- Claude Haiku for faster responses
- Max tokens configuration
Google Gemini examples
npx ts-node google.ts
Demonstrates:
- Basic text generation with Gemini
- Streaming with Gemini Flash
- System messages
- Multi-turn conversations
- Temperature control
How it works
The meteredModel wrapper automatically:
- Intercepts API Calls: Wraps any AI SDK v2 language model
- Tracks Token Usage: Captures input tokens, output tokens, and model information
- Reports to Stripe: Sends meter events to Stripe after each generation
- Handles Streaming: Works with both streaming and non-streaming responses
Basic usage pattern
import { meteredModel } from '@stripe/ai-sdk-billing-wrapper';
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const model = meteredModel(
openai('gpt-4o-mini'),
process.env.STRIPE_API_KEY,
'cus_xxxxx'
);
const { text } = await generateText({
model,
prompt: 'Hello!',
});
Supported providers
The wrapper works with any AI SDK provider that implements the v2 specification (LanguageModelV2):
Supported:
- OpenAI (
@ai-sdk/openai) - Anthropic Claude (
@ai-sdk/anthropic) - Google Gemini (
@ai-sdk/google) - Azure OpenAI (via
@ai-sdk/openai) - Amazon Bedrock (
@ai-sdk/amazon-bedrock) - Together AI (via
createOpenAI) - Any custom provider implementing
LanguageModelV2
Not supported:
- Groq (
@ai-sdk/groq) - uses v1 specification - Any provider using
LanguageModelV1
The wrapper enforces v2-only models at TypeScript compile time.
Stripe meter events
Each API call generates meter events sent to Stripe:
{
event_name: 'token-billing-tokens',
payload: {
stripe_customer_id: 'cus_xxxxx',
value: '100',
model: 'openai/gpt-4o-mini',
token_type: 'input'
}
}
Metadata included:
- Provider (e.g., "openai", "anthropic", "google")
- Model ID (e.g., "gpt-4o-mini", "claude-sonnet-4")
- Input tokens
- Output tokens
Error handling
The wrapper handles errors gracefully:
- Invalid Customer ID: Throws an error before making the API call
- Unsupported Models: TypeScript prevents usage at compile time
- Stripe API Errors: Logged but don't interrupt the AI generation
- Missing Tokens: Handles responses without usage information