How to correctly obtain tokens usage from ChatMessageContent response #10322
-
|
Hi, I am programming a chatbot in .NET and trying to obtain the token usage for each AI execution. I have seen that I should use the Metadata property and the Usage field. When deserializing the 'Usage' field, I have often seen the object being cast to 'CompletionUsage'. However, this type is only available in the Semantic Kernel Python version and not in the newer .NET versions.
My question is whether there is something like 'CompletionUsage' in .NET that I am missing or if I should simply use the native type from the OpenAI package. It works fine, but I find it strange that this type seems to be available only in the Python version. Execution prints: CompletionUsage class in Python: OS: Windows |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Tagging @markwallace-microsoft |
Beta Was this translation helpful? Give feedback.
-
|
Hello, I am currently using the group chat auto-generation feature, which includes semantic agents. Could you kindly let me know if there is a way to monitor token usage? |
Beta Was this translation helpful? Give feedback.
-
|
Hi, is there any updates on this since january? |
Beta Was this translation helpful? Give feedback.


@AdriAJ1 you are right, we don't have abstraction for Usage data in the C# library as the usage at the time and still a very specific OpenAI feature.
When using the OpenAI connector against OpenAI compatible API's you may have a better experience using the
InnerContentinstead of theMetadata:See here:
semantic-kernel/dotnet/samples/Concepts/ChatCompletion/OpenAI_ChatCompletion.cs
Line 49 in b8795b7
Retrieving the token count:
semantic-kernel/dotnet/samples/Concepts/ChatCompletion/OpenAI_ChatCompletion.cs
Lines 183 to 185 in b8795b7