From e7c1f402e7b170eb52acb66cba0aeb6aec485242 Mon Sep 17 00:00:00 2001 From: Ilia Sokolov Date: Sun, 26 Jul 2026 09:41:01 +0200 Subject: [PATCH 1/2] Add time-to-first-content metric to reducer sample --- .../MultipleProviders_ChatHistoryReducer.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs b/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs index e3ee157b35b1..7bd3c6d900ca 100644 --- a/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs +++ b/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. +using System.Diagnostics; using System.Text; using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.Connectors.OpenAI; @@ -124,16 +125,33 @@ public async Task ShowHowToReduceChatHistoryToLastMessageStreamingAsync() Console.WriteLine($"\n>>> User:\n{userMessage}"); var response = new StringBuilder(); + var timeToFirstContent = Stopwatch.StartNew(); + var firstContentObserved = false; var chatUpdates = chatService.GetStreamingChatMessageContentsAsync(chatHistory); await foreach (var chatUpdate in chatUpdates) { - response.Append((string?)chatUpdate.Content); + var content = (string?)chatUpdate.Content; + response.Append(content); + + if (!firstContentObserved && !string.IsNullOrEmpty(content)) + { + timeToFirstContent.Stop(); + firstContentObserved = true; + Console.WriteLine( + $"Time to first content, including chat-history reduction: {timeToFirstContent.ElapsedMilliseconds} ms"); + } if (chatUpdate.InnerContent is StreamingChatCompletionUpdate openAiChatUpdate) { totalTokenCount += openAiChatUpdate.Usage?.TotalTokenCount ?? 0; } } + + if (!firstContentObserved) + { + Console.WriteLine("Time to first content was not observed because the stream returned no text."); + } + chatHistory.AddAssistantMessage(response.ToString()); Console.WriteLine($"\n>>> Assistant:\n{response}"); } From 9e110d877a21ae39609e114c57dd2cce9e574300 Mon Sep 17 00:00:00 2001 From: Ilia Sokolov Date: Thu, 30 Jul 2026 12:04:37 +0200 Subject: [PATCH 2/2] Report completion latency for textless streams --- .../ChatCompletion/MultipleProviders_ChatHistoryReducer.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs b/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs index 7bd3c6d900ca..aa8d223b2bf8 100644 --- a/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs +++ b/dotnet/samples/Concepts/ChatCompletion/MultipleProviders_ChatHistoryReducer.cs @@ -149,7 +149,9 @@ public async Task ShowHowToReduceChatHistoryToLastMessageStreamingAsync() if (!firstContentObserved) { - Console.WriteLine("Time to first content was not observed because the stream returned no text."); + timeToFirstContent.Stop(); + Console.WriteLine( + $"No text content was observed; time to stream completion, including chat-history reduction: {timeToFirstContent.ElapsedMilliseconds} ms"); } chatHistory.AddAssistantMessage(response.ToString());