Try Web Demo · Request Early Access API Key · Trust Center · Case Studies · API Quickstart · Trust Boundary
AI Token Counter is a model-aware token and API cost estimation workbench for teams comparing LLM prompts, multimodal inputs, PDFs, tool definitions, model pricing, and tokenizer accuracy across providers.
It is built for one practical question:
"If I send this input to different AI models, how many tokens will it use, how much might it cost, and how trustworthy is the estimate?"
The hosted workbench is available now: tokens-counter.vercel.app.
Hosted API keys are currently issued manually through Early Access. This is the right path if you want to:
- add token and cost estimates to an internal tool,
- compare provider and proxy pricing before shipping an AI workflow,
- audit prompt, PDF, image, or tool-schema overhead across models,
- test the API contract before requesting custom pricing profiles or private deployment support.
Early Access keys use conservative limits while API key management, persistent quota, and billing are hardened.
For the hosted product view, see the Trust Center.
- Local browser estimates stay local unless you enable official provider counting.
- Official counting can send prompts, messages, images, PDFs, or tool schemas to the selected provider API through your server-side keys.
- Token and cost estimates are planning data, not provider invoices.
- Closed-source models, multimodal inputs, tool calls, caching, and provider optimizations can change final billable usage.
- Public hosted API access requires a bearer key and rate limits; do not embed long-lived keys in browser code.
AI Token Counter,也就是这个项目里的「Token 点钞机」,用于对比主流 AI 模型的文本、图片、PDF、工具调用 Token 数量和 API 费用。它会明确区分官方计数、本地精确 tokenizer、本地估算和不支持的能力,避免把闭源模型的第三方估算误标成官方结果。
当前仓库采用 MIT License 开源。第三方 tokenizer、模型、provider API 和价格来源仍遵循各自的许可证和服务条款。
- Highlights
- Early Access
- Trust Boundary
- Screenshots
- When To Use It
- Accuracy Model
- Supported Providers
- Quick Start
- Environment Variables
- External API
- Project Structure
- Adding A New Model
- Privacy And Security
- Roadmap
- Contributing
- Commercial Use
- License
- 198 model catalog entries across OpenAI, Anthropic, Google, DeepSeek, Qwen, GLM/Z.AI, Mistral, Meta Llama, xAI, Cohere, Baidu ERNIE, Doubao, Moonshot/Kimi, StepFun, MiniMax, and Xiaomi MiMo.
- Text, image, PDF, and tool-call inputs in one workspace.
- Accuracy labels for every result: official exact, official estimate, local exact, local estimate, or unsupported.
- Provider-aware image handling: when images are uploaded, models without vision capability are disabled in the selector.
- Pricing profiles: use the official/catalog pricing profile or the CC Switch-style preset profile when proxy/coding-tool billing differs.
- External API v1 for
/api/v1/models,/api/v1/estimates, and/api/v1/tokens/count. - Apple-style UI theme with dark/light mode, provider logos, drawer comparison view, local history, Markdown copy, and CSV export.
- Privacy-first default: local estimates stay in the browser unless the user enables official provider counting APIs.
If the screenshot is outdated after UI changes, replace screenshot.png before publishing.
Use this project when you need to:
- compare prompt cost before choosing a model,
- estimate text/image/PDF/tool-call token usage,
- check whether a model supports a requested input mode,
- compare official pricing and proxy/coding-tool pricing,
- expose token counting and cost estimates to internal tools through an API,
- audit tokenizer assumptions before using estimates commercially.
Token estimation is not one uniform problem. The app intentionally separates methods:
| Accuracy | Meaning |
|---|---|
official_exact |
Provider API returns an exact count for the requested payload. |
official_estimate |
Provider API returns a documented estimate or a model-side count that can differ from final billed usage. |
local_exact |
The app has an explicit local tokenizer implementation or mapped tokenizer asset for raw text. |
local_estimate |
The app uses a family tokenizer, local heuristic, or formula-based estimate. |
unsupported |
The selected model does not support that input capability. |
Important caveats:
- Chat templates, tool/function schemas, system messages, cached tokens, and provider-side optimizations can change final API usage.
- Image/video/PDF counting is especially provider-specific.
- Pricing changes frequently; always confirm with the provider before using estimates for billing or procurement.
See Tokenizer Mapping And License Audit for the latest tokenizer research notes.
| Provider | Model entries | Counting approach |
|---|---|---|
| OpenAI | 60 | js-tiktoken, official count API fallback, image formulas |
| Anthropic Claude | 24 | official count_tokens API |
| Google Gemini | 11 | official countTokens API plus local image fallback |
| DeepSeek | 9 | mapped Hugging Face tokenizer assets where available |
| Alibaba Qwen | 19 | mapped Qwen tokenizer assets for open models; hosted aliases marked conservatively |
| Z.AI / GLM | 10 | official tokenizer API path |
| Xiaomi MiMo | 6 | mapped open checkpoint tokenizer where available |
| Mistral | 11 | local estimate until mistral-common integration |
| Meta Llama | 4 | local estimate with model-license warnings |
| xAI Grok | 10 | local estimate until official tokenize API integration |
| Cohere | 4 | official tokenize API for supported models; local fallback elsewhere |
| Baidu ERNIE | 5 | mapped ERNIE tokenizer asset |
| ByteDance / Doubao | 6 | local estimate until official calculator integration |
| Moonshot / Kimi | 9 | local estimate until official estimate API integration |
| StepFun | 2 | official token-count API for supported models |
| MiniMax | 8 | local estimate; official image count where required |
- Frontend: Vue 3, TypeScript, Vite, Pinia, Element Plus, Lucide Icons
- API server: Hono on Node.js
- Tokenizers:
js-tiktoken, lightweight Hugging Face tokenizer loader, provider-specific estimate rules - Documents and images:
pdfjs-dist, browser-side image metadata extraction - Tests: Vitest
Requirements:
- Node.js 24 or newer is recommended for the current local setup.
- npm is used by the checked-in lockfile.
git clone git@github.com:Shiaoming123/Tokens-Counter.git
cd Tokens-Counter
npm install
cp .env.example .env
npm run devDefault local URLs:
- Web:
http://localhost:5173 - API:
http://localhost:8787
The Vite dev server proxies /api/* to the local Hono server.
# Server
PORT=8787
TOKEN_COUNTER_API_KEY=
TOKEN_COUNTER_API_KEYS=
TOKEN_COUNTER_RATE_LIMIT_MAX=120
TOKEN_COUNTER_RATE_LIMIT_WINDOW_MS=3600000
# Public repository link used by the header GitHub icon
VITE_APP_GITHUB_URL=https://github.com/Shiaoming123/Tokens-Counter
# Official count APIs
OPENAI_API_KEY=
ANTHROPIC_API_KEY=
GEMINI_API_KEY=
ZAI_API_KEY=
ZHIPU_API_KEY=
COHERE_API_KEY=
MOONSHOT_API_KEY=
STEPFUN_API_KEY=
XAI_API_KEY=Notes:
- API keys must stay server-side. Do not expose provider keys in client code.
- Without official provider keys, the app falls back to local counting where possible and marks results accordingly.
TOKEN_COUNTER_API_KEYprotects the external/api/v1/*API when configured. Use comma-separatedTOKEN_COUNTER_API_KEYSfor simple rotation or multiple clients.TOKEN_COUNTER_RATE_LIMIT_MAXandTOKEN_COUNTER_RATE_LIMIT_WINDOW_MScontrol the in-memory per-key/IP external API limiter.
# Start frontend and API together
npm run dev
# Start only Vite
npm run dev:web
# Start only Hono API
npm run dev:api
# Run tests
npm test
# Type-check and build production assets
npm run build
# Validate model/pricing/license catalog integrity
npm run validate:catalog
# Serve the API and built frontend from dist/
npm startThe public API surface is versioned under /api/v1.
export BASE_URL="https://tokens-counter.vercel.app"
export TOKEN_COUNTER_API_KEY=<your-api-key>
curl "$BASE_URL/api/v1/models" \
-H "Authorization: Bearer $TOKEN_COUNTER_API_KEY"curl "$BASE_URL/api/v1/estimates" \
-H "Authorization: Bearer $TOKEN_COUNTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"models": ["gpt-4o", "claude-sonnet-4.5", "gemini-2.5-flash"],
"input": {
"text": "Compare the API cost of this prompt."
},
"options": {
"output_tokens": 1000,
"pricing_profile": "official"
}
}'Successful responses include per-result usage, cost, accuracy, method, and trust metadata. Missing or invalid bearer keys return 401; rate-limited requests return 429.
Endpoints:
GET /api/v1/modelsPOST /api/v1/estimatesPOST /api/v1/tokens/count
Full spec: External Token API Specification
Production hardening checklist: External API Production Checklist
server/
index.ts # Hono API server and official count proxies
env.ts # Server environment parsing
src/
components/ # Vue UI components
core/
accuracy/ # Accuracy labels and UI metadata
api/ # Frontend API clients
cost/ # Cost calculation and currency conversion
count/ # Result builder and method merging
document/ # PDF text extraction
estimate/ # External API estimate service
history/ # LocalStorage history and export helpers
models/ # Model registry, provider labels, ordering
pricing/ # Pricing profile resolution
tokenizers/ # tiktoken, Hugging Face loader, approximations
tools/ # Tool/function schema token estimation
vision/ # Image token formulas
data/
models.json # Model capability catalog
model-pricing.json # Catalog/official pricing table
pricing-profiles.json # Alternate pricing profiles
licenses.json # Tokenizer/provider license notices
stores/ # Pinia stores
types/ # Domain types
workers/ # Browser tokenizer worker
docs/
external-token-api.md
external-api-production-checklist.md
tokenizer-research-2026-05-17.md
test/
*.test.ts # Vitest coverage for counting, pricing, API, registry
Most model changes should start in data files:
- Add or update the model in
src/data/models.json. - Add pricing in
src/data/model-pricing.jsonorsrc/data/pricing-profiles.json. - Add or update the license/source notice in
src/data/licenses.json. - If the model claims
local_exactwith a Hugging Face-style tokenizer, add an explicit mapping insrc/core/tokenizers/tokenizerLoader.ts. - Run:
npm test
npm run buildThe registry test fails if a local_exact Hugging Face-style tokenizer does not have an explicit repo mapping.
- Local text/image/PDF estimates are processed in the browser where possible.
- Official count mode sends payloads to the selected provider API through the server.
- Local history is stored in browser LocalStorage.
- Provider API keys are read from server environment variables.
- Public API access should use
TOKEN_COUNTER_API_KEYand a real rate limiter before production use. - Do not log full prompts, images, PDFs, or tool payloads in production by default.
- Expand official count APIs for xAI, Moonshot/Kimi, Volcano Ark, and provider-specific multimodal payloads.
- Replace generic Mistral fallback with
mistral-common. - Add chat-template-aware counting for structured messages and tools.
- Add account/API-key management, real rate limits, and usage analytics.
- Add deployment docs for Vercel, Cloudflare Pages, and a single Node service.
- Add a public pricing/profile editor for teams with custom proxy billing.
- Add more automated checks for tokenizer license and pricing-source freshness.
Contributions are welcome after the repository is made public.
Good first contributions:
- correct a model price with an official source link,
- add a missing model catalog entry,
- improve tokenizer mapping accuracy,
- add a provider logo or capability flag,
- improve documentation or API examples,
- add focused tests for a tokenizer or cost formula.
Before opening a pull request:
npm test
npm run buildPlease keep pricing/tokenizer changes source-backed. Include the provider documentation, model card, pricing page, or API documentation that justifies the change.
This project is designed to support both open-source adoption and future paid services:
- free public UI,
- paid hosted API keys,
- team pricing profiles,
- private deployments,
- custom model catalog maintenance,
- consulting for LLM cost estimation workflows.
This project is open-source under the MIT License.
Third-party tokenizer, model, provider API, and pricing-source notices are tracked in LICENSES.md and src/data/licenses.json.
This project builds on the work of many open-source and provider ecosystems, including Vue, Vite, Hono, Element Plus, js-tiktoken, pdf.js, Simple Icons, Hugging Face tokenizer assets, and official model-provider APIs.
