Feature Comparison
| Method | Accuracy | Best for |
|---|---|---|
| Anthropic count_tokens API | Highest | Messages, tools, images, PDFs, and final preflight checks. |
| Characters / 4 estimate | Rough | Fast drafting and back-of-envelope planning. |
| Words * 1.3 estimate | Rough | English prose estimates when code is not dominant. |
| Third-party tokenizers | Varies | Exploration, not billing-grade decisions. |
Official Token Count API
Anthropic's token counting endpoint accepts structured message inputs similar to the Messages API and returns input_tokens. The docs note that counts are estimates and actual message usage may differ slightly.
curl https://api.anthropic.com/v1/messages/count_tokens \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--data '{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "Count the tokens in this prompt."}
]
}'Quick Local Estimate
When you do not have API access, estimate conservatively and leave a safety margin.
rough_tokens_from_chars = Math.ceil(characters / 4) rough_tokens_from_words = Math.ceil(words * 1.3) safe_prompt_budget = model_context_window * 0.8
What Changes Token Count
Token counts depend on the model, language, code density, JSON shape, tool definitions, images, PDFs, and hidden structure added by the API or product surface.
- Code and JSON often tokenize differently from plain prose.
- Tool definitions and long terminal output can add many input tokens.
- Images and PDFs should be counted with the official endpoint when possible.
- Do not reuse old token counts after changing models or prompt structure.
When To Use This Page
Use this page as the auxiliary token counter reference for Claude context planning, Claude Code usage review, and Claude 3.5 Sonnet calculator pages.
FAQ
Is there an official Claude tokenizer?
Anthropic provides a token counting API for Claude messages. It is the best option for preflight token counts.
Is token counting free?
Anthropic's token counting docs describe the endpoint as free but rate limited separately from message creation.
Can I count Claude tokens from plain text only?
You can estimate from plain text, but structured messages with tools, images, or PDFs should be counted through the API.
Why do token estimates differ from billed usage?
Counts can include system optimizations or differ slightly from final message usage. Billing reflects your content according to Anthropic's billing system.