Usage & Billing
The AI Gateway provides endpoints to monitor your credit balance, track usage, and retrieve detailed information about specific generations.
The Usage & Billing API is available at the following base URL:
https://ai-gateway.vercel.sh/v1
The AI Gateway supports the following Usage & Billing endpoints:
GET /credits
- Check your credit balance and usage informationGET /generation
- Retrieve detailed information about a specific generation
Check your AI Gateway credit balance and usage information.
GET /credits
const apiKey = process.env.AI_GATEWAY_API_KEY || process.env.VERCEL_OIDC_TOKEN;
const response = await fetch('https://ai-gateway.vercel.sh/v1/credits', {
method: 'GET',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
});
const credits = await response.json();
console.log(credits);
import os
import requests
api_key = os.getenv("AI_GATEWAY_API_KEY") or os.getenv("VERCEL_OIDC_TOKEN")
response = requests.get(
"https://ai-gateway.vercel.sh/v1/credits",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
)
credits = response.json()
print(credits)
{
"balance": "95.50",
"total_used": "4.50"
}
balance
: The remaining credit balancetotal_used
: The total amount of credits used
Retrieve detailed information about a specific generation by its ID. This endpoint allows you to look up usage data, costs, and metadata for any generation created through the AI Gateway. Generation information is available shortly after the generation completes. Note much of this data is also included in the providerMetadata
field of the chat completion responses.
GET /generation?id={generation_id}
id
(required): The generation ID to look up (format:gen_<ulid>
)
const generationId = 'gen_01ARZ3NDEKTSV4RRFFQ69G5FAV';
const response = await fetch(
`https://ai-gateway.vercel.sh/v1/generation?id=${generationId}`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.AI_GATEWAY_API_KEY}`,
'Content-Type': 'application/json',
},
},
);
const generation = await response.json();
console.log(generation);
import os
import requests
generation_id = 'gen_01ARZ3NDEKTSV4RRFFQ69G5FAV'
response = requests.get(
f"https://ai-gateway.vercel.sh/v1/generation?id={generation_id}",
headers={
"Authorization": f"Bearer {os.getenv('AI_GATEWAY_API_KEY')}",
"Content-Type": "application/json",
},
)
generation = response.json()
print(generation)
{
"data": {
"id": "gen_01ARZ3NDEKTSV4RRFFQ69G5FAV",
"total_cost": 0.00123,
"usage": 0.00123,
"created_at": "2024-01-01T00:00:00.000Z",
"model": "gpt-4",
"is_byok": false,
"provider_name": "openai",
"streamed": true,
"latency": 200,
"generation_time": 1500,
"tokens_prompt": 100,
"tokens_completion": 50,
"native_tokens_prompt": 100,
"native_tokens_completion": 50,
"native_tokens_reasoning": 0,
"native_tokens_cached": 0
}
}
id
: The generation IDtotal_cost
: Total cost in USD for this generationusage
: Usage cost (same as total_cost)created_at
: ISO 8601 timestamp when the generation was createdmodel
: Model identifier used for this generationis_byok
: Whether this generation used Bring Your Own Key credentialsprovider_name
: The provider that served this generationstreamed
: Whether this generation used streaming (true
for streamed responses,false
otherwise)latency
: Time to first token in millisecondsgeneration_time
: Total generation time in millisecondstokens_prompt
: Number of prompt tokenstokens_completion
: Number of completion tokensnative_tokens_prompt
: Native prompt tokens (provider-specific)native_tokens_completion
: Native completion tokens (provider-specific)native_tokens_reasoning
: Reasoning tokens used (if applicable)native_tokens_cached
: Cached tokens used (if applicable)
Generation IDs: Generation IDs are included in chat completion responses
as the
id
field as well as in the provider metadata returned in the response.
Was this helpful?