Hello 俊岚 吴,
If you are developing with Python, it is recommended to use the tiktoken package to calculate the token usage.
Another simpler method is to check the token consumption for a request from the HTTP response. A straightforward example of a response can be found in the Microsoft Learn documentation.
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "...",
"role": "assistant"
}
}
],
"created": 1679001781,
"id": "chatcmpl-6upLpNYYOx2AhoOYxl9UgJvF4aPpR",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 39,
"prompt_tokens": 58,
"total_tokens": 97
}
}
Best regards,
Charlie
If you find my response helpful, please consider accepting this answer and voting 'yes' to support the community. Thank you!