liveBook · Manning

Tokenization

As explained in AI Agents in Action, Second Edition

Tokenization is the process of converting text into discrete tokens that a language model can process.

Tokenization converts text into discrete pieces—words, word parts, punctuation, or symbols—that a language model can process. The model maps each token to a numerical index in its vocabulary, predicts probabilities for the next index, and then generates the corresponding token. section 2.1.1 section 2.1.2

Use this mental model when estimating what an LLM will consume:

  • Text is not measured reliably by characters or words; token boundaries depend on the model’s vocabulary encoding. section 2.1.2
  • Formatting counts too: braces, quotation marks, field names, and other JSON overhead become tokens. section 2.1.2
  • Token counts affect context limits, uncertainty, and cost, with input and output tokens priced separately. section 2.1.2 section 2.6
  • Measure actual counts with tiktoken or Agents SDK telemetry instead of estimating from visible text length. section 2.1.2 section 2.6

Plain text versus JSON

Equivalent content can consume different numbers of tokens. One comparison tokenizes regular text into six tokens but JSON into thirteen because punctuation, quotes, braces, and field names are tokenized as well. JSON remains usable, but its structure adds token overhead. section 2.1.2

Figure 2.1 The training cycle and inference/generation process of a large language model. On the left is the model’s training process, in which documents are ingested to train the LLM in a first pass. On the right, a user enters a prompt, which is first tokenized and fed into the model. The model then outputs probabilities it uses to sample and produce the next token.

Trace how documents become training inputs and how a user prompt becomes tokenized input, next-token probabilities, and generated output. section 2.1.1

Figure 2.2 A comparison of tokenization in regular text and JSON

Compare how regular text and JSON are divided into tokens, highlighting why equivalent content can have different token costs. section 2.1.2

How AI Agents in Action, Second Edition covers it

How deep it goes
Explains token pieces, vocabulary indices, generation, JSON overhead, and measurement, but stops short of tokenizer algorithm internals.
Examples
Compares regular text with JSON and recommends measuring counts using tiktoken or Agents SDK telemetry.
What it assumes
Assumes only basic text familiarity and teaches from zero how language models represent text as numerical token indices.
What you can do after
Apply it via tiktoken or Agents SDK telemetry to measure tokens and manage budgets, context windows, and prompt overhead.
How it connects
Connects tokenization with large language models, vocabulary encoding, probability distributions, sampling, prompting, JSON, context windows, and telemetry.

Also explained in

Compare how 25 books explain tokenization →

Related concepts

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