TiktokenSharp 1.0.5

TiktokenSharp

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."

If you want to use the ChatGPT C# library that integrates this repository and implements context-based conversation, please visit ChatGPTSharp.

Getting Started

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

Update

1.0.4 20230424

  • Add method TikToken.GetEncoding(encodingName).

1.0.3 20230321

  • GetEncodingSetting now supports the model of gpt-4 and also allows for encoding names to be directly passed in.

1.0.2 20230317

  • add method TikToken.PBEFileDirectory to allow for custom storage directory of bpe files. the path needs to be set before TikToken.EncodingForModel().

1.0.1 20230313

  • p50k_base encoding algorithm that supports the text-davinci-003 model.

.NET Standard 2.0

.NET Standard 2.1

  • No dependencies.

Version Downloads Last updated
1.2.0 7 11/26/2025
1.1.8 120 08/14/2025
1.1.7 21 06/03/2025
1.1.6 4 06/06/2025
1.1.5 4 06/06/2025
1.1.4 4 06/06/2025
1.1.2 4 06/06/2025
1.1.1 4 06/28/2025
1.1.0 4 06/06/2025
1.0.9 5 06/28/2025
1.0.8 4 06/28/2025
1.0.7 4 06/28/2025
1.0.6 4 06/28/2025
1.0.5 4 06/28/2025
1.0.4 4 06/28/2025
1.0.3 4 06/28/2025
1.0.2 4 06/28/2025
1.0.1 4 06/06/2025
1.0.0 4 06/06/2025