rust-tiktoken

Library Listed

A high-performance pure-Rust implementation of OpenAI's tiktoken BPE tokenizer

Checked · Sources

Downloads
25,714
Dependents
12
Stars
8
Forks
2
Contributors
2
Commits
197
Latest release
4.1.2 ·

About

From the project README ·

English | 简体中文 | 日本語 · tiktoken.golia.jp — live in-browser playground

Upgrading from 3.5.x? 3.6.0 and 3.8.0 corrected token ids that diverged from the reference tokenizers — recompute cached counts and stored id sequences. Details: CHANGELOG.

Upgrading to 4.0? Token ids and the API are unchanged. The one break: vocabularies now live behind default-on vocab-* features, so a dependency written as default-features = false must name the ones it uses.

The fastest Rust BPE tokenizer, plus its WebAssembly bindings. Drop-in compatible with OpenAI tiktoken and the mainstream open models (Llama 3, DeepSeek, Qwen, Mistral, Kimi, GLM, MiniMax). Hand-written scanners for ASCII and CJK, a key-size-layered vocabulary and whole-piece memoisation make it 5–49x faster than tiktoken-rs natively and 2–4x faster than gpt-tokenizer in the browser — CJK prose included.

Crates in this workspace

PathCrate / PackageDescriptionVersion
tiktoken/tiktokenRust BPE tokenizer — 17 encodings, 116 models, multi-provider pricing
tiktoken-wasm/tiktoken-wasm (Rust)WASM binding crate for the above
tiktoken-wasm/@goliapkg/tiktoken-wasm (npm)Same, published to npm via wasm-pack
Read the full README

The two crates live in one workspace and are versioned in lockstep — every release bumps and publishes both at the same version number.

Highlights

  • Hand-written pre-tokenizer for ASCII and CJK — letters, digits, punctuation, contractions, Han/kana/hangul runs and fullwidth forms all resolve without the regex engine, which remains the arbiter (property-tested equivalence) and the fallback for rare shapes.
  • 17 encodings · 116 models · 11 providers — OpenAI (GPT-4/4o/4.1/4.5, GPT-5.x, o1/o3/o4-mini, gpt-oss), Llama 3/4, DeepSeek V3/V4, Qwen, Mistral, Kimi K2/K3, GLM-4/5, MiniMax M2; plus USD cost estimation (Anthropic & Google included for pricing).
  • Lean & portable — all 17 vocabularies embedded in 5.1 MB and opt-out per vocabulary (a cl100k-only build carries 373 KB), key-size-layered vocabulary with whole-piece memoisation, hybrid linear/heap BPE merge, optional rayon parallelism, allocation-free count(), pure Rust with zero C dependencies, and a self-contained wasm build.

Full API, supported-model tables, and benchmarks live in the per-crate READMEs: tiktoken/ · tiktoken-wasm/.

Quick start

Rust
[dependencies]
tiktoken = "4"
// by encoding name
let enc = tiktoken::get_encoding("cl100k_base").unwrap();
let tokens = enc.encode("hello world");
assert_eq!(enc.decode_to_string(&tokens).unwrap(), "hello world");

// count without allocating a token vector
let n = enc.count("The quick brown fox.");

// or resolve by model name
let enc = tiktoken::encoding_for_model("gpt-4o").unwrap();
WebAssembly (browser / Node.js)
npm install @goliapkg/tiktoken-wasm
import init, { getEncoding } from '@goliapkg/tiktoken-wasm'
await init()
const enc = getEncoding('o200k_base')
const tokens = enc.encode('hello world')

Performance

On an Apple M4 Mac mini, encode is 5–49x faster than tiktoken-rs and 5–29x faster than Python tiktoken: 29–49x on ASCII, 15–17x on Chinese and Japanese prose, 5x even on an adversarial no-repeat CJK corpus. In the browser (wasm) it is 2–4x faster than gpt-tokenizer. Full tables and methodology: tiktoken/README.md#performance.

Build

cargo test -p tiktoken
cargo fmt --all --check
cargo clippy --workspace --lib -- -D warnings

# WASM (requires wasm-pack: cargo install wasm-pack)
cd tiktoken-wasm
wasm-pack build --target web --release --scope goliapkg

Release

tiktoken and tiktoken-wasm are versioned in lockstep and released together via git-flow (no PRs):

# release gate: the full property sweep (~35 minutes; CI runs a smoke sample)
PROPTEST_CASES=25000 cargo test --workspace --all-targets

git flow release start X.Y.Z
# bump versions to X.Y.Z: tiktoken/Cargo.toml, tiktoken-wasm/Cargo.toml
# (and its tiktoken path-dep); finalize both CHANGELOGs
git flow release finish X.Y.Z                       # merge → master, tag vX.Y.Z, back-merge develop
git tag -a tiktoken-wasm-vX.Y.Z vX.Y.Z^{commit} -m "tiktoken-wasm X.Y.Z"
git push origin master develop vX.Y.Z tiktoken-wasm-vX.Y.Z
# tag `v*` publishes the tiktoken crate; `tiktoken-wasm-v*` publishes the wasm crate + npm

The 23 pre-tokenizer and 12 round-trip property tests pin the hand-written scanners against the regex engine. PROPTEST_CASES sets how much new ground each run covers; the seeds in proptest-regressions/ replay before any random case at every setting, so past failures are always re-checked.

License

Licensed under either of MIT or Apache-2.0, at your option.

Runs on
GPU support unknown
What shows it
  • no dependency or feature matches a backend row
Native code
None found
  • No native code found
Python bindings
No Python binding crate found
License
Apache-2.0 OR MIT

Compare with similar projects

Order: strongest tier first, then most downloads, then name, with unmeasured values last. A library counts downloads over 30 days. An app counts binary downloads per release. An app with none counts its crate downloads over 30 days. ML Rust runs this catalogue, so its closest project always gets a row.

rust-tiktoken beside the projects that share a topic with it
ProjectDownloadsDependentsStarsLatest releaseRuns on (GPU)Native codeLicense
rust-tiktoken This page Listed

A high-performance pure-Rust implementation of OpenAI's tiktoken BPE tokenizer

25,714 downloads over 30 days 12 8 4.1.2 GPU support unknown No native code found Apache-2.0 OR MIT
rust_tokenizers Listed Dormant

High performance tokenizers for Rust

Python bindings

27,821 downloads over 30 days 12 345 8.1.1 GPU support unknown No native code found Apache-2.0
splintr Listed

Tokenizer for Rust and Python: pure Rust by default, with 18 bundled vocabularies and four backends covering BPE, SentencePiece and WordPiece. Its output matches Hugging Face tokenizers id for id on 14 model families, including GPT-2, Qwen, Whisper, T5 and BERT.

Python bindings

8,204 downloads over 30 days 7 63 0.22.2 GPU support unknown Binds native code: pcre2-sys (feature pcre2) MIT
shimmytok Listed

Pure Rust tokenizer for GGUF models with llama.cpp compatibility (SentencePiece + BPE + WPM + UGM + RWKV)

914 downloads over 30 days 2 26 0.8.5 GPU support unknown No native code found Apache-2.0 OR MIT
tiktoken App Listed

tiktoken is a fast BPE tokeniser for use with OpenAI's models.

0 binary downloads per release Not applicable 19,621 Not measured Not measured MIT
xberg New

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.

26,095 downloads over 30 days 2 9,565 1.3.10 CUDA, Metal Binds native code: libheif-sys, libz-sys, xberg_libwpd, xberg_tesseract, clipper-sys and 4 more. Build uses a native toolchain: cc, bindgen (feature bindings), cmake (feature build-tesseract), pkg-config (feature dynamic-linking), vcpkg (feature dynamic-linking) MIT
Why ListedNo archive, New or Featured rule places it, so it is Listed.

What it lacks for Featured

  • It has 8 active dependents and 8 new dependents in 365 days. Featured needs 11 active dependents or 9 new dependents in 365 days. The repository route reads 8 stars, 2 forks and 2 contributors. It needs 4,539 stars, 434 forks and 79 contributors together.

What else the check found

  • The last activity is on 13 Aug 2026, 88 days ago. The rules expect an update within 365 days. Dormancy rule

Exact rules: Listed, Featured library. The check on applied thresholds version 9.

Every rule outcome, as the check records it
  • expected-interval: version 4.1.2 base 300, raised to the 365-day floor: E = 365 days
  • dormancy: last activity 2026-08-13, 88 days ago, E = 365: active
  • featured: not featured: active_users 8 < 11 and new_adopters 8 < 9 and repository route stars 8 (needs 4539), forks 2 (needs 434), contributors 2 (needs 79)
  • listed: listed: no archive, new or featured rule applies

Measurements and sources

Each signal row shows the raw value, its source and the fetch date. The Sources list gathers every distinct source on this page. Movement needs more history.

Sources6 sources
Adoption8 active dependents, 8 new dependents in 365 days1 not measured
Adoption measurements
SignalValueSourceFetchedCounts
Active dependents8crates.io database dumpYes
New dependents (365 days)8crates.io database dumpYes
Peak active dependents8crates.io database dumpYes
Active dependents as a share of the peak1crates.io database dumpYes
Binary downloads per releaseNot measuredYes
MaintenanceLatest human commit: 2 months ago3 not measured
Maintenance measurements
SignalValueSourceFetchedCounts
Latest human commit2026-08-13GitHub repositoryYes
Latest release2026-08-13crates.io database dumpYes
First release2026-03-08crates.io database dumpYes
Latest version4.1.2crates.io database dumpYes
Days the oldest outside issue waits for a reply91GitHub repositoryYes
Maintainer replies over 365 days0GitHub repositoryYes
Repository archivedfalseGitHub repositoryYes
Repository gonefalseGitHub repositoryYes
Release commit or manifest in the repotruecrates.io packageYes
Published docstruecrates.io database dumpYes
Latest release fails cargo checkNot measuredYes
Build check dateNot measuredYes
Build check toolchainNot measuredYes
SecurityUnpatched vulnerabilities: none
Security measurements
SignalValueSourceFetchedCounts
Unpatched vulnerabilities past the grace windownoneRustSec advisory databaseYes
RustSec unmaintained advisorynoneRustSec advisory databaseYes
Deprecation noticenonecrates.io database dumpYes
Deprecated requirementnonecrates.io database dumpYes
Distribution19 releases, 25,714 downloads over 30 days5 not measured
Distribution measurements
SignalValueSourceFetchedCounts
Releases19crates.io database dumpYes
Downloads over 30 days25714crates.io database dumpYes
Binary downloads over the last 3 releasesNot measuredYes
Homebrew installs on request over 365 daysNot measuredYes
Repology repository familiesNot measuredYes
Docker pull growth over 90 daysNot measuredYes
Docker pulls at the growth baselineNot measuredYes
Every version a prereleasefalsecrates.io database dumpYes
Every version 0.0.xfalsecrates.io database dumpYes
Every version yankedfalsecrates.io database dumpYes
Crate deleted from crates.iofalsecrates.io database dumpYes
Repository8 stars, 2 forks, 2 contributors, 197 commits. Stars alone never qualify a project.
Repository measurements
SignalValueSourceFetchedCounts
GitHub stars8GitHub repositoryYes — one part of the repository route
GitHub forks2GitHub repositoryYes — one part of the repository route
Contributors with a GitHub account2GitHub contributorsYes — one part of the repository route
Commits on the default branch197GitHub repositoryNo
Repository's main crate left out of the entrynonecrates.io database dumpYes
Facts that never count
Facts that never count measurements
SignalValueSourceFetchedCounts
LicenseApache-2.0 OR MITcrates.io database dumpNo
Runs on, from listed crates and their direct dependenciesGPU support unknowncrates.io database dumpNo
What shows each backendno dependency or feature matches a backend rowcrates.io database dumpNo
Native code, from listed crates and their dependenciesNo native code foundcrates.io database dumpNo
Python bindings, from direct dependencies of listed cratesNo Python binding crate foundcrates.io database dumpNo
Crates that depend on a listed crate directly12crates.io database dumpNo

Corrections and review history

Report a correction

No correction applies to this entry.

Judgments

A judgment enters only through an approved issue and a merged pull request.

No judgment applies to this entry.