Sub topic 1
What a token is and why counts differ by model
Prerequisites
This chapter assumes you can read basic Python and have used an AI coding assistant. It also assumes you know a large language model (LLM) is a program that reads text and predicts more text. You don't need any other background.
What a token is
A token is the unit of text a model reads and writes. A tokenizer is the component that splits text into tokens before the model sees it. Each token maps to an integer ID from a fixed vocabulary. The model works on those IDs, not on characters or words.
A token can be a whole word, part of a word, a single punctuation mark, or a run of whitespace. Common words such as the are usually one token. Rare words, identifiers like getUserProfileById, and unusual strings are split into several pieces.
Why counts differ by model
Each model family ships its own tokenizer with its own vocabulary. The same text can therefore produce different counts on different models. A tokenizer with a larger vocabulary often packs the same text into fewer tokens. A model whose tokenizer saw little code or little non-English text during training may split those inputs into more pieces.
Three consequences matter for coding agents:
- Price comparisons are incomplete. A model with a lower price per token can still cost more if its tokenizer produces more tokens for your code.
- Limits are measured in tokens. The same file may fit in one model's context window and overflow another's.
- Content type changes the ratio. English prose, source code, JSON, and minified files tokenize at different rates. Code with long identifiers and symbols often needs more tokens per character than plain prose.
A rough estimate
A common rule of thumb for English text is about four characters per token. It is only a starting point. This function applies it:
def estimate_tokens(text):
"""Rough token estimate: about 4 characters per token."""
return max(1, round(len(text) / 4))
snippet = "def add(a, b):\n return a + b\n"
print(len(snippet)) # 32
print(estimate_tokens(snippet)) # 8Use estimates for planning only. For billing, the provider's reported counts are the source of truth.
Use the counts the provider reports
Each API response includes a usage object with the input and output token counts the provider actually charged. Many providers also offer a token-counting endpoint that returns a count before you send a request. You can use usage data to see how far your estimate is off:
def estimate_error(estimated, reported):
"""Fraction by which an estimate differs from the reported count."""
return (estimated - reported) / reported
# Placeholder numbers: estimated from characters vs. reported by the API.
print(estimate_error(estimated=8, reported=10)) # -0.2A negative result means you underestimated. If this error is consistently large for your codebase, scale your estimates or switch to the provider's counting tool.
Common errors
- Reusing one model's count for another. Counts from one tokenizer don't carry over to a different model family. Even versions within one family can differ.
- Counting words instead of tokens. A 100-word comment and 100 words of dense code won't have the same token count.
- Ignoring output tokens. The model's reply is tokenized and counted too, and later chapters show that output tokens are usually priced higher.
- Forgetting hidden text. System prompts, tool definitions, and formatting all count, even when you never type them.
Exercise
You compare two models for a refactoring task. Model A charges less per token, but you notice it reports 30% more input tokens than Model B for the same source files. Why can you not conclude that Model A is cheaper, and what should you do?
Show answer
Cost is token count times price per token, and the two models use different tokenizers. Model A's lower rate may be cancelled out by its higher count. Send the same files to both models, read the input token counts from each response's usage data, and compute the cost per request with each model's own rates before comparing.