A token can hold part of a UTF-8 character. The zip includes cl100k_base.tiktoken.
dotnet add package RecursiveAI.OneNlpRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneNlp.Tokenizers; byte[] bytes = TiktokenTokenizer .Cl100kBase("cl100k_base.tiktoken") .DecodeBytes([45918]); Console.WriteLine(Convert.ToHexString(bytes));
E8AAThe two entries used on this page are below. Each line in the ranks file has the token’s bytes in Base64, then its ID. The zip includes the full file and its MIT license.
lQ== 243 6Ko= 45918
DecodeBytes throws ArgumentException if an ID isn’t in the loaded encoding.
Unzip the example, then dotnet run.
Variations
Join the bytes before decoding text. Here the second ID supplies the missing byte. The first line prints the joined bytes; the second interprets them as UTF-8.
using System.Text; using RecursiveAI.OneNlp.Tokenizers; var tokenizer = TiktokenTokenizer.Cl100kBase( "cl100k_base.tiktoken"); byte[] bytes = tokenizer.DecodeBytes([45918, 243]); Console.WriteLine(Convert.ToHexString(bytes)); Console.WriteLine(Encoding.UTF8.GetString(bytes));
E8AA95 誕
Related recipes
The short answer
Use TiktokenTokenizer.DecodeBytes(ids) with the same encoding that produced the IDs. It puts the token bytes into one byte[] in input order, without trying to interpret them as text. Use it for a single token, a slice, or the whole sequence. Turn the result into text only when you have the complete UTF-8 bytes you need.
How many bytes are in a UTF-8 character?
A Unicode code point takes one to four bytes in UTF-8. The character 誕 takes three: E8 AA 95. In this cl100k_base example, the token with ID 45918 carries the first two bytes, and the token with ID 243 carries the third. Neither byte fragment is valid UTF-8 on its own. That’s why the snippet at the top prints E8AA in hex instead of trying to print a character.
Decoding each ID to a string and concatenating the strings won’t recover this character. OneNLP’s Decode interprets bytes as UTF-8 and can produce a replacement character (�) when the bytes are incomplete. IdToToken is useful for looking at a token, but its invalid UTF-8 display form, here <0xE8><0xAA>, isn’t the original byte sequence either. The variation keeps both pieces intact until they can be decoded together.
Which tiktoken encoding should I use to decode IDs?
Use the encoding that generated the IDs, not simply the vocabulary you already have. A rank file maps numbers to byte sequences. A number can be present in two encodings and map to different bytes, so a successful decode doesn’t prove you chose the right file. If you store token IDs for later use, keep the encoding name with them. If you have a custom vocabulary, keep its exact rank file too.
DecodeBytes includes the UTF-8 spelling of special tokens by default. If your input includes control markers and you want only the content bytes, pass skipSpecialTokens: true. That’s a deliberate change to the result: it omits those IDs rather than recovering everything in the sequence. See the tiktoken API guide for loading other vocabularies.