Topic 5 of 5 · 4 min read
Estimating tokens with tokenizer tools
Goal: Estimate a document's token count by rule of thumb and confirm it with a tokenizer tool or an API usage report.
You can estimate tokens quickly with a rule of thumb, then confirm with a tokenizer tool before the work, or with the usage numbers an API returns after it. An estimate is good for planning. A count is what you should trust for a budget.
Check yourself · 1 of 3
About how many tokens is a 1,000-word English article, by the usual rule of thumb?
Why it matters
You cannot manage cost or context limits that you cannot measure. The habit is simple: guess first, then measure. Over time your guesses get close, and you notice when a document is much more expensive than it looks.
Three ways to get a number
- Rule of thumb. For ordinary English text, one token is about four characters, or about three quarters of a word. So 1,000 words is around 1,300 tokens. OpenAI's help page gives these figures. They do not hold for code, tables, numbers or other languages, where counts run higher.
- Tokenizer playground. Tools such as the OpenAI Tokenizer and Tiktokenizer let you paste text and see the exact split and count. Use the tokenizer that matches your model.
- The API's own numbers. Provider APIs return a usage report with input and output tokens for every call, and some offer a token-counting endpoint that counts a request before you send it. This is the number you are billed on.
A worked estimate
A 6,000-word report in English: 6,000 divided by 0.75 gives about 8,000 tokens. Add 400 tokens for your instructions and ask for a 300-token summary. Input is about 8,400 tokens and output about 300. Check the real figures with a tool, because a report full of tables and figures will cost more than the rule says.
For engineers: count in code
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
text = open("report.txt").read()
print(len(text.split()), "words")
print(len(enc.encode(text)), "tokens")
This counts with one OpenAI encoding. Another model family will give a slightly different number, so for billing use the usage field the API returns.
Before and after
Before: "It is about ten pages, so it should fit." The request fails because the context is full.
After: Estimate 8,000 tokens by rule of thumb, confirm with a tokenizer tool, compare with the model's context window and leave room for the answer.
What goes wrong
- People use the English rule of thumb for code, JSON or another language and under-count.
- People count only the document and forget system instructions, chat history and the answer.
- People count with one vendor's tokenizer and treat the result as exact for another vendor's model.
- People never read the usage field, so surprises show up only on the invoice.
Examples
Estimate then confirm
A 3,000-word article. Estimate: 3,000 divided by 0.75 is about 4,000 tokens. Paste it into a tokenizer tool and read 4,300. Close enough to plan with. Record the real figure from the tool and use it for the budget.
Reading a usage report
An API response shows input_tokens: 8412 and output_tokens: 296. Multiply each by your provider's current price per token to get the real cost of that call. Log these numbers per request so you can spot a prompt that suddenly grows.
Key points
- For ordinary English, one token is roughly four characters or three quarters of a word.
- The rule of thumb is for planning; use a tokenizer tool or the API's usage report for real numbers.
- Count the whole request: instructions, history, pasted text and the room you need for the answer.
- The rule of thumb under-counts code, numbers, tables and many non-English languages.
- The usage fields in an API response show the input and output tokens you are actually billed for.
Common mistakes
- Applying the English rule of thumb to code, tables or other languages.
- Counting only the document and forgetting instructions and chat history.
- Treating one vendor's tokenizer count as exact for another vendor's model.
- Never checking the usage numbers the API returns.
Check yourself · 2 of 3
Which number should you trust for what you were actually billed on an API call?
Check yourself · 3 of 3
You must plan a budget for summarising Python source files. Why should you not use the English rule of thumb alone?
Practice items, not counted towards your mastery.
Start here
- Tokenizer (OpenAI) (opens in a new tab)ToolFree
Paste any text and see the token boundaries and count, coloured piece by piece.
- Tiktokenizer (compare tokenizers) (opens in a new tab)ToolFree
Switch between model tokenizers and see how the same text splits differently.
- Andrej Karpathy: minbpe and the GPT tokenizer lecture (opens in a new tab)ToolFree
A small tokenizer you can read, plus the lecture in text and video form on why spelling, arithmetic and non-English text go wrong.
More for this topic
- What are tokens and how to count them (OpenAI Help) (opens in a new tab)ArticleFree
OpenAI's short page with the English rule of thumb and how to read token counts.
- tiktoken (OpenAI) (opens in a new tab)ToolFree
The small open-source library that counts tokens in code, with examples.
- Token counting (Claude docs) (opens in a new tab)DocsFree
How to count tokens for a request before sending it, and what the usage fields mean.