What Are Tokens in Large Language Models? A Plain-English Guide to How LLMs Process Text

    Tokens are the basic units of text that large language models use to process and generate language. This article explains what tokens are, why models use subword tokenization instead of raw words or characters, how byte pair encoding (BPE) works, and how tokenization affects context windows and API pricing. It also covers practical tips for estimating token usage and optimizing costs in real-world LLM applications.

    What Is a Token?

    A token is the smallest unit of text that a large language model processes. Rather than reading text character by character or word by word, the model first splits the input into a sequence of discrete fragments. Each fragment corresponds to an ID in the model’s vocabulary and is converted into a vector for computation.

    A token can be a full word, half a word, a single character, a punctuation mark, or a common combination of characters. Think of tokens as the building blocks the model uses to assemble text. The exact boundaries between tokens depend on the tokenization scheme. Different models use different vocabularies and rules, so the same sentence may produce different token counts in different models.

    Why Not Simply Split by Characters or Words?

    Splitting text by individual characters keeps the vocabulary small but creates very long sequences. Longer sequences increase computational cost, and a single character often fails to capture the meaning of fixed multi-character expressions.

    Splitting by whole words preserves semantic clarity but bloats the vocabulary. It also leads to out-of-vocabulary (OOV) problems: new words, typos, and proper nouns may not have a corresponding entry.

    Subword tokenization sits between these two approaches. Common words remain intact as single units, while rare words are broken into shorter subword pieces. This balances vocabulary size with the ability to represent arbitrary text. Most modern large language models use this approach.

    How Byte Pair Encoding Works

    Byte Pair Encoding (BPE) is a widely used subword tokenization algorithm. Its training process works as follows:

    1. Start with a vocabulary of individual characters.
    2. Count how often adjacent fragments co-occur in the training corpus.
    3. Merge the most frequent pair into a new fragment and add it to the vocabulary.
    4. Repeat steps 2 and 3 until the vocabulary reaches a preset size.

    At inference time, the system tokenizes new text by applying the merge rules learned during training. High-frequency words typically take up a single token, while low-frequency words get split into multiple tokens.

    Understanding Tokenization with a Simple Example

    The following code demonstrates a longest-match tokenization approach. Real tokenizers use more complex rules and much larger vocabularies (typically tens of thousands to hundreds of thousands of entries), but this example illustrates the core idea.

    1. def toy_tokenize(text, vocab):
    2. """Tokenize text using longest-match. For illustration only."""
    3. tokens = []
    4. i = 0
    5. while i < len(text):
    6. matched = None
    7. # Try the longest candidate fragment first
    8. for j in range(len(text), i, -1):
    9. if text[i:j] in vocab:
    10. matched = text[i:j]
    11. break
    12. if matched is None:
    13. matched = text[i] # Fall back to a single character
    14. tokens.append(matched)
    15. i += len(matched)
    16. return tokens
    17. vocab = {"大语言", "模型", "中的", "Token", "是", "什么"}
    18. print(toy_tokenize("大语言模型中的Token是什么", vocab))
    19. # ['大语言', '模型', '中的', 'Token', '是', '什么']

    The output shows the sentence split into six fragments. The result depends entirely on the vocabulary. If a word is missing from the vocabulary, it will be split into finer pieces and consume more tokens.

    Estimating Token Counts from Text Length

    The following rules of thumb can help you estimate token usage. Exact values vary by tokenization scheme, so treat these as rough approximations.

    • In English text, 1 token roughly equals 3 to 4 characters, or about 0.75 words.
    • In Chinese text, 1 character roughly equals 1 to 2 tokens.
    • Punctuation, numbers, code, and special symbols tend to have higher token density, consuming more tokens for the same length of content.

    These estimates only give an order of magnitude. For precise counts, use the tokenizer that corresponds to the specific model you are working with.

    Why Context Windows Are Measured in Tokens

    A model can only process a limited amount of input and output in a single pass. This limit is called the context window, and it is measured in tokens. The window includes the system prompt, conversation history, user input, and the model’s generated response.

    Some models now support context windows of up to one million tokens, which roughly corresponds to hundreds of thousands of characters of text in a single pass. For exact capabilities and limitations, check the model’s official documentation.

    Two caveats apply. First, longer windows generally increase computational cost and latency; a large window does not mean you should fill it every time. Second, when content grows longer, the model may find it harder to focus on critical information, so it is best to include only task-relevant material.

    How Token-Based Pricing Works

    Many LLM services charge based on token usage, typically metering input and output separately and often at different rates. Prices are usually quoted per million tokens.

    The basic cost formula is:

    1. cost = input tokens ÷ 1,000,000 × input price per million
    2. + output tokens ÷ 1,000,000 × output price per million

    For example, if the input price is a and the output price is b (both per million tokens), a call that consumes 10,000 input tokens and 2,000 output tokens costs (10000 ÷ 1,000,000) × a + (2000 ÷ 1,000,000) × b. Specific pricing, caching rules, and discount structures vary by provider and plan, so always refer to the service’s official pricing page.

    Practical Engineering Recommendations

    1. Estimate before calling: Estimate input tokens before submitting a request to avoid exceeding the context window or incurring unexpected cost.
    2. Manage conversation history: In multi-turn interactions, periodically compress or truncate older messages and keep only essential context.
    3. Keep prompts concise: Remove repetitive descriptions and turn fixed instructions into stable templates to reduce per-call overhead.
    4. Track usage: Log input and output token counts for each call to analyze cost patterns and detect anomalies.
    5. Measure with real data: Token density differs significantly across Chinese, English, and code. Budget and capacity planning should be based on actual business data rather than generic estimates.

    Understanding tokens means understanding how the model sees text. Once you grasp how text is split, how usage is measured, and how billing works, you can more accurately evaluate model performance, control costs, and build more stable LLM applications.