Stripe integration
AI Gateway can automatically send usage events to your Stripe billing meters, enabling seamless usage-based billing for your AI applications. The integration allows you to track costs, token usage, and other metrics directly in Stripe without manual data handling. You can use this to track per-customer usage and automatically bill customers based on their AI consumption.
- Automated Billing: Automatically report usage events to Stripe meters for usage-based billing.
- Real-time Tracking: Send events in real-time as soon as responses are received from AI providers.
- Cost Visibility: Track detailed cost information per request, including provider and model metadata.
- Flexible Templates: Use preset templates for common metrics or create custom payloads.
Before setting up Stripe integration, you need:
- A Stripe account with usage-based billing ↗ configured
- Stripe API key with appropriate permissions
- Billing meters ↗ set up in your Stripe account
To configure Stripe integration in the dashboard:
- Log into the Cloudflare dashboard ↗ and select your account.
- Go to AI > AI Gateway.
- Select your gateway.
- Select Settings.
- Scroll down to Stripe Integration.
- Add your Stripe API Key.
- Configure one or more JSON payloads for meter events.
- On the first payload, select Import a Preset > Input tokens.
- On the second payload, select Import a Preset > Output tokens.
This configuration sends two usage events to your Stripe meter: one for input tokens and another for output tokens.
To configure Stripe integration using the API:
-
Create an API token with the following permissions:
AI Gateway - ReadAI Gateway - Edit
-
Get your Account ID.
-
Using that API token and Account ID, send a
PUTrequest to update your gateway with Stripe integration settings.
AI Gateway provides several preset templates for common usage metrics:
- Input tokens - Tracks the number of input tokens processed
- Output tokens - Tracks the number of output tokens generated
- Cached read tokens - Tracks tokens read from cache
- Cached write tokens - Tracks tokens written to cache
Each preset template is pre-configured to derive customerId and event values through custom metadata passed in AI Gateway requests.
You can create custom JSON payloads beyond the preset templates. The following template variables are available for use in event payloads:
| Variable | Description |
|---|---|
$cost | Total cost of the request in USD |
$model | AI model used (e.g., gpt-4o-mini, gpt-5) |
$provider | Provider name (e.g., openai, anthropic) |
$input_tokens | Number of input tokens processed |
$output_tokens | Number of output tokens generated |
$cached_read_tokens | Tokens read from cache |
$cached_write_tokens | Tokens written to cache |
$metadata_metadataKey | Custom metadata field from cf-aig-metadata header |
For custom metadata variables, replace metadataKey with your actual field name. For example, $metadata_customerId retrieves the customerId field from your custom metadata.
{ "event_name": "ai_request", "timestamp": "2024-01-15T10:30:00Z", "properties": { "cost": "$cost", "model": "$model", "provider": "$provider", "input_tokens": "$input_tokens", "output_tokens": "$output_tokens", "customer_id": "$metadata_customerId" }}The AI Gateway Stripe integration follows this process:
-
Token Generation: AI Gateway generates and caches short-lived authentication tokens using Stripe's Create stream authentication session API ↗.
-
Metadata Processing: AI Gateway calculates cost and token-related metadata for your request, handling edge cases across different APIs and providers.
-
Template Processing: The system replaces template variables in your configured payloads with actual values from the request.
-
Event Timing: The timestamp on events is set to the exact time the AI Gateway request was made.
-
Event Submission: Authentication tokens are used to send configured payloads to Stripe's Create billing meter API ↗.
-
Real-time Delivery: Events are sent to Stripe in real-time as soon as responses are received from upstream AI providers.
AI Gateway includes robust error handling for Stripe API failures:
- Retry Logic: Failed requests are automatically retried up to 5 times
- Exponential Backoff: Retry delays increase exponentially to avoid overwhelming Stripe's API
- Graceful Degradation: AI Gateway requests continue to function normally even if Stripe integration fails
To use Stripe integration effectively:
- Configure custom metadata in your AI Gateway requests to provide customer identification and event details
- Ensure your Stripe billing meters are properly configured to receive the events
- Set up appropriate customer records in Stripe that match your metadata customer IDs
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark