tokenx-rs

Library Listed

Fast token count estimation for LLMs at 96% accuracy without a full tokenizer

Checked · Sources

Downloads
1,235
Dependents
3
Stars
3
Forks
1
Contributors
1
Commits
5
Latest release
0.1.0 ·

About

From the project README ·

Rust port of tokenx by Johann Schopplich

Fast token count estimation for LLMs at 96% accuracy without a full tokenizer.

Why?

  • No vocabulary files: Full tokenizers like tiktoken require 2-4MB of BPE vocabulary data
  • Zero dependencies: No regex, no allocations — just a single-pass character scanner
  • Fast: ~8x faster than the original Node.js implementation on Latin text
  • Accurate enough: 96% accuracy is sufficient for token budget estimation and streaming display
  • Universal: Works across all LLM providers (OpenAI, Anthropic, Google, etc.)

Installation

[dependencies]
tokenx-rs = "0.1"

Usage

use tokenx_rs::estimate_token_count;

let tokens = estimate_token_count("Hello, world!");
println!("Estimated tokens: {}", tokens);
Check token limits
use tokenx_rs::is_within_token_limit;

if is_within_token_limit(prompt, 4096) {
    // safe to send
}
Split text into chunks
use tokenx_rs::split_by_tokens;

let chunks = split_by_tokens(long_text, 1000);

How it works

The estimator makes a single pass over the input string, classifying each character and grouping runs of similar characters into segments. Each segment is scored by type:

Read the full README
Segment TypeRule
Whitespace0 tokens
CJK characters1 token per character
Digit sequences1 token
Short words (≤3 bytes)1 token
Punctuation runsceil(len / 2)
Alphanumeric wordsceil(len / chars_per_token)
Other (emojis, mixed)1 token per character

Language-specific diacritics (German, French, Spanish) adjust the chars_per_token ratio for more accurate estimates on non-English text.

There is no regex, no string splitting allocation, and no runtime dependencies.

Performance

Benchmarked against the original tokenx Node.js library on the same machine (Apple Silicon, Node v20, rustc 1.91):

Inputtokenx (Node.js)tokenx-rs (Rust)Speedup
Short text (~9 words)830 ns107 ns7.8x
Medium text (~900 words)89.6 µs10.9 µs8.2x
Long text (~27k words)2.91 ms332 µs8.8x
CJK text (1200 chars)6.34 µs4.04 µs1.6x
Code (~100 fn blocks)443 µs52.9 µs8.4x

Run benchmarks yourself with cargo bench.

Accuracy

Accuracy benchmarks from the original tokenx project (compared against tiktoken cl100k_base):

ContentActual TokensEstimatedDeviation
Short English text19190.00%
German text with umlauts48492.08%
Kafka - Metamorphosis (English)31,79632,3251.66%
Kafka - Die Verwandlung (German)35,30933,9703.79%
道德經 - Laozi (Chinese)11,71211,4272.43%
羅生門 - Akutagawa (Japanese)9,51710,53510.70%
TypeScript ES5 declarations49,29351,5994.68%

License

MIT

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
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.

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

Fast token count estimation for LLMs at 96% accuracy without a full tokenizer

1,235 downloads over 30 days 3 3 0.1.0 GPU support unknown No native code found MIT
whatlang-rs Featured

Fast and lightweight language identification library for Rust.

882,019 downloads over 30 days 74 1,087 0.18.0 GPU support unknown No native code found MIT
whichlang Listed

A blazingly fast and lightweight language detection library for Rust.

52,880 downloads over 30 days 11 464 0.1.1 GPU support unknown No native code found MIT

Binding for the sentencepiece tokenizer

30,249 downloads over 30 days 18 24 0.14.0 GPU support unknown Binds native code: sentencepiece-sys. Build uses a native toolchain: cc, cmake, pkg-config Apache-2.0 OR MIT
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
kitoken Listed

Fast tokenizer for language models, supporting BPE, Unigram and WordPiece tokenization

2,949 downloads over 30 days 3 62 0.11.1 GPU support unknown No native code found BSD-2-Clause
tokie Listed

Blazingly fast tokenizer - 50x faster tokenization, 10x smaller model files, 100% accurate drop-in replacement for HuggingFace

711 downloads over 30 days 3 56 0.1.4 GPU support unknown No native code found Apache-2.0 OR MIT
Why ListedNo archive, New or Featured rule places it, so it is Listed.

What it lacks for Featured

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

What else the check found

  • The last activity is on 6 Feb 2026, 276 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 0.1.0 base 80, capped at 1 releases = 50, raised to the 365-day floor: E = 365 days
  • dormancy: last activity 2026-02-06, 276 days ago, E = 365: active
  • featured: not featured: active_users 3 < 11 and new_adopters 3 < 9 and repository route stars 3 (needs 4539), forks 1 (needs 434), contributors 1 (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
Adoption3 active dependents, 3 new dependents in 365 days1 not measured
Adoption measurements
SignalValueSourceFetchedCounts
Active dependents3crates.io database dumpYes
New dependents (365 days)3crates.io database dumpYes
Peak active dependents3crates.io database dumpYes
Active dependents as a share of the peak1crates.io database dumpYes
Binary downloads per releaseNot measuredYes
MaintenanceLatest human commit: 9 months ago3 not measured
Maintenance measurements
SignalValueSourceFetchedCounts
Latest human commit2026-02-06GitHub repositoryYes
Latest release2026-02-06crates.io database dumpYes
First release2026-02-06crates.io database dumpYes
Latest version0.1.0crates.io database dumpYes
Days the oldest outside issue waits for a reply117GitHub 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
Distribution1 releases, 1,235 downloads over 30 days5 not measured
Distribution measurements
SignalValueSourceFetchedCounts
Releases1crates.io database dumpYes
Downloads over 30 days1235crates.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
Repository3 stars, 1 forks, 1 contributors, 5 commits. Stars alone never qualify a project.
Repository measurements
SignalValueSourceFetchedCounts
GitHub stars3GitHub repositoryYes — one part of the repository route
GitHub forks1GitHub repositoryYes — one part of the repository route
Contributors with a GitHub account1GitHub contributorsYes — one part of the repository route
Commits on the default branch5GitHub repositoryNo
Repository's main crate left out of the entrynonecrates.io database dumpYes
Facts that never count
Facts that never count measurements
SignalValueSourceFetchedCounts
LicenseMITcrates.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 directly3crates.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.