Yes it’s possible, but you’ll have to do clever programming to get it all neat and tidy.
Getting user info from an organization ID:
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"
}
Querying usage data for a specific date:
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;
Adding up two date ranges:
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.