liveBook · Manning

Token

As explained in The AI Pocket Book

A discrete text unit fed to a language model as input, which may correspond to a subword or word rather than an individual letter.

A token is a discrete piece of text that a language model can read or generate. It may be a common word, a word fragment, a character, a symbol, or a special control code. For example, “hungryish” can be assembled from “hungry” and “ish,” even when the complete word is absent from the vocabulary. section 1.8 section 1.9

The token pipeline works in a few practical steps:

  • A tokenizer subdivides raw text into valid vocabulary items, roughly favoring the largest available pieces. section 1.9
  • The wrapper converts those tokens into integer IDs representing their positions in the model’s vocabulary. section 1.9
  • The language model predicts a likely next token, and the wrapper repeatedly feeds that output back to generate a complete response. section 1.9 section 1.34
  • The vocabulary balances useful word-sized tokens against smaller pieces; making it contain every word and derivative would make it unnecessarily huge. section 1.12

One prompt, several tokens

The prompt “The dog’s bark was barely” becomes “The | dog | ’s | bark | was | barely,” then the model may append “audible.” It continues predicting tokens until it produces a special end-of-text token. section 1.9

We| the| People| of| the| United| States|,| in| Order| to| form| a| more| perfect| Union|,| establish| Justice|,| insure| domestic| Tran|qu|ility|,| provide| for| the| common| defense|,| promote| the| general| Welfare|,| and| secure| the| Bless|ings| of| Liberty| to| ourselves| and| our| Poster|ity|,| do| ord|ain| and| establish| this| Constitution| for| the| United| States| of| America|.

Compare English and French tokenization directly: familiar English words often remain intact, while French words are split into less meaningful pieces. section 1.11

Tokens are also a practical cost and fairness issue. APIs commonly charge for input and output tokens, and retaining a longer chat history increases the input cost. English-optimized vocabularies may split French or non-Latin text into many more tokens, making those prompts more expensive and sometimes harder to contextualize. section 1.10 section 1.11

How The AI Pocket Book covers it

How deep it goes
Defines vocabulary items and traces tokenization, integer IDs, prediction, and stopping, while deferring detailed vocabulary construction.
Examples
Uses dog prompts, hungryish, English and French constitutional text, Telugu, API bills, and end-of-text markers.
What you can do after
Evaluate tokenized prompts, estimate token-related costs, and understand why models generate text piece by piece.
How it connects
Connects tokens to vocabulary, tokenization, language models, embeddings, transformer architecture, and machine learning.

Also explained in

Compare how 31 books explain token →

Related concepts

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