Cache cl100k_base locally + retry on network errors (cherry-pick upstream #183)#2
Merged
Merged
Conversation
`_get_encoding` used to mark tiktoken unusable on ANY exception, including a transient `IncompleteRead` while pulling cl100k_base.tiktoken from the Azure blob CDN. A single dropped connection on first use permanently demoted the whole process to the length-based fallback heuristic, silently losing tokenization precision for every later request until restart. Two changes: 1. Pin `TIKTOKEN_CACHE_DIR` (via `setdefault`, so operator overrides win) to a repo-local `.tiktoken_cache/` directory. The ~1.6 MB BPE blob is fetched at most once per working copy and reused across restarts, eliminating the per-process re-download that exposed us to CDN flakiness in the first place. `.tiktoken_cache/` added to .gitignore. 2. Separate `ImportError` (tiktoken truly missing → cache the disabled state permanently) from runtime errors (network/IO failure during BPE download → do NOT cache, return None this call, retry on next call). One transient blip no longer kills tokenization for the lifetime of the process. Tests (tests/unit/test_tokenizer.py::TestEncodingCacheAndRetry): - TIKTOKEN_CACHE_DIR is set to repo-local `.tiktoken_cache` on module import. - Network failure does not poison the `_encoding` sentinel. - After a failure, the next call retries and a recovered encoding is cached. - `ImportError` is still cached so we don't re-import a missing package on every call (regression guard for the previous behavior).
Bump for the tokenizer-cache cherry-pick (upstream jwadow#183). Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
36c36e8 to
0c9bb70
Compare
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.
Summary
Cherry-pick of upstream PR jwadow/kiro-gateway#183 (commit
11703f3by @1c3z)._get_encoding()used to mark tiktoken permanently unusable on ANY exception — including a transientIncompleteReadwhile pullingcl100k_base.tiktokenfrom the Azure CDN on first use. A single dropped connection silently demoted the whole process to the length-based fallback heuristic until restart, losing tokenization precision for every later request.TIKTOKEN_CACHE_DIRto a repo-local.tiktoken_cache/(viasetdefault, so operator overrides win) → the ~1.6 MB BPE blob is fetched at most once and reused across restarts.ImportError(tiktoken truly missing) as permanent; network/transient errors leave_encoding=Noneso the next call retries..gitignore: ignore.tiktoken_cache/.Valuable in containers where first-call network flakiness shouldn't permanently degrade token accounting.
Version
Bumps
APP_VERSIONto2.4.dev.13-fork.3.Merge order
Fork-number series. Intended merge order: fork.2 (jwadow#215) → fork.3 (this) → fork.4 (jwadow#182). Out-of-order merges produce a one-line
APP_VERSIONconflict — keep the highest fork number.Test plan
pytest -q→ 1695 passed (includes the PR's 4 new tests)