mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-06 19:49:40 +00:00
Added the exact token count for Anthropic
This commit is contained in:
10 files changed
+387
-8
No files matched your search
@@ -14,6 +14,7 @@ namespace AIStudio.Provider.Anthropic;
|
||||
/// </remarks>
|
||||
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.
|
||||
/// </summary>
|
||||
/// <param name="serverSentEvent">The event to read.</param>
|
||||
/// <returns>The text of this event, empty when it carried none.</returns>
|
||||
/// <returns>The text of this event, empty when it carried none, and the usage of the message start.</returns>
|
||||
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;
|
||||
|
||||
@@ -9,4 +9,14 @@ namespace AIStudio.Provider.Anthropic;
|
||||
/// <param name="Index">Which content block the event belongs to; blocks are correlated by it.</param>
|
||||
/// <param name="ContentBlock">The block as it opens, for a content block start.</param>
|
||||
/// <param name="Delta">The piece this event adds, for a content block delta or a message delta.</param>
|
||||
public readonly record struct AnthropicStreamLine(string? Type, int Index, JsonElement ContentBlock, AnthropicStreamDelta Delta);
|
||||
public readonly record struct AnthropicStreamLine(string? Type, int Index, JsonElement ContentBlock, AnthropicStreamDelta Delta)
|
||||
{
|
||||
/// <summary>
|
||||
/// The message the stream opens with, for a message start.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public AnthropicStreamMessage? Message { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
namespace AIStudio.Provider.Anthropic;
|
||||
|
||||
/// <summary>
|
||||
/// The message a streamed Anthropic messages call opens with, as far as it is read.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public sealed record AnthropicStreamMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// What the request carried, where the stream states it.
|
||||
/// </summary>
|
||||
public AnthropicUsage? Usage { get; init; }
|
||||
}
|
||||
@@ -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.
|
||||
/// </remarks>
|
||||
/// <param name="TextDelta">The text this line carried, empty when it carried none.</param>
|
||||
public readonly record struct AnthropicStreamPart(string TextDelta)
|
||||
/// <param name="Usage">What the provider said the request carried, unknown on every line but the message start.</param>
|
||||
public readonly record struct AnthropicStreamPart(string TextDelta, TokenUsage Usage = default)
|
||||
{
|
||||
/// <summary>
|
||||
/// The part of a line that says nothing to the user, such as an opening or closing block.
|
||||
/// </summary>
|
||||
public static AnthropicStreamPart Nothing => new(string.Empty);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Whether this part has anything to show at all.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public bool HasContent => this.TextDelta.Length > 0;
|
||||
}
|
||||
@@ -57,14 +57,16 @@ public sealed class AnthropicToolCallingAdapter(Model chatModel, IList<IMessageB
|
||||
|
||||
//
|
||||
// The text goes out while it is being written; the blocks are put back together behind
|
||||
// it, because they have to return to the provider exactly as they arrived.
|
||||
// it, because they have to return to the provider exactly as they arrived. The usage goes
|
||||
// out with every round: which of them describes the conversation is the loop's decision,
|
||||
// which knows which round this is.
|
||||
//
|
||||
var accumulator = new AnthropicMessageStreamAccumulator();
|
||||
await foreach (var serverSentEvent in streamRequestAsync(request, token))
|
||||
{
|
||||
var part = accumulator.Process(serverSentEvent);
|
||||
if (part.HasContent)
|
||||
yield return ToolCallingStreamEvent.TextDelta(part.TextDelta);
|
||||
if (part.HasContent || part.Usage.IsKnown)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, [], Usage: part.Usage));
|
||||
}
|
||||
|
||||
var response = accumulator.Build();
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
namespace AIStudio.Provider.Anthropic;
|
||||
|
||||
/// <summary>
|
||||
/// What Anthropic reports a messages call carried, as it opens the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public sealed record AnthropicUsage
|
||||
{
|
||||
/// <summary>
|
||||
/// What the request carried after its last cache breakpoint, which is all of it without caching.
|
||||
/// </summary>
|
||||
public int? InputTokens { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// What the request wrote to the cache.
|
||||
/// </summary>
|
||||
public int? CacheCreationInputTokens { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// What the request read from the cache.
|
||||
/// </summary>
|
||||
public int? CacheReadInputTokens { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// States what this block reports, as far as it can be believed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
/// <returns>The usage, or TokenUsage.UNKNOWN when the block states nothing usable.</returns>
|
||||
public TokenUsage ToTokenUsage() => this.InputTokens is { } inputTokens
|
||||
? TokenUsage.OfReported(inputTokens + (this.CacheCreationInputTokens ?? 0) + (this.CacheReadInputTokens ?? 0))
|
||||
: TokenUsage.UNKNOWN;
|
||||
}
|
||||
@@ -9,12 +9,38 @@ namespace AIStudio.Provider.Anthropic;
|
||||
/// <param name="Delta">The delta of the response line.</param>
|
||||
public readonly record struct ResponseStreamLine(string Type, int Index, Delta Delta) : IResponseStreamLine
|
||||
{
|
||||
/// <summary>
|
||||
/// The message the stream opens with, on the message start event only.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public AnthropicStreamMessage? Message { get; init; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ContainsContent() => this != default && !string.IsNullOrWhiteSpace(this.Delta.Text);
|
||||
|
||||
/// <inheritdoc />
|
||||
public ContentStreamChunk GetContent() => new(this.Delta.Text, []);
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public TokenUsage GetUsage() => this.Message?.Usage?.ToTokenUsage() ?? TokenUsage.UNKNOWN;
|
||||
|
||||
#region Implementation of IAnnotationStreamLine
|
||||
|
||||
//
|
||||
|
||||
Reference in new issue
Block a user