ViseoIberia.AI.Pricing.Token
1.1.3
See the version list below for details.
dotnet add package ViseoIberia.AI.Pricing.Token --version 1.1.3
NuGet\Install-Package ViseoIberia.AI.Pricing.Token -Version 1.1.3
<PackageReference Include="ViseoIberia.AI.Pricing.Token" Version="1.1.3" />
<PackageVersion Include="ViseoIberia.AI.Pricing.Token" Version="1.1.3" />
<PackageReference Include="ViseoIberia.AI.Pricing.Token" />
paket add ViseoIberia.AI.Pricing.Token --version 1.1.3
#r "nuget: ViseoIberia.AI.Pricing.Token, 1.1.3"
#:package ViseoIberia.AI.Pricing.Token@1.1.3
#addin nuget:?package=ViseoIberia.AI.Pricing.Token&version=1.1.3
#tool nuget:?package=ViseoIberia.AI.Pricing.Token&version=1.1.3
<p align="center"> <img src="https://raw.githubusercontent.com/viseoiberia/viseoiberia.ai.pricing.token/main/icon.png" alt="Logo" width="200"/> </p>
Pricing Token
What is pricing token?
Pricing Token is a .NET library designed to simplify and automate the calculation of AI operation costs based on token usage. It enables developers to accurately estimate and convert the cost of Azure OpenAI and other LLM services by supporting configurable pricing models and real-time currency conversion. The library provides flexible integration for .NET Standard 2.0, .NET 8, and .NET 9 applications, allowing you to track token consumption, apply dynamic or static pricing, and convert costs between currencies using up-to-date exchange rates. With extensible options for pricing sources, regions, and retry policies, it is ideal for enterprise scenarios requiring precise cost management and reporting for AI workloads. Seamlessly integrates via dependency injection and is easily customizable to fit your organization's pricing and budgeting requirements.
Key Features
- Token Cost Calculation: Compute the cost of AI operations based on token usage and configurable pricing.
- Currency Conversion: Convert token costs between different currencies using up-to-date exchange rates.
- Flexible Pricing Sources: Support for both static configuration and dynamic API-based pricing.
- .NET Integration: Designed for easy use in any .NET Standard 2.0, .NET 8, or .NET 9 application.
- Extensible Options: Customizable pricing models, regions, and retry policies.
Supported framework
- Netstandard 2.0
- NET 8.0
- NET 9.0
Installation
Install the VISEOIberia.AI.Pricing.Token library for .NET with NuGet:
dotnet add package VISEOIberia.AI.Pricing.Token
NuGet Install-Package VISEOIberia.AI.Pricing.Token -Version x.y.z
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="VISEOIberia.AI.Pricing.Token" Version="x.y.z" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
Quickstart
Dependency Injection
1. Static Pricing Configuration (FromConfiguration)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddPricingToken(new PricingTokenOptions
{
SourceMode = PriceSourceMode.FromConfiguration,
Currency = new CurrencyOption
{
DefaultCurrency = CurrencyCode.USD // or any supported currency
// ConverterType = typeof(MyCustomCurrencyConverterService) // optional
},
Price = new PriceOption
{
Model = "gpt-4o",
Region = "westeurope",
DefaultInputCost = 0.002535, // Cost per 1K input tokens
DefaultOuputCost = 0.001354, // Cost per 1K output tokens
UnitOfMeasureValue = 1000
// ProviderType = typeof(MyCustomPriceProviderService) // optional
},
RetryPolicy = new RetryPolicy
{
RetryCount = 3,
BaseDelaySeconds = 2
}
});
2. Dynamic Pricing via API (ViaAPI)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddPricingToken(new PricingTokenOptions
{
SourceMode = PriceSourceMode.ViaApi,
Currency = new CurrencyOption
{
DefaultCurrency = CurrencyCode.EUR
},
Price = new PriceOption
{
Model = "gpt-4o",
Region = "westeurope"
// ProviderType = typeof(MyCustomPriceProviderService) // optional
},
RetryPolicy = new RetryPolicy
{
RetryCount = 5,
BaseDelaySeconds = 3
}
});
3. No Pricing Calculation (None)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddPricingToken(new PricingTokenOptions
{
SourceMode = PriceSourceMode.None
});
Usage
public class MyPricingService
{
private readonly ITokenPriceService _tokenPriceService;
public MyPricingService(ITokenPriceService tokenPriceService)
{
_tokenPriceService = tokenPriceService;
}
public async Task GetAndUseTokenPricingAsync()
{
// Get current pricing info (input/output cost, currency, etc.)
CostInfo costInfo = await _tokenPriceService.GetPriceAsync();
// Example: Calculate the cost for a given number of tokens
int inputTokens = 1538;
double inputCost = _tokenPriceService.GetPrice(costInfo.Input, inputTokens);
int outputTokens = 1200;
double outputCost = _tokenPriceService.GetPrice(costInfo.Output, outputTokens);
Console.WriteLine($"Input cost: {inputCost} {costInfo.Currency}");
Console.WriteLine($"Output cost: {outputCost} {costInfo.Currency}");
}
}
Example Response (CostInfo)
{
"Currency": "EUR",
"Input": {
"Price": 0.002535,
"UnitOfMeasure": "1k",
"UnitOfMeasureValue": 1000,
"Type": "Consumption"
},
"Output": {
"Price": 0.001354,
"UnitOfMeasure": "1k",
"UnitOfMeasureValue": 1000,
"Type": "Consumption"
}
}
Troubleshooting
Common Issues
Incorrect or Missing
SourceMode
- If
SourceMode
is not set or is set to an unsupported value, the library will not calculate prices as expected. Always setSourceMode
to one of:FromConfiguration
,ViaApi
, orNone
.
- If
Missing Model in API Mode
- When using
SourceMode = PriceSourceMode.ViaApi
, thePrice.Model
property is required. If not set, you will receive a validation error or no pricing data. - Solution:
options.SourceMode = PriceSourceMode.ViaApi; options.Price.Model = "gpt-4o"; // Required
- When using
Missing Input/Output Cost in Configuration Mode
- When using
SourceMode = PriceSourceMode.FromConfiguration
, bothPrice.DefaultInputCost
andPrice.DefaultOuputCost
must be set and greater than zero. If not, validation will fail and pricing will not be calculated. - Solution:
options.SourceMode = PriceSourceMode.FromConfiguration; options.Price.DefaultInputCost = 0.002535; options.Price.DefaultOuputCost = 0.001354;
Null or Invalid Options
- If
PricingTokenOptions
is not properly configured or is missing required fields, the library will throw an exception at startup or when callingValidate()
. Always ensure your configuration is complete and valid.
- If
Custom Provider/Converter Not Registered
- If you specify a custom
ProviderType
orConverterType
, ensure the corresponding service is registered in the DI container. Otherwise, dependency resolution will fail.
- If you specify a custom
API Connectivity Issues
- When using
ViaApi
mode, ensure your application can reach the external pricing API. Network issues, firewalls, or invalid endpoints will prevent price retrieval.
- When using
Currency Code Mismatch
- If you set a
DefaultCurrency
that is not supported by your pricing or conversion provider, you may get unexpected results or errors.
- If you set a
- When using
Tip:
Call the Validate()
method on your PricingTokenOptions
during startup to catch misconfigurations early.
Contribute
One of the easiest ways to participate is to engage in discussions in the GitHub repository. Bug reports and fixes are welcome!
For new features, components, or extensions, please open an issue and discuss with us before sending a PR. This is to avoid rejection as we might be taking the core in a different direction, but also to consider the impact on the larger ecosystem.
To learn more and get started:
- Ask questions in the GitHub discussions
Contributors
License
This project is licensed under the MIT License.
Copyright (c) VISEO Iberia. All rights reserved.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Ensure.That (>= 10.1.0)
- Microsoft.Extensions.Caching.Memory (>= 9.0.7)
- Microsoft.Extensions.Http (>= 9.0.7)
- Microsoft.Extensions.Http.Polly (>= 9.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- System.Net.Http.Json (>= 9.0.7)
-
net8.0
- Ensure.That (>= 10.1.0)
- Microsoft.Extensions.Caching.Memory (>= 9.0.7)
- Microsoft.Extensions.Http (>= 9.0.7)
- Microsoft.Extensions.Http.Polly (>= 9.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
-
net9.0
- Ensure.That (>= 10.1.0)
- Microsoft.Extensions.Caching.Memory (>= 9.0.7)
- Microsoft.Extensions.Http (>= 9.0.7)
- Microsoft.Extensions.Http.Polly (>= 9.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ViseoIberia.AI.Pricing.Token:
Package | Downloads |
---|---|
VISEOIberia.AI.Document
This package allows you to analyze documents page by page using Azure OpenAI. For each page, it extracts textual content and generates semantic descriptions of any images, effectively combining OCR capabilities with multimodal AI. Ideal for intelligent document processing pipelines, it transforms scanned or image-based documents into structured, enriched data using the power of Azure OpenAI models. |
GitHub repositories
This package is not used by any popular GitHub repositories.