OpenAi API - get usage tokens in response of the api requests?

Hello all, I was making some testing using the OpenAI api, and I want collect the token usage. In the API reference I can see thi example:

{
  "model": "text-davinci-002",
  "prompt": "Say this is a test",
  "max_tokens": 6,
  "temperature": 0,
  "top_p": 1,
  "n": 1,
  "stream": false,
  "logprobs": null,
  "stop": "\n"
}
{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-002",
  "choices": [
    {
      "text": "\n\nThis is a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 6,
    "total_tokens": 11
  }
}

At the end of the second code block, I can see an usage property with the info that I am searching, but the java api doesn’t have this object in the CompletionResult object. I can add it or this block only appears in the reference documentation

1 Like