From 81d2288e46ff1877f70b05ae1178c780ff0ecd6d Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 15 Nov 2026 13:07:46 -0400 Subject: [PATCH] fix(anthropic): preserve streaming input and cache usage --- .../Providers/ChatCompletionProvider.cs | 2 +- .../Providers/ProviderHelper.cs | 22 ++++++++ .../Plugins/AnthropicStreamedUsageTests.cs | 50 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/BotSharp.Core.UnitTests/Plugins/AnthropicStreamedUsageTests.cs diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs index 16673402d..208a5ded3 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs @@ -246,7 +246,7 @@ public async Task GetChatCompletionsStreamingAsync(Agent agent, var contentBlock = choice.ContentBlock; var delta = choice.Delta; - tokenUsage = delta?.Usage ?? startMsg?.Usage ?? choice.Usage; + tokenUsage = ProviderHelper.UpdateStreamedUsage(tokenUsage, choice); if (delta != null) { diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ProviderHelper.cs index d3de974d7..c4b81c894 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ProviderHelper.cs @@ -14,6 +14,28 @@ public static AnthropicClient GetAnthropicClient(string provider, string model, return client; } + public static Usage? UpdateStreamedUsage(Usage? current, MessageResponse response) + { + if (response.StreamStartMessage?.Usage is { } initialUsage) + { + current = initialUsage; + } + + var outputUsage = response.Delta?.Usage ?? response.Usage; + if (outputUsage != null) + { + if (current == null) + { + return outputUsage; + } + + // Later usage contains cumulative output tokens, not the initial input/cache counts. + current.OutputTokens = outputUsage.OutputTokens; + } + + return current; + } + public const string MaxTokensMessage = "AI response exceeded max output length"; /// diff --git a/tests/BotSharp.Core.UnitTests/Plugins/AnthropicStreamedUsageTests.cs b/tests/BotSharp.Core.UnitTests/Plugins/AnthropicStreamedUsageTests.cs new file mode 100644 index 000000000..a8e5149f7 --- /dev/null +++ b/tests/BotSharp.Core.UnitTests/Plugins/AnthropicStreamedUsageTests.cs @@ -0,0 +1,50 @@ +using System.Text.Json; +using Anthropic.SDK.Messaging; +using BotSharp.Plugin.AnthropicAI.Providers; +using Xunit; + +namespace BotSharp.Core.UnitTests.Plugins; + +public class AnthropicStreamedUsageTests +{ + [Fact] + public void StreamingUsage_PreservesInputAndCacheAcrossEvents() + { + string[] events = + [ + """{"message":{"usage":{"input_tokens":120,"cache_read_input_tokens":1000,"cache_creation_input_tokens":200,"output_tokens":1}}}""", + """{"delta":{"text":"hello"}}""", + """{"usage":{"output_tokens":8}}""", + """{"delta":{"usage":{"output_tokens":12}}}""", + """{"type":"message_stop"}""" + ]; + Usage? usage = null; + foreach (var json in events) + { + usage = ProviderHelper.UpdateStreamedUsage(usage, JsonSerializer.Deserialize(json)!); + } + + Assert.NotNull(usage); + Assert.Equal(120, usage.InputTokens); + Assert.Equal(1000, usage.CacheReadInputTokens); + Assert.Equal(200, usage.CacheCreationInputTokens); + Assert.Equal(12, usage.OutputTokens); + } + + [Fact] + public void StreamingUsage_WithoutUsage_RemainsNull() + { + Assert.Null(ProviderHelper.UpdateStreamedUsage(null, new MessageResponse())); + } + + [Fact] + public void StreamingUsage_OutputOnly_IsRetained() + { + var response = JsonSerializer.Deserialize("""{"usage":{"output_tokens":9}}""")!; + var usage = ProviderHelper.UpdateStreamedUsage(null, response); + + Assert.NotNull(usage); + Assert.Equal(0, usage.InputTokens); + Assert.Equal(9, usage.OutputTokens); + } +}