mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-17 22:57:59 +00:00
Added the exact token count for the OpenAI Responses API
This commit is contained in:
10 files changed
+363
-12
No files matched your search
@@ -7,6 +7,16 @@ namespace AIStudio.Provider.OpenAI;
|
||||
/// </summary>
|
||||
public sealed record ResponsesResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// The output items after which the usage still describes the request as it was sent.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
private static readonly HashSet<string> 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<JsonElement> Output { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// What OpenAI says the response cost. Only the completed response carries it.
|
||||
/// </summary>
|
||||
public ResponsesUsage? Usage { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// States what the request of this response carried, as far as it can be believed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
/// <returns>The usage, or TokenUsage.UNKNOWN when the response states nothing usable.</returns>
|
||||
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<ResponsesFunctionCallItem> GetFunctionCalls() => this.Output
|
||||
.Where(x => ReadString(x, "type").Equals("function_call", StringComparison.Ordinal))
|
||||
.Select(x => new ResponsesFunctionCallItem
|
||||
|
||||
Reference in new issue
Block a user