mirror of
https://github.com/stripe/ai.git
synced 2026-09-14 18:39:59 +08:00
40df32bd97
* 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>
Stripe AI SDK Provider Examples
This directory contains examples demonstrating how to use the Stripe AI SDK Provider to interact with various LLM models through Stripe's llm.stripe.com proxy.
Overview
The Stripe AI SDK Provider is a custom provider for the Vercel AI SDK that routes all requests through Stripe's LLM proxy, automatically tracking token usage for billing purposes.
Setup
- Install dependencies (from the ai-sdk-provider directory):
cd llm/ai-sdk-provider
npm install
- Set up environment variables in the examples directory:
cd examples
cp .env.example .env
Then edit .env and add your credentials:
# Required: Your Stripe API key
STRIPE_API_KEY=sk_test_...
# Required: Your Stripe Customer ID
STRIPE_CUSTOMER_ID=cus_...
Running examples
Each example file demonstrates different use cases. Run them from the examples directory:
OpenAI models
cd examples
npx ts-node openai.ts
Examples include:
- Simple text generation
- Streaming responses
- Multi-turn conversations
- Reasoning models (o3)
- Tool calling
Google Gemini models
npx ts-node google.ts
Examples include:
- Text generation with Gemini 2.5 Pro
- Streaming with Gemini Flash
- Using Gemini Lite models
- Custom headers
Anthropic Claude models
npx ts-node anthropic.ts
Examples include:
- Simple text generation
- Streaming with Claude Opus
- Claude Sonnet and Haiku models
- Tool calling
- Per-call customer ID override
Supported models
OpenAI
openai/gpt-5openai/gpt-5-miniopenai/gpt-5-nanoopenai/gpt-4.1openai/gpt-4.1-miniopenai/gpt-4.1-nanoopenai/gpt-4oopenai/gpt-4o-miniopenai/o3,openai/o3-mini,openai/o3-proopenai/o1,openai/o1-mini,openai/o1-pro
Google Gemini
google/gemini-2.5-progoogle/gemini-2.5-flashgoogle/gemini-2.5-flash-litegoogle/gemini-2.0-flashgoogle/gemini-2.0-flash-lite
Anthropic Claude
anthropic/claude-opus-4-1anthropic/claude-opus-4anthropic/claude-sonnet-4anthropic/claude-3-7-sonnetanthropic/claude-3-5-haikuanthropic/claude-3-haiku
Usage patterns
Basic setup
import { createStripe } from '@stripe/ai-sdk/provider';
import { generateText } from 'ai';
const stripeLLM = createStripe({
apiKey: process.env.STRIPE_API_KEY!,
customerId: process.env.STRIPE_CUSTOMER_ID, // Optional default
});
const model = stripe('openai/gpt-5');
Customer ID options
You can specify the customer ID in three ways (in order of priority):
- Per-call override (highest priority):
await generateText({
model: stripe('openai/gpt-5'),
prompt: 'Hello!',
providerOptions: {
stripe: {
customerId: 'cus_override'
}
}
});
- Model-level setting:
const model = stripe('openai/gpt-5', {
customerId: 'cus_model_level'
});
- Provider-level default:
const stripeLLM = createStripe({
apiKey: '...',
customerId: 'cus_default'
});
Streaming
import { streamText } from 'ai';
const result = await streamText({
model: stripe('google/gemini-2.5-flash'),
prompt: 'Tell me a story',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
Tool calling
const result = await generateText({
model: stripe('anthropic/claude-sonnet-4'),
prompt: 'What is the weather?',
tools: {
getWeather: {
description: 'Get weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string' }
},
required: ['location']
},
execute: async ({ location }) => {
return { temperature: 72, condition: 'Sunny' };
}
}
}
});
How it works
- All requests are routed to
https://llm.stripe.com/chat/completions - Your Stripe API key is included as the
Authorizationheader - The customer ID is included as the
X-Stripe-Customer-IDheader - Stripe automatically tracks token usage and bills the customer according to your Token Billing configuration