openai-python
openai-python copied to clipboard
How can I correctly count the usage of each API Key?
Describe the feature or improvement you're requesting
I hope to count the usage consumption of several API Keys under my OpenAI account.
On the help page, I saw this introduction.
I tried to enter this command in python:
import openai
openai.api_key = "sk-vaU0ES******fhvr"
usages = openai.usage.list()
for usage in usages.data:
print(f"Key: {usage.key}")
print(f"Total requests: {usage.total_requests}")
print(f"Total cost: {usage.total_cost}")
But I got the prompt
AttributeError: module 'openai' has no attribute 'usage'
How can I correctly count the usage of each API Key?
The example JSON you quote above is an example response that you get back from (for instance) a call to openai.Completion.create(...)
. The JSON openai sends you back from your API request will include relevant usage data, and you can aggregate that data on your local system in whatever way makes sense for your application.
For example, if you call r = openai.Completion.create(api_key="...", model="text-davinci-003", prompt="...")
Then your usage data for that one call will be available in r["usage"]
.
As far as I know, their API does not yet support a means of querying OpenAI metrics such as per-account usage, per-key usage, credit balance, etc.
Hope that helps!
The example JSON you quote above is an example response that you get back from (for instance) a call to
openai.Completion.create(...)
. The JSON openai sends you back from your API request will include relevant usage data, and you can aggregate that data on your local system in whatever way makes sense for your application.For example, if you call
r = openai.Completion.create(api_key="...", model="text-davinci-003", prompt="...")
Then your usage data for that one call will be available inr["usage"]
.As far as I know, their API does not yet support a means of querying OpenAI metrics such as per-account usage, per-key usage, credit balance, etc.
Hope that helps!
Thank you very much for your reply, this is very useful!
As far as I know, their API does not yet support a means of querying OpenAI metrics such as per-account usage, per-key usage, credit balance, etc.
Not something that is currently supported but will flag it to the team!
I have similar issues, but I'm using the stream version of Prompt. As you can see below, there is no usage data in the response. How can I turn it on?
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'role': 'assistant'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': 'Hello'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': '!'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': ' How'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': ' can'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': ' I'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': ' assist'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': ' you'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': ' today'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {'content': '?'}, 'index': 0, 'finish_reason': None}]}
{'id': 'chatcmpl-6q4o7tTbjo7y1EuMNAqvBUkvCJ0PL', 'object': 'chat.completion.chunk', 'created': 1677869555, 'model': 'gpt-3.5-turbo-0301', 'choices': [{'delta': {}, 'index': 0, 'finish_reason': 'stop'}]}
As far as I know, their API does not yet support a means of querying OpenAI metrics such as per-account usage, per-key usage, credit balance, etc.
Not something that is currently supported but will flag it to the team!
Hope this implemented soon. I really need usage on each key for cost management!
+1 Usage/cost of each api key is also a desired function to me