If you estimate Claude costs with a "token counter" website, there's a good chance the number is wrong — sometimes by a lot.
Most online counters use OpenAI's tokenizer (tiktoken / o200k_base) for every model, Claude included. That's understandable: Anthropic doesn't publish Claude's tokenizer. But Claude splits text differently, so those counts are estimates — and they're consistently low.
How different is it?
Anthropic has an official endpoint, POST /v1/messages/count_tokens, that returns the exact number of input tokens for a given model. I ran the same texts through it and through GPT-5's tokenizer:
| Text | GPT-5 | Claude (Opus / Sonnet / Haiku 5.5) | Difference |
|---|---|---|---|
Hello world, how are you today? |
8 | 10 | +25% |
| A small JavaScript function | 23 | 29 | +26% |
| Mixed French / Chinese / Arabic / Russian | 26 | 33 | +27% |
| A one-line SQL query | 50 | 92 | +84% |
| Emoji and math symbols | 31 | 29 | −6% |
(Text-only counts; small samples, so treat them as indicative, not universal.)
Two takeaways:
- "Claude ≈ GPT + 10%" is not a safe rule of thumb. Prose and code came out ~25% higher, and the SQL query nearly doubled.
- It depends on the content, so the only reliable number is the one from Claude's own tokenizer.
Getting the exact count yourself
The endpoint is free to call (it's rate-limited, and your account needs some credit for the API to respond at all).
TypeScript (@anthropic-ai/sdk):
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const { input_tokens } = await client.messages.countTokens({
model: 'claude-opus-5-5',
messages: [{ role: 'user', content: 'Hello world, how are you today?' }],
});
console.log(input_tokens);
Python (anthropic):
from anthropic import Anthropic
client = Anthropic() # reads ANTHROPIC_API_KEY
resp = client.messages.count_tokens(
model="claude-opus-5-5",
messages=[{"role": "user", "content": "Hello world, how are you today?"}],
)
print(resp.input_tokens)
Gotcha 1: the message wrapper
count_tokens counts the whole message, which includes a few tokens of framing around your text. If you want the tokens of the text alone (to compare with other tokenizers), measure the framing once and subtract it:
async function countText(model: string, text: string) {
const count = async (content: string) =>
(await client.messages.countTokens({ model, messages: [{ role: 'user', content }] })).input_tokens;
const overhead = (await count('a')) - 1; // "a" is a single token
return (await count(text)) - overhead;
}
(Cache the overhead per model — it doesn't change.)
Gotcha 2: you don't need to count per model
I expected different counts for Opus, Sonnet, Haiku and Fable. I tested English, mixed-language text, emoji, SQL and a 5,000-token source file: all current Claude models returned identical counts every time — they share a tokenizer. One call covers all of them. (Older generations can differ, so re-check when a new model ships.)
Gotcha 3: don't put your API key in the browser
If you build a counter for others, the key has to stay server-side. I used a tiny Cloudflare Worker that receives the text, calls count_tokens, and returns only the numbers. Because it's a public endpoint, it also needs basic protection:
- an origin check, so only your own pages can call it
- a per-IP rate limit (Cloudflare's rate-limit binding works well)
- a cache keyed by a SHA-256 hash of the text — identical text never hits the API twice, and you never store the text itself
- a back-off after a 429, so a flood can't exhaust your account's rate limit
What about GPT and Gemini?
-
GPT-5.x: OpenAI's
o200k_basetokenizer is public, so you can count exactly — even in the browser (thegpt-tokenizernpm package). -
GPT-6 and Gemini: their tokenizers aren't public (Gemini has a
countTokensAPI method), so any browser-side number is an estimate — and a good tool should say so.
A free counter that does all this
I wrapped the above into a free tool — paste text, see exact Claude and GPT-5 counts plus estimated cost across 17 models, with prices checked against the providers' pricing pages:
👉 boring-web.com/claude-token-counter
No signup, and the text isn't stored. If you spot a wrong price or a missing model, let me know in the comments.
Have you seen big gaps between Claude and GPT token counts on your own data? I'd love to know which content types are the worst — SQL surprised me.
Top comments (0)