Skip to content

Handle missing tiktoken encoding data in api token counting#266

Merged
bsbodden merged 10 commits intoredis:mainfrom
bhavana-giri:fix/237-tiktoken-fallback
Apr 6, 2026
Merged

Handle missing tiktoken encoding data in api token counting#266
bsbodden merged 10 commits intoredis:mainfrom
bhavana-giri:fix/237-tiktoken-fallback

Conversation

@bhavana-giri
Copy link
Copy Markdown
Contributor

@bhavana-giri bhavana-giri commented Apr 3, 2026

Summary

  • make working-memory token counting resilient when tiktoken cannot load cl100k_base
  • cache the encoding after the first successful load and fall back to a character-based estimate when unavailable
  • add regression coverage for both direct token counting and the GET /v1/working-memory/{session_id}?model_name=... API path

Testing

  • git commit pre-commit hooks (ruff, ruff format, typos, trailing whitespace, EOF checks)
  • python3 -m py_compile agent_memory_server/api.py tests/test_issue_237.py

Closes #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 tiktoken can’t load.

Overview
Prevents GET /v1/working-memory/{session_id} (and summarization/truncation logic) from failing when tiktoken.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_tokens throughout 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.

@jit-ci
Copy link
Copy Markdown

jit-ci bot commented Apr 3, 2026

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.

Copy link
Copy Markdown
Contributor

@nkanu17 nkanu17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but it's worth checking the copilot review comments

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 tiktoken encoding 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 when tiktoken.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.

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@bsbodden bsbodden self-assigned this Apr 6, 2026
@bsbodden bsbodden self-requested a review April 6, 2026 15:04
Copy link
Copy Markdown
Collaborator

@bsbodden bsbodden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@bsbodden bsbodden merged commit 8bcd4c8 into redis:main Apr 6, 2026
22 checks passed
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.

_calculate_messages_token_count crashes when tiktoken encodings are unavailable

4 participants