I am using chat completion to analyze an image and I want to keep track of how many tokens I use by accessing the “usage” key.
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload).json()
input_tokens_consumed += response['usage']['prompt_tokens']
The last time I ran this code I got a KeyError for ‘usage’.
Does this mean the competition didn’t occur so it didn’t return any usage?
Can that be caused due to an internet problem?
How can I handle this error?
Any suggestion is accepted, thank you!
1 Like
if you didn’t get a response back and it just gave you an error, I’d say it didn’t generate an output and you didn’t get charged for it. If you did get an output then yes, it should appear on the usage tab.
2 Likes
Have you tried looking into the OpenAIError
module? Should help you with internet issues and the like.
Note: I’m basing this off my experience with the openai
Python library, but it should be similar with… Node.js, I’m guessing you are using?
1 Like
Yes, in Production code you should check for an error response before attempting to access a property from an assumed successful response.
If you’re are claiming this is a bug first show us the full response from the server.
1 Like
I was indeed handling errors using try and except but somehow my program still stopped which is more weird. I think it’s something from my code. Thanks for responding tho
1 Like
I will, thanks for the suggestion
1 Like