LLM Token Counter
How big is your prompt, really? Paste any text — a system prompt, a document, a whole
conversation — and this counter tells you the exact token count using the real BPE
tokenizers behind the OpenAI model family (o200k_base for GPT-4o/4.1/GPT-5,
cl100k_base for GPT-4 and GPT-3.5 Turbo, plus r50k_base,
p50k_base and gpt2), with a clearly-labelled estimate mode for other
model families like Llama, Qwen and Mistral. Then budget it against the model's context
window: how much room is left for the conversation history you're re-sending every turn,
and how much of the window your requested output will eat.
1 · Your text
2 · Model & tokenizer
3 · Context window & output budget
The output budget is not extra: the model generates into the same window, so a prompt + requested output that exceeds the context window will be truncated or rejected. The ratio field only applies in estimate mode — English prose averages roughly 3–4 characters per token; code and non-Latin scripts tokenize denser.
Result
| Metric | Value |
|---|
All tokenizers, one text
| Tokenizer | Tokens | Δ vs selected |
|---|
Conversation growth
Chat APIs re-send the whole history on every turn. If this text is one new user message in an existing conversation, how long does it survive?
| After… | Context used | Status |
|---|
How the LLM Token Counter works
Large language models do not read characters — they read tokens, which are
chunks of text produced by a tokenizer, usually a byte-pair encoding (BPE). The
encoder starts with individual bytes and merges the most frequent pairs until its
vocabulary is full, so common words like "the" become one token while rare words, code
symbols and non-Latin scripts fragment into several. The exact merge list — the BPE rank
table — is what differs between model families, and it is published by OpenAI for its own
models. This counter loads those real tables in your browser (via the open-source
js-tiktoken port of OpenAI's reference implementation) and runs the same
algorithm their API uses, so the number you see here is the number the API will bill you
for.
tokens (exact) = BPE_rank_table.encode(text) # per model family tokens (estimate)≈ characters ÷ 3..4 # other families, ±20% window_used = system prompt + history + this message headroom = context_window − window_used − max_output_tokens
For model families whose tokenizer tables are not publicly distributed (Llama, Qwen, Mistral, Gemma), the counter switches to estimate mode and labels every number as an approximation — you can tune the characters-per-token ratio to match your model's documented behaviour. That is honest: a labelled ±20% estimate beats a false-precision number that pretends to be exact.
Worked example: one prompt, five tokenizers
Take a typical system-plus-user prompt — 436 characters, 72 words of plain English:
You are a senior DevOps engineer answering questions about Linux servers. Be concise, prefer commands over prose, and always mention the risk of destructive operations before suggesting them. User: My Ubuntu 24.04 VPS is running low on disk space. The /var/log directory is over 12 GB after a failed rsync job kept retrying and writing verbose output. How do I safely free up space without breaking journald or my monitoring agent?
Counted with each tokenizer (verified against the reference implementation):
90 tokens on o200k_base (GPT-4o, GPT-4.1, GPT-5),
92 on cl100k_base (GPT-4, GPT-3.5 Turbo 1106+), and
94 on r50k_base, p50k_base and
gpt2. For English prose the spread is small — about 4.8 characters per
token everywhere — which is why "divide by four" feels good enough. Now feed the same
counter a 297-character Python function: o200k_base says 77 tokens,
cl100k_base says 76, but gpt2 says 159 — the
older vocabulary has no merged pieces for common code patterns, so it fragments. When
your prompt is heavy on code, URLs, JSON or non-English text, the tokenizer choice
stops being a footnote and starts changing your budget by 20–100%.
Prompt size, conversation history and output budgets
Three numbers matter when you plan around a context window, and they all share one budget. First, your prompt — the system message plus whatever you send. Second, the history: stateless chat APIs re-send every previous turn on each request, so a 20-turn conversation at roughly 512 tokens per turn already carries ~10,000 tokens of overhead before your new message arrives. Third, the output: the model generates its reply inside the same window, so a 16,384-token output reservation on GPT-4o consumes 12.5% of its 131,072-token window whether or not it uses all of it. The conversation-growth table above projects where your context lands turn by turn and tells you how many more turns fit before the window (or your output reservation) runs out.
| Model | Context window | Max output | Tokenizer |
|---|---|---|---|
| GPT-4o / 4o mini | 131,072 | 16,384 | o200k_base (exact) |
| GPT-4.1 family | 1,048,576 | 32,768 | o200k_base (exact) |
| GPT-5 | 400,000 | 128,000 | o200k_base (exact) |
| o3 / o4-mini | 200,000 | 100,000 | o200k_base (exact) |
| GPT-4 Turbo (0125) | 128,000 | 4,096 | cl100k_base (exact) |
| Llama 3.1 / 3.2 | 131,072 (advertised) | config-dependent | estimate mode |
For local models the "context window" is whatever you set at server start — pair this page with the LLM VRAM Calculator to find the largest context your GPU can actually afford, then come back here and budget it.
Common mistakes when budgeting tokens
- Dividing by four for everything. Fine for English prose, badly wrong for code, JSON, URLs and non-Latin scripts — GPT-2's tokenizer alone doubles the count on a small Python function. Use the exact tokenizer for your model family whenever one is published.
- Counting only the visible prompt. The system message, tool schemas, few-shot examples and every prior turn are all billed. A "short" question in a long conversation can easily be the smallest part of the request.
- Forgetting that output shares the window. Requesting 16k of output on a 128k model permanently reserves a quarter of the context — your effective history budget shrinks by the same amount.
- Assuming one token = one word or one character. Tokens are sub-word: "unbelievable" is several tokens, "the" is one, and emoji often cost 2–3 each. Count them; don't guess.
- Planning around the advertised maximum context. "Up to 128k" describes the architecture. Your effective limit is the smaller of the window, your output reservation and — for local models — the KV-cache memory your GPU can hold.
- Mixing up tokenizers across providers. A prompt that measured 90 tokens on GPT-4o's tokenizer is not automatically 90 tokens on the model you actually deployed. Re-count with the right table — the compare button here does it in one click.
Related tools
If you're working around LLMs, these pair well with the counter:
Suggested
Luke Keam | techgeek.biz