Foundations

Token Intelligence

Language models do not read characters or words. They read tokens: frequent character sequences from a fixed vocabulary, each with a numeric ID. Everything you pay for and every limit you hit is counted in tokens.

What Is a Token in AI?0:51 · beginner

What is a token?Beginner

A token is an entry in the model’s vocabulary. Common words are often one token (the, hello), rarer words split into pieces (token + ization), and spaces usually attach to the following word. In English text a token averages about four characters, but that ratio changes with language, code, and formatting.

Why models use tokensBeginner

A vocabulary of characters would make sequences very long; a vocabulary of whole words could not handle new words, typos, or code. Subword tokens are the compromise: a vocabulary of roughly 50,000 to 200,000 pieces that can spell anything while keeping common text short.

Tokens, words, and charactersBeginner

The same sentence has different lengths depending on the unit. Try it in the lab below: paste a sentence and compare characters, words, and tokens.

UnitUsed for
CharactersStorage, UI limits
WordsReading time, human estimates
TokensModel input, context limits, API billing

Token IDsBeginner

The model never sees text. The tokenizer maps each piece to an integer ID, and the model works on the sequence of IDs. Decoding maps IDs back to text. The lab shows the real IDs for the o200k_base and cl100k_base tokenizers.

Tokenization algorithmsIntermediate

Most modern tokenizers learn their vocabulary from data.

  • Byte Pair Encoding (BPE): start from bytes or characters and repeatedly merge the most frequent adjacent pair into a new token. GPT models use byte-level BPE (tiktoken), which can encode any byte sequence.
  • WordPiece: similar merging, but chooses merges that most increase the likelihood of the training data. Used by BERT; continuation pieces are marked with ##.
  • SentencePiece: a library that treats text as a raw stream (spaces included, often shown as ▁) and trains BPE or unigram vocabularies. Used by T5 and earlier Llama models.
  • Unigram: starts from a large vocabulary and removes pieces that contribute least, keeping a probabilistic segmentation.

Byte-level tokens and other languagesIntermediate

Byte-level BPE guarantees any text can be encoded, but scripts that are rare in the training data get fewer merges. A Tamil or Hindi sentence often needs several times more tokens than its English translation, and one character can even span more than one token. The lab groups such byte fragments so you can see them.

Special tokensIntermediate

Tokenizers reserve IDs for control markers such as end of text (<|endoftext|>) or chat role boundaries. Chat APIs insert these around your messages, which is why a request uses a few more tokens than the visible text.

JSON, Markdown, and codeIntermediate

Punctuation, indentation, and repeated keys all cost tokens. Pretty-printed JSON can use far more tokens than compact JSON, and uniform arrays of objects can often be written more compactly as TOON (one header, then rows) without losing data. The lab compares the three.

Input, output, cached, and reasoning tokensIntermediate

Providers bill input (prompt) and output (completion) tokens separately, and output is usually several times more expensive per token. Repeated prompt prefixes can be served from a prompt cache at a discount. Models that reason before answering produce reasoning tokens that are billed as output even when they are not returned. Images and audio are converted into tokens too, by provider-specific rules.

cost = input_tokens × input_price / 1,000,000
     + cached_tokens × cached_price / 1,000,000
     + output_tokens × output_price / 1,000,000

Context windows and limitsIntermediate

A model’s context window is the maximum number of tokens it can attend to at once: your instructions, history, documents, tool definitions, and the answer it is generating. Many models also cap output tokens separately.

Why models count differentlyAdvanced

Each model family trains its own tokenizer with its own vocabulary size, so the same text produces different counts. Only a model’s own tokenizer gives an exact count. PromptFlowEngine runs the OpenAI tokenizers exactly and uses them as a labelled estimate for other families.

Reducing token usage without losing qualityAdvanced

Fewer tokens are only better if the answer stays as good. Measure quality alongside savings.

  • Remove filler and duplicated instructions
  • Simplify instructions without dropping requirements
  • Reuse templates so stable prefixes can be cached
  • Select only the context the task needs; compress the rest
  • Convert uniform JSON to TOON when the round trip is lossless
  • Cap and shape the output (length, format)
  • Route simple tasks to smaller, cheaper models

Key takeaways

  • Tokens are the unit of limits and billing.
  • Counts depend on the model’s tokenizer: exact only with that tokenizer.
  • Measure quality whenever you cut tokens.
Count and reduce tokens in the WorkspaceCompare JSON and TOON in the PromptOps Lab