AI-Studio/app/MindWork AI Studio/Chat/ReportedTokenUsage.cs
j-erler be1e6532fb
Some checks are pending
Build and Release / Verify (push) Waiting to run
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
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) Blocked by required conditions
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) Blocked by required conditions
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) Blocked by required conditions
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) Blocked by required conditions
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) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Added the exact token count where the provider reports it (#989)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
2026-09-23 20:17:02 +02:00

55 lines
2.5 KiB
C#

using AIStudio.Provider;
namespace AIStudio.Chat;
/// <summary>
/// What a provider said the request behind one answer cost, as it is stored on that answer.
/// </summary>
/// <remarks>
/// The number, the model it was charged for, and the conversation it was counted on travel as one
/// value. Each of them is meaningless without the others, and loose fields on the answer could be
/// set, cleared, or copied apart.
///
/// A plain number rather than a TokenUsage: that type only ever comes out of its own factory, which
/// is what keeps an impossible usage from existing, while a stored value has to be readable back by
/// the serializer. ToTokenUsage is the way back, and it treats a chat file somebody edited by hand
/// the same way the reading side treats a provider's JSON.
/// </remarks>
public sealed record ReportedTokenUsage
{
/// <summary>
/// What everything sent to the model cost.
/// </summary>
public int PromptTokens { get; init; }
/// <summary>
/// Which model the number was charged for.
/// </summary>
/// <remarks>
/// A token count belongs to the tokenizer which produced it. Switch the model of a chat, and
/// the same conversation is worth a different number of tokens -- so the reported one stops
/// being an answer about the request which is about to be sent, and the estimate, wrong as it
/// is, is at least wrong about the right model.
/// </remarks>
public string ModelId { get; init; } = string.Empty;
/// <summary>
/// How many blocks the conversation had when the request went out, this answer included.
/// </summary>
/// <remarks>
/// What tells an outdated report apart without anybody having to remember anything about it.
/// Blocks are only ever added at the end, so while this answer is the last block, a thread with
/// the same count is the thread the provider saw. A lower count means an earlier message was
/// deleted, and that message is still inside the reported number.
///
/// Taken when the request is sent rather than when the report arrives: whatever is deleted
/// while the answer streams in was still part of what the provider counted.
/// </remarks>
public int BlockCount { get; init; }
/// <summary>
/// States the stored number as a usage again.
/// </summary>
/// <returns>The usage, or TokenUsage.UNKNOWN when the stored number states nothing usable.</returns>
public TokenUsage ToTokenUsage() => TokenUsage.OfReported(this.PromptTokens);
}