liveBook · Manning

Tokenization

As explained in Build AI-Enhanced Web Apps

Tokenization is the conversion of text or other input into a sequence of tokens or numerical token identifiers.

Tokenization converts natural-language instructions, images, or other input into token sequences and numerical token IDs that an LLM can process, then translates numerical output back into human-readable text or media. Prompts therefore pass through an intermediary numerical representation rather than reaching the model as raw language. section 5.1.1

A practical tokenization pipeline connects user input to model output:

  • Human input supplies the raw prompt or other media. section 5.1.1
  • Tokenization splits the input and maps pieces to IDs from a predefined vocabulary or tokenizer model. section 5.1.1
  • The LLM processes numerical inputs and produces numerical outputs that become text or other human-readable media. section 5.1.1
  • Count both prompt and generated tokens when estimating service usage, because many LLM services bill on processed tokens. section 5.1.1

Counting prompt tokens with JavaScript

Install and use `@dqbd/tiktoken` to encode text for a selected model: `const enc = encoding_for_model("gpt-3.5-turbo"); const tokens = enc.encode(text); enc.free(); return tokens.length;`. Count the prompt and system message separately, then add their lengths to estimate the request's input-token total. section 5.1.1

Figure 5.2 The end-to-end process of how human input is transformed, processed by an LLM and converted back into human-readable output

Trace the complete transformation from human input through token IDs and LLM processing to human-readable output. section 5.1.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains the input-output pipeline, numerical IDs, billing, and JavaScript token counting, but stops short of tokenizer internals.
Examples
Uses a translation prompt, a system message, token-counting code, and an end-to-end processing diagram.
What you can do after
Apply it via @dqbd/tiktoken to count model-specific prompt and system-message tokens.
How it connects
Connects prompts, LLMs, token IDs, predefined vocabularies, numerical representations, and billing.

Also explained in

Compare how 25 books explain tokenization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.