Tokens Explained: What You Are Actually Billed For
A token is the unit a language model reads and writes. It is usually a piece of a word, a whole short word, a number fragment or a punctuation mark. APIs bill by input tokens (what you send) and output tokens (what the model generates), and context limits are also measured in tokens.
How text becomes tokens
Before the model sees your text, a tokenizer splits it into pieces from a fixed vocabulary. Common English words are often one token. Rare words, names, code identifiers and non-English text break into several. Whitespace and punctuation can also take their own tokens.
As a rough rule for English prose, one token is a bit under four characters, or about three quarters of a word. Treat that only as an estimate. Code, JSON with many symbols, and languages such as Japanese or Arabic often use more tokens per character. Each provider uses its own tokenizer, so the same text yields different counts on different models, and even between model generations from the same provider.
Why tokens matter to you
- Cost: price lists show dollars per million input tokens and per million output tokens. Output is typically priced several times higher than input.
- Limits: the context window is the maximum of input plus output tokens in one request.
- Latency: more output tokens means a longer wait, because tokens are generated one after another.
- Rate limits: many providers cap tokens per minute as well as requests per minute.
Measure, do not guess
Every response reports the real numbers in the usage field. You can also count before sending with the token counting endpoint, which is covered in a later post.
import anthropic
client = anthropic.Anthropic()
samples = {
"plain": "The quick brown fox jumps over the lazy dog.",
"code": "def add(a: int, b: int) -> int:\n return a + b",
"json": '{"user_id": 4821, "tags": ["a", "b"], "active": true}',
}
for name, text in samples.items():
count = client.messages.count_tokens(
model="claude-haiku-5-5",
messages=[{"role": "user", "content": text}],
)
print(name, len(text), "chars ->", count.input_tokens, "tokens")
The result includes a small fixed overhead for the message wrapper, so the numbers are slightly higher than the raw text alone. Notice that the code and JSON samples have a worse tokens-per-character ratio than the plain sentence.
A worked cost example
Suppose a request has 2,000 input tokens and 500 output tokens, and your model costs $1 per million input tokens and $5 per million output tokens (illustrative numbers, always check the current price page). Input costs 2,000 / 1,000,000 x $1 = $0.002. Output costs 500 / 1,000,000 x $5 = $0.0025. The call costs $0.0045. At 100,000 calls per day, that is $450 per day, so token discipline matters at scale.
Where hidden tokens come from
The text you type is not the only thing counted. Your system prompt, every earlier message in the history, tool definitions, and the formatting wrapper around each message all count as input. Images and PDFs are converted into tokens too, and large ones can cost thousands. On the output side, reasoning tokens produced during thinking are billed as output even if you never display them. When a bill is higher than you expected, one of these hidden sources is usually the cause.
Why the same text counts differently
Tokenizers are trained on large text collections and keep the most frequent character sequences as single tokens. English prose is well covered, so it compresses well. Source code with unusual identifiers, minified JSON, long hexadecimal strings and many non-Latin scripts compress worse. If your application handles those, measure with real samples before estimating cost. Also remember that a new model generation can ship with a different tokenizer, so the same prompt may cost a different number of tokens after an upgrade.
Do not use another vendor's tokenizer
Libraries built for one provider's tokenizer give wrong counts for another's. Use the provider's own counting API when accuracy matters, and keep a safety margin when you budget against a context window.
Takeaways
- Tokens are not words. Count with the provider's tool.
- Output tokens cost more and add latency.
- Long system prompts and long histories are paid for on every call.
- Log usage from every response to see where tokens go.
Comments
No comments yet. Have a question or a tip? Be the first to share it.