feat(memory): accurate token counting with tiktoken-rs#816
Merged
Conversation
c1a9888 to
bfee3a6
Compare
Introduce TokenCounter backed by tiktoken-rs cl100k_base with DashMap cache (10k cap). Provides accurate token counting for context budget allocation, tool schema token estimation via OpenAI formula, and graceful fallback to chars/4 when tokenizer is unavailable. Includes input size guard (64KB) to prevent cache pollution from oversized input.
bfd6402 to
1f2968c
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
chars/4heuristic (up to 30% error) withtiktoken-rscl100k_base tokenizerTokenCounterwith DashMap cache (10k cap, random eviction) for amortized O(1) lookupschars/4when tokenizer initialization failsestimate_tokens()call sites across zeph-memory, zeph-core, zeph-indexCloses #789, closes #794, closes #795, closes #796, closes #797, closes #798
Test plan
cargo +nightly fmt --checkpassescargo clippy --workspace -- -D warnings— 0 warningscargo nextest run --workspace --lib --bins— 2553/2553 passed