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:
llm
tokens
pricing
openai
anthropic
gemini
ai
Tutorial

Why count tokens before calling an LLM API?

Every request to OpenAI, Anthropic or Google is billed in input tokens and output tokens — not words or characters. A long system prompt, few-shot examples or multi-turn history consumes context window budget before your user question. Without measuring first, you hit a 400 truncation error or an invoice surprise at month end. On FastMinify, the LLM token counter, LLM pricing calculator and context window calculator run entirely in your browser — your text never leaves the device. Explore the AI & LLM tools hub for the full 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
Fast workflow: paste, count, estimate, adjust — no account or install

Honest limits and best practices

What these tools do not replace

FastMinify targets fast browser-side planning — not accounting reconciliation or runtime optimization.

No provider billing API calls — manual rate table, USD only in v1
Estimate tier (Claude, Gemini): ±5–15% — do not rely on it alone for tight budgets
No image, audio or tool-call token rules in v1
Output tokens not auto-derived in pricing calculator v1
Real invoices may include discounts, regions or cached-token nuances — check the provider dashboard
Reduce tokens and cost before the call

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

Shorten system instructions — every token repeats on each request
Deduplicate RAG context — do not resend near-identical chunks
Smaller model for simple tasks — compare via Compare all models
Batch API for non-latency-sensitive workloads — −50% lever in the calculator
Prompt caching for stable system prompts — model the cached slice 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 — client-side processing only
No account required — no identity ↔ prompt correlation
512 KiB max — split very large payloads before counting
Format message JSON locally with json-formatter before API messages mode
For production: instrument API logs (usage tokens returned by the provider) as a complement

Tokens, tiktoken and heuristics: what you 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 — 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 may vary slightly between models — compare on the target model
Format message JSON first with the JSON formatter to avoid parse errors
Exact (OpenAI) vs Estimate (Claude, Gemini)

FastMinify lazy-loads js-tiktoken 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 — we apply a characters-per-token heuristic (±5–15% typical) with an Estimate badge.

Exact — js-tiktoken lazy-load, same encoding family as OpenAI docs
Estimate — conservative heuristic, never presented as official billing
Auto mode: detects raw text vs JSON messages array format
API messages mode: [{"role","content"}] array with ChatML overhead
For OpenAI byte-exact disputes, compare against the official OpenAI tokenizer
Input, output and context window

Cost depends on both directions: prompt (input) and completion (output). The context window caps input + reserved output — the context window calculator subtracts an output reserve (15% default) to estimate effective input budget.

Input tokens — system prompt, RAG, history, user question
Output tokens — enter manually in the pricing calculator (v1 does not derive them from the prompt)
Context window — published limit per model (manually maintained table on FastMinify)
Safe / Near limit / Won't fit status by % of effective budget consumed
Large RAG: count injected context separately from the user message

Three-tool workflow: count, estimate, check the window

Step 1 — Count with the LLM token counter

Paste your prompt or a JSON messages array into the LLM token counter. Max 512 KiB UTF-8. Toggle Compare all models to see counts across the full registry.

Raw text or API messages JSON mode
Exact badge (OpenAI) or Estimate badge (Claude, Gemini) per model
BPE token pill visualization for Exact-tier models only
Local counting — no upload to OpenAI, Anthropic or Google
Copy counts to feed the pricing calculator
Step 2 — Estimate USD cost

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

Input / output / total USD breakdown per call
Batch API lever (−50% simplified on subtotal)
Prompt caching lever — cached slice billed at 10% of input rate
Monthly projection: calls/day × 30 days
Compare all models — cost table for every registry model
Step 3 — Check the context window

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

Configurable output reserve (15% default)
Safe (<80%), Near limit (≥80%), Won't fit (overflow) statuses
Per-model view on the FastMinify registry
Same 512 KiB limit and Exact/Estimate tiers
Chain to the pricing calculator once headroom is confirmed
Scenario — RAG prototype before production

You assemble system prompt + 20 vector chunks + user question for GPT-4o.

1

Count the full context

Paste everything (or the messages array) in the token counter. Note input tokens on the target model and check Exact vs Estimate badge.

2

Test context headroom

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

3

Project monthly cost

In the pricing calculator, enter input tokens, estimate output tokens (e.g. 500), enable caching if the system prompt is stable, then multiply by expected calls/day.

Conclusion

Counting tokens before the API call avoids truncation and billing surprises. Use the LLM token counter for an honest breakdown (Exact for OpenAI, Estimate elsewhere), the context window calculator to validate RAG or multi-turn headroom, then the pricing calculator to project USD cost — all in the browser without sending your prompt to providers.

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