tiktoken-rs
Library Featured
Library for encoding and decoding with the tiktoken library in Rust
- Downloads
- 3,939,182
- Dependents
- 449
- Stars
- 410
- Forks
- 76
- Contributors
- 19
- Commits
- 208
- Latest release
- 0.12.1 ·
About
Rust library for tokenizing text with OpenAI models using tiktoken.
This library provides a set of ready-made tokenizer libraries for working with GPT, tiktoken and related OpenAI models. Use cases cover tokenizing and counting tokens in text inputs.
This library is built on top of the tiktoken library and includes some additional features and enhancements for ease of use with Rust code.
Supported OpenAI models include GPT-5.4, GPT-5, GPT-4.1, GPT-4o, o1, o3, o4-mini, and gpt-oss models.
Scope: This crate is focused on OpenAI tokenizers (tiktoken). For non-OpenAI models (Llama, Gemini, Mistral, etc.), use the HuggingFace
tokenizerscrate.
Examples
For full working examples for all supported features, see the examples directory in the repository.
Usage
Add the library to your project:
cargo add tiktoken-rs
Use the examples below in your Rust code.
Counting tokens
use tiktoken_rs::o200k_base;
let bpe = o200k_base().unwrap();
let tokens = bpe.encode_with_special_tokens(
"This is a sentence with spaces"
);
println!("Token count: {}", tokens.len());
For repeated calls, use the singleton to avoid re-initializing the tokenizer:
Read the full README
use tiktoken_rs::o200k_base_singleton;
let bpe = o200k_base_singleton();
let tokens = bpe.encode_with_special_tokens(
"This is a sentence with spaces"
);
println!("Token count: {}", tokens.len());
Encoding text
CoreBPE::encode mirrors upstream tiktoken and returns a Result.
Propagate or unwrap the result before using the tokens:
use tiktoken_rs::o200k_base;
let bpe = o200k_base().unwrap();
let allowed = bpe.special_tokens();
let (tokens, last_piece_token_len) = bpe.encode("hello <|endoftext|>", &allowed).unwrap();
The generic encode_as and count helpers also return Result.
The crate requires Rust 1.85 or newer.
Counting max_tokens parameter for a chat completion request
use tiktoken_rs::{get_chat_completion_max_tokens, ChatCompletionRequestMessage};
let messages = vec![
ChatCompletionRequestMessage {
content: Some("You are a helpful assistant that only speaks French.".to_string()),
role: "system".to_string(),
..Default::default()
},
ChatCompletionRequestMessage {
content: Some("Hello, how are you?".to_string()),
role: "user".to_string(),
..Default::default()
},
ChatCompletionRequestMessage {
content: Some("Parlez-vous francais?".to_string()),
role: "system".to_string(),
..Default::default()
},
];
let max_tokens = get_chat_completion_max_tokens("o1-mini", &messages).unwrap();
println!("max_tokens: {}", max_tokens);
Counting max_tokens parameter for a chat completion request with async-openai
Enable the async-openai feature in your Cargo.toml file.
# #[cfg(feature = "async-openai")]
# {
use tiktoken_rs::async_openai::get_chat_completion_max_tokens;
use async_openai::types::chat::{
ChatCompletionRequestMessage, ChatCompletionRequestSystemMessage,
ChatCompletionRequestSystemMessageContent, ChatCompletionRequestUserMessage,
ChatCompletionRequestUserMessageContent,
};
let messages = vec![
ChatCompletionRequestMessage::System(ChatCompletionRequestSystemMessage {
content: ChatCompletionRequestSystemMessageContent::Text(
"You are a helpful assistant that only speaks French.".to_string(),
),
name: None,
}),
ChatCompletionRequestMessage::User(ChatCompletionRequestUserMessage {
content: ChatCompletionRequestUserMessageContent::Text(
"Hello, how are you?".to_string(),
),
name: None,
}),
];
let max_tokens = get_chat_completion_max_tokens("o1-mini", &messages).unwrap();
println!("max_tokens: {}", max_tokens);
# }
tiktoken supports these encodings used by OpenAI models:
| Encoding name | OpenAI models |
|---|---|
o200k_harmony | gpt-oss-20b, gpt-oss-120b |
o200k_base | GPT-5 series, o1/o3/o4 series, gpt-4o, gpt-4.5, gpt-4.1, codex-* |
cl100k_base | gpt-4, gpt-3.5-turbo, text-embedding-ada-002, text-embedding-3-* |
p50k_base | Code models, text-davinci-002, text-davinci-003 |
p50k_edit | Edit models like text-davinci-edit-001, code-davinci-edit-001 |
r50k_base (or gpt2) | GPT-3 models like davinci |
Context sizes
get_context_size uses the GPT-6 family default for any name beginning with
gpt-6, including variants and snapshots. GPT-6 tokenizer lookup and token-budget
helpers are unsupported because no encoding mapping is available.
| Model | Context window |
|---|---|
gpt-6* | 1,050,000 |
gpt-5.4, gpt-5.4-pro | 1,050,000 |
gpt-4.1, gpt-4.1-mini, gpt-4.1-nano | 1,047,576 |
gpt-5, gpt-5-mini, gpt-5-nano, gpt-5.4-mini, gpt-5.4-nano | 400,000 |
o1, o3, o3-mini, o3-pro, o4-mini | 200,000 |
codex-mini | 200,000 |
gpt-oss | 131,072 |
gpt-4o, gpt-4o-mini | 128,000 |
o1-mini, gpt-5.3-codex-spark | 128,000 |
gpt-3.5-turbo | 16,385 |
gpt-4 | 8,192 |
See the examples in the repo for use cases. For more context on the different tokenizers, see the OpenAI Cookbook
Encountered any bugs?
If you encounter any bugs or have any suggestions for improvements, please open an issue on the repository.
Acknowledgements
Thanks @spolu for the original code, and .tiktoken files.
License
This project is licensed under the MIT License.
Compatibility
- 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.
| Project | Downloads | Dependents | Stars | Latest release | Runs on (GPU) | Native code | License |
|---|---|---|---|---|---|---|---|
|
Library for encoding and decoding with the tiktoken library in Rust |
3,939,182 downloads over 30 days | 449 | 410 | 0.12.1 | 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 |
|
sentencex Listed
Sentence segmentation library with wide language support optimized for speed and utility. |
114,914 downloads over 30 days | 1 | 145 | 0.1.34 | 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 |
|
sentencepiece-rs Listed
Rust runtime reimplementation of SentencePiece model loading, normalization, encoding, and decoding. |
30,659 downloads over 30 days | 5 | 2 | 0.2.2 | 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 |
|
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 FeaturedIt passes every Featured gate.
What it meets
- It has 298 active dependents and 267 new dependents in 365 days. Featured needs 11 active dependents or 9 new dependents in 365 days.
- Active dependents are at 100% of their peak. Featured needs at least 50%.
- Docs are present. The release commit or a manifest ties the crate to the repository.
- No vulnerability advisory stays unpatched past the grace window. At least one version is neither a prerelease nor 0.0.x.
- The last activity is on 24 Sep 2026, 46 days ago. The rules expect an update within 1200 days. Dormancy rule
Exact rules: Featured library. The check on applied thresholds version 9.
Every rule outcome, as the check records it
expected-interval: version 0.12.1 base 300, doubled for 3939182 downloads per month, doubled for 298.25 active users: E = 1200 daysdormancy: last activity 2026-09-24, 46 days ago, E = 1200: activefeatured: featured: active_users 298.25, new_adopters 267, former_glory 1
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
- Description: the crates.io description
- crates.io database dump, fetched
- RustSec advisory database, fetched
- GitHub repository, fetched
- crates.io package, fetched
- GitHub contributors, fetched
Adoption298 active dependents, 267 new dependents in 365 days1 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Active dependents | 298.25 | crates.io database dump | Yes | |
| New dependents (365 days) | 267 | crates.io database dump | Yes | |
| Peak active dependents | 294.25 | crates.io database dump | Yes | |
| Active dependents as a share of the peak | 1 | crates.io database dump | Yes | |
| Binary downloads per release | Not measured | Yes |
MaintenanceLatest human commit: 46 days ago3 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Latest human commit | 2026-09-24 | GitHub repository | Yes | |
| Latest release | 2026-09-24 | crates.io database dump | Yes | |
| First release | 2023-02-02 | crates.io database dump | Yes | |
| Latest version | 0.12.1 | crates.io database dump | Yes | |
| Days the oldest outside issue waits for a reply | 298 | GitHub repository | Yes | |
| Maintainer replies over 365 days | 15 | GitHub repository | Yes | |
| Repository archived | false | GitHub repository | Yes | |
| Repository gone | false | GitHub repository | Yes | |
| Release commit or manifest in the repo | true | crates.io package | Yes | |
| Published docs | true | crates.io database dump | Yes | |
| Latest release fails cargo check | Not measured | Yes | ||
| Build check date | Not measured | Yes | ||
| Build check toolchain | Not measured | Yes |
SecurityUnpatched vulnerabilities: none
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Unpatched vulnerabilities past the grace window | none | RustSec advisory database | Yes | |
| RustSec unmaintained advisory | none | RustSec advisory database | Yes | |
| Deprecation notice | none | crates.io database dump | Yes | |
| Deprecated requirement | none | crates.io database dump | Yes |
Distribution36 releases, 3,939,182 downloads over 30 days5 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Releases | 36 | crates.io database dump | Yes | |
| Downloads over 30 days | 3939182 | crates.io database dump | Yes | |
| Binary downloads over the last 3 releases | Not measured | Yes | ||
| Homebrew installs on request over 365 days | Not measured | Yes | ||
| Repology repository families | Not measured | Yes | ||
| Docker pull growth over 90 days | Not measured | Yes | ||
| Docker pulls at the growth baseline | Not measured | Yes | ||
| Every version a prerelease | false | crates.io database dump | Yes | |
| Every version 0.0.x | false | crates.io database dump | Yes | |
| Every version yanked | false | crates.io database dump | Yes | |
| Crate deleted from crates.io | false | crates.io database dump | Yes |
Repository410 stars, 76 forks, 19 contributors, 208 commits. Stars alone never qualify a project.
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| GitHub stars | 410 | GitHub repository | Yes — one part of the repository route | |
| GitHub forks | 76 | GitHub repository | Yes — one part of the repository route | |
| Contributors with a GitHub account | 19 | GitHub contributors | Yes — one part of the repository route | |
| Commits on the default branch | 208 | GitHub repository | No | |
| Repository's main crate left out of the entry | none | crates.io database dump | Yes |
Facts that never count
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| License | MIT | crates.io database dump | No | |
| Runs on, from listed crates and their direct dependencies | GPU support unknown | crates.io database dump | No | |
| What shows each backend | no dependency or feature matches a backend row | crates.io database dump | No | |
| Native code, from listed crates and their dependencies | No native code found | crates.io database dump | No | |
| Python bindings, from direct dependencies of listed crates | No Python binding crate found | crates.io database dump | No | |
| Crates that depend on a listed crate directly | 449 | crates.io database dump | No |
Corrections and review history
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.