Skip to content

Commit cad387b

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

File tree

1 file changed

+143
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)