TokenKit 1.2.0
dotnet tool install --global TokenKit --version 1.2.0
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
dotnet tool install --local TokenKit --version 1.2.0
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=TokenKit&version=1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nuke :add-package TokenKit --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
๐ง TokenKit
TokenKit โ A professional .NET 8.0 library and CLI for tokenization, validation, cost estimation, and model registry management across multiple LLM providers (OpenAI, Anthropic, Gemini, etc.).
โจ Features
| Category | Description |
|---|---|
| ๐ข Tokenization | Analyze text or files and count tokens using multiple encoder engines (simple, SharpToken, ML.Tokenizers) |
| ๐ฐ Cost Estimation | Automatically calculate estimated API cost based on model metadata |
| โ Prompt Validation | Validate prompt length against model context limits |
| ๐งฉ Model Registry | Manage model metadata (maxTokens, pricing, encodings, providers) via JSON registry |
| โ๏ธ CLI & SDK | Use TokenKit as a .NET library or a global CLI tool |
| ๐งฎ Multi-Encoder Support | Dynamically select tokenization engines via --engine flag |
| ๐ฆ Self-contained Data | Local registry stored in Registry/models.data.json, auto-updatable |
| ๐ Live Model Scraper | Optional OpenAI API key support to fetch real-time model data |
| ๐ Structured Logging | All CLI commands logged to tokenkit.log with rotation (1MB max) |
| ๐คซ Quiet & JSON Modes | Machine-readable (--json) and silent (--quiet) output modes for automation |
| ๐จ CLI Polish | Colorized output, ASCII banner, and improved user experience |
โ๏ธ Installation
๐ฆ As a Library (NuGet)
dotnet add package TokenKit
๐ป As a Global CLI Tool
dotnet tool install -g TokenKit
๐ Usage (All-in-One Guide)
๐น Analyze Inline Text
tokenkit analyze "Hello from TokenKit!" --model gpt-4o
๐น Analyze File Input
tokenkit analyze prompt.txt --model gpt-4o
๐น Pipe Input (stdin)
echo "This is piped text input" | tokenkit analyze --model gpt-4o
Example Output:
{
"Model": "gpt-4o",
"Provider": "OpenAI",
"TokenCount": 4,
"EstimatedCost": 0.00002,
"Valid": true
}
๐น Validate Prompt Length
tokenkit validate "A very long prompt to validate" --model gpt-4o
{
"IsValid": true,
"Message": "OK"
}
๐น List Registered Models
tokenkit models list
Filter by Provider
tokenkit models list --provider openai
JSON Output
tokenkit models list --json
๐น Update Model Data
Default Update (Offline Fallback)
tokenkit update-models
Using OpenAI API Key
tokenkit update-models --openai-key sk-xxxx
From JSON (stdin)
cat newmodels.json | tokenkit update-models
Example Input:
[
{
"Id": "gpt-4o-mini",
"Provider": "OpenAI",
"MaxTokens": 64000,
"InputPricePer1K": 0.002,
"OutputPricePer1K": 0.01,
"Encoding": "cl100k_base"
}
]
๐น Scrape Latest Model Data (Preview)
tokenkit scrape-models --openai-key sk-xxxx
If no key is provided, TokenKit uses the local offline model registry.
Example Output:
๐ Fetching latest OpenAI model data...
โ
Retrieved 3 models:
- OpenAI: gpt-4o (128000 tokens)
- OpenAI: gpt-4o-mini (64000 tokens)
- OpenAI: gpt-3.5-turbo (4096 tokens)
๐น CLI Output Modes
JSON Mode
tokenkit analyze "Hello" --model gpt-4o --json
Outputs pure JSON:
{
"Model": "gpt-4o",
"Provider": "OpenAI",
"TokenCount": 7,
"EstimatedCost": 0.000105,
"Engine": "simple",
"Valid": true
}
Quiet Mode
tokenkit analyze "Silent test" --model gpt-4o --quiet
No console output. Log entry saved to tokenkit.log.
๐งฉ Programmatic SDK Example
using TokenKit.Registry;
using TokenKit.Services;
var model = ModelRegistry.Get("gpt-4o");
var tokenizer = new TokenizerService();
var result = tokenizer.Analyze("Hello from TokenKit!", model!.Id);
var cost = CostEstimator.Estimate(model, result.TokenCount);
Console.WriteLine($"Tokens: {result.TokenCount}, Cost: ${cost}");
๐ฆ Model Registry
TokenKit stores all model metadata in:
Registry/models.data.json
Each entry includes:
{
"Id": "gpt-4o",
"Provider": "OpenAI",
"MaxTokens": 128000,
"InputPricePer1K": 0.005,
"OutputPricePer1K": 0.015,
"Encoding": "cl100k_base"
}
๐งช Testing & Quality Assurance
TokenKit maintains 100% test coverage using xUnit and Codecov.
Run tests locally:
dotnet test --collect:"XPlat Code Coverage"
๐งญ Future Enhancements
| Feature | Description |
|---|---|
| ๐ Extended Provider Support | Add Gemini, Claude, and Mistral integrations |
| ๐พ Persistent Config Profiles | Store model defaults and pricing overrides per project |
| ๐งฎ Batch Analysis | Analyze multiple files or prompts in a single command |
| ๐ Report Generation | Export CSV/JSON summaries of token usage and estimated cost |
| ๐ง LLM-Aware Cost Planner | Simulate conversation cost across multi-turn dialogues |
| ๐งฉ IDE Integrations | VS Code and JetBrains plugins for inline token analysis |
| โ๏ธ Custom Encoders | Support community-built encoders and language models |
๐ก License
Licensed under the MIT License.
ยฉ 2025 Andrew Clements โ Flow Labs / TokenKit
| Product | Versions 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.
This package has no dependencies.