AiTokenTracker 0.4.0

dotnet add package AiTokenTracker --version 0.4.0
                    
NuGet\Install-Package AiTokenTracker -Version 0.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AiTokenTracker" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AiTokenTracker" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="AiTokenTracker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AiTokenTracker --version 0.4.0
                    
#r "nuget: AiTokenTracker, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AiTokenTracker@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AiTokenTracker&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=AiTokenTracker&version=0.4.0
                    
Install as a Cake Tool

AiTokenTracker .NET SDK

AiTokenTracker captures outbound LLM traffic and sends it to your ingest API for centralized token/cost parsing.

This SDK supports three integration paths so teams can adopt with minimal refactoring:

  1. HttpClient Handler Integration (preferred)
    • Use .AddAiTokenTracker() on IHttpClientFactory clients.
    • Captures full request/response envelopes.
  2. SDK Wrapper Integration
    • Use IAiTokenTrackerSdkClient when provider SDK calls bypass your HttpClient pipeline.
    • Captures full request/response payloads from DTOs.
  3. Diagnostics Fallback
    • Captures metadata-only for LLM calls not covered by #1 or #2.
    • Useful for visibility and gap detection.

Installation

dotnet add package AiTokenTracker --version <latest>

Quick Start

Register once in DI:

using AiTokenTracker;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAiTokenTracker(options =>
{
    options.AuthToken = "your-subscription-or-api-key";
    options.EnableDiagnosticsFallback = true;
});

Then for preferred full-fidelity capture on HTTP clients you own:

builder.Services
    .AddHttpClient("OpenAiHttp")
    .AddAiTokenTracker();

Integration Modes

1) HttpClient Handler Integration (Preferred)

Best when your outbound LLM calls go through IHttpClientFactory.

builder.Services.AddHttpClient("LlmClient")
    .AddAiTokenTracker();

Any classified LLM request sent by this client is captured with full body.

Scoped Custom Filters For Automatic Capture

You can attach custom filters to automatic handler/diagnostics capture by opening an interception scope in the current async flow:

var scope = AiTokenTrackerInterceptionScope.Create(new Dictionary<string, string>
{
    ["Workflow"] = "AutoPublish",
    ["JobId"] = "job-42"
});

scope.Open();
try
{
    using HttpClient llmClient = httpClientFactory.CreateClient("LlmClient");
    using HttpResponseMessage response = await llmClient.PostAsync("/v1/responses", content, cancellationToken);
}
finally
{
    scope.Close();
}

You can also use using:

using var scope = AiTokenTrackerInterceptionScope.Create(new Dictionary<string, string>
{
    ["Workflow"] = "AutoPublish"
});
scope.Open();
await llmClient.PostAsync("/v1/responses", content, cancellationToken);

Use one scope per logical request/workflow and always close in finally to avoid leaking context.

2) SDK Wrapper Integration (For SDKs that bypass HttpClientFactory)

If a provider SDK hides transport internals, wrap calls with IAiTokenTrackerSdkClient. providerHint is optional and should be passed when known. method is optional and defaults to "POST" when omitted.

public sealed class TrackedOpenAiClient
{
    private readonly OpenAIClient _inner;
    private readonly IAiTokenTrackerSdkClient _tracker;

    public TrackedOpenAiClient(OpenAIClient inner, IAiTokenTrackerSdkClient tracker)
    {
        _inner = inner;
        _tracker = tracker;
    }

    public async Task<Response> CreateResponseAsync(string model, string userPrompt, CancellationToken cancellationToken)
    {
        var responseClient = _inner.GetOpenAIResponseClient(model);

        var requestPayload = new
        {
            userInputText = userPrompt
        };

        IAiTokenTrackerCallScope scope = _tracker.BeginLlmCall(
            request: requestPayload,
            providerHint: "openai");

        try
        {
            var response = await responseClient.CreateResponseAsync(userPrompt, cancellationToken: cancellationToken);
            _ = scope.CompleteAsync(response.Value, 200, cancellationToken: CancellationToken.None);
            return response.Value;
        }
        catch (Exception ex)
        {
            _ = scope.FailAsync(ex, cancellationToken: CancellationToken.None);
            throw;
        }
    }
}

This pattern guarantees capture even when provider SDKs do not run through your tracked HttpClient handlers.

3) Diagnostics Fallback

Enabled by default:

options.EnableDiagnosticsFallback = true;

The fallback listens to System.Net.Http diagnostics and records likely LLM calls not captured by the first two integrations.

Important:

  • Fallback is generally metadata-only (method/url/status/headers).
  • It is intended for visibility and missed-call detection, not primary full-fidelity ingest.

Services Registered

AddAiTokenTracker(...) registers:

  • AiTokenTrackerDelegatingHandler
  • IAiTokenTrackerIngestionClient
  • IAiTokenTrackerSdkClient
  • ILlmRequestClassifier default implementation (DefaultLlmRequestClassifier)
  • Diagnostics fallback hosted service
  • Named ingest client: AiTokenTracker.IngestionClient

Configuration

AiTokenTrackerOptions:

  • AuthToken (string, required)
  • EnableDiagnosticsFallback (bool, default: true)

Custom Provider Classification

The SDK includes DefaultLlmRequestClassifier heuristics (OpenAI/Anthropic/Gemini/Unify/common paths).

To customize, implement ILlmRequestClassifier:

public interface ILlmRequestClassifier
{
    bool TryClassify(HttpRequestMessage request, out string? providerHint);
}

Register your classifier in DI. It will run alongside defaults.


Choosing a Strategy

Use this guideline:

  • If call path uses HttpClientFactory: use Handler Integration.
  • If call path uses provider SDK abstraction: use Wrapper Integration.
  • Keep Diagnostics Fallback on to identify uncovered traffic.

Recommended rollout for large codebases:

  1. Enable SDK + diagnostics fallback globally.
  2. Add .AddAiTokenTracker() to known LLM HttpClient registrations.
  3. Add wrapper integration only at central SDK service boundaries.
  4. Use diagnostics events to locate remaining uncovered flows.

Troubleshooting

No ingest events received

Check:

  1. AuthToken is configured and non-empty.
  2. Request is classified as LLM traffic.
  3. If using provider SDKs, confirm wrapper integration is applied where the call is made.

Calls are visible but token fields are zero

That usually means payload shape was not full-fidelity or not parseable by server analyzers. Ensure handler/wrapper path is providing request/response bodies.

Duplicate events

If both handler and wrapper are applied to the exact same call path, you can ingest duplicates. Use one primary integration path per call flow.


Notes

  • The SDK intentionally sends envelopes asynchronously to avoid blocking LLM call latency.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 71 5/27/2026
0.3.9 89 5/20/2026
0.3.8 95 5/20/2026
0.3.7 89 5/20/2026
0.3.6 89 5/20/2026
0.3.5 91 5/12/2026
0.3.4 88 5/12/2026
0.3.3 90 5/12/2026
0.2.0 107 3/24/2026
0.1.0 106 3/24/2026