Skip to contents

This function extracts the usage data from the parsed HTTP response of an API call to the OpenAI chat completions endpoint.

Usage

usage(response)

Arguments

response

a list object representing the HTTP response

Value

a data frame with the usage statistics of the API call (how many tokens used)

Examples

# A hand-built response, shaped like the list chat_completion() returns
response <- list(id = "chatcmpl-123", object = "chat.completion",
  created = 1700000000, model = "gpt-4o-mini",
  usage = list(prompt_tokens = 5, completion_tokens = 3, total_tokens = 8),
  choices = data.frame(index = 0))
response$choices$message <- data.frame(role = "assistant", content = "Hi!")
usage(response)
#>   prompt_tokens completion_tokens total_tokens
#> 1             5                 3            8