Tiktoken 0.9.2

Tiktoken

Nuget package dotnet License: MIT Discord

Due to the lack of a C# version of cl100k_base encoding (gpt-3.5-turbo), I have implemented a basic solution with encoding and decoding methods based on the official Rust implementation.

Currently, cl100k_base p50k_base has been implemented. Other encodings will be added in future submissions. If you encounter any issues or have questions, please feel free to submit them on the lssues."

Usage

using Tiktoken;

var encoding = Encoding.ForModel("gpt-3.5-turbo");
var tokens = encoding.Encode("hello world"); // [15339, 1917]
var text = encoding.Decode(tokens); // hello world

var encoding = Encoding.Get("cl100k_base");
var tokens = encoding.Encode("hello world"); // [15339, 1917]
var text = encoding.Decode(tokens); // 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.

Below are the file download links: p50k_base.tiktoken cl100k_base.tiktoken

Benchmarks

You can view the reports for each version here


BenchmarkDotNet=v0.13.5, OS=macOS Ventura 13.3.1 (a) (22E772610a) [Darwin 22.4.0]
Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
.NET SDK=7.0.203
  [Host]     : .NET 7.0.5 (7.0.523.17405), Arm64 RyuJIT AdvSIMD
  Job-IGIIQY : .NET 7.0.5 (7.0.523.17405), Arm64 RyuJIT AdvSIMD DEBUG

BuildConfiguration=Debug  

Method Data Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
SharpTokenV1_0_28 Hello, World! 3.334 μs 0.0497 μs 0.0465 μs 1.00 0.00 0.6752 0.0038 4.14 KB 1.00
TiktokenSharpV1_0_5 Hello, World! 6.640 μs 0.0173 μs 0.0162 μs 1.99 0.03 2.1820 0.0458 13.41 KB 3.24
Tiktoken Hello, World! 8.957 μs 0.0392 μs 0.0327 μs 2.68 0.04 2.1820 0.0458 13.41 KB 3.24
SharpTokenV1_0_28 King(...)edy. [275] 62.740 μs 0.2538 μs 0.2119 μs 1.00 0.00 8.5449 0.4883 52.89 KB 1.00
TiktokenSharpV1_0_5 King(...)edy. [275] 21.973 μs 0.1803 μs 0.1599 μs 0.35 0.00 5.0964 0.3052 31.34 KB 0.59
Tiktoken King(...)edy. [275] 23.805 μs 0.0975 μs 0.0814 μs 0.38 0.00 5.7678 0.3357 35.38 KB 0.67

Showing the top 20 packages that depend on Tiktoken.

Packages Downloads
Anthropic
Generated C# SDK based on official Anthropic OpenAPI specification.
4
Anthropic
Generated C# SDK based on official Anthropic OpenAPI specification.
3

.NET Standard 2.0

  • No dependencies.

.NET Standard 2.1

  • No dependencies.

Version Downloads Last updated
3.1.4 3 03/26/2026
3.1.3 2 03/26/2026
3.1.2 2 03/26/2026
3.1.2-alpha.0.1 3 03/26/2026
3.1.1 2 03/26/2026
3.1.1-alpha.0.1 2 03/26/2026
3.1.0 2 03/26/2026
3.1.0-rc.1.4 2 03/26/2026
3.1.0-rc.1.3 2 03/26/2026
3.1.0-rc.1.2 2 03/26/2026
3.1.0-rc.1.1 2 03/26/2026
3.1.0-rc.1 2 03/26/2026
3.0.1-alpha.0.4 3 03/26/2026
3.0.0 2 03/26/2026
2.2.0 2 03/06/2026
2.1.1 2 03/06/2026
2.1.0 2 03/06/2026
2.0.3 2 03/06/2026
2.0.2 2 03/06/2026
2.0.1 2 03/06/2026
2.0.0 3 03/06/2026
1.2.0 2 03/06/2026
1.1.3 2 03/06/2026
1.1.2 2 03/06/2026
1.1.1 2 03/06/2026
1.1.0 2 03/06/2026
1.0.2 2 03/06/2026
1.0.1 2 03/06/2026
1.0.0 2 03/06/2026
0.9.7 2 03/06/2026
0.9.6 2 03/06/2026
0.9.5 2 03/06/2026
0.9.4 2 03/06/2026
0.9.3 2 03/06/2026
0.9.2 2 03/06/2026