# How GPT Actually 'Reads' Text: From Bytes to Tokens and Embeddings

> This article breaks down exactly how large language models like GPT understand text. From raw computer voltage and UTF-8 bytes through BPE tokenization, vocabulary building, and finally turning tokens into high-dimensional embedding vectors, we reveal the complete process of how GPT 'reads'.

- Source: https://www.waytoagi.com/en/kb/learning-path/gpt-tokenizer-bpe-from-scratch
- Original (Feishu, Chinese): https://waytoagi.feishu.cn/wiki/E0PSw6xdOiBuzykGSiWcrdxQnch
- Updated: 2026-11-10

---

> 🔗 Original Link: [https://mp.weixin.qq.com/s/8eF9KxVz...](https://mp.weixin.qq.com/s/8eF9KxVzJ_EP8WcxXJFcWA)

Original Author: Vice Minister of the Ministry of Personnel is Me  
Cloud Scribe  January 3, 2026 09:00  Beijing

![](https://www.waytoagi.com/api/kb-media/Y9P2b1V4Popo7SxDjLMcYPL9nRh)

Many articles about tokenizers jump straight to “words” and “subwords.” But if you truly want to understand how models like GPT “read and understand text,” we need to start from the very beginning.

**It is worth emphasizing that the fundamental principles this article will unpack are not only the magic behind GPT — they are also the common foundation that powers all top-tier large language models such as Qwen, Doubao, DeepSeek, Kimi, and many others.**

In this article, I want to fully document:

> **How text is transformed step by step from electrical signals into a sequence of integer IDs that the model can accept as input, and then, after the model generates its output, how those IDs are converted back into human-readable text.**

![](https://www.waytoagi.com/api/kb-media/GiF8bJI8oopPHWxzMKPcLOHznAb)

How Tokens Are Born


## **The Road to Tokens: Starting from 0–256**

Before any language model or tokenizer ever existed, there was one inescapable truth:

> **Computers do not understand “characters”; they only understand high and low voltage levels.**

These voltage states are logically abstracted into **0s and 1s (bits)**.

However, it is important to stress:

> **Language models do not process data at the bit level.**

Bits are too low-level, the sequences are far too long, and different encoding schemes produce completely different bit sequences.

Before any text reaches the tokenizer, it is first passed through **UTF-8 encoding**:

![](https://www.waytoagi.com/api/kb-media/OsQmbw7xaoWczGxUH0rcx8hCnkd)

UTF-8

For example, the string `我爱中国 china` is stored and processed inside the computer as a sequence of **bytes** (a byte is the underlying 8-bit binary number consisting of 0s and 1s):

![](https://www.waytoagi.com/api/kb-media/JMOAbXPogoRm0UxC0PocbI8Pnze)

UTF-8 encoding

At this stage, we have already generated tokens — except each token is simply an 8-bit binary number.  
If we fed this long string of 0101… directly into the model, the sequence length would explode (potentially billions of tokens), and the computational cost would be catastrophic.

So we need to compress this sequence.

We know that:

- Mathematically, 8 binary bits can represent 2⁸ = 256 possible states.
- These range from `00000000` (0) to `11111111` (255).

Therefore, we can convert `我爱中国 china` as follows:

144 bits (18×8) → 18 integer numbers

![](https://www.waytoagi.com/api/kb-media/OiJGbRSV4ozuTixzzyqceEAxnOe)

Byte compression ratio

Of course, 18 integers is still somewhat long, so we need to reduce the length further.

This is where a very practical — yet extremely important — idea emerges:

> **Can we compress “byte combinations that frequently appear together” into a single new token?**

This is exactly the starting point of **Byte-Pair Encoding (BPE)**.

![](https://www.waytoagi.com/api/kb-media/JOvRbA7Kpoi41nx4jiYcKONnnxg)

The beginning of BPE

## **How Does the BPE Algorithm Step-by-Step “Merge” Tokens?**

From what we just learned, we know the initial state: the vocabulary contains only 256 tokens.

Everything starts from the simplest state:

**Initial Vocabulary**:

```
{0, 1, 2, ..., 255}
```

Each token corresponds to a byte value.

The training corpus is represented as sequences like this:

```
[..., 230, 136, 145, 231, 136, 177, 228, 184, 173, 229, 155, 189, 32, 99, 104, 105, 110, 97, ...]
```

![](https://www.waytoagi.com/api/kb-media/JsZFbdendoEFEMxd07cc9V8fnbg)

BPE Initial State

---

BPE counts across the **entire training corpus**:

> Which **adjacent token pairs** appear most frequently?

Suppose it finds that:

```
(230,136,145)
```

frequently appears together in a large amount of Chinese text (this is a common UTF-8 prefix for many Chinese characters).

Then:

1. It decides to add a new token ID: `256`
2. It defines a rule:

```
256 = (230,136,145)
```

3. It then replaces all occurrences of this pair across the entire dataset.

At this point:

> **Token `257` now fully represents the Chinese character “我” (I/me)**

![](https://www.waytoagi.com/api/kb-media/Ar0IbLdM8oCqmoxCRakcdgjcnpe)

Merging

The same process happens for:

- “爱” (love)
- “中国” (China)
- Even the entire phrase “中国”
- And the English word `china`

![](https://www.waytoagi.com/api/kb-media/VJBGbcLyoo8eqkxP8iJc2hcJnzh)

Final Token Table

Eventually, you might get results like this:

| Token ID | Corresponding Text |
| --- | --- |
| 257 | 我 |
| 301 | 爱 |
| 812 | 中国 |
| 2048 | china |

Thus, the sentence `我爱中国 china` may ultimately be represented as:

```
[257, 301, 812, 2048]
```

> What used to be more than a dozen bytes is now reduced to **just 4 tokens**.


## **When Does BPE Stop Merging?**

This process repeats continuously:

- Bytes → Characters
- Characters → Common Words
- Words → Common Phrases

Until:

> **The vocabulary size reaches the preset limit**

For example, 30k, 50k, 100k, or similar scales.


## **Finally, We Build the Vocabulary**

After BPE training is complete, we obtain a **fixed vocabulary**:

> **It is essentially a mapping between tokens and text fragments**

- `0 – 255`: Base byte tokens (always present, used as fallback)
- `256 – N`: Subwords, words, and phrases learned through BPE

![](https://www.waytoagi.com/api/kb-media/Qlnpb7pjzoB90KxPu99c2mP4ndc)

Final Vocabulary

This vocabulary is then used during both the model’s training phase and inference phase. You can think of it as a dictionary — mapping characters to pinyin, and pinyin back to specific characters.

![](https://www.waytoagi.com/api/kb-media/YUbebby3roENBUx1SJjcmnWhnnd)

Mapping

Of course, the above is only a high-level explanation of the tokenization process. Many details were omitted.

For example, before applying BPE during actual training, the original text sequence may undergo various regularization and cleaning steps. This largely depends on the final training objective. If you are training a coding model, then **four spaces must be treated as a single dedicated token**, so you cannot remove that formatting during the cleaning stage.

So what happens next? What comes after this? Many of you probably already have some idea: LLMs are all about modeling probabilities. But where do these probabilities come from?

**Embedding (Vectorization)**

We just discussed how a mapping table between tokens and text is created. Next, we need to build a static weight matrix — a two-dimensional matrix with a shape roughly like this: `[vocabulary size, feature dimension]`.

> **Rows**: Correspond to the vocabulary size (e.g., GPT-4 has approximately 100,277 rows). These rows match the number of tokens in the vocabulary we just mentioned — each row represents one unique token.  
> **Columns**: Correspond to the model’s feature dimension (e.g., Llama 3 uses 4,096 columns). This number is chosen arbitrarily. Essentially, it answers the question: “How many dimensions do I need to describe this token?”

Take “apple” as an example. It’s a single token, but it can be described across many dimensions: size, color, variety, growing region, whether it’s a fruit or a phone, and so on.

**Content**: Each row stores the **feature vector** corresponding to a specific Token ID.

- Row 0: Stores the 4,096 floating-point numbers for Token `0`.
- Row 257: Stores the 4,096 floating-point numbers for Token `257` (for example, the Chinese character “幻”).

![](https://www.waytoagi.com/api/kb-media/Y1EFbxKc2oVHhExqqM4cw7vFn8f)

**Weight Matrix**

At this point, you might say, “I understand what this matrix does, but what are its initial values?”  
**The answer is: they are completely randomly generated.** A random function is called to initialize the matrix. During subsequent training, what we call “training” is, in my understanding, the process of continuously updating the values in this weight matrix so that they become closer to reality. Once pre-training is complete, these parameters are fixed.

When training a real large language model, the input is constructed as a high-dimensional matrix (or tensor). Don’t be intimidated by the term “tensor” — it can be understood very simply:

Take the sentence `我爱中国 china`. Suppose it consists of 4 tokens. Each token is retrieved as a vector of [4,096 feature values]. When we feed it to the model, it becomes the following matrix:

```
[ 我   [4096 feature values],
  爱   [4096 feature values],
  中国 [4096 feature values],
  china [4096 feature values] ]
```

It still feels abstract? Let’s look at the diagram:

![](https://www.waytoagi.com/api/kb-media/EIwHbNKpPokCEXxvKHYcbEgGnIb)

**Tensor**

Of course, this is just a single sentence. In practice, we don’t train the model one sentence at a time. If we input 32 sentences at once, the input matrix becomes a 3-dimensional tensor — essentially a 3D cube:

![](https://www.waytoagi.com/api/kb-media/IcWabSBenosBA0x9H88cS9Amnuc)

**3D Tensor**

Even at this stage, we’re not done yet. The model still doesn’t know the *position* of each token. `我爱中国` and `中国爱我` have completely different meanings. If their token weights in the matrix are identical, their weighted sums would also be identical, which would cause huge confusion during model training.

![](https://www.waytoagi.com/api/kb-media/FSxrbtR6Go78tCxh5tkcFGt7nyd)

**Why Do We Need Positional Encoding?**

This is why we must give each token positional information. It’s like two officials both holding the title “Vice Minister of the Ministry of Personnel.” We give the first one an ID card from Chaoyang District and the second one from Tongzhou District.

![](https://www.waytoagi.com/api/kb-media/AFOjbKKMloXmcWxCxhVct9LenSa)

**Vice Minister of Personnel**

However, the most advanced large models today (such as DeepSeek) believe that simply issuing fixed ID cards isn’t smart enough. They use a more ingenious method: **instead of giving a fixed address, they give each token a “compass.”**

Here’s how it works:

- You’re the 1st token? Rotate the pointer in your vector **1 degree** to the left.
- You’re the 2nd token? Rotate the pointer in your vector **2 degrees** to the left.
- You’re the 100th token? Rotate it **100 degrees**.

![](https://www.waytoagi.com/api/kb-media/PINFbNfG6oJWm4xA6I8cVNoLn9c)

**Angle 2**

**Why go through all this trouble of “rotating”?**  
Because the model doesn’t actually care about your absolute position (whether you live in Beijing or Shanghai). It only cares about **relative position** — how close two tokens are to each other.

Through rotation, something magical happens: no matter where these two tokens appear in a long document, as long as they remain adjacent, the **angle difference between their pointers is always exactly 1 degree**. The model only needs to measure the angle and instantly understands: “Oh, these two belong together!”

![](https://www.waytoagi.com/api/kb-media/P9IJbeetOoSClnxh8facj0pxnNc)

**Angle**

Congratulations! You have now grasped the core magic behind today’s dominant large language models — **RoPE (Rotary Position Embedding)**.

At this point, you should have a clear basic understanding of tokens and embeddings in LLM pre-training.

You can also watch the video for further explanation.

Finally, feel free to follow us.

#AI #AIGC #GPT #LargeLanguageModels #Tokenizer #Embedding #RoPE #ArtificialIntelligence
