From 4becb98ee2e9314233fd3a6fc984c0b7cd36dc9b Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 24 Sep 2026 12:49:21 +0200 Subject: [PATCH] Added the exact token count for Anthropic --- .../AnthropicMessageStreamAccumulator.cs | 11 +- .../Provider/Anthropic/AnthropicStreamLine.cs | 12 +- .../Anthropic/AnthropicStreamMessage.cs | 18 +++ .../Provider/Anthropic/AnthropicStreamPart.cs | 9 +- .../Anthropic/AnthropicToolCallingAdapter.cs | 8 +- .../Provider/Anthropic/AnthropicUsage.cs | 49 ++++++++ .../Provider/Anthropic/ResponseStreamLine.cs | 26 ++++ app/Tests/Provider/AnthropicUsageTests.cs | 116 ++++++++++++++++++ .../AnthropicMessageStreamAccumulatorTests.cs | 35 +++++- .../AnthropicToolCallingAdapterTests.cs | 111 +++++++++++++++++ 10 files changed, 387 insertions(+), 8 deletions(-) create mode 100644 app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamMessage.cs create mode 100644 app/MindWork AI Studio/Provider/Anthropic/AnthropicUsage.cs create mode 100644 app/Tests/Provider/AnthropicUsageTests.cs create mode 100644 app/Tests/Provider/ToolCalling/AnthropicToolCallingAdapterTests.cs diff --git a/app/MindWork AI Studio/Provider/Anthropic/AnthropicMessageStreamAccumulator.cs b/app/MindWork AI Studio/Provider/Anthropic/AnthropicMessageStreamAccumulator.cs index 075a1978..f12c157f 100644 --- a/app/MindWork AI Studio/Provider/Anthropic/AnthropicMessageStreamAccumulator.cs +++ b/app/MindWork AI Studio/Provider/Anthropic/AnthropicMessageStreamAccumulator.cs @@ -14,6 +14,7 @@ namespace AIStudio.Provider.Anthropic; /// public sealed class AnthropicMessageStreamAccumulator { + private const string EVENT_MESSAGE_START = "message_start"; private const string EVENT_BLOCK_START = "content_block_start"; private const string EVENT_BLOCK_DELTA = "content_block_delta"; private const string EVENT_BLOCK_STOP = "content_block_stop"; @@ -32,7 +33,7 @@ public sealed class AnthropicMessageStreamAccumulator /// Takes the next event of the stream and returns what it has to show. /// /// The event to read. - /// The text of this event, empty when it carried none. + /// The text of this event, empty when it carried none, and the usage of the message start. public AnthropicStreamPart Process(ServerSentEvent serverSentEvent) { if (serverSentEvent.Data.Length is 0) @@ -51,6 +52,14 @@ public sealed class AnthropicMessageStreamAccumulator switch (line.Type) { + case EVENT_MESSAGE_START: + // + // What the request carried, read the same way as on the plain text path and for + // the same reason: the start states this request alone, cf. ResponseStreamLine. + // + var usage = line.Message?.Usage?.ToTokenUsage() ?? TokenUsage.UNKNOWN; + return usage.IsKnown ? new AnthropicStreamPart(string.Empty, usage) : AnthropicStreamPart.Nothing; + case EVENT_BLOCK_START: this.openBlocks[line.Index] = new AnthropicContentBlockBuilder(line.ContentBlock); return AnthropicStreamPart.Nothing; diff --git a/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamLine.cs b/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamLine.cs index e58ffff1..c32a0e93 100644 --- a/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamLine.cs +++ b/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamLine.cs @@ -9,4 +9,14 @@ namespace AIStudio.Provider.Anthropic; /// Which content block the event belongs to; blocks are correlated by it. /// The block as it opens, for a content block start. /// The piece this event adds, for a content block delta or a message delta. -public readonly record struct AnthropicStreamLine(string? Type, int Index, JsonElement ContentBlock, AnthropicStreamDelta Delta); \ No newline at end of file +public readonly record struct AnthropicStreamLine(string? Type, int Index, JsonElement ContentBlock, AnthropicStreamDelta Delta) +{ + /// + /// The message the stream opens with, for a message start. + /// + /// + /// Not a positional parameter, because nobody but the serializer ever builds this line with + /// one. It is what states the usage, for the reason given at ResponseStreamLine.GetUsage. + /// + public AnthropicStreamMessage? Message { get; init; } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamMessage.cs b/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamMessage.cs new file mode 100644 index 00000000..660400f3 --- /dev/null +++ b/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamMessage.cs @@ -0,0 +1,18 @@ +// ReSharper disable ClassNeverInstantiated.Global +namespace AIStudio.Provider.Anthropic; + +/// +/// The message a streamed Anthropic messages call opens with, as far as it is read. +/// +/// +/// It arrives on the message start event, before any content, and states what the request +/// carried. Its content is always empty there -- the blocks follow as events of their own -- so +/// the usage is all there is to read. +/// +public sealed record AnthropicStreamMessage +{ + /// + /// What the request carried, where the stream states it. + /// + public AnthropicUsage? Usage { get; init; } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamPart.cs b/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamPart.cs index bc8f1bd1..8c6f96b2 100644 --- a/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamPart.cs +++ b/app/MindWork AI Studio/Provider/Anthropic/AnthropicStreamPart.cs @@ -8,15 +8,20 @@ namespace AIStudio.Provider.Anthropic; /// and doing so would be a feature of its own rather than a side effect of streaming. /// /// The text this line carried, empty when it carried none. -public readonly record struct AnthropicStreamPart(string TextDelta) +/// What the provider said the request carried, unknown on every line but the message start. +public readonly record struct AnthropicStreamPart(string TextDelta, TokenUsage Usage = default) { /// /// The part of a line that says nothing to the user, such as an opening or closing block. /// public static AnthropicStreamPart Nothing => new(string.Empty); - + /// /// Whether this part has anything to show at all. /// + /// + /// The usage is not part of that: it is nothing to show, and whether it reaches the answer at + /// all is the tool calling loop's decision, which knows which round this is. + /// public bool HasContent => this.TextDelta.Length > 0; } \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Anthropic/AnthropicToolCallingAdapter.cs b/app/MindWork AI Studio/Provider/Anthropic/AnthropicToolCallingAdapter.cs index cee4d160..851a49f8 100644 --- a/app/MindWork AI Studio/Provider/Anthropic/AnthropicToolCallingAdapter.cs +++ b/app/MindWork AI Studio/Provider/Anthropic/AnthropicToolCallingAdapter.cs @@ -57,14 +57,16 @@ public sealed class AnthropicToolCallingAdapter(Model chatModel, IList +/// What Anthropic reports a messages call carried, as it opens the stream. +/// +/// +/// The input arrives in up to three parts, and only their sum is what the request carried: the +/// input tokens are just those after the last cache breakpoint, the other two are what was written +/// to and read from the cache before it. Read on 2026-09-24 at +/// https://platform.claude.com/docs/en/build-with-claude/prompt-caching. +/// +/// A missing cache part means that nothing was cached, not that its size is unknown: the API +/// caches only for a request which asks for it with cache_control, which AI Studio never does on +/// its own -- somebody could, though, through the additional API parameters. A missing input part +/// is different, and without it the block states nothing. +/// +/// The output tokens are left unread on purpose, for the reason given at TokenUsage: they include +/// the model's thinking, which no later request carries. +/// +public sealed record AnthropicUsage +{ + /// + /// What the request carried after its last cache breakpoint, which is all of it without caching. + /// + public int? InputTokens { get; init; } + + /// + /// What the request wrote to the cache. + /// + public int? CacheCreationInputTokens { get; init; } + + /// + /// What the request read from the cache. + /// + public int? CacheReadInputTokens { get; init; } + + /// + /// States what this block reports, as far as it can be believed. + /// + /// + /// The one way from the wire to a usage, shared by the plain text path and the tool calling + /// path, so that what counts as believable is decided in a single place. + /// + /// The usage, or TokenUsage.UNKNOWN when the block states nothing usable. + public TokenUsage ToTokenUsage() => this.InputTokens is { } inputTokens + ? TokenUsage.OfReported(inputTokens + (this.CacheCreationInputTokens ?? 0) + (this.CacheReadInputTokens ?? 0)) + : TokenUsage.UNKNOWN; +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Anthropic/ResponseStreamLine.cs b/app/MindWork AI Studio/Provider/Anthropic/ResponseStreamLine.cs index 195f164c..a416b318 100644 --- a/app/MindWork AI Studio/Provider/Anthropic/ResponseStreamLine.cs +++ b/app/MindWork AI Studio/Provider/Anthropic/ResponseStreamLine.cs @@ -9,12 +9,38 @@ namespace AIStudio.Provider.Anthropic; /// The delta of the response line. public readonly record struct ResponseStreamLine(string Type, int Index, Delta Delta) : IResponseStreamLine { + /// + /// The message the stream opens with, on the message start event only. + /// + /// + /// Not a positional parameter, because nobody but the serializer ever builds this line with + /// one. Only the opening event carries a message, which makes it the only line with a usage. + /// + public AnthropicStreamMessage? Message { get; init; } + /// public bool ContainsContent() => this != default && !string.IsNullOrWhiteSpace(this.Delta.Text); /// public ContentStreamChunk GetContent() => new(this.Delta.Text, []); + /// + /// + /// Read off the message start event, the first line of the stream, and never off the message + /// delta at its end. That one carries a usage as well, but a cumulative one: once the model + /// used a server tool, it holds what the tool fed back into the same request. The example in + /// the streaming documentation, read on 2026-09-24 at + /// https://platform.claude.com/docs/en/build-with-claude/streaming, shows 2,679 input tokens at + /// the start of a message with a web search and 10,682 at its end. No later request carries + /// those search results; the start states exactly what this one carried. + /// + /// The number arriving before the answer is no problem, because it describes the request, not + /// the answer. A stream which breaks off afterward leaves the answer with whatever arrived up + /// to then, nothing at all included, and the next request carries exactly that -- which is + /// what the answer's text is counted as. + /// + public TokenUsage GetUsage() => this.Message?.Usage?.ToTokenUsage() ?? TokenUsage.UNKNOWN; + #region Implementation of IAnnotationStreamLine // diff --git a/app/Tests/Provider/AnthropicUsageTests.cs b/app/Tests/Provider/AnthropicUsageTests.cs new file mode 100644 index 00000000..dfa816cc --- /dev/null +++ b/app/Tests/Provider/AnthropicUsageTests.cs @@ -0,0 +1,116 @@ +using System.Text.Json; + +using AIStudio.Provider; +using AIStudio.Provider.Anthropic; + +namespace AIStudio.Tests.Provider; + +/// +/// Checks that what Anthropic says a request carried is read off the right line of the stream. +/// +/// +/// Anthropic states a usage twice per message: at its start, and cumulatively at its end. Only the +/// start describes the request as it was sent. The end adds whatever a server tool fed back into +/// the same request, which no later request carries -- read that one, and a single web search +/// makes the chat look four times as large as it is. +/// +[TestFixture] +public sealed class AnthropicUsageTests +{ + /// + /// The opening line of a message, as the streaming documentation shows it. + /// + private const string MESSAGE_START = + """ + {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-opus-5-5", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}} + """; + + [Test] + public void TheOpeningLineStatesWhatTheRequestCarried() + { + var line = Read(MESSAGE_START); + + Assert.Multiple(() => + { + Assert.That(line.GetUsage().IsKnown, Is.True); + Assert.That(line.GetUsage().PromptTokens, Is.EqualTo(25)); + + // It carries no answer, which is why it has to be read before the content check drops it: + Assert.That(line.ContainsContent(), Is.False); + }); + } + + [Test] + public void WhatWasCachedCountsAsWell() + { + // + // With caching, the input tokens are only what comes after the last cache breakpoint. The + // request carried all three parts. + // + var line = Read( + """ + {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","content":[],"model":"claude-opus-5-5","usage":{"input_tokens":50,"cache_creation_input_tokens":1000,"cache_read_input_tokens":2000,"output_tokens":1}}} + """); + + Assert.That(line.GetUsage().PromptTokens, Is.EqualTo(3050)); + } + + [Test] + public void WithoutItsInputTokensABlockStatesNothing() + { + // + // The cache parts alone are not the request: the part after the breakpoint is missing, and + // it is the one part every request has. + // + var line = Read( + """ + {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","content":[],"model":"claude-opus-5-5","usage":{"cache_read_input_tokens":2000}}} + """); + + Assert.That(line.GetUsage().IsKnown, Is.False); + } + + [Test] + public void TheClosingLineOfAMessageWithAWebSearchStatesNothing() + { + // + // The example of the streaming documentation: 2,679 input tokens at the start, 10,682 at + // the end, the difference being the search results. The end is cumulative, and the next + // request carries none of what the search added. + // + var line = Read( + """ + {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":10682,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":510,"server_tool_use":{"web_search_requests":1}}} + """); + + Assert.That(line.GetUsage().IsKnown, Is.False); + } + + [Test] + public void AnOpeningLineWithoutAUsageStatesNothing() + { + var line = Read( + """ + {"type": "message_start", "message": {"id": "msg_01...", "type": "message", "role": "assistant", "content": [], "model": "claude-opus-5-5", "stop_reason": null, "stop_sequence": null}} + """); + + Assert.That(line.GetUsage().IsKnown, Is.False); + } + + [Test] + public void ALineOfTheAnswerStatesNothing() + { + var line = Read( + """ + {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi"}} + """); + + Assert.Multiple(() => + { + Assert.That(line.GetUsage().IsKnown, Is.False); + Assert.That(line.ContainsContent(), Is.True); + }); + } + + private static ResponseStreamLine Read(string data) => JsonSerializer.Deserialize(data, ProviderJsonOptions.OPTIONS); +} \ No newline at end of file diff --git a/app/Tests/Provider/ToolCalling/AnthropicMessageStreamAccumulatorTests.cs b/app/Tests/Provider/ToolCalling/AnthropicMessageStreamAccumulatorTests.cs index 04714a5e..d0f8e99b 100644 --- a/app/Tests/Provider/ToolCalling/AnthropicMessageStreamAccumulatorTests.cs +++ b/app/Tests/Provider/ToolCalling/AnthropicMessageStreamAccumulatorTests.cs @@ -218,7 +218,40 @@ public sealed class AnthropicMessageStreamAccumulatorTests Assert.That(response, Is.Null, "An unfinished message is not handed on as if it were finished."); } - + + [Test] + public void TheMessageStartStatesWhatTheRequestCarried() + { + const string MESSAGE_START = """{"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"claude-opus-5-5","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":2679,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":3}}}"""; + + var part = new AnthropicMessageStreamAccumulator().Process(Event(MESSAGE_START)); + var response = Read( + MESSAGE_START, + """{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}""", + """{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello."}}""", + """{"type":"content_block_stop","index":0}""", + """{"type":"message_stop"}"""); + + Assert.Multiple(() => + { + Assert.That(part.Usage.PromptTokens, Is.EqualTo(2679), "The start of the message states what the request carried."); + Assert.That(part.HasContent, Is.False, "And it has nothing to show."); + Assert.That(response!.Content, Has.Count.EqualTo(1), "Nor does it become a block of the message."); + }); + } + + [Test] + public void TheMessageDeltaStatesNothingAboutTheRequest() + { + // + // Its usage is cumulative and holds whatever a server tool fed back into the same request. + // + var part = new AnthropicMessageStreamAccumulator().Process(Event( + """{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":10682,"output_tokens":510}}""")); + + Assert.That(part.Usage.IsKnown, Is.False); + } + private static AnthropicResponse? Read(params string[] data) { var accumulator = new AnthropicMessageStreamAccumulator(); diff --git a/app/Tests/Provider/ToolCalling/AnthropicToolCallingAdapterTests.cs b/app/Tests/Provider/ToolCalling/AnthropicToolCallingAdapterTests.cs new file mode 100644 index 00000000..3b68b4ce --- /dev/null +++ b/app/Tests/Provider/ToolCalling/AnthropicToolCallingAdapterTests.cs @@ -0,0 +1,111 @@ +using System.Runtime.CompilerServices; + +using AIStudio.Provider; +using AIStudio.Provider.Anthropic; + +namespace AIStudio.Tests.Provider.ToolCalling; + +/// +/// Checks what a round of a tool calling conversation with Anthropic passes on about its cost. +/// +/// +/// Every round is a request of its own, and every one of them states at its start what it carried. +/// The adapter passes each of those on and none of the cumulative ones at the end of a message. +/// Which round describes the conversation is the tool calling loop's decision, checked in +/// ToolCallingLoopTests. +/// +[TestFixture] +public sealed class AnthropicToolCallingAdapterTests +{ + [Test] + public async Task EveryRoundPassesOnWhatItsRequestCarried() + { + var adapter = Adapter( + [ + """{"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","content":[],"usage":{"input_tokens":1200,"output_tokens":1}}}""", + """{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_1","name":"web_search","input":{}}}""", + """{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"query\":\"weather\"}"}}""", + """{"type":"content_block_stop","index":0}""", + """{"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":20}}""", + """{"type":"message_stop"}""", + ], + [ + """{"type":"message_start","message":{"id":"msg_2","type":"message","role":"assistant","content":[],"usage":{"input_tokens":9800,"output_tokens":1}}}""", + """{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}""", + """{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"It is sunny."}}""", + """{"type":"content_block_stop","index":0}""", + """{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":12000,"output_tokens":30}}""", + """{"type":"message_stop"}""", + ]); + + var firstRound = await Usages(adapter); + + // + // What the loop does between two rounds: the model's turn and the tool's result become part + // of the next request. + // + adapter.RecordAssistantTurn(); + adapter.RecordToolResult("toolu_1", "Sunny, 24 degrees."); + + var secondRound = await Usages(adapter); + + Assert.Multiple(() => + { + Assert.That(firstRound, Is.EqualTo(new[] { 1200 }), "The first round states the conversation up to the question."); + Assert.That(secondRound, Is.EqualTo(new[] { 9800 }), "The second round states its own request, and the cumulative number at its end stays behind."); + }); + } + + [Test] + public async Task TheTextStillGoesOutNextToTheUsage() + { + var adapter = Adapter( + [ + """{"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","content":[],"usage":{"input_tokens":1200,"output_tokens":1}}}""", + """{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}""", + """{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello."}}""", + """{"type":"content_block_stop","index":0}""", + """{"type":"message_stop"}""", + ]); + + var written = new List(); + await foreach (var streamEvent in adapter.ExecuteRoundAsync(null, true)) + if (streamEvent.Delta is { Content.Length: > 0 } delta) + written.Add(delta.Content); + + Assert.That(written, Is.EqualTo(new[] { "Hello." })); + } + + /// + /// Runs the next round and returns the prompt of every usage it passed on. + /// + private static async Task> Usages(AnthropicToolCallingAdapter adapter) + { + var usages = new List(); + await foreach (var streamEvent in adapter.ExecuteRoundAsync(null, true)) + if (streamEvent.Delta is { Usage.IsKnown: true } delta) + usages.Add(delta.Usage.PromptTokens); + + return usages; + } + + /// + /// Builds an adapter whose requests are answered by the given rounds, one after another. + /// + private static AnthropicToolCallingAdapter Adapter(params string[][] rounds) + { + var nextRound = 0; + return new(new Model("claude-test", null), [], "You are a helpful assistant.", 1024, new Dictionary(), [], (_, token) => Lines(rounds[nextRound++], token)); + } + + private static async IAsyncEnumerable Lines(string[] data, [EnumeratorCancellation] CancellationToken token = default) + { + foreach (var line in data) + { + token.ThrowIfCancellationRequested(); + yield return new ServerSentEvent($"data: {line}", line); + } + + await Task.CompletedTask; + } +} \ No newline at end of file