I would like to know if this is the best way to know how many tokens were consumed and can calculate costs.
I am not sure about costs, are those costs correct?
double costPer1KInput = 0.0015; // example for GPT-4o-mini input
double costPer1KOutput = 0.002; // example for GPT-4o-mini output
using Azure;
using Azure.AI.OpenAI;
var client = new OpenAIClient(
new Uri("https://YOUR-RESOURCE-NAME.openai.azure.com/"),
new AzureKeyCredential("YOUR-API-KEY"));
var options = new ChatCompletionsOptions()
{
DeploymentName = "gpt-4o-mini", // your deployment
Messages =
{
new ChatRequestSystemMessage("You are a helpful assistant."),
new ChatRequestUserMessage("Write a haiku about autumn.")
}
};
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(options);
ChatCompletions completions = response.Value;
// Access token usage info
Console.WriteLine($"Prompt tokens: {completions.Usage.PromptTokens}");
Console.WriteLine($"Completion tokens: {completions.Usage.CompletionTokens}");
Console.WriteLine($"Total tokens: {completions.Usage.TotalTokens}");
Then I found this to evaluate costs:
double costPer1KInput = 0.0015; // example for GPT-4o-mini input
double costPer1KOutput = 0.002; // example for GPT-4o-mini output
double inputCost = completions.Usage.PromptTokens / 1000.0 * costPer1KInput;
double outputCost = completions.Usage.CompletionTokens / 1000.0 * costPer1KOutput;
double totalCost = inputCost + outputCost;
Console.WriteLine($"Estimated cost: ${totalCost:F4}");