AI tokens vs words: count a request before estimating cost
A token is a unit produced by a particular model’s tokenizer, not a fixed number of words or characters. For an exact text count, use the matching tokenizer and settings. For a complete API request, include its message structure and supported tools, then compare the estimate with the provider’s returned usage.
In this guide
- Separate words, Unicode characters and tokens
- Count a local text sample with a known tokenizer
- Include the entire request when planning context
- Build a reproducible comparison worksheet
- Reserve output space before adding more input
- Compare estimates with actual usage and cost
- Common questions
- Sources & definitions
A word counter is useful for an article-length requirement, but it cannot reliably tell you whether an AI request fits a model’s context or what that request will cost. The same visible text can be split differently by different tokenizers, and a request often contains more than the text pasted into one message.
This workflow separates raw-text measurement, request measurement and billed usage. It includes an offline counting example and a worksheet you can fill with actual results. It deliberately does not invent exact token counts for a model that has not been run.
Separate words, Unicode characters and tokens
A visible word may become one token, several smaller tokens or a combination that includes punctuation and spaces. Hugging Face’s tokenizer introduction explains word, character and subword approaches. Language, spelling, code, whitespace and tokenizer vocabulary can all affect the split.
A Unicode character count answers another question. Some characters need multiple code units or bytes, and a visible symbol can contain several Unicode code points. None of these quantities has a universal conversion to model tokens. Treat a characters-per-token rule as a rough heuristic for a stated sample, not a property of all languages.
| Task | Useful measurement | Limit |
|---|---|---|
| Check article length | Words or Unicode characters | Does not establish model tokens |
| Measure a raw text file for one tokenizer | Token IDs from that tokenizer and settings | May exclude chat formatting and request overhead |
| Estimate a structured API request | Provider-supported counting or exact request formatting | May differ from final usage |
| Reconcile billed usage | Actual usage fields and current billing definitions | Not an estimate of another provider’s request |
Count a local text sample with a known tokenizer
For a tokenizer that uses the Hugging Face tokenizers format, load the correct local tokenizer.json and encode a UTF-8 text file. Record the model or tokenizer identifier, tokenizer-file revision, package version and whether special tokens are included. The example targets the published tokenizers 0.23.2 API, disables padding and truncation, and counts raw text without adding special tokens.
Use a tokenizer file obtained from the intended model’s official distribution. This example does not download a model, send text to a server or reproduce a hosted chat API’s full request format. The count becomes useful only when the tokenizer and settings match the specific question you are measuring.
# Example environment: tokenizers==0.23.2
from pathlib import Path
from importlib.metadata import version
from tokenizers import Tokenizer
# Use the matching tokenizer.json already saved locally.
tokenizer = Tokenizer.from_file("tokenizer.json")
tokenizer.no_padding()
tokenizer.no_truncation()
text = Path("sample.txt").read_text(encoding="utf-8")
encoded = tokenizer.encode(text, add_special_tokens=False)
print("tokenizers version:", version("tokenizers"))
print("raw-text tokens:", len(encoded.ids))
# Record tokenizer identity/revision separately.
# This is not a complete chat-request or billing count.Include the entire request when planning context
A chat request can include system instructions, conversation history, retrieved passages, tool definitions and formatting in addition to the latest user message. Chat templates can insert control tokens around roles and message boundaries. Counting only the last pasted paragraph can therefore understate the input.
For a hosted provider, use its documented request-counting facility when available and supported for your input types. Claude’s token-counting documentation describes structured input counting and notes cases that need actual usage reporting instead. Treat any preflight result according to that provider’s stated estimation limits, especially for tools, images and documents.
Keep credentials and sensitive customer text out of unknown online counters. A local tokenizer can help with raw text, while a provider endpoint sends the supplied request to that provider. Decide which workflow is appropriate for the data before measuring it.
Build a reproducible comparison worksheet
Choose a few representative samples: a normal support message, a code snippet, a table and a passage in each language your application uses. Preserve the exact input bytes so the comparison does not accidentally change line endings, punctuation or whitespace between runs.
For each sample, record the tokenizer identifier and revision, special-token setting, raw-text count and full-request estimate when available. Leave a value unmeasured rather than substituting a guessed ratio. If two tokenizers produce different counts, that is a measured difference for these samples, not a ranking of their language quality.
| Sample | Tokenizer and revision | Raw-text count | Full-request count or estimate |
|---|---|---|---|
| Support message | Record exact identifier | Measure locally | Measure with supported request format |
| Code snippet | Record exact identifier | Measure locally | Measure with supported request format |
| Second-language passage | Record exact identifier | Measure locally | Measure with supported request format |
Reserve output space before adding more input
For a simplified planning example, assume your chosen model permits a combined 32,000-token context for the request type you use. Reserve 4,000 for output and 2,000 as a planning buffer. That leaves 26,000 for measured input. If instructions, history and tools use 8,000 of that budget, 18,000 remains for retrieved content.
These are explicit hypothetical numbers, not a current model specification. Providers can define context, output, reasoning and tool usage differently. Check the current model contract, then enter your actual quantities into the context-budget calculator. A request fitting the context limit does not imply that every input passage is useful.
Hypothetical planning budget:
32,000 combined context
− 4,000 output reserve
− 2,000 planning buffer
= 26,000 input budget
26,000 − 8,000 instructions/history/tools
= 18,000 available for retrieved contentCompare estimates with actual usage and cost
After a small real test, save the provider’s returned usage fields and compare them with your estimate for the same request. Account for any documented cached input, output, reasoning or other billable categories separately. An estimate that omitted a tool definition cannot be reconciled by changing the word-to-token ratio.
Use the AI token-cost calculator with actual token counts and the current prices you provide. Keep input and output separate and retain the price units, such as cost per million tokens. A useful cost worksheet is reproducible from measured usage and stated rates; it does not depend on a universal conversion from words.
Common questions
Is one token always four characters?
No. That can only be a rough heuristic for some text and tokenizers. Language, code, punctuation, whitespace and tokenizer design affect the result.
Why does my local text count differ from an API’s usage?
The request can include formatting, instructions, history, tools and input types absent from the raw text sample. The tokenizer or special-token settings may also differ. Compare the same complete request and the provider’s definitions.
Can this site’s word counter calculate an exact model token count?
No. It reports text measurements such as words and Unicode characters. Use a matching tokenizer or supported provider counting method for model tokens, then use the cost and context tools with those measured inputs.
Sources & definitions
- Hugging Face LLM Course: Tokenizers
- Claude Platform: Token counting
- Hugging Face tokenizers 0.23.2: published Python package and API examples
- Hugging Face Transformers: Chat templates
Method checked: