tiktoken

Fast BPE tokenizer for OpenAI models, providing WASM and pure-JS bindings for Node, browsers, and edge runtimes.

Library
npm
v1.0.22
1,074 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
47 /100 Fair
Development Activity 0
Maintenance 44
Community 52
Maturity 52
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
67 /100 Good
Architecture 78
Code Quality 62
Innovation 74
Learning Curve 55

tiktoken is the JavaScript/WASM port of OpenAI’s Python tiktoken library, giving Node.js, Bun, and browser-based applications the same byte-pair-encoding tokenizer used internally by GPT models. It ships prebuilt encodings for every OpenAI encoding scheme (gpt2, r50k_base, p50k_base, p50k_edit, cl100k_base, o200k_base) so developers can count tokens, truncate prompts, or estimate API costs without making a network call.

The repository builds two distinct npm packages from a shared Rust core compiled to WebAssembly: tiktoken, a full-featured WASM build with 1-to-1 feature parity with the Python original, and js-tiktoken, a pure-JavaScript port for environments like Vercel Edge Functions or Cloudflare Workers where WASM support is limited or undesired. Both expose the same encode/decode/encoding_for_model API surface, and a lightweight tiktoken/lite entry point lets consumers load only the encoding they need instead of bundling every vocabulary.

What You Get

  • Precompiled BPE encodings for every OpenAI tokenizer scheme (gpt2, r50k_base, p50k_base, p50k_edit, cl100k_base, o200k_base) available via a single get_encoding() call
  • A model-aware encoding_for_model() helper that maps any current OpenAI model name, including gpt-4o, gpt-4.1, gpt-5, and the o-series reasoning models, to its correct encoding
  • A lightweight tiktoken/lite plus tiktoken/load entry point for loading only the ranks you need, sized for edge runtimes like Cloudflare Workers and Vercel Edge Functions
  • A pure-JavaScript sibling package (js-tiktoken) for runtimes where WASM isn’t well supported, with matching encode/decode semantics
  • Support for custom vocabularies and special tokens by constructing a Tiktoken instance directly from your own BPE ranks and pattern string

Common Use Cases

  • Counting tokens client-side or server-side before sending a prompt to the OpenAI API, to stay under context-window limits
  • Estimating and tracking per-request API costs by converting text length into exact billed token counts
  • Truncating or chunking long documents to fit a fixed token budget for RAG pipelines and chat history windows
  • Running consistent tokenization in edge functions (Vercel, Cloudflare Workers) where the Python tiktoken library isn’t available

Under The Hood

Architecture tiktoken is built as a Rust/WASM monorepo with a clear layered design: a shared core crate (core/src/lib.rs) implements the byte-pair-merge tokenization algorithm (CoreBPENative) independent of any binding target, a wasm/ crate wraps that core with wasm-bindgen to expose a Tiktoken class (wasm/src/lib.rs) compiled via wasm-pack into the published tiktoken npm package, and a separate pure-JavaScript js-tiktoken package (under js/) reimplements the same encode/decode surface for WASM-constrained runtimes like Vercel Edge Functions. Turborepo and Yarn workspaces coordinate builds across these packages plus a legacy-compatibility alias package (wasm/alias), and Changesets drives versioned, independent npm publishes for each.

Tech Stack The tokenizer core relies on fancy_regex for pattern matching and rustc_hash::FxHashMap for fast rank lookups, bridged to JavaScript via wasm-bindgen and gloo-utils; the WASM build is produced with wasm-pack build --target bundler, then post-processed by TypeScript scripts (scripts/postprocess.ts, scripts/alias.ts) that generate the final dist/ output. Tests run under Vitest, and the README documents explicit integration recipes for Node.js, Bun, Vite, Next.js, Create React App (via Craco), Vercel Edge Runtime, Cloudflare Workers, and Electron, reflecting broad target-runtime support.

Code Quality Vitest test files under wasm/test/ (test_simple_public.test.ts, init_error.test.ts) cover the public API’s encode/decode round-trip across multiple encodings and its error-throwing behavior on invalid model/encoding names, and CI (.github/workflows/build_js.yml) enforces yarn build and yarn test on every pull request. Error handling in the Rust layer is explicit, converting internal failures into typed JsError/unwrap_throw exceptions rather than swallowing them, though test coverage is comparatively thin for a tokenizer with this much downstream usage, and no dedicated lint step is wired into CI.

API Design The public API deliberately mirrors the original Python tiktoken almost one-to-one (get_encoding, encoding_for_model, .encode()/.decode()), which lowers the learning curve for teams porting existing Python token-counting logic into JavaScript. A tiktoken/lite entry point paired with a registry-driven load() helper is a JS-specific addition for loading a single encoding’s ranks on demand in bundle-size-constrained edge environments, and the README’s per-runtime setup guides are unusually thorough; the main rough edge is the requirement to manually call .free() to release WASM memory, an unfamiliar pattern for typical JS developers.

Used by 23 apps in this directory

TypeScript
100%
Other

Activepieces

AI Assistants · Automation · Mcp

24,756

Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.

View details
92
Repo Health
85
Technical
64
Dependency
Built with
TypeScript 100%
Updated 4 days ago
TypeScript
82%
Other

AFFiNE

Collaboration · Note Taking · Productivity

73,040

Write, draw, and plan in one infinite canvas — the open-source alternative to Notion and Miro that keeps your data yours.

View details
91
Repo Health
87
Technical
67
Dependency
Built with
TypeScript 82%
Rust 11%
Updated 5 days ago
JavaScript
95%
MIT

AnythingLLM

AI Assistants · Automation · Developer Tools

66,533

The all-in-one AI platform for private document chat, no-code agents, and local LLMs with zero setup friction.

View details
90
Repo Health
78
Technical
64
Dependency
Built with
JavaScript 95%
Updated 6 days ago
TypeScript
98%
Apache 2.0

Cline

AI Code Assistants

69,455

An open-source AI coding agent that lives in your editor and terminal — reads and edits your codebase, runs commands, browses the web, and requires human approval for every action by default.

View details
90
Repo Health
74
Technical
65
Dependency
Built with
TypeScript 98%
Updated 4 days ago
TypeScript
84%
Apache 2.0

Continue

AI Code Assistants · AI Development · Automation

36,049

Open-source coding agent for VS Code, JetBrains, and CLI with support for 30+ LLM providers.

View details
73
Repo Health
88
Technical
62
Dependency
Built with
TypeScript 84%
Updated 5 days ago
TypeScript
45%
Other

Convex Backend

Databases · Developer Tools

12,617

Open-source reactive database that lets developers build live-updating apps with pure TypeScript, strong consistency, and real-time subscriptions—no separate API layer required.

View details
87
Repo Health
82
Technical
68
Dependency
Built with
TypeScript 45%
Rust 43%
Updated 6 days ago
TypeScript
95%
AGPL 3.0

Docmost

CMS · Collaboration · Note Taking

21,801

Self-hosted collaborative wiki and knowledge base with real-time editing, diagrams, AI assistance, and enterprise access controls — a modern alternative to Confluence and Notion.

View details
89
Repo Health
70
Technical
69
Dependency
Built with
TypeScript 95%
Updated 5 days ago
TypeScript
73%
AGPL 3.0

Firecrawl

AI Development · Developer Tools

185,614

Turn any website into clean, LLM-ready data with a single API call — no proxy headaches, no scraping complexity.

View details
89
Repo Health
83
Technical
65
Dependency
Built with
TypeScript 73%
Python 13%
Updated 4 days ago
TypeScript
62%
Other

Flowise

AI Development · Automation · Developer Tools

55,490

Drag-and-drop visual builder for AI agents, RAG pipelines, and multi-agent systems—deploy anywhere in minutes.

View details
82
Repo Health
77
Technical
63
Dependency
Built with
TypeScript 62%
JavaScript 27%
Updated 1 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers