![]() |
VOOZH | about |
dotnet add package TiktokenSharp --version 1.2.1
NuGet\Install-Package TiktokenSharp -Version 1.2.1
<PackageReference Include="TiktokenSharp" Version="1.2.1" />
<PackageVersion Include="TiktokenSharp" Version="1.2.1" />Directory.Packages.props
<PackageReference Include="TiktokenSharp" />Project file
paket add TiktokenSharp --version 1.2.1
#r "nuget: TiktokenSharp, 1.2.1"
#:package TiktokenSharp@1.2.1
#addin nuget:?package=TiktokenSharp&version=1.2.1Install as a Cake Addin
#tool nuget:?package=TiktokenSharp&version=1.2.1Install as a Cake Tool
This library is a C# implementation of the token count calculation, referencing OpenAI's official Rust language version. Currently, the encoding algorithms for o200k_base, cl100k_base, and p50k_base have been implemented. You can directly obtain the corresponding encoding algorithm using the model name.
TiktokenSharp is available as NuGet package.
using TiktokenSharp;
//use model name
TikToken tikToken = TikToken.EncodingForModel("gpt-3.5-turbo");
var i = tikToken.Encode("hello world"); //[15339, 1917]
var d = tikToken.Decode(i); //hello world
//use encoding name
TikToken tikToken = TikToken.GetEncoding("cl100k_base");
var i = tikToken.Encode("hello world"); //[15339, 1917]
var d = tikToken.Decode(i); //hello world
When using a new encoder for the first time, the required tiktoken files for the encoder will be downloaded from the internet. This may take some time. Once the download is successful, subsequent uses will not require downloading again. You can set TikToken.PBEFileDirectory before using the encoder to modify the storage path of the downloaded tiktoken files, or you can pre-download the files to avoid network issues causing download failures.
Why are the tiktoken files not integrated into the package? On one hand, this would make the package size larger. On the other hand, I want to stay as consistent as possible with OpenAI's official Python code.
If you are deploying cloud functions, such as "Azure App Service," which cannot read/write local files, please package tiktoken files(PBE Dir) with the publish files.
Below are the file download links: p50k_base.tiktoken cl100k_base.tiktoken o200k_base.tiktoken
TiktokenSharp.BenchmarkRun benchmark:
dotnet run --project TiktokenSharp.Benchmark/TiktokenSharp.Benchmark.csproj --framework net10.0 -c Release
List benchmarks:
dotnet run --project TiktokenSharp.Benchmark/TiktokenSharp.Benchmark.csproj --framework net10.0 -c Release -- --list flat
<details> <summary>Code:</summary>
private GptEncoding _sharpToken = GptEncoding.GetEncoding("cl100k_base");
private TikToken _tikToken = TikToken.GetEncoding("cl100k_base");
private string _kLongText = "King Lear, one of Shakespeare's darkest and most savage plays, tells the story of the foolish and Job-like Lear, who divides his kingdom, as he does his affections, according to vanity and whim. Lear’s failure as a father engulfs himself and his world in turmoil and tragedy.";
[Benchmark]
public int SharpToken()
{
var sum = 0;
for (var i = 0; i < 10000; i++)
{
var encoded = _sharpToken.Encode(_kLongText);
var decoded = _sharpToken.Decode(encoded);
sum += decoded.Length;
}
return sum;
}
[Benchmark]
public int TiktokenSharp()
{
var sum = 0;
for (var i = 0; i < 10000; i++)
{
var encoded = _tikToken.Encode(_kLongText);
var decoded = _tikToken.Decode(encoded);
sum += decoded.Length;
}
return sum;
}
</details>
| Method | Job | Runtime | Mean | Error | StdDev | Median | Gen0 | Allocated |
|---|---|---|---|---|---|---|---|---|
| SharpToken | .NET 10.0 | .NET 10.0 | 58.24 ms | 1.159 ms | 2.885 ms | 56.97 ms | 1000.0000 | 22.13 MB |
| TiktokenSharp | .NET 10.0 | .NET 10.0 | 40.85 ms | 0.806 ms | 0.754 ms | 40.71 ms | 1076.9231 | 13.28 MB |
| SharpToken | .NET 8.0 | .NET 8.0 | 67.04 ms | 1.281 ms | 1.258 ms | 67.42 ms | 1500.0000 | 22.13 MB |
| TiktokenSharp | .NET 8.0 | .NET 8.0 | 42.97 ms | 0.851 ms | 1.107 ms | 42.63 ms | 1076.9231 | 13.28 MB |
PBEFileDirectory/BaseDirectory to system Temp.TiktokenSharp.Test target framework to .NET 10.TiktokenSharp.Test into dedicated project TiktokenSharp.Benchmark (net8.0 + net10.0).--benchmark mode from test runner and keep TiktokenSharp.Test focused on functional validation.| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on TiktokenSharp:
| Package | Downloads |
|---|---|
|
Mythosia.AI
## What's New in v6.6.0 ### New model - Anthropic: Claude Fable 5 (claude-fable-5) — Anthropic's new top model tier above Opus (1M context window, 128K max output). ### Fable 5 API contract handled automatically - Unsupported temperature is omitted; extended thinking uses adaptive mode (thinking.type=adaptive + output_config.effort); the thinking parameter is omitted entirely when disabled (Fable 5 rejects an explicit thinking.type=disabled). ### Fixed - Opus 4.7 / 4.8 max output tokens corrected to 128K (previously fell into the generic opus-4 32K bucket, which capped the thinking-budget max_tokens auto-adjustment). - QuickAskAsync / QuickAskWithImageAsync: provider routing now matches real lowercase model ids (previously every model except o3* threw ArgumentException), and the requested model is actually applied to the created service instead of the provider default. - Vision calls no longer silently swap claude-sonnet-4-x / claude-haiku-4-5 to Sonnet 4.6 (the vision-capability gate now recognizes all vision-capable model families). Requires Mythosia.AI.Abstractions v2.4.0. --- ## Documentation - Basic Usage: https://github.com/AJ-comp/Mythosia.AI/wiki - Advanced Features: https://github.com/AJ-comp/Mythosia.AI/wiki/Advanced-Features - Release Notes: https://github.com/AJ-comp/Mythosia.AI/wiki/Release-Notes - GitHub: https://github.com/AJ-comp/Mythosia.AI |
|
|
NjxyChatAISDK
支持通过API调用OpenAI、DeepSeek、Doubao |
|
|
Mythosia.AI.Providers.Alibaba
Alibaba Cloud Qwen provider package for Mythosia.AI. Includes QwenService with expanded Qwen 3 / 3.5 model constants, platform-specific thinking request handling across DashScope, vLLM, and Ollama, token usage streaming support, and Mythosia.AI v6.4.0 compatibility. Documentation - GitHub: https://github.com/AJ-comp/Mythosia.AI - Release Notes: core/Mythosia.AI.Providers.Alibaba/RELEASE_NOTES.md |
|
|
TokenFlow.Tokenizers
Model-specific tokenizer implementations for TokenFlow.AI (OpenAI, Claude, etc.). |
|
|
Serina.Semantic.Ai.Pipelines
Serina Pipelines for Semantic Kernel allows to build flexible Ai Proccessing pipelines. |
Showing the top 3 popular GitHub repositories that depend on TiktokenSharp:
| Repository | Stars |
|---|---|
|
StartupHakk/OpenMonoAgent.ai
(BETA) AI shouldn't have a meter. Unlimited tokens. Forever. Your machine. Your agent. Use it from anywhere. Terminal-native coding agent powered by local LLMs — 100% open source, free forever, and installed with a single command. Proudly built on C#/.NET, because AI tooling should be infrastructure, not a subscription.
|
|
|
MayDay-wpf/AIBotPublic
AIBot PRO 是一个基于.NET 8 的 AI聚合客户端 to C 弱 to B 可以集成众多AI产品(ChatGPT,Gemini,Claude,文心一言,通义千问,讯飞星火),无感切换对话,支持知识库、插件开发、AI流程引擎(workflow)、以及开放平台对外输出定制化的特色AI API
|
|
|
dmitry-brazhenko/SharpToken
SharpToken is a C# library for tokenizing natural language text. It's based on the tiktoken Python library and designed to be fast and accurate.
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 1.2.1 | 27,653 | 2/22/2026 | |
| 1.2.0 | 42,816 | 11/19/2025 | |
| 1.1.8 | 79,017 | 8/14/2025 | |
| 1.1.7 | 83,762 | 3/14/2025 | |
| 1.1.6 | 26,129 | 12/24/2024 | |
| 1.1.5 | 43,674 | 10/8/2024 | |
| 1.1.4 | 133,571 | 5/14/2024 | |
| 1.1.2 | 636 | 5/14/2024 | 1.1.2 is deprecated because it has critical bugs. |
| 1.1.1 | 298 | 5/14/2024 | 1.1.1 is deprecated because it has critical bugs. |
| 1.1.0 | 14,899 | 4/8/2024 | |
| 1.0.9 | 24,554 | 2/8/2024 | |
| 1.0.8 | 19,577 | 12/27/2023 | |
| 1.0.7 | 39,222 | 10/10/2023 | |
| 1.0.6 | 104,732 | 6/25/2023 | |
| 1.0.5 | 53,752 | 5/8/2023 | |
| 1.0.4 | 3,258 | 4/24/2023 | |
| 1.0.3 | 3,405 | 3/21/2023 | |
| 1.0.2 | 1,064 | 3/17/2023 | |
| 1.0.1 | 1,649 | 3/13/2023 | |
| 1.0.0 | 1,099 | 3/7/2023 |