Count LLM Tokens and Estimate API Cost Locally (OpenAI, Claude, Gemini)

Count LLM Tokens and Estimate API Cost Locally (OpenAI, Claude, Gemini)

Before calling an LLM API: count tokens, estimate USD cost and check context-window fit — 100% in the browser without sending your prompt.

14.08.2026
7 min read
Share this article:
llmTokenspricingopenaianthropicgeminiaiTutorial

Counting tokens before you call an LLM API

Every request to OpenAI, Anthropic or Google is billed in input tokens and output tokens, not in words or characters. A long system prompt, few-shot examples or a multi-turn history use up context window budget before your user question. Without measuring first, you learn about an overflow from a 400 error and about the cost from the invoice at the end of the month. On FastMinify, the LLM token counter, the LLM pricing calculator and the context window calculator run entirely in your browser. Your text never leaves the device. The AI & LLM tools hub links the whole workflow.

Anticipate USD cost per call before hitting the API
Verify prompt + history fit the target model context window
Compare multiple models (GPT, Claude, Gemini) on the same text
100% local: no calls to provider tokenizers or APIs
A short workflow: paste, count, estimate and adjust, with no account and no install

Honest limits and best practices

What these tools do not replace

FastMinify is for quick planning in the browser. It does not replace accounting reconciliation or runtime optimization.

No calls to provider billing APIs. Rates come from a manual table, in USD only
Estimate tier (Claude, Gemini): ±5–15%, so do not rely on it alone for tight budgets
No token rules for images, audio or tool calls
The pricing calculator does not derive output tokens from the prompt
Real invoices may include discounts, regions or cached-token nuances, so check the provider dashboard
Reduce tokens and cost before the call

Counting often reveals obvious padding in system prompts and duplicated context.

Shorten system instructions, since every token repeats on each request
Deduplicate RAG context: do not resend near-identical chunks
Use a smaller model for simple tasks, and compare them with Compare all models
Use the Batch API for workloads that are not latency-sensitive, with the −50% option in the calculator
Use prompt caching for stable system prompts, and model the cached part separately
Privacy and sensitive data

Unlike many online counters that upload text to a server, FastMinify tokenizes locally. Useful for prompts with proprietary code, PII or internal specs.

No server logging of pasted content, since processing stays client-side
No account required, so there is no link between an identity and a prompt
512 KiB maximum: split very large payloads before counting
Format message JSON locally with json-formatter before API messages mode
In production, also instrument your API logs with the usage tokens the provider returns

What tokens, tiktoken and heuristics actually measure

Tokens ≠ words ≠ characters

A token can be a whole word, a syllable, punctuation or a fragment. "Development" may span several tokens depending on the model; "API" is often one. That is why a word counter does not replace a token counter for LLM billing.

Providers charge per million tokens, with input and output priced separately
API messages mode adds ChatML overhead (+3 tokens per message, +1 per name, +3 reply priming)
Tool definitions, structured JSON and code blocks count as tokenized text
The same prompt can vary slightly between models, so compare on the target model
Format message JSON first with the JSON formatter to avoid parse errors
Exact for OpenAI, Estimate for Claude and Gemini

FastMinify loads js-tiktoken on demand for OpenAI models (o200k_base or cl100k_base encodings) and shows an Exact badge. For Claude and Gemini, Anthropic and Google do not publish browser tokenizers, so we apply a characters-per-token heuristic (±5–15% typical) and show an Estimate badge.

Exact: js-tiktoken loaded on demand, same encoding family as the OpenAI docs
Estimate: a conservative heuristic, never presented as official billing
Auto-detect: tells raw text from a JSON messages array
API messages mode: [{"role","content"}] array with ChatML overhead
To settle a billing dispute with OpenAI, compare against the official OpenAI tokenizer
Input, output and context window

Cost depends on both directions: the prompt, which is input, and the completion, which is output. The context window caps input plus reserved output. The context window calculator subtracts an output reserve, 15% by default, to estimate the effective input budget.

Input tokens: system prompt, RAG, history, user question
Output tokens: enter them by hand in the pricing calculator, which does not derive them from the prompt
Context window: the limit published for each model, kept in a table that we maintain by hand
Verdicts Safe, Near limit and Won't fit, based on the share of the effective budget used
Large RAG: count injected context separately from the user message

Counting, estimating and checking the window in three tools

First, count with the LLM token counter

Paste your prompt or a JSON messages array into the LLM token counter. The limit is 512 KiB of UTF-8. Turn on Compare all models to see the counts for every model in the table.

Raw text mode or API messages mode
An Exact badge for OpenAI models, an Estimate badge for Claude and Gemini
Token breakdown (BPE) for Exact models only
Local counting: nothing is uploaded to OpenAI, Anthropic or Google
Copy the stats to carry the counts into the pricing calculator
Then, estimate the cost in USD

Transfer the input and output token counts into the LLM pricing calculator, or paste the prompt to derive input tokens through the same countTokensAsync pipeline. Rates come from a manually reviewed llm-models.json table, and its verification date is shown on the tool.

Breakdown of input, output and total cost in USD per call
Batch API lever (−50% simplified on subtotal)
Prompt caching option: the calculator bills cached tokens at 10% of the input rate
Monthly projection: calls/day × 30 days
Compare all models: a cost table for every listed model
Finally, check the context window

The context window calculator reuses the same counting pipeline and compares your prompt to the effective budget (window minus output reserve). Use it before multi-turn agents or large RAG pastes.

Configurable output reserve (15% default)
Three verdicts: Safe below 80%, Near limit from 80%, and Won't fit once the budget overflows
Per-model view on the FastMinify registry
Same 512 KiB limit and Exact/Estimate tiers
Move on to the pricing calculator once the margin is confirmed
A RAG prototype before production

You assemble a system prompt, 20 vector chunks and a user question for GPT-4o.

1

Count the full context

Paste everything, or the messages array, into the token counter. Note the input tokens for the target model and check whether the badge says Exact or Estimate.

2

Check the headroom

Open the context window calculator with the same text. If the verdict is Near limit or Won't fit, trim chunks or switch to a model with a larger window.

3

Project monthly cost

In the pricing calculator, enter the input tokens and an estimate of the output tokens (for example 500). Fill in the cached input if the system prompt is stable, then enter the expected calls per day to get the monthly figure.

Conclusion

Counting tokens before the API call avoids truncation and billing surprises. Use the LLM token counter for a clear breakdown (Exact for OpenAI, Estimate elsewhere). Use the context window calculator to check RAG or multi-turn headroom, then the pricing calculator to project the USD cost. All of it runs in the browser, so your prompt never goes to a provider.

Count on the target model, since tokens vary between model families
Treat Estimate as planning, not as official Claude or Gemini billing
Reserve output headroom in the context window calculator
Check the pricing date in the pricing calculator before committing large budgets
Use the word counter for SEO text stats and the token counter for LLM billing
Share this article
Share this article: