liveBook · Manning

Token

As explained in AI Agents in Action, Second Edition

A token is a discrete unit of text represented numerically for processing by a language model.

A token is a word or word part represented by a numerical index in a language model’s vocabulary. Tokenization breaks text into these units; the model predicts possible next-token indices, and a sampling step selects one to generate. section 2.1.1 section 2.1.2

Text length does not reliably indicate token count. Punctuation, quotes, and field names in JSON can increase the count, affecting the cost of sending data to a model. section 2.1.2

JSON and regular text

A JSON block and equivalent regular text are shown as 13 and six tokens, respectively. Measure text with tiktoken or use token counts reported by the OpenAI Agents SDK; providers price input and output tokens separately. section 2.1.2 section 2.6

Figure 2.2 A comparison of tokenization in regular text and JSON

Compare how regular text and JSON are split into tokens. section 2.1.2

How AI Agents in Action, Second Edition covers it

How deep it goes
Defines text-to-index tokenization and connects next-token prediction to sampling.
Examples
Compares JSON at 13 tokens with equivalent regular text at six tokens.
What you can do after
Apply tiktoken or SDK telemetry to measure token counts and manage budgets and context windows.
How it connects
Links tokens to tokenization, model vocabularies, probability distributions, sampling, and prompting.

Also explained in

Compare how 31 books explain token →

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.