← All posts

AI basics › How LLMs work

What is a token in AI? How LLMs read and write text

What a token is in an LLM, how tokenizers split text into subwords, why Korean often needs more tokens, and how tokens set context limits and prices.

If you use AI tools like ChatGPT or Claude for a while, the word “token” starts showing up more often than you’d expect. API price lists quote prices per million tokens, and new model announcements often mention how many tokens the context window holds. These days AI agents also work on their own for long stretches, and the tokens they use turn straight into cost, so the word comes up more than ever. Still, if someone asks what a token actually is, perhaps “something like a word” is the only answer that comes to mind.

In an LLM, a token is the unit the model reads and writes text in. Instead of reading characters directly, the model splits text into pieces called tokens and reads the sequence of numbers attached to those pieces. As the tiktoken README puts it, language models don’t see text the way we do; they see a sequence of numbers (tiktoken). Inside the model, those numbers are turned into vectors for the computation, and when the model writes an answer, it adds tokens one at a time. The tool that splits text into tokens is called a tokenizer. A token can be a whole word, but it can also be part of a word, a single character, or sometimes something even smaller than a character. OpenAI’s docs estimate that, for English text, one token is roughly 4 characters or 0.75 words (OpenAI key concepts).

What a sentence looks like as tokens

It’s hard to get a feel for this from a description alone, so let’s split an English sentence and a Korean sentence with the same meaning. OpenAI’s open-source library tiktoken lets you run the tokenizers its models use right in Python, and you install it with pip install tiktoken. The code below splits both sentences with the encoding GPT-4o uses, then prints the character, word, byte, and token counts along with the token pieces.

import tiktoken

enc = tiktoken.encoding_for_model("gpt-4o")
print(enc.name)

for text in ["The weather was nice today, so I went for a walk.",
             "오늘 날씨가 좋아서 산책을 나갔습니다."]:
    ids = enc.encode(text)
    pieces = [enc.decode_single_token_bytes(i) for i in ids]
    print(f"{len(text)} chars, {len(text.split())} words, "
          f"{len(text.encode('utf-8'))} bytes, {len(ids)} tokens")
    # a piece that is only part of a character prints as "�"
    print([p.decode("utf-8", errors="replace") for p in pieces])
o200k_base
49 chars, 11 words, 49 bytes, 13 tokens
['The', ' weather', ' was', ' nice', ' today', ',', ' so', ' I', ' went', ' for', ' a', ' walk', '.']
21 chars, 5 words, 53 bytes, 14 tokens
['오늘', ' 날', '씨', '가', ' 좋아', '서', ' 산', '책', '을', ' 나', '�', '�', '습니다', '.']

The first line shows that the encoding GPT-4o uses is called o200k_base. The English sentence became 13 tokens, and most words are one token each. Notice, though, that the space before a word is part of the token, as in ' weather'. The Korean sentence became 14 tokens. 오늘 (“today”) and 습니다 (a polite verb ending) are whole tokens, while 날씨가 (“the weather”) was split into three pieces. And 갔, a syllable inside 나갔습니다 (“went out”), didn’t even survive as one character; it printed as two ‘�’ marks. Once you see how the token vocabulary is built, these odd pieces start to make sense.

How the vocabulary is built with BPE

A tokenizer splits text using a token vocabulary prepared in advance. If the vocabulary were filled with whole words, it would struggle with words it has never seen, and if it were filled with single characters, every sentence would become very long. That’s why many LLMs use subwords, pieces in between that are often smaller than a word. In an ACL 2016 paper, Rico Sennrich, Barry Haddow, and Alexandra Birch proposed representing rare words as sequences of subword units, and they borrowed a compression algorithm called byte pair encoding (BPE) to do it.

The way BPE builds a vocabulary is simple. As Hugging Face’s LLM course explains, it starts with individual characters as the base vocabulary. It then finds the pair of pieces that most often appear side by side in the training text, merges them into a new token, and repeats until the vocabulary reaches a chosen size. If the training text contained only walk, walked, and walking, the pairs between w, a, l, and k would be the most common. Walk would then become one token first, and pieces like ed and ing would be merged after that. New text is split by applying these merge rules in the order they were learned. As a result, pieces that were common in the training text end up as large tokens, while rare ones stay broken into small pieces.

Some tokenizers, like GPT-2’s, start from bytes instead of characters, which is called byte-level BPE. There are only 256 possible bytes, and any character can be written as bytes, so a character missing from the vocabulary never stops the tokenizer. The two ‘�’ pieces in the output above are exactly this kind of byte piece.

How tokens differ from words and characters

Because of this, tokens don’t line up neatly with words or with characters. Splitting a few words and one character with the same encoding makes the difference easy to see. The b'...' values in the output are the bytes each token holds.

import tiktoken

enc = tiktoken.get_encoding("o200k_base")
for word in ["walk", "walked", "tokenization", "Hello", " Hello", "HELLO", "갔"]:
    ids = enc.encode(word)
    print(repr(word), ids, [enc.decode_single_token_bytes(i) for i in ids])
'walk' [26072] [b'walk']
'walked' [26072, 295] [b'walk', b'ed']
'tokenization' [10346, 2860] [b'token', b'ization']
'Hello' [13225] [b'Hello']
' Hello' [32949] [b' Hello']
'HELLO' [111642, 2699] [b'HEL', b'LO']
'갔' [2571, 242] [b'\xea\xb0', b'\x94']

walked became two tokens, walk and ed, and that walk has the same number, 26072, as walk on its own. tokenization was split into token and ization. Capital letters and spaces change the result too, so Hello and Hello with a space in front are different tokens, and HELLO became two. The last line, 갔, takes 3 bytes in UTF-8, and since o200k_base has no token for the whole character, it was split into a 2-byte piece and a 1-byte piece. Those are the two pieces that printed as ‘�’ in the first output, and put back together they become 갔 again.

Lining up the two sentences from the first example, unit by unit, gives the following.

Unit How it’s split English Korean
Characters One character each (spaces included) 49 21
Words By spaces 11 5
Bytes Size when stored as UTF-8 49 53
Tokens The tokenizer’s vocabulary (o200k_base) 13 14

A Korean syllable takes 3 bytes, so the Korean sentence has fewer than half as many characters as the English one, yet more bytes. The token count isn’t simply proportional to any of these. It depends on which tokenizer does the splitting.

Why Korean often takes more tokens

BPE merges the pieces that appear most often in its training text first. So if the text used to build a tokenizer was mostly English, English words easily become whole tokens, while Korean tends to stay split into syllables or bytes. This is why Korean often needs more tokens than English for the same meaning. The size of the gap, though, varies a lot from one tokenizer to another. Let’s count the same two sentences with three OpenAI encodings that ship with tiktoken and two tokenizers from small models published on Hugging Face. Qwen2.5-0.5B is described as supporting more than 29 languages, Korean among them, and HyperCLOVAX-SEED-Text-Instruct-0.5B is a model NAVER released with a strong focus on Korean.

import tiktoken
from transformers import AutoTokenizer

en = "The weather was nice today, so I went for a walk."
ko = "오늘 날씨가 좋아서 산책을 나갔습니다."
print(f"{'tokenizer':<23}{'vocab':>8}{'English':>9}{'Korean':>8}")

for name in ["gpt2", "cl100k_base", "o200k_base"]:
    enc = tiktoken.get_encoding(name)
    n = [len(enc.encode(s)) for s in (en, ko)]
    print(f"{name:<23}{enc.n_vocab:>8}{n[0]:>9}{n[1]:>8}")

for name, repo in [("Qwen2.5-0.5B", "Qwen/Qwen2.5-0.5B"),
                   ("HyperCLOVAX-SEED-0.5B",
                    "naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B")]:
    # downloads only the tokenizer files, not the model weights
    tok = AutoTokenizer.from_pretrained(repo)
    n = [len(tok(s, add_special_tokens=False)["input_ids"]) for s in (en, ko)]
    print(f"{name:<23}{len(tok):>8}{n[0]:>9}{n[1]:>8}")
tokenizer                 vocab  English  Korean
gpt2                      50257       13      46
cl100k_base              100277       13      25
o200k_base               200019       13      14
Qwen2.5-0.5B             151665       13      16
HyperCLOVAX-SEED-0.5B    110524       13      11

The English sentence came to 13 tokens with all five tokenizers, probably because common English words are single tokens in every one of these vocabularies. The Korean sentence, on the other hand, took 46 tokens with gpt2, then dropped to 25 with cl100k_base, which GPT-4 uses, and to 14 with o200k_base. With HyperCLOVAX-SEED it took 11, fewer than the English. What’s more, HyperCLOVAX-SEED’s vocabulary is only a little over half the size of o200k_base’s. It seems that what the vocabulary was filled with matters more than how big it is.

Of course, one sentence is too little to draw a conclusion from, so I also counted six sentence pairs. Korean took 3.5 to 7.2 times as many tokens as English with gpt2 and 1.0 to 1.9 times as many with o200k_base, and with HyperCLOVAX-SEED it took fewer than English in only two of the six pairs. A NeurIPS 2023 paper by Aleksandar Petrov and colleagues also reports that the same text translated into different languages can differ in token length by up to 15 times. The authors point out that this gap carries over into cost, processing time, and how much text the model can take as context.

Context windows and prices are counted in tokens

Tokens are worth understanding because both how much a model can handle at once and what it costs are measured in them. The context window is all the text a model can refer to when it writes a response, and according to Anthropic’s docs, that includes the response itself. Its size is given in tokens too. The model card for Qwen2.5-0.5B, used above, lists a context length of 32,768 tokens, and HyperCLOVAX-SEED’s lists 4K tokens. When I checked Claude’s docs on October 27, 2026, several models were listed with a 1M-token window. That means if the same content takes more tokens in one language, that much less of it fits in the window.

Prices work the same way. The API price lists from OpenAI, Anthropic, and Google all quote prices per million tokens and price input tokens and output tokens separately. In the three tables I looked at on October 27, 2026, output cost more per token than input for text models. Prices change often, so I won’t copy the numbers here.

How to count tokens

For a rough estimate, you can use OpenAI’s rule mentioned earlier and count about 4 characters per token. Google’s Gemini docs likewise put 100 tokens at roughly 60 to 80 English words. The English sentence from the first example, at 49 characters and 13 tokens, fits these rules well. The Korean sentence, though, was 21 characters and 14 tokens, so applying the same rule to Korean misses by a wide margin.

When you need an exact number, the official tools from each company are the safer choice. For OpenAI models, you can count with tiktoken yourself, or use POST /v1/responses/input_tokens, which returns the input token count before you send a request (token counting guide). According to that guide, though, local tokenizers like tiktoken have trouble counting requests with images, files, or tool definitions accurately. For Claude, POST /v1/messages/count_tokens counts the input tokens of a message (Token counting). According to its docs, it’s free to use but has a requests-per-minute limit, and the number it returns is an estimate. For Gemini, the docs linked above describe count_tokens.

The tokenizer can also change between models from the same company. According to Anthropic’s docs, which I checked on October 27, 2026, Claude Opus 4.7 introduced a newer tokenizer that produces roughly 30% more tokens for the same text than earlier models. That’s why the docs recommend recounting with the ID of the model you plan to use rather than reusing counts from an older one.

If you’re new to this, I’d suggest copying the first example as it is and running a prompt you use often, once in English and once in another language you use, such as Korean. Seeing for yourself how different the token counts are for the same meaning, and where each word gets cut, makes the numbers on price lists and context windows feel much more concrete. I ran these examples on October 27, 2026, on macOS with Python 3.13.12, tiktoken 0.14.0, and transformers 5.20.2.

AI tools helped with the research for this post.