Guide
How LLM tokenization works
By Logappradeep · ServiceNow developer
Large language models never see raw characters or words. Before a prompt reaches the model, a tokenizer splits it into tokens: integer IDs that each stand for a chunk of text. Context-window limits, API pricing, and maximum output lengths are all measured in tokens, so knowing the count before you send a request helps you control cost and avoid truncated answers.
Byte pair encoding in brief
OpenAI models use byte pair encoding (BPE). Training starts from raw bytes and repeatedly merges the most frequent adjacent pairs into new vocabulary entries until the vocabulary reaches a target size. Common English words end up as a single token, usually including their leading space, while rare words, long identifiers, and many non-Latin scripts are split into several pieces. A word like tokenization may become token + ization, and a long hexadecimal hash can cost a token for every two or three characters.
This counter implements the encodings OpenAI publishes through its tiktoken library:
- o200k_base: GPT-4o, GPT-4o mini, o1 and o3-mini, with a vocabulary of roughly 200,000 entries.
- cl100k_base: GPT-4, GPT-3.5 Turbo and OpenAI's embedding models.
- p50k_base, r50k_base and gpt2: older Codex, davinci and GPT-2 era models.
Reading the numbers
The chars per token ratio is a quick health check. Ordinary English prose tends to land around four characters per token. Source code, JSON, minified data, and languages such as Hindi, Tamil, or Japanese usually produce lower ratios, which means more tokens for the same amount of text. The visual breakdown colors each token separately and shows its ID on hover, making expensive patterns easy to spot: long runs of indentation, repeated separators, or Base64 blobs pasted into a prompt.
Gotchas when budgeting a prompt
- Chat formatting adds overhead. The API wraps every message with role markers and separators, so a conversation costs a few more tokens per message than its raw text.
- Other vendors count differently. Claude, Gemini, Llama, and Mistral ship their own tokenizers. Numbers from this page are a reasonable estimate for those models, not an exact figure.
- Whitespace and case matter.
Hello,HelloandHELLOcan map to different tokens, so reformatting text changes its count. - The response needs room too. The context window has to hold the prompt plus the output you expect, and output tokens are typically billed at a higher rate than input tokens.
Privacy
Tokenization runs entirely in your browser using a JavaScript port of tiktoken, so your prompt is never sent to a server or to any AI provider. Inputs up to 2,000 characters are remembered in your browser's local storage so they survive a refresh; clearing the box removes the saved text.