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


I’ve had the same question but with the Python lib.
The answer is there for both node and python in the API doc page.

For node.js, this will include all info in the response:

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: "Say this is a test",
  max_tokens: 7,
  temperature: 0,