Hi! I’m wondering if it’s possible to get usage data via token key?
I want to display in my plugin all the data in the usage tab - OpenAI Platform.
how can I do it ?
Hi! I’m wondering if it’s possible to get usage data via token key?
I want to display in my plugin all the data in the usage tab - OpenAI Platform.
how can I do it ?
Not really possible currently, see other topics on this forum like this one.
Yes it’s possible, but you’ll have to do clever programming to get it all neat and tidy.
curl "https://api.openai.com/v1/organizations/$organization" \
-H "Authorization: Bearer $token" -X 'GET'
Response:
{
"object": "organization",
"id": "{My organization ID}",
"created": 1646014400,
"title": "Personal",
"name": "{My user ID}",
"description": "Personal org for {My EMAIL}",
"personal": true,
"is_default": true,
"role": "owner"
}
curl -X 'GET' "https://api.openai.com/v1/usage?date=2023-07-10&user_public_id=$user" \
-H "Authorization: Bearer $token" \
-H "OpenAI-Organization: $organization"
Response:
{
"object": "list",
"data": [
{
"aggregation_timestamp": 1688961200,
"n_requests": 1,
"operation": "completion",
"snapshot_id": "gpt-4-0613",
"n_context": 1,
"n_context_tokens_total": 380,
"n_generated": 1,
"n_generated_tokens_total": 314
},
...
],
"ft_data": [],
"dalle_api_data": [],
"whisper_api_data": [],
"current_usage_usd": 0.0
}
That returns an array of JSON
objects for each time you’ve invoked the API and details the tokens used. You’ll need to add those up and convert it into cost based on the model pricing.
The way I personally use it, which is much easier;
curl -s "https://api.openai.com/dashboard/billing/usage?end_date=2023-08-01&start_date=2023-07-01" \
-H "Authorization: Bearer $token" \
-H "OpenAI-Organization: $organization"
Response:
{
"object": "list",
"daily_costs": [
{
"timestamp": 1688169400.0,
"line_items": [
{
"name": "GPT-4",
"cost": 0.0
},
{
"name": "Chat models",
"cost": 0.0
},
{
"name": "InstructGPT",
"cost": 0.0
},
{
"name": "Fine-tuning models",
"cost": 0.0
},
{
"name": "Embedding models",
"cost": 0.0
},
{
"name": "Image models",
"cost": 0.0
},
{
"name": "Audio models",
"cost": 0.0
}
]
},
...
"total_usage": 79.68185
These results conveniently combine all your costs for you based on the date range in the total_usage
key.
79.68185 meaning, 79 cents.
All costs will be in cents? 1$ 9cents = “total_usage”: 109.68185 , yes ?
And here’s another bit of code, more reverse engineering of what is undocumented API.
organization user to usage
The convoluted “organization” system that doesn’t really align with a developer providing AI services or a company using them - which would require each employee or user to have a personal OpenAI account:
An account can have one organization:
API keys are unique to a user account.
The user can be invited to an organization.
The user can select the default organization for all API keys.
The user can specify a different organization of theirs in an API call.
(then this is confused by the “user” field in an API call, which seemingly can only serve to let you blame one of your users when OpenAI tries to kick you for content.)