What Is a Token in Large Language Models? Principles and Calculation Explained

    Token is the smallest unit of text that large language models process. This article explains what tokens are, why models use subword tokenization instead of words or characters, how tokens flow through a model, how token counts differ across languages, and how tokens relate to context windows and API costs—plus practical tips for developers to estimate and optimize token usage.

    What Is a Token in Large Language Models? Principles and Calculation Explained

    Whenever people talk about AI—especially conversational large language models—one word keeps coming up: token. Some say “this model supports a 128K token context,” others say “it’s priced per million tokens,” and still others note that “models bill you by the token.” So what exactly is a token? Why don’t LLMs just process text by character or by word? This article breaks the concept down from principle to practice.

    1. The Essence of a Token: “Text Fragments” from the Model’s Perspective

    Computers cannot understand text directly—they can only process numbers. Before any text is fed into a model, it must first be converted into a sequence of numbers. The “intermediate unit” that makes this conversion possible is the token.

    Simply put, a token is the smallest unit of text that an LLM processes. It might be a complete word, part of a word, a punctuation mark, or even a space. The model doesn’t “see” the raw text we write—it sees a sequence of token IDs.

    Here’s an intuitive example. The English sentence:

    1. I love machine learning

    might be split into tokens like this:

    1. ["I", " love", " machine", " learning"] → 4 tokens

    For Chinese text:

    1. 我爱机器学习 (I love machine learning)

    it might be split into:

    1. ["我", "爱", "机器", "学习"] → 4 tokens

    or alternatively:

    1. ["我", "爱", "机", "器", "学", "习"] → 6 tokens

    How the text is actually split depends on the tokenizer strategy used when the model was trained.

    2. Why Not Split by Word or by Character?

    A natural question arises: why not use whole words or individual characters as the unit? This comes down to the flaws of two extreme approaches.

    2.1 Problems with Word-Level Splitting

    If complete words were the unit, the model’s vocabulary would become enormous. New words appear constantly in natural language—names of people, products, proper nouns, misspellings… No vocabulary, however large, can cover them all. Any unseen word becomes unprocessable—the well-known “out-of-vocabulary (OOV)” problem.

    2.2 Problems with Character-Level Splitting

    If text were split down to individual characters or even bytes, the vocabulary would be tiny and no unknown words would exist. However, a long sentence would turn into an extremely long token sequence. The model would have to process much longer sequences, computation costs would rise sharply, and since a single character carries very weak semantic meaning, learning long-range dependencies would become harder.

    2.3 The Compromise: Subword Tokenization

    The industry standard is a middle ground: subword tokenization. Common algorithmic approaches include BPE (Byte Pair Encoding), WordPiece, and SentencePiece, which share similar core logic:

    • Count the co-occurrence frequency of character pairs across large corpora;
    • Merge frequently occurring character combinations into standalone tokens;
    • Keep common words as complete tokens, while rare words are broken into multiple subword fragments.

    For example, the English word “unhappiness” might be split into:

    1. ["un", "happi", "ness"] → 3 tokens

    This keeps the vocabulary size manageable (typically tens of thousands to over a hundred thousand tokens) while still being able to represent any text—including emojis, code symbols, and less common languages.

    3. How Tokens Flow Through the Model

    Once you understand tokenization, let’s look at the complete lifecycle of a token inside a model:

    1. Tokenization: Raw text is split into a token sequence by the tokenizer;
    2. Mapping to IDs: Each token corresponds to a unique integer ID in the vocabulary;
    3. Conversion to vectors: IDs are converted into high-dimensional vectors via an embedding lookup table—for example, a 768- or 4096-dimensional array of floating-point numbers;
    4. Model computation: The vector sequence passes through multiple Transformer layers, undergoing attention computation and feature transformation;
    5. Output prediction: Based on all preceding tokens, the model predicts the probability distribution of the next token, samples from it, and generates a new token;
    6. Detokenization: The generated tokens are converted back into human-readable text through the tokenizer’s reverse process.

    One key insight: LLMs generate text token by token. The model outputs only one token at a time; the fluency you perceive comes from high-speed, continuous prediction. This also explains why model responses appear character by character.

    4. Token Efficiency Differences Across Languages

    Token count and text length don’t correspond linearly, and differences across languages are significant:

    • English: On average, 1 token ≈ 0.75 words; in other words, 1 word ≈ 1.3 tokens;
    • Chinese: Under common tokenization granularity, 1 Chinese character typically consumes 1–2 tokens, depending on how well the tokenizer is optimized for Chinese;
    • Code and special symbols: Indentation, quotes, and brackets in code are often split into multiple tokens, leading to higher token consumption.

    A rough rule-of-thumb estimate:

    1. 1 token ≈ 4 English characters ≈ 0.5–1 Chinese character
    2. 1000 tokens ≈ 750 English words ≈ 500–1000 Chinese characters

    In development, if you need precise counting, you can use the tokenizer tool corresponding to your model to count locally. The general idea looks like this:

    1. # Pseudocode: use the target model's tokenizer to count tokens
    2. encoding = tokenizer.encode("你好,世界")
    3. print(len(encoding)) # Output the token count

    Note: tokenizers are not interchangeable between models. The same text may produce different token counts under different models, so always base your estimates on the tokenizer of the model you actually use.

    5. How Tokens Relate to Context Windows and Costs

    The practical significance of understanding tokens shows up mainly in two areas.

    5.1 Context Window Limits

    The amount of content a model can “remember” at once is measured in tokens—this is the context window. For example, a model supporting 8K tokens means the total token count of input text plus model output cannot exceed 8,192. When building long-document Q&A or multi-turn conversation applications, developers must manage their token budget carefully:

    • How much does the system prompt occupy;
    • How much conversation history to retain;
    • How much space retrieved reference materials take up;
    • How much room to reserve for the model’s response.

    Common strategies for handling window overflow include summarizing and compressing conversation history, keeping only the most recent turns, and segmenting long documents for retrieval.

    5.2 Billing Based on Usage

    LLM services are typically billed by token, with input and output priced separately—output is generally more expensive per token than input. Therefore, optimizing prompt length, avoiding irrelevant content in the context, and controlling output length are all direct ways to reduce costs.

    6. Practical Recommendations for Developers

    1. Use tokenizer tools for precise estimation: Before launching your application, use the target model’s tokenizer to measure token consumption of typical requests and plan your budget accordingly;
    2. Keep system prompts concise: Every sentence in a prompt is billed repeatedly with each request—redundant descriptions amplify costs;
    3. Manage conversation history wisely: In multi-turn applications, trim or summarize history to prevent unbounded context growth;
    4. Watch out for token amplification in Chinese and code: Content that “doesn’t look long” may consume far more tokens than expected;
    5. Pay attention to both input and output costs: Getting the model to “stop rambling” improves user experience and saves money at the same time.

    Conclusion

    At its core, a token is the bridge between large language models and text: it is the smallest unit of text segmentation and the unified measure for model computation, length limits, and cost billing. Understanding tokens not only helps you make sense of model specifications, but also enables you to precisely control context, optimize prompts, and manage API costs in real development. Mastering tokens is, arguably, the first lesson in mastering LLM application development.