---
title: "Decode tiktoken IDs to raw bytes in C#"
url: https://recursiveai.org/onenlp/cookbook/decode-tiktoken-ids-to-exact-bytes/
description: "Use OneNLP’s DecodeBytes in C# to recover the exact bytes of tiktoken IDs, even when a single token contains only part of a UTF-8 character."
version: "1.0.0"
---

# Decode tiktoken IDs to raw bytes in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/decode-tiktoken-ids-to-exact-bytes/decode-tiktoken-ids-to-exact-bytes.zip)

A token can hold part of a UTF-8 character. The zip includes [`cl100k_base.tiktoken`](https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken).

```shell
dotnet add package RecursiveAI.OneNlp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneNlp.Tokenizers;
byte[] bytes = TiktokenTokenizer
    .Cl100kBase("cl100k_base.tiktoken")
    .DecodeBytes([45918]);
Console.WriteLine(Convert.ToHexString(bytes));
```

```text
E8AA
```

The two entries used on this page are below. Each line in the [ranks file](https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken) has the token’s bytes in Base64, then its ID. The zip includes the full file and its MIT license.

```text
lQ== 243
6Ko= 45918
```

`DecodeBytes` throws `ArgumentException` if an ID isn’t in the loaded encoding.

Unzip [the example](https://recursiveai.org/onenlp/cookbook/decode-tiktoken-ids-to-exact-bytes/decode-tiktoken-ids-to-exact-bytes.zip), 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.

```csharp
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));
```

```text
E8AA95
誕
```

## Related recipes

- [Count tiktoken tokens in C#](https://recursiveai.org/onenlp/cookbook/count-llm-tokens/)
- [Trim text to a token limit in C#](https://recursiveai.org/onenlp/cookbook/trim-text-to-token-budget/)

## 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](https://www.rfc-editor.org/rfc/rfc3629#section-3). 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](https://recursiveai.org/onenlp/docs/tokenizers-bpe-tiktoken/) for loading other vocabularies.
