Added the exact token count for Anthropic and OpenAI (#1004)
Build and Release / Determine run mode (push) Has been cancelled
Build and Release / Verify (push) Has been cancelled
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Sync Flatpak repo (push) Has been cancelled
Build and Release / Collect Flatpak artifacts (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-09-24 13:36:06 +02:00
1 parent 0c585144e2
commit 4baf21656a
25 files changed
+837 -51

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
using System.Text.Json;
using AIStudio.Provider;
using AIStudio.Provider.Anthropic;
namespace AIStudio.Tests.Provider;
/// <summary>
/// Checks that what Anthropic says a request carried is read off the right line of the stream.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
[TestFixture]
public sealed class AnthropicUsageTests
{
/// <summary>
/// The opening line of a message, as the streaming documentation shows it.
/// </summary>
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<ResponseStreamLine>(data, ProviderJsonOptions.OPTIONS);
}
+95
View File
@@ -0,0 +1,95 @@
using System.Text.Json;
using AIStudio.Provider;
using AIStudio.Provider.OpenAI;
namespace AIStudio.Tests.Provider;
/// <summary>
/// Checks that what OpenAI says a Responses API call cost is read off the completed event, and only
/// believed when it describes the request.
/// </summary>
/// <remarks>
/// The completed event states the input of the whole response. That is the request as it was sent,
/// unless OpenAI ran a hosted tool along the way: what such a tool found is charged as input of the
/// same response, and no later request carries it. Believe the number then, and a single web search
/// makes the chat look several times as large as it is.
/// </remarks>
[TestFixture]
public sealed class ResponsesUsageTests
{
private const string COMPLETED_PREFIX = """{"type":"response.completed","sequence_number":42,"response":{"id":"resp_1","object":"response","status":"completed","model":"gpt-5","output":[""";
private const string MESSAGE_ITEM = """{"id":"msg_1","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"Hi there!","annotations":[]}]}""";
private const string USAGE_SUFFIX = ""","usage":{"input_tokens":2006,"input_tokens_details":{"cached_tokens":1920},"output_tokens":300,"output_tokens_details":{"reasoning_tokens":120},"total_tokens":2306}}}""";
[Test]
public void TheCompletedEventStatesWhatTheRequestCarried()
{
//
// The cached tokens are a share of the input tokens, not an addition to them: the request
// carried 2,006 tokens, 1,920 of which came out of the cache.
//
var line = Read(COMPLETED_PREFIX + MESSAGE_ITEM + "]" + USAGE_SUFFIX);
Assert.Multiple(() =>
{
Assert.That(line.GetUsage().IsKnown, Is.True);
Assert.That(line.GetUsage().PromptTokens, Is.EqualTo(2006));
});
}
[Test]
public void ReasoningAndFunctionCallsLeaveTheInputAlone()
{
//
// Both are output the model wrote itself. Neither makes OpenAI add anything to the input,
// and the function call's result comes back in the next request, which states its own.
//
var line = Read(COMPLETED_PREFIX +
"""{"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"gAAAAAB0aXRs"},""" +
"""{"type":"function_call","call_id":"call_1","name":"web_search","arguments":"{\"query\":\"weather\"}"}""" +
"]" + USAGE_SUFFIX);
Assert.That(line.GetUsage().PromptTokens, Is.EqualTo(2006));
}
[Test]
public void AHostedWebSearchStatesNothing()
{
//
// OpenAI searched on its own, and what it found is part of the input tokens. The next
// request carries none of it.
//
var line = Read(COMPLETED_PREFIX +
"""{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"search","query":"weather"}},""" +
MESSAGE_ITEM +
"]" + USAGE_SUFFIX);
Assert.That(line.GetUsage().IsKnown, Is.False);
}
[Test]
public void AnOutputItemNobodyKnowsStatesNothing()
{
//
// A hosted tool OpenAI adds later is treated like the web search until somebody checked
// what it does to the input.
//
var line = Read(COMPLETED_PREFIX +
"""{"type":"future_tool_call","id":"ft_1","status":"completed"},""" +
MESSAGE_ITEM +
"]" + USAGE_SUFFIX);
Assert.That(line.GetUsage().IsKnown, Is.False);
}
[Test]
public void ACompletedEventWithoutAUsageStatesNothing()
{
var line = Read(COMPLETED_PREFIX + MESSAGE_ITEM + "]}}");
Assert.That(line.GetUsage().IsKnown, Is.False);
}
private static ResponsesCompletedStreamLine Read(string data) => JsonSerializer.Deserialize<ResponsesCompletedStreamLine>(data, ProviderJsonOptions.OPTIONS)!;
}
@@ -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();
@@ -0,0 +1,111 @@
using System.Runtime.CompilerServices;
using AIStudio.Provider;
using AIStudio.Provider.Anthropic;
namespace AIStudio.Tests.Provider.ToolCalling;
/// <summary>
/// Checks what a round of a tool calling conversation with Anthropic passes on about its cost.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
[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<string>();
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." }));
}
/// <summary>
/// Runs the next round and returns the prompt of every usage it passed on.
/// </summary>
private static async Task<List<int>> Usages(AnthropicToolCallingAdapter adapter)
{
var usages = new List<int>();
await foreach (var streamEvent in adapter.ExecuteRoundAsync(null, true))
if (streamEvent.Delta is { Usage.IsKnown: true } delta)
usages.Add(delta.Usage.PromptTokens);
return usages;
}
/// <summary>
/// Builds an adapter whose requests are answered by the given rounds, one after another.
/// </summary>
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<string, object>(), [], (_, token) => Lines(rounds[nextRound++], token));
}
private static async IAsyncEnumerable<ServerSentEvent> Lines(string[] data, [EnumeratorCancellation] CancellationToken token = default)
{
foreach (var line in data)
{
token.ThrowIfCancellationRequested();
yield return new ServerSentEvent($"data: {line}", line);
}
await Task.CompletedTask;
}
}
@@ -13,11 +13,9 @@ namespace AIStudio.Tests.Provider.ToolCalling;
/// </summary>
/// <remarks>
/// Every round of a tool conversation is a request of its own, and every one of them reports what
/// it cost. Only the first one describes what the next question will be sent after: every later
/// round carries the tool calls and their results on top, none of which is sent again once the
/// answer stands. Passing on the last report instead would put the chat at the size of everything
/// the tools returned, which is the one number a person watching their context window must not
/// see as exact.
/// it cost. The adapter passes on each report as it arrives, the line without choices included.
/// Which of them describes the conversation is not its decision: that is the tool calling loop's,
/// which knows which round this is, and is checked in ToolCallingLoopTests.
///
/// What a round asks for is one tool call at a time, wherever the provider lets it ask: a provider
/// which rejects the question fails the whole request, so it is not asked at all.
@@ -30,7 +28,7 @@ public sealed class ChatCompletionToolCallingAdapterTests
private const string SECOND_ROUND_USAGE = """{"choices":[],"usage":{"prompt_tokens":9800,"completion_tokens":150}}""";
[Test]
public async Task OnlyTheFirstRoundPassesOnWhatItsRequestCost()
public async Task EveryRoundPassesOnWhatItsRequestCost()
{
var adapter = Adapter(
[
@@ -57,8 +55,8 @@ public sealed class ChatCompletionToolCallingAdapterTests
Assert.Multiple(() =>
{
Assert.That(firstRound, Is.EqualTo(new[] { 1200 }), "The first round's prompt is the conversation up to the question.");
Assert.That(secondRound, Is.Empty, "The second round's prompt holds the tool result as well, which the next question is not sent with.");
Assert.That(firstRound, Is.EqualTo(new[] { 1200 }), "The first round reports the conversation up to the question.");
Assert.That(secondRound, Is.EqualTo(new[] { 9800 }), "The second round reports its own request, tool result included, and leaves it to the loop to drop.");
});
}
@@ -105,6 +105,33 @@ public sealed class ResponsesStreamAccumulatorTests
Assert.That(response!.GetTextOutput(), Is.EqualTo("Complete"), "The closing event is the round, and the collected items were only there in case it never came.");
}
[Test]
public void TheCompletedEventStatesWhatTheRequestCost()
{
var accumulator = new ResponsesStreamAccumulator();
var part = accumulator.Process(Event(COMPLETED_PREFIX + REASONING_ITEM + """],"usage":{"input_tokens":2679,"input_tokens_details":{"cached_tokens":0},"output_tokens":510}}}"""));
Assert.Multiple(() =>
{
Assert.That(part.Usage.IsKnown, Is.True, "The closing event is the one line which states what the request cost.");
Assert.That(part.Usage.PromptTokens, Is.EqualTo(2679));
Assert.That(part.HasContent, Is.False, "And there is nothing on it to show.");
Assert.That(accumulator.Build()!.Output, Has.Count.EqualTo(1), "Reading the usage leaves the round as it was.");
});
}
[Test]
public void ARoundPutBackTogetherFromItsItemsStatesNoCost()
{
//
// The gateway which never sends the closing event never sends the usage either, which
// leaves the round to the estimate.
//
var response = Read("""{"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Partial"}]}}""");
Assert.That(response!.GetUsage().IsKnown, Is.False);
}
[Test]
public void AStreamWhichSaidNothingIsAFailedRound()
{
@@ -0,0 +1,112 @@
using System.Runtime.CompilerServices;
using AIStudio.Provider;
using AIStudio.Provider.OpenAI;
namespace AIStudio.Tests.Provider.ToolCalling;
/// <summary>
/// Checks what a round of a tool calling conversation with the Responses API passes on about its cost.
/// </summary>
/// <remarks>
/// Every round is a request of its own, and every one of them states at its end what it cost -- as
/// long as OpenAI ran no hosted tool in it. Which round describes the conversation is the tool
/// calling loop's decision, checked in ToolCallingLoopTests.
/// </remarks>
[TestFixture]
public sealed class ResponsesToolCallingAdapterTests
{
[Test]
public async Task EveryRoundPassesOnWhatItsRequestCost()
{
var adapter = Adapter(
[
"""{"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","call_id":"call_1","name":"web_search","arguments":"{\"query\":\"weather\"}"}}""",
"""{"type":"response.completed","response":{"id":"resp_1","model":"gpt-5","output":[{"type":"function_call","call_id":"call_1","name":"web_search","arguments":"{\"query\":\"weather\"}"}],"usage":{"input_tokens":1200,"output_tokens":20}}}""",
],
[
"""{"type":"response.output_text.delta","delta":"It is sunny."}""",
"""{"type":"response.completed","response":{"id":"resp_2","model":"gpt-5","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"It is sunny."}]}],"usage":{"input_tokens":9800,"output_tokens":30}}}""",
]);
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("call_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.");
});
}
[Test]
public async Task ARoundWithAHostedWebSearchPassesOnNothingButItsText()
{
var adapter = Adapter(
[
"""{"type":"response.output_text.delta","delta":"It is sunny."}""",
"""{"type":"response.completed","response":{"id":"resp_1","model":"gpt-5","output":[{"type":"web_search_call","id":"ws_1","status":"completed"},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"It is sunny."}]}],"usage":{"input_tokens":12000,"output_tokens":30}}}""",
]);
var usages = new List<int>();
var written = new List<string>();
await foreach (var streamEvent in adapter.ExecuteRoundAsync(null, true))
{
if (streamEvent.Delta is not { } delta)
continue;
if (delta.Usage.IsKnown)
usages.Add(delta.Usage.PromptTokens);
if (delta.Content.Length > 0)
written.Add(delta.Content);
}
Assert.Multiple(() =>
{
Assert.That(usages, Is.Empty, "What the search found is part of the number, and no later request carries it.");
Assert.That(written, Is.EqualTo(new[] { "It is sunny." }), "The answer itself goes out as always.");
});
}
/// <summary>
/// Runs the next round and returns the prompt of every usage it passed on.
/// </summary>
private static async Task<List<int>> Usages(ResponsesToolCallingAdapter adapter)
{
var usages = new List<int>();
await foreach (var streamEvent in adapter.ExecuteRoundAsync(null, true))
if (streamEvent.Delta is { Usage.IsKnown: true } delta)
usages.Add(delta.Usage.PromptTokens);
return usages;
}
/// <summary>
/// Builds an adapter whose requests are answered by the given rounds, one after another.
/// </summary>
private static ResponsesToolCallingAdapter Adapter(params string[][] rounds)
{
var nextRound = 0;
return new(new Model("gpt-5", null), [], new Dictionary<string, object>(), [], [], (_, token) => Lines(rounds[nextRound++], token));
}
private static async IAsyncEnumerable<ServerSentEvent> Lines(string[] data, [EnumeratorCancellation] CancellationToken token = default)
{
foreach (var line in data)
{
token.ThrowIfCancellationRequested();
yield return new ServerSentEvent($"data: {line}", line);
}
await Task.CompletedTask;
}
}
@@ -215,7 +215,48 @@ public sealed class ToolCallingLoopTests
Assert.That(chunks.Select(x => x.Content), Has.None.Contains(NO_ANSWER), "A stop is not a failure to answer, so it is not reported as one.");
});
}
[Test]
public async Task OnlyTheFirstRoundsUsageReachesTheAnswer()
{
//
// Every round is a request of its own, and every one of them reports what it cost. Only
// the first one describes what the next question will be sent after: every later round
// carries the tool calls and their results on top, none of which is sent again once the
// answer stands.
//
var adapter = new ScriptedAdapter(
[Usage(1200), Completed(string.Empty, [Call("call-1")])],
[Text(ANSWER), Usage(9800), Completed(ANSWER)]);
var usages = (await Collect(adapter)).Where(chunk => chunk.Usage.IsKnown).Select(chunk => chunk.Usage.PromptTokens);
Assert.That(usages, Is.EqualTo(new[] { 1200 }), "The second round's prompt holds the tool result as well, which the next question is not sent with.");
}
[Test]
public async Task ALaterRoundsUsageLeavesItsTextAndNothingElse()
{
//
// Some providers send the usage next to the last piece of text rather than on a line of
// its own. Dropping the usage must not drop that text, and a delta which carried nothing
// but the usage must not turn into an empty chunk of its own.
//
var withUsage = await Collect(new ScriptedAdapter(
[Completed(string.Empty, [Call("call-1")])],
[Usage(9800), Usage(9800, ANSWER), Completed(ANSWER)]));
var withoutUsage = await Collect(new ScriptedAdapter(
[Completed(string.Empty, [Call("call-1")])],
[Text(ANSWER), Completed(ANSWER)]));
Assert.Multiple(() =>
{
Assert.That(withUsage.Select(x => x.Content), Is.EqualTo(withoutUsage.Select(x => x.Content)), "The same chunks arrive as if the round had reported nothing.");
Assert.That(withUsage.Select(x => x.Usage.IsKnown), Has.None.True, "And none of them carries the usage on.");
});
}
/// <summary>
/// As many rounds calling one tool each as it takes to use up the tool budget.
/// </summary>
@@ -225,7 +266,9 @@ public sealed class ToolCallingLoopTests
.ToList();
private static ToolCallingStreamEvent Text(string text) => ToolCallingStreamEvent.TextDelta(text);
private static ToolCallingStreamEvent Usage(int promptTokens, string text = "") => ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(text, [], TokenUsage.Of(promptTokens)));
private static ToolCallingStreamEvent Completed(string text, IReadOnlyList<ToolCallingRequestedCall>? calls = null, IReadOnlyList<ISource>? sources = null)
=> ToolCallingStreamEvent.RoundCompleted(new ToolCallingRound(text, calls ?? [], sources ?? []));