A lightweight Cloudflare Worker that counts tokens in text using the GPT-4o encoding (cl200k_base). Includes a web UI and a REST API.
- Real-time token counting — counts update as you type (debounced)
- REST API —
POST /api/tokenizefor programmatic access - Character & word counts — displayed alongside token count
- Edge-deployed — runs on Cloudflare's global network with minimal latency
- Node.js (v20+)
- A Cloudflare account (Workers Paid plan for deployment)
npm install
npm run devOpen http://localhost:8787 in your browser.
npx wrangler login # authenticate with Cloudflare (first time only)
npm run deployYour worker will be live at https://tokenizer.<your-subdomain>.workers.dev.
Request:
{ "text": "Hello, how many tokens is this?" }Response:
{ "tokenCount": 8 }Limits: max 100,000 characters per request.
curl -X POST https://tokenizer.example.workers.dev/api/tokenize \
-H "Content-Type: application/json" \
-d '{"text": "Hello, world!"}'- Hono — lightweight web framework for edge runtimes
- gpt-tokenizer — pure JS BPE tokenizer (GPT-4o / cl200k_base)
- Cloudflare Workers — serverless edge compute

