Repository navigation
Conversation
Contributor
Merging this PR will not alter performance
Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
jsvisa
force-pushed
the
litellm_restore_tokenizer_rust_optin
branch
from
October 23, 2026 13:22
5eaf835 to
bb670ce
Compare
Token counter and tokenizer were shipped as RUST_OPT_IN in BerriAI#42174 and moved back to PYTHON_ONLY the same day in BerriAI#42517, whose actual bug was Messages dropping the Anthropic beta inference header. The tokenizer rollback was precautionary, bundled with the Messages fix, and never revisited. The native implementation and its parity tests have been green since, and the spend-tracking caller already falls back to Python counting on RuntimeError and ValueError. RUST_OPT_IN stays default-off, so nothing changes for callers that do not set LITELLM_RUST. Signed-off-by: Delweng <delweng@gmail.com>
jsvisa
force-pushed
the
litellm_restore_tokenizer_rust_optin
branch
from
November 11, 2026 06:06
c96fe20 to
3b32bef
Compare
Contributor
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
How it solves it:
Intentional product change: users who set LITELLM_RUST=1 now get native token counting and tokenizing for tiktoken and Anthropic encodings, with Python fallback on failure. Counts are identical (verified below). Users who never set the env var see no change
User Flow
Before: an operator who sets LITELLM_RUST=1 gets nothing for token counting
After: the same setup counts with the native codec and the same numbers
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Before (f2118fc)
opt-in does nothing
LITELLM_RUST=1 uv run python -c "from litellm.rust_bridge.catalog import Route, RouteContext, decision; print(decision(RouteContext(Route.TOKENIZER, provider='tiktoken')))"Decision.PYTHON, the flag cannot reach the routecounting stays on Python
LITELLM_RUST=1 uv run python -c "from litellm.rust_bridge.tokenizer import get_encoding; print(type(get_encoding('o200k_base')))"tiktoken.core.EncodingAfter (bb670ceff2)
opt-in reaches the route
Decision.RUST_WITH_FALLBACKDecision.PYTHONcounting runs on the native codec
litellm.litellm_core_utils.tokenizer.OpenAIEncoding, the wrapper over the Rust tiktoken codeclive proxy with LITELLM_RUST=1
LITELLM_RUST=1 LITELLM_MASTER_KEY=sk-local-proof uv run python litellm/proxy/proxy_cli.py --config litellm/proxy/dev_config.yaml --port 4124curl -s http://localhost:4124/utils/token_counter -H "Authorization: Bearer sk-local-proof" -H "Content-Type: application/json" -d '{"model": "gpt-4o", "prompt": "The quick brown fox jumps over the lazy dog"}'{"total_tokens":9,"request_model":"gpt-4o","model_used":"gpt-4o","tokenizer_type":"openai_tokenizer","count_source":"local_estimate","original_response":null,"error":false,"error_message":null,"status_code":null}rust crates re-verified after the rebase
cargo test -p litellm-token-counter -p litellm-token-counter-fast -p litellm-token-counter-tiktoken -p litellm-token-counter-huggingfaceType
🆕 New Feature
Caveats
Medium
Low
make checkcould not run locally, it needs node >= 24.14.1 for the UI bootstrap--base upstream/main, all greenFinal Attestation