openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

How can I correctly count the usage of each API Key?

Open peiyun1982 opened this issue 2 years ago • 2 comments

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?

peiyun1982 avatar Feb 27 '23 13:02 peiyun1982

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!

memorable avatar Feb 27 '23 15:02 memorable

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!

Thank you very much for your reply, this is very useful!

peiyun1982 avatar Feb 28 '23 15:02 peiyun1982

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!

logankilpatrick avatar Mar 03 '23 16:03 logankilpatrick

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'}]}

myfingerhurt avatar Mar 03 '23 19:03 myfingerhurt

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!

yudataguy avatar May 03 '23 02:05 yudataguy

+1 Usage/cost of each api key is also a desired function to me

henrywithu avatar Sep 09 '23 01:09 henrywithu