This function extracts the usage data from the parsed HTTP response of an API call to the OpenAI chat completions endpoint.
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
