
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.
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.
Honest limits and best practices
FastMinify targets fast browser-side planning — not accounting reconciliation or runtime optimization.
Counting often reveals obvious fat in system prompts and duplicated context.
Unlike many online counters that upload text to a server, FastMinify tokenizes locally. Useful for prompts with proprietary code, PII or internal specs.
Tokens, tiktoken and heuristics: what you actually measure
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.
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.
[{"role","content"}] array with ChatML overheadCost 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.
Three-tool workflow: count, estimate, check the window
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.
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.
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.
You assemble system prompt + 20 vector chunks + user question for GPT-4o.
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.
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.
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.
Related Articles

Test API payloads and config files against JSON Schema — JSON Pointer path errors.

operationId, tags, error responses: rules that prevent spec debt on OpenAPI files.

Schema errors, broken $ref, missing responses: validate your OpenAPI 3.0–3.2 API contract in the browser.