liveBook · Manning

Tokenizer

As explained in Generative AI in Action

A tokenizer is a component that segments or encodes text into discrete units for processing by a language model.

A tokenizer converts text into discrete tokens that language models process. Token counts affect context quality, response coherence, cost, and performance, making tokenization an operational concern for enterprise applications. section 2.8.3

The tiktoken library implements byte-pair encoding (BPE), a reversible, lossless method that produces compact token sequences and generalizes recurring language patterns. Use the encoding associated with the target model; encoding_for_model() can select it when the encoding name is uncertain. section 2.8.3

Listing 2.1 Counting tokens for GPT
import tiktoken as tk

def count_tokens(string: str, encoding_name: str) -> int:
    # Get the encoding
    encoding = tk.get_encoding(encoding_name)  #1

    # Encode the string
    encoded_string = encoding.encode(string)

    # Count the number of tokens
    num_tokens = len(encoded_string)
    return num_tokens

# Define the input string
prompt = “I have a white dog named Champ”

# Display the number of tokens in the String
print(“Number of tokens:” , count_tokens(prompt, “cl100k_base”))

Count tokens by selecting an encoding, encoding the input string, and measuring the resulting sequence. The example uses cl100k_base with a short prompt. section 2.8.3

How Generative AI in Action covers it

How deep it goes
Explains token counting, BPE properties, and model-specific encodings, while stopping short of tokenizer training internals.
Examples
Uses tiktoken code to count tokens for a prompt and compare cl100k_base, p50k_base, and r50k_base encodings.
What it assumes
Assumes basic Python familiarity and introduces token counting through a concrete tiktoken API example.
What you can do after
Operate tokenization via tiktoken to count, encode, decode, and select model-compatible encodings.
How it connects
Connects tokenization with byte-pair encoding, prompt engineering, language models, pretraining, and fine-tuning.

Also explained in

Compare how 16 books explain tokenizer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.