{"id":1136,"date":"2023-08-30T09:52:35","date_gmt":"2023-08-30T16:52:35","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=1136"},"modified":"2024-01-12T17:56:13","modified_gmt":"2024-01-13T01:56:13","slug":"track-your-token-usage-and-costs-with-semantic-kernel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/track-your-token-usage-and-costs-with-semantic-kernel\/","title":{"rendered":"Track Your Token Usage and Costs with Semantic Kernel"},"content":{"rendered":"<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-89\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png\" alt=\"Image skpatternlarge\" width=\"1638\" height=\"136\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png 1638w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-300x25.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-1024x85.png 1024w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-768x64.png 768w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-1536x128.png 1536w\" sizes=\"(max-width: 1638px) 100vw, 1638px\" \/><\/p>\n<p>This is a follow up blog post to <a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/unlock-the-power-of-telemetry-in-semantic-kernel-sdk\/\">Unlock the Power of Telemetry in Semantic Kernel SDK<\/a>. We are excited to share we have added logging and metering for prompt, completion, and total tokens for each request to Azure OpenAI\/Open AI using Semantic Kernel. This feature will help you monitor and optimize your API usage and costs, as well as troubleshoot any issues that may arise.<\/p>\n<p>Example of logging in Azure Application Insights<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-logging.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1140\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-logging.png\" alt=\"Image Token logging\" width=\"606\" height=\"185\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-logging.png 606w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-logging-300x92.png 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/a><\/p>\n<p>Example of metering in Azure Application Insights<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-metering.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1141\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-metering.png\" alt=\"Image Token metering\" width=\"624\" height=\"252\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-metering.png 624w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Token-metering-300x121.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>With this update, <em>Microsoft.SemanticKernel.Connectors.AI.OpenAI<\/em> package captures the metrics listed below.<\/p>\n<ul>\n<li>SemanticKernel.Connectors.OpenAI.PromptTokens &#8211; number of prompt tokens used.<\/li>\n<li>SemanticKernel.Connectors.OpenAI.CompletionTokens &#8211; number of completion tokens used.<\/li>\n<li>SemanticKernel.Connectors.OpenAI.TotalTokens &#8211; total number of tokens used.<\/li>\n<\/ul>\n<h2><strong>Benefits of logging and metering with Semantic Kernel<\/strong><\/h2>\n<p>Logging and metering your requests with Semantic Kernel have several key benefits:<\/p>\n<ul>\n<li>You can easily keep track of your API usage and costs, as each token corresponds to a unit of billing. You can also compare the token usage across different models and parameters to find the optimal settings for your use case.<\/li>\n<li>You can troubleshoot any issues or errors that may occur during your requests, as each request is logged with its prompt, completion, and token count. You can also use the logs to analyze the performance and quality of your completions.<\/li>\n<li>You can leverage the power and flexibility of Semantic Kernel to orchestrate your requests with other AI services or plugins, while still having full visibility and control over your token usage.<\/li>\n<\/ul>\n<h2><strong>Next Steps<\/strong><\/h2>\n<ul>\n<li>Try out Semantic Kernel by visiting our <a href=\"http:\/\/aka.ms\/sk\/repo\">Github repo<\/a>. Don\u2019t forget to support our project by star\u2019ing our repo.<\/li>\n<li>You can also check out our <a href=\"http:\/\/aka.ms\/sk-docs\">documentation<\/a> for more examples and tutorials on how to use Semantic Kernel.<\/li>\n<li>Join our <a href=\"http:\/\/aka.ms\/sk-community\">community<\/a> to contribute or if you have question.<\/li>\n<\/ul>\n<p><em>We hope you enjoy using Semantic Kernel to create amazing AI\u00a0<\/em><span style=\"text-align: var(--bs-body-text-align);\"><i>apps.<\/i><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a follow up blog post to Unlock the Power of Telemetry in Semantic Kernel SDK. We are excited to share we have added logging and metering for prompt, completion, and total tokens for each request to Azure OpenAI\/Open AI using Semantic Kernel. This feature will help you monitor and optimize your API usage [&hellip;]<\/p>\n","protected":false},"author":116113,"featured_media":1144,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-semantic-kernel"],"acf":[],"blog_post_summary":"<p>This is a follow up blog post to Unlock the Power of Telemetry in Semantic Kernel SDK. We are excited to share we have added logging and metering for prompt, completion, and total tokens for each request to Azure OpenAI\/Open AI using Semantic Kernel. This feature will help you monitor and optimize your API usage [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/116113"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=1136"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1136\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/1144"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=1136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=1136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=1136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}