litellm/litellm-rust/crates/token-counter
2026-09-20 14:12:58 -07:00
..
benches refactor(rust): split token counter backends 2026-09-20 14:12:58 -07:00
src refactor(rust): split token counter backends 2026-09-20 14:12:58 -07:00
tests refactor(rust): split token counter backends 2026-09-20 14:12:58 -07:00
Cargo.toml refactor(rust): split token counter backends 2026-09-20 14:12:58 -07:00
README.md refactor(rust): split token counter backends 2026-09-20 14:12:58 -07:00

Token counting

Tokenizer is the text-counting interface. TokenCounter applies LiteLLM request, message, and tool accounting using any implementation of that interface

The fast feature provides fast::FastTokenizer from litellm-token-counter-fast. TokenCounter::from_json_fast, TokenCounter::from_cl100k_ranks, and TokenCounter::from_o200k_ranks use this implementation

The huggingface feature provides huggingface::HuggingFaceTokenizer through the upstream tokenizers library. TokenCounter::from_json uses this implementation

The tiktoken feature provides tiktoken::TiktokenTokenizer through tiktoken-rs. Select an encoding with TokenCounter::from_tiktoken. The supported names are cl100k_base, o200k_base, o200k_harmony, p50k_base, p50k_edit, r50k_base, and gpt2

The Hugging Face and tiktoken backends are enabled by default. The hand-written fast backend is opt-in. With default-features = false, callers can supply their own Tokenizer to TokenCounter::new without compiling a built-in backend

Budget checks, cost calculation, and the max_tokens adjustment policy belong to litellm-core-utils. The counter does not own prices, budgets, or request limits

Run the feature matrix with:

cargo test -p litellm-token-counter
cargo test -p litellm-token-counter --no-default-features
cargo test -p litellm-token-counter --no-default-features --features fast
cargo test -p litellm-token-counter --no-default-features --features huggingface
cargo test -p litellm-token-counter --no-default-features --features tiktoken