Skip to content

Commit 5c04fdf

Browse files
author
Rishit Bansal
committed
[AI Gateway]: documentation for new stripe integration feature
1 parent 98f90ef commit 5c04fdf

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
pcx_content_type: configuration
3+
title: Stripe integration
4+
sidebar:
5+
order: 6
6+
---
7+
8+
import { TabItem, Tabs } from "~/components";
9+
10+
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.
11+
12+
- **Automated Billing**: Automatically report usage events to Stripe meters for usage-based billing.
13+
- **Real-time Tracking**: Send events in real-time as soon as responses are received from AI providers.
14+
- **Cost Visibility**: Track detailed cost information per request, including provider and model metadata.
15+
- **Flexible Templates**: Use preset templates for common metrics or create custom payloads.
16+
17+
## Prerequisites
18+
19+
Before setting up Stripe integration, you need:
20+
21+
- A Stripe account with [usage-based billing](https://docs.stripe.com/billing/subscriptions/usage-based) configured
22+
- Stripe API key with appropriate permissions
23+
- [Billing meters](https://docs.stripe.com/billing/subscriptions/usage-based/meters/create) set up in your Stripe account
24+
25+
## Configuration
26+
27+
<Tabs syncKey="dashPlusAPI"> <TabItem label="Dashboard">
28+
29+
To configure Stripe integration in the dashboard:
30+
31+
1. Log into the [Cloudflare dashboard](https://dash.cloudflare.com/) and select your account.
32+
2. Go to **AI** > **AI Gateway**.
33+
3. Select your gateway.
34+
4. Select **Settings**.
35+
5. Scroll down to **Stripe Integration**.
36+
6. Add your **Stripe API Key**.
37+
7. Configure one or more JSON payloads for meter events.
38+
39+
### Example Setup
40+
41+
1. On the first payload, select **Import a Preset** > **Input tokens**.
42+
2. On the second payload, select **Import a Preset** > **Output tokens**.
43+
44+
This configuration sends two usage events to your Stripe meter: one for input tokens and another for output tokens.
45+
46+
</TabItem> <TabItem label="API">
47+
48+
To configure Stripe integration using the API:
49+
50+
1. [Create an API token](/fundamentals/api/get-started/create-token/) with the following permissions:
51+
- `AI Gateway - Read`
52+
- `AI Gateway - Edit`
53+
54+
2. Get your [Account ID](/fundamentals/account/find-account-and-zone-ids/).
55+
3. Using that API token and Account ID, send a [`PUT` request](/api/resources/ai_gateway/methods/update/) to update your gateway with Stripe integration settings.
56+
57+
</TabItem> </Tabs>
58+
59+
## Payload Templates
60+
61+
AI Gateway provides several preset templates for common usage metrics:
62+
63+
### Available Presets
64+
65+
1. **Input tokens** - Tracks the number of input tokens processed
66+
2. **Output tokens** - Tracks the number of output tokens generated
67+
3. **Cached read tokens** - Tracks tokens read from cache
68+
4. **Cached write tokens** - Tracks tokens written to cache
69+
70+
Each preset template is pre-configured to derive `customerId` and `event` values through [custom metadata](/ai-gateway/observability/custom-metadata/) passed in AI Gateway requests.
71+
72+
### Custom Payloads
73+
74+
You can create custom JSON payloads beyond the preset templates. The following template variables are available for use in event payloads:
75+
76+
| Variable | Description |
77+
|----------|-------------|
78+
| `$cost` | Total cost of the request in USD |
79+
| `$model` | AI model used (e.g., gpt-4o-mini, gpt-5) |
80+
| `$provider` | Provider name (e.g., openai, anthropic) |
81+
| `$input_tokens` | Number of input tokens processed |
82+
| `$output_tokens` | Number of output tokens generated |
83+
| `$cached_read_tokens` | Tokens read from cache |
84+
| `$cached_write_tokens` | Tokens written to cache |
85+
| `$metadata_metadataKey` | [Custom metadata](/ai-gateway/observability/custom-metadata/) field from cf-aig-metadata header |
86+
87+
For custom metadata variables, replace `metadataKey` with your actual field name. For example, `$metadata_customerId` retrieves the `customerId` field from your custom metadata.
88+
89+
### Example Custom Payload
90+
91+
```json
92+
{
93+
"event_name": "ai_request",
94+
"timestamp": "2024-01-15T10:30:00Z",
95+
"properties": {
96+
"cost": "$cost",
97+
"model": "$model",
98+
"provider": "$provider",
99+
"input_tokens": "$input_tokens",
100+
"output_tokens": "$output_tokens",
101+
"customer_id": "$metadata_customerId"
102+
}
103+
}
104+
```
105+
106+
## How It Works
107+
108+
The AI Gateway Stripe integration follows this process:
109+
110+
1. **Token Generation**: AI Gateway generates and caches short-lived authentication tokens using Stripe's [Create stream authentication session API](https://docs.stripe.com/api/v2/billing/meter-event-stream/session/create).
111+
112+
2. **Metadata Processing**: AI Gateway calculates cost and token-related metadata for your request, handling edge cases across different APIs and providers.
113+
114+
3. **Template Processing**: The system replaces template variables in your configured payloads with actual values from the request.
115+
116+
4. **Event Timing**: The timestamp on events is set to the exact time the AI Gateway request was made.
117+
118+
5. **Event Submission**: Authentication tokens are used to send configured payloads to Stripe's [Create billing meter API](https://docs.stripe.com/api/v2/billing/meter-event-stream/create).
119+
120+
6. **Real-time Delivery**: Events are sent to Stripe in real-time as soon as responses are received from upstream AI providers.
121+
122+
## Error Handling
123+
124+
AI Gateway includes robust error handling for Stripe API failures:
125+
126+
- **Retry Logic**: Failed requests are automatically retried up to 5 times
127+
- **Exponential Backoff**: Retry delays increase exponentially to avoid overwhelming Stripe's API
128+
- **Graceful Degradation**: AI Gateway requests continue to function normally even if Stripe integration fails
129+
130+
## Usage Requirements
131+
132+
To use Stripe integration effectively:
133+
134+
- Configure [custom metadata](/ai-gateway/observability/custom-metadata/) in your AI Gateway requests to provide customer identification and event details
135+
- Ensure your Stripe billing meters are properly configured to receive the events
136+
- Set up appropriate customer records in Stripe that match your metadata customer IDs
137+
138+
:::note
139+
140+
The Stripe integration sends events based on successful AI provider responses. Failed or blocked requests are not reported to Stripe meters.
141+
142+
:::

0 commit comments

Comments
 (0)