Handle missing tiktoken encoding data in api token counting#266
Handle missing tiktoken encoding data in api token counting#266bsbodden merged 10 commits intoredis:mainfrom
Conversation
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
There was a problem hiding this comment.
Pull request overview
This PR makes working-memory token counting resilient when tiktoken can’t load the cl100k_base encoding (e.g., in air-gapped / restricted-egress environments), preventing API requests and summarization/truncation logic from failing due to missing tokenizer data.
Changes:
- Add a cached
tiktokenencoding loader with a safe character-based fallback token estimator. - Route working-memory token counting and summarization token calculations through a shared
_count_text_tokens()helper. - Add regression tests covering both direct token counting and the
GET /v1/working-memory/{session_id}?model_name=...path whentiktoken.get_encoding()raises.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| agent_memory_server/api.py | Adds cached encoding load + fallback estimation and updates working-memory token counting/summarization to use it. |
| tests/test_issue_237.py | Adds regression coverage ensuring token counting + working-memory GET do not 500 when tiktoken encoding load fails. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

Summary
tiktokencannot loadcl100k_baseGET /v1/working-memory/{session_id}?model_name=...API pathTesting
git commitpre-commit hooks (ruff,ruff format,typos, trailing whitespace, EOF checks)python3 -m py_compile agent_memory_server/api.py tests/test_issue_237.pyCloses #237.
Note
Low Risk
Low risk: changes are limited to token-counting/truncation paths and add a conservative fallback plus regression tests to prevent 500s when
tiktokencan’t load.Overview
Prevents
GET /v1/working-memory/{session_id}(and summarization/truncation logic) from failing whentiktoken.get_encoding("cl100k_base")cannot load by introducing a cached encoder with a 5-minute backoff and a character-based token estimate fallback.Refactors token counting to use
_count_text_tokensthroughout and adds regression tests covering both direct token counting and the API path, including retry/backoff behavior in_get_tiktoken_encoding.Reviewed by Cursor Bugbot for commit d1bddbc. Bugbot is set up for automated code reviews on this repo. Configure here.