Skip to content

feat(rust_bridge): restore the Rust opt-in for token counting routes - #46005

Open
jsvisa wants to merge 1 commit into
BerriAI:mainfrom
jsvisa:litellm_restore_tokenizer_rust_optin
Open

jsvisa wants to merge 1 commit into
BerriAI:mainfrom
jsvisa:litellm_restore_tokenizer_rust_optin

Conversation

@jsvisa

@jsvisa jsvisa commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

How it solves it:

  • Restore both routes to RUST_OPT_IN in the route catalog
  • Default stays off, LITELLM_RUST=1 opts in, Python fallback intact

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

  1. They set LITELLM_RUST=1 on the proxy and restart it
  2. They send POST http://localhost:4000/utils/token_counter with a gpt-4o prompt
  3. The response returns the count, computed by the Python tokenizer
  4. No setting they can touch routes counting to the native codec

After: the same setup counts with the native codec and the same numbers

  1. They set LITELLM_RUST=1 on the proxy and restart it
  2. They send the same POST http://localhost:4000/utils/token_counter request
  3. The response returns the same count, computed by the native codec
  4. Without the env var, counting stays on Python, unchanged

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Before (f2118fc)

opt-in does nothing

  1. LITELLM_RUST=1 uv run python -c "from litellm.rust_bridge.catalog import Route, RouteContext, decision; print(decision(RouteContext(Route.TOKENIZER, provider='tiktoken')))"
  2. Prints Decision.PYTHON, the flag cannot reach the route

counting stays on Python

  1. LITELLM_RUST=1 uv run python -c "from litellm.rust_bridge.tokenizer import get_encoding; print(type(get_encoding('o200k_base')))"
  2. Prints tiktoken.core.Encoding
  3. Counting "The quick brown fox jumps over the lazy dog" gives 9 tokens

After (bb670ceff2)

opt-in reaches the route

  1. Same decision command as Before
  2. Prints Decision.RUST_WITH_FALLBACK
  3. Without the env var the same command still prints Decision.PYTHON

counting runs on the native codec

  1. Same get_encoding command as Before
  2. Prints litellm.litellm_core_utils.tokenizer.OpenAIEncoding, the wrapper over the Rust tiktoken codec
  3. Counting the same text gives the same 9 tokens

live proxy with LITELLM_RUST=1

  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 4124
  2. curl -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"}'
  3. Returns {"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

  1. cargo test -p litellm-token-counter -p litellm-token-counter-fast -p litellm-token-counter-tiktoken -p litellm-token-counter-huggingface
  2. 196 tests pass, 0 failures

Type

🆕 New Feature

Caveats

Medium

Low

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@codspeed

codspeed Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 35 untouched benchmarks


Comparing jsvisa:litellm_restore_tokenizer_rust_optin (c96fe20) with main (f2118fc)

Open in CodSpeed

@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jsvisa
jsvisa force-pushed the litellm_restore_tokenizer_rust_optin branch from 5eaf835 to bb670ce Compare October 23, 2026 13:22
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
jsvisa force-pushed the litellm_restore_tokenizer_rust_optin branch from c96fe20 to 3b32bef Compare November 11, 2026 06:06
@veria-ai

veria-ai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Reviews

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant