diff --git a/app/MindWork AI Studio/Provider/BaseProvider.cs b/app/MindWork AI Studio/Provider/BaseProvider.cs index 98a34c74..63465592 100644 --- a/app/MindWork AI Studio/Provider/BaseProvider.cs +++ b/app/MindWork AI Studio/Provider/BaseProvider.cs @@ -1157,17 +1157,44 @@ public abstract class BaseProvider : IProvider, ISecretId // Check if annotations are supported: var annotationSupported = typeof(TAnnotation) != typeof(NoResponsesAnnotationStreamLine) && typeof(TAnnotation) != typeof(NoChatCompletionAnnotationStreamLine); + var isCompleted = false; await foreach (var serverSentEvent in this.ReadServerSentEventsAsync(providerName, "responses call", requestBuilder, token)) { - // Check if the line is the end of the stream. This one is read off the raw line - // rather than off a payload, because it has none: + // Check if the line announces the end of the stream. This one is read off the raw + // line rather than off a payload, because it has none: if (serverSentEvent.Line.StartsWith("event: response.completed", StringComparison.InvariantCulture)) - yield break; + { + isCompleted = true; + continue; + } // Skip lines without a payload: if (serverSentEvent.Data.Length is 0) continue; + // + // The payload after the announcement is the whole response, and the only line which + // states what the request cost. The stream ends here whether that can be read or not, + // which keeps the end independent of how a gateway orders the fields of the payload. + // + if (isCompleted) + { + var usage = TokenUsage.UNKNOWN; + try + { + usage = JsonSerializer.Deserialize(serverSentEvent.Data, JSON_SERIALIZER_OPTIONS)?.GetUsage() ?? TokenUsage.UNKNOWN; + } + catch + { + // Invalid JSON data states nothing, and the answer is complete either way. + } + + if (usage.IsKnown) + yield return new(string.Empty, [], Usage: usage); + + yield break; + } + // // Find delta lines: // diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesCompletedStreamLine.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesCompletedStreamLine.cs index 1591d562..d60059f6 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ResponsesCompletedStreamLine.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesCompletedStreamLine.cs @@ -10,4 +10,15 @@ namespace AIStudio.Provider.OpenAI; /// /// The type of the stream event. /// The response as a non-streamed call would have returned it. -public sealed record ResponsesCompletedStreamLine(string Type, ResponsesResponse? Response); \ No newline at end of file +public sealed record ResponsesCompletedStreamLine(string Type, ResponsesResponse? Response) +{ + /// + /// States what the request of this call carried, 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 line states nothing usable. + public TokenUsage GetUsage() => this.Response?.GetUsage() ?? TokenUsage.UNKNOWN; +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesResponse.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesResponse.cs index 285bca00..91f10628 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ResponsesResponse.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesResponse.cs @@ -7,6 +7,16 @@ namespace AIStudio.Provider.OpenAI; /// public sealed record ResponsesResponse { + /// + /// The output items after which the usage still describes the request as it was sent. + /// + /// + /// A list of the items which are known to leave the input alone, rather than one of those which + /// do not: a hosted tool OpenAI adds later would otherwise inflate the number without anybody + /// noticing. + /// + private static readonly HashSet OUTPUT_ITEMS_LEAVING_THE_INPUT_ALONE = ["message", "reasoning", "function_call"]; + public string Id { get; init; } = string.Empty; public string Model { get; init; } = string.Empty; @@ -15,6 +25,30 @@ public sealed record ResponsesResponse public IList Output { get; init; } = []; + /// + /// What OpenAI says the response cost. Only the completed response carries it. + /// + public ResponsesUsage? Usage { get; init; } + + /// + /// States what the request of this response carried, as far as it can be believed. + /// + /// + /// Only for a response which ran no hosted tool. When OpenAI runs one, such as its web search, + /// what the tool found is charged as input tokens of the same response, cf. the pricing read on + /// 2026-09-24 at https://developers.openai.com/api/docs/pricing. No later request carries what + /// the tool found, so the number would describe a conversation larger than the one there is -- + /// the same reason why the tool calling loop keeps only the usage of its first round. + /// + /// A response put back together from its items, for a gateway which never sent the completed + /// event, has no usage. Nor is one read off a response which ended as incomplete, because it + /// ran out of output tokens: that is rare enough for the estimate to cover it. + /// + /// The usage, or TokenUsage.UNKNOWN when the response states nothing usable. + public TokenUsage GetUsage() => this.Output.All(x => OUTPUT_ITEMS_LEAVING_THE_INPUT_ALONE.Contains(ReadString(x, "type"))) + ? this.Usage?.ToTokenUsage() ?? TokenUsage.UNKNOWN + : TokenUsage.UNKNOWN; + public IReadOnlyList GetFunctionCalls() => this.Output .Where(x => ReadString(x, "type").Equals("function_call", StringComparison.Ordinal)) .Select(x => new ResponsesFunctionCallItem diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamAccumulator.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamAccumulator.cs index 19070c82..19463e1c 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamAccumulator.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamAccumulator.cs @@ -27,7 +27,7 @@ public sealed class ResponsesStreamAccumulator /// Takes the next event of the stream and returns what it has to show. /// /// The event to read. - /// The text and sources of this event, both empty when it carried neither. + /// The text and sources of this event, both empty when it carried neither, and the usage of the completed event. public ResponsesStreamPart Process(ServerSentEvent serverSentEvent) { if (serverSentEvent.Data.Length is 0) @@ -61,8 +61,15 @@ public sealed class ResponsesStreamAccumulator switch (eventType) { case EVENT_COMPLETED: - this.completedResponse = TryDeserialize(serverSentEvent.Data)?.Response ?? this.completedResponse; - return ResponsesStreamPart.Nothing; + var completedLine = TryDeserialize(serverSentEvent.Data); + this.completedResponse = completedLine?.Response ?? this.completedResponse; + + // + // What the request carried, read the same way as on the plain text path and with + // the same exception, a response which ran a hosted tool, cf. ResponsesResponse. + // + var usage = completedLine?.GetUsage() ?? TokenUsage.UNKNOWN; + return usage.IsKnown ? new ResponsesStreamPart(string.Empty, [], usage) : ResponsesStreamPart.Nothing; case EVENT_TEXT_DELTA: var deltaLine = TryDeserialize(serverSentEvent.Data); diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamPart.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamPart.cs index fc40da97..936efc57 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamPart.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesStreamPart.cs @@ -5,15 +5,20 @@ namespace AIStudio.Provider.OpenAI; /// /// The text this line carried, empty when it carried none. /// The sources this line announced, empty when it announced none. -public readonly record struct ResponsesStreamPart(string TextDelta, IList Sources) +/// What the provider said the request cost, unknown on every line but the completed event. +public readonly record struct ResponsesStreamPart(string TextDelta, IList Sources, TokenUsage Usage = default) { /// /// The part of a line which says nothing to the user, such as a bookkeeping event. /// public static ResponsesStreamPart 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 || this.Sources.Count > 0; } \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesToolCallingAdapter.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesToolCallingAdapter.cs index bdd16ff7..5409f775 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ResponsesToolCallingAdapter.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesToolCallingAdapter.cs @@ -59,14 +59,16 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList b // // The text goes out while it is being written, the round only once the stream closed it. - // Sources travel with the text because the API announces them as it cites them. + // Sources travel with the text because the API announces them as it cites them. 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 ResponsesStreamAccumulator(); await foreach (var serverSentEvent in streamRequestAsync(request, token)) { var part = accumulator.Process(serverSentEvent); - if (part.HasContent) - yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources)); + if (part.HasContent || part.Usage.IsKnown) + yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage)); } var response = accumulator.Build(); diff --git a/app/MindWork AI Studio/Provider/OpenAI/ResponsesUsage.cs b/app/MindWork AI Studio/Provider/OpenAI/ResponsesUsage.cs new file mode 100644 index 00000000..1a161ecb --- /dev/null +++ b/app/MindWork AI Studio/Provider/OpenAI/ResponsesUsage.cs @@ -0,0 +1,31 @@ +// ReSharper disable ClassNeverInstantiated.Global +namespace AIStudio.Provider.OpenAI; + +/// +/// What OpenAI reports a Responses API call cost, as it closes the stream. +/// +/// +/// The input tokens are the whole request. What the API took from its cache is a share of them, +/// not an addition to them, which is why that detail stays unread. Read on 2026-09-24 at +/// https://developers.openai.com/api/docs/guides/prompt-caching. +/// +/// The block states more than this, the output and its reasoning share among it. Those are left +/// unread on purpose, for the reason given at TokenUsage: no later request carries them. +/// +public sealed record ResponsesUsage +{ + /// + /// What everything sent to the model cost, the cached part included. + /// + public int? InputTokens { get; init; } + + /// + /// States what this block reports, as far as it can be believed. + /// + /// + /// Whether the block describes the request at all is not decided here but by the response + /// around it, cf. ResponsesResponse. + /// + /// The usage, or TokenUsage.UNKNOWN when the block states nothing usable. + public TokenUsage ToTokenUsage() => TokenUsage.OfReported(this.InputTokens); +} \ No newline at end of file diff --git a/app/Tests/Provider/ResponsesUsageTests.cs b/app/Tests/Provider/ResponsesUsageTests.cs new file mode 100644 index 00000000..da422d80 --- /dev/null +++ b/app/Tests/Provider/ResponsesUsageTests.cs @@ -0,0 +1,95 @@ +using System.Text.Json; + +using AIStudio.Provider; +using AIStudio.Provider.OpenAI; + +namespace AIStudio.Tests.Provider; + +/// +/// Checks that what OpenAI says a Responses API call cost is read off the completed event, and only +/// believed when it describes the request. +/// +/// +/// 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. +/// +[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(data, ProviderJsonOptions.OPTIONS)!; +} \ No newline at end of file diff --git a/app/Tests/Provider/ToolCalling/ResponsesStreamAccumulatorTests.cs b/app/Tests/Provider/ToolCalling/ResponsesStreamAccumulatorTests.cs index 61957cba..8536eee6 100644 --- a/app/Tests/Provider/ToolCalling/ResponsesStreamAccumulatorTests.cs +++ b/app/Tests/Provider/ToolCalling/ResponsesStreamAccumulatorTests.cs @@ -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() { diff --git a/app/Tests/Provider/ToolCalling/ResponsesToolCallingAdapterTests.cs b/app/Tests/Provider/ToolCalling/ResponsesToolCallingAdapterTests.cs new file mode 100644 index 00000000..b2134a5b --- /dev/null +++ b/app/Tests/Provider/ToolCalling/ResponsesToolCallingAdapterTests.cs @@ -0,0 +1,112 @@ +using System.Runtime.CompilerServices; + +using AIStudio.Provider; +using AIStudio.Provider.OpenAI; + +namespace AIStudio.Tests.Provider.ToolCalling; + +/// +/// Checks what a round of a tool calling conversation with the Responses API passes on about its cost. +/// +/// +/// 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. +/// +[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(); + var written = new List(); + 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."); + }); + } + + /// + /// Runs the next round and returns the prompt of every usage it passed on. + /// + private static async Task> Usages(ResponsesToolCallingAdapter 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 ResponsesToolCallingAdapter Adapter(params string[][] rounds) + { + var nextRound = 0; + return new(new Model("gpt-5", null), [], 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